From 41157c803b661e57ed39021c112377768551b8e8 Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Sat, 14 May 2016 15:28:49 -0700 Subject: [PATCH] fix selection updating closes #664 --- core/selection.js | 9 +++++---- modules/toolbar.js | 14 +++++++------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/selection.js b/core/selection.js index 6629ff3b6d..66027f1754 100644 --- a/core/selection.js +++ b/core/selection.js @@ -1,4 +1,5 @@ import Parchment from 'parchment'; +import clone from 'clone'; import equal from 'deep-equal'; import BreakBlot from '../blots/break'; import Emitter from './emitter'; @@ -23,16 +24,16 @@ class Selection { this.cursor = Parchment.create('cursor', this); // savedRange is last non-null range this.lastRange = this.savedRange = new Range(0, 0); - ['keyup', 'mouseup', 'touchend', 'touchleave'].forEach((eventName) => { + ['keyup', 'mouseup', 'touchend', 'touchleave', 'focus', 'blur'].forEach((eventName) => { this.root.addEventListener(eventName, () => { // When range used to be a selection and user click within the selection, // the range now being a cursor has not updated yet without setTimeout - setTimeout(this.update.bind(this, Emitter.sources.USER), 1); + setTimeout(this.update.bind(this, Emitter.sources.USER), 100); }); }); this.emitter.on(Emitter.events.TEXT_CHANGE, (delta) => { if (delta.length() > 0) { - this.update(Emitter.sources.SILENT); + setTimeout(this.update.bind(this, Emitter.sources.SILENT), 1); } }); this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => { @@ -251,7 +252,7 @@ class Selection { this.cursor.restore(); } if (source === Emitter.sources.SILENT) return; - this.emitter.emit(Emitter.events.SELECTION_CHANGE, this.lastRange, source); + this.emitter.emit(Emitter.events.SELECTION_CHANGE, clone(this.lastRange), source); } } } diff --git a/modules/toolbar.js b/modules/toolbar.js index 7709375d6b..0026e345ae 100644 --- a/modules/toolbar.js +++ b/modules/toolbar.js @@ -170,29 +170,29 @@ Toolbar.DEFAULTS = { let range = this.quill.getSelection(); if (range != null) { let startLength = this.quill.getLength(); - this.quill.removeFormat(range); + this.quill.removeFormat(range, Quill.sources.USER); let endLength = this.quill.getLength(); // account for embed removals - this.quill.setSelection(range.index, range.length - (startLength-endLength)); + this.quill.setSelection(range.index, range.length - (startLength-endLength), Quill.sources.SILENT); } }, direction: function(value) { let align = this.quill.getFormat()['align']; if (value === 'rtl' && align == null) { - this.quill.format('align', 'right'); + this.quill.format('align', 'right', Quill.sources.USER); } else if (!value && align === 'right') { - this.quill.format('align', false); + this.quill.format('align', false, Quill.sources.USER); } - this.quill.format('direction', value); + this.quill.format('direction', value, Quill.sources.USER); }, indent: function(value) { let range = this.quill.getSelection(); let formats = this.quill.getFormat(range); let indent = parseInt(formats.indent || 0); if (value === '+1') { - this.quill.format('indent', indent + 1); + this.quill.format('indent', indent + 1, Quill.sources.USER); } else if (value === '-1') { - this.quill.format('indent', indent - 1); + this.quill.format('indent', indent - 1, Quill.sources.USER); } } }