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() { selectedText: function() {
var t; var html = '';
t = '';
if (window.getSelection) { if (typeof window.getSelection != "undefined") {
t = window.getSelection().toString(); var sel = window.getSelection();
} else if (document.getSelection) { if (sel.rangeCount) {
t = document.getSelection().toString(); var container = document.createElement("div");
} else if (document.selection) { for (var i = 0, len = sel.rangeCount; i < len; ++i) {
t = document.selection.createRange().text; 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 // Determine the position of the caret in an element

View File

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

View File

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

View File

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