Skip to content

Commit

Permalink
fix selection updating
Browse files Browse the repository at this point in the history
closes #664
  • Loading branch information
jhchen committed May 14, 2016
1 parent cd987c0 commit 41157c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
9 changes: 5 additions & 4 deletions core/selection.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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, () => {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions modules/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 41157c8

Please sign in to comment.