From aa7c735d347f23c9c13de53e66c4688e52c63aa8 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 13 Sep 2016 11:36:17 -0400 Subject: [PATCH] FIX: Improve selecting text over line breaks --- .../discourse/lib/utilities.js.es6 | 12 ++-- .../discourse/views/quote-button.js.es6 | 72 ++++++++----------- 2 files changed, 38 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/utilities.js.es6 b/app/assets/javascripts/discourse/lib/utilities.js.es6 index 32f47016354..a756e896d39 100644 --- a/app/assets/javascripts/discourse/lib/utilities.js.es6 +++ b/app/assets/javascripts/discourse/lib/utilities.js.es6 @@ -102,14 +102,18 @@ export function selectedText() { } } + html = html.replace(/
/g, "\n"); + // Strip out any .click elements from the HTML before converting it to text - var div = document.createElement('div'); + const div = document.createElement('div'); div.innerHTML = html; - var $div = $(div); + + const $div = $(div); + // Find all emojis and replace with its title attribute. - $div.find('img.emoji').replaceWith(function() { return this.title; }); + $div.find('img.emoji').replaceWith(() => this.title); $('.clicks', $div).remove(); - var text = div.textContent || div.innerText || ""; + const text = div.textContent || div.innerText || ""; return String(text).trim(); } diff --git a/app/assets/javascripts/discourse/views/quote-button.js.es6 b/app/assets/javascripts/discourse/views/quote-button.js.es6 index e0b5e21434f..665498733d2 100644 --- a/app/assets/javascripts/discourse/views/quote-button.js.es6 +++ b/app/assets/javascripts/discourse/views/quote-button.js.es6 @@ -13,10 +13,8 @@ export default Ember.View.extend({ isMouseDown: false, isTouchInProgress: false, - /** - The button is visible whenever there is something in the buffer - (ie. something has been selected) - **/ + // The button is visible whenever there is something in the buffer + // (ie. something has been selected) visible: Em.computed.notEmpty('controller.buffer'), render(buffer) { @@ -32,12 +30,9 @@ export default Ember.View.extend({ @method didInsertElement **/ didInsertElement() { - const controller = this.get('controller'), - view = this; + const controller = this.get('controller'); - var onSelectionChanged = function() { - view.selectText(window.getSelection().anchorNode, controller); - }; + let onSelectionChanged = () => this.selectText(window.getSelection().anchorNode, controller); // Windows Phone hack, it is not firing the touch events // best we can do is debounce this so we dont keep locking up @@ -50,43 +45,36 @@ export default Ember.View.extend({ onSelectionChanged = _.debounce(onSelectionChanged, 500); } - $(document) - .on("mousedown.quote-button", function(e) { - view.set('isMouseDown', true); + $(document).on("mousedown.quote-button", e => { + this.set('isMouseDown', true); - if (ignoreElements(e)) { return; } + if (ignoreElements(e)) { return; } - // deselects only when the user left click - // (allows anyone to `extend` their selection using shift+click) - if (!window.getSelection().isCollapsed && - e.which === 1 && - !e.shiftKey) controller.deselectText(); - }) - .on('mouseup.quote-button', function(e) { - if (ignoreElements(e)) { return; } + // deselects only when the user left click + // (allows anyone to `extend` their selection using shift+click) + if (!window.getSelection().isCollapsed && + e.which === 1 && + !e.shiftKey) controller.deselectText(); + }).on('mouseup.quote-button', e => { + if (ignoreElements(e)) { return; } - view.selectText(e.target, controller); - view.set('isMouseDown', false); - }) - .on('selectionchange', function() { - // there is no need to handle this event when the mouse is down - // or if there a touch in progress - if (view.get('isMouseDown') || view.get('isTouchInProgress')) return; - // `selection.anchorNode` is used as a target - onSelectionChanged(); - }); + this.selectText(e.target, controller); + this.set('isMouseDown', false); + }).on('selectionchange', () => { + // there is no need to handle this event when the mouse is down + // or if there a touch in progress + if (this.get('isMouseDown') || this.get('isTouchInProgress')) { return; } + // `selection.anchorNode` is used as a target + onSelectionChanged(); + }); - // Android is dodgy, touchend often will not fire - // https://code.google.com/p/android/issues/detail?id=19827 - if (!isAndroid) { - $(document) - .on('touchstart.quote-button', function(){ - view.set('isTouchInProgress', true); - }) - .on('touchend.quote-button', function(){ - view.set('isTouchInProgress', false); - }); - } + // Android is dodgy, touchend often will not fire + // https://code.google.com/p/android/issues/detail?id=19827 + if (!isAndroid) { + $(document) + .on('touchstart.quote-button', () => this.set('isTouchInProgress', true)) + .on('touchend.quote-button', () => this.set('isTouchInProgress', false)); + } }, selectText(target, controller) {