FIX: Don't include link counts when selecting text to quote a post.

This commit is contained in:
Robin Ward 2013-03-13 15:50:44 -04:00
parent 213d3e5c10
commit 518910a54d
4 changed files with 52 additions and 31 deletions

View File

@ -103,16 +103,30 @@ Discourse.Utilities = {
},
selectedText: function() {
var t;
t = '';
if (window.getSelection) {
t = window.getSelection().toString();
} else if (document.getSelection) {
t = document.getSelection().toString();
} else if (document.selection) {
t = document.selection.createRange().text;
var html = '';
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
return String(t).trim();
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
// Strip out any .click elements from the HTML before converting it to text
var div = document.createElement('div');
div.innerHTML = html;
$('.clicks', $(div)).remove();
var text = div.textContent || div.innerText || "";
return String(text).trim();
},
// Determine the position of the caret in an element

View File

@ -10,6 +10,11 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
needs: ['topic', 'composer'],
started: null,
init: function() {
this._super();
$LAB.script(assetPath('defer/html-sanitizer-bundle'));
},
// If the buffer is cleared, clear out other state (post)
bufferChanged: (function() {
if (this.blank('buffer')) {
@ -35,6 +40,7 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
selectedText = Discourse.Utilities.selectedText();
if (this.get('buffer') === selectedText) return;
if (this.get('lastSelected') === selectedText) return;
this.set('post', e.context);
this.set('buffer', selectedText);
top = e.pageY + 5;
@ -44,35 +50,33 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
top = this.started[1] - 50;
left = ((left - this.started[0]) / 2) + this.started[0] - ($quoteButton.width() / 2);
}
$quoteButton.css({
top: top,
left: left
});
$quoteButton.css({ top: top, left: left });
this.started = null;
return false;
},
quoteText: function(e) {
var buffer, composerController, composerOpts, composerPost, post, quotedText,
_this = this;
e.stopPropagation();
post = this.get('post');
composerController = this.get('controllers.composer');
composerOpts = {
/**
Quote the currently selected text
@method quoteText
**/
quoteText: function() {
var post = this.get('post');
var composerController = this.get('controllers.composer');
var composerOpts = {
post: post,
action: Discourse.Composer.REPLY,
draftKey: this.get('post.topic.draft_key')
};
// If the composer is associated with a different post, we don't change it.
if (composerPost = composerController.get('content.post')) {
if (composerPost.get('id') !== this.get('post.id')) {
var composerPost = composerController.get('content.post');
if (composerPost && (composerPost.get('id') !== this.get('post.id'))) {
composerOpts.post = composerPost;
}
}
buffer = this.get('buffer');
quotedText = Discourse.BBCode.buildQuoteBBCode(post, buffer);
var buffer = this.get('buffer');
var quotedText = Discourse.BBCode.buildQuoteBBCode(post, buffer);
if (composerController.wouldLoseChanges()) {
composerController.appendText(quotedText);
} else {
@ -83,4 +87,5 @@ Discourse.QuoteButtonController = Discourse.Controller.extend({
this.set('buffer', '');
return false;
}
});

View File

@ -11,7 +11,7 @@ Discourse.QuoteButtonView = Discourse.View.extend({
classNameBindings: ['hasBuffer'],
render: function(buffer) {
buffer.push("quote reply");
buffer.push(Em.String.i18n("post.quote_reply"));
},
hasBuffer: (function() {
@ -25,17 +25,18 @@ Discourse.QuoteButtonView = Discourse.View.extend({
didInsertElement: function() {
// Clear quote button if they click elsewhere
var _this = this;
var quoteButtonView = this;
return $(document).bind("mousedown.quote-button", function(e) {
if ($(e.target).hasClass('quote-button')) return;
if ($(e.target).hasClass('create')) return;
_this.controller.mouseDown(e);
_this.set('controller.lastSelected', _this.get('controller.buffer'));
return _this.set('controller.buffer', '');
quoteButtonView.controller.mouseDown(e);
quoteButtonView.set('controller.lastSelected', quoteButtonView.get('controller.buffer'));
return quoteButtonView.set('controller.buffer', '');
});
},
click: function(e) {
e.stopPropagation();
return this.get('controller').quoteText(e);
}

View File

@ -517,6 +517,7 @@ en:
post:
reply: "Replying to {{link}} by {{replyAvatar}} {{username}}"
reply_topic: "Reply to {{link}}"
quote_reply: "quote reply"
edit: "Editing {{link}} by {{replyAvatar}} {{username}}"
post_number: "post {{number}}"
in_reply_to: "in reply to"