Skip to content

Commit

Permalink
Force selection update on format
Browse files Browse the repository at this point in the history
We do not update selection natively when the node and offset has not
changed, however issues appear to arise when its parentage has changed
in the case of formatting. In Chrome there is sometimes a cursor
rendering bug, in Firefox the next character will be in the incorrect
location.

Fix #803
  • Loading branch information
jhchen committed Jul 19, 2016
1 parent c0ce4aa commit a2ee3da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 4 additions & 2 deletions core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class Quill {
[index, length, formats, source] = overload(index, length, name, value, source);
let range = this.getSelection();
let change = this.editor.formatLine(index, length, formats, source);
this.setSelection(range, Emitter.sources.SILENT);
this.selection.setRange(range, true, Emitter.sources.SILENT);
this.selection.scrollIntoView();
return change;
}

Expand All @@ -157,7 +158,8 @@ class Quill {
[index, length, formats, source] = overload(index, length, name, value, source);
let range = this.getSelection();
let change = this.editor.formatText(index, length, formats, source);
this.setSelection(range, Emitter.sources.SILENT);
this.selection.setRange(range, true, Emitter.sources.SILENT);
this.selection.scrollIntoView();
return change;
}

Expand Down
15 changes: 11 additions & 4 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class Selection {
}
}

setNativeRange(startNode, startOffset, endNode = startNode, endOffset = startOffset) {
setNativeRange(startNode, startOffset, endNode = startNode, endOffset = startOffset, force = false) {
debug.info('setNativeRange', startNode, startOffset, endNode, endOffset);
if (startNode != null && (this.root.parentNode == null || startNode.parentNode == null || endNode.parentNode == null)) {
return;
Expand All @@ -222,7 +222,7 @@ class Selection {
if (startNode != null) {
if (!this.hasFocus()) this.root.focus();
let nativeRange = this.getNativeRange();
if (nativeRange == null ||
if (nativeRange == null || force ||
startNode !== nativeRange.start.node || startOffset !== nativeRange.start.offset ||
endNode !== nativeRange.end.node || endOffset !== nativeRange.end.offset) {
let range = document.createRange();
Expand All @@ -238,7 +238,11 @@ class Selection {
}
}

setRange(range, source = Emitter.sources.API) {
setRange(range, force = false, source = Emitter.sources.API) {
if (typeof force === 'string') {
source = force;
force = false;
}
debug.info('setRange', range);
if (range != null) {
let indexes = range.collapsed ? [range.index] : [range.index, range.index + range.length];
Expand All @@ -250,7 +254,10 @@ class Selection {
[node, offset] = leaf.position(offset, i !== 0);
args.push(node, offset);
});
this.setNativeRange(...args);
if (args.length < 2) {
args = args.concat(args);
}
this.setNativeRange(...args, force);
} else {
this.setNativeRange(null);
}
Expand Down

0 comments on commit a2ee3da

Please sign in to comment.