FIX: Improve selecting text over line breaks

This commit is contained in:
Robin Ward 2016-09-13 11:36:17 -04:00
parent e46204d195
commit aa7c735d34
2 changed files with 38 additions and 46 deletions

View File

@ -102,14 +102,18 @@ export function selectedText() {
}
}
html = html.replace(/<br>/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();
}

View File

@ -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,9 +45,8 @@ 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; }
@ -61,17 +55,15 @@ export default Ember.View.extend({
if (!window.getSelection().isCollapsed &&
e.which === 1 &&
!e.shiftKey) controller.deselectText();
})
.on('mouseup.quote-button', function(e) {
}).on('mouseup.quote-button', e => {
if (ignoreElements(e)) { return; }
view.selectText(e.target, controller);
view.set('isMouseDown', false);
})
.on('selectionchange', function() {
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 (view.get('isMouseDown') || view.get('isTouchInProgress')) return;
if (this.get('isMouseDown') || this.get('isTouchInProgress')) { return; }
// `selection.anchorNode` is used as a target
onSelectionChanged();
});
@ -80,12 +72,8 @@ export default Ember.View.extend({
// 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);
});
.on('touchstart.quote-button', () => this.set('isTouchInProgress', true))
.on('touchend.quote-button', () => this.set('isTouchInProgress', false));
}
},