From 9b253509e29687ed4c01a302544d8fbeac4a5acd Mon Sep 17 00:00:00 2001 From: Jason Chen Date: Sun, 29 May 2016 23:22:59 -0700 Subject: [PATCH] fix #703 --- blots/cursor.js | 9 ++++++++- core/selection.js | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/blots/cursor.js b/blots/cursor.js index c5e4bdc3f3..9de63ff936 100644 --- a/blots/cursor.js +++ b/blots/cursor.js @@ -79,7 +79,14 @@ class Cursor extends Embed { } this.remove(); if (range != null && range.start.node === textNode && range.end.node === textNode) { - this.selection.setNativeRange(textNode, Math.max(0, range.start.offset - 1), textNode, Math.max(0, range.end.offset - 1)); + // optimize() might move the cursor after our restore + let [start, end] = [range.start.offset, range.end.offset].map(function(offset) { + return Math.max(0, Math.min(textNode.data.length, offset - 1)); + }); + setTimeout(() => { + // optimize() might move selection after us + this.selection.setNativeRange(textNode, start, textNode, end); + }, 1); } } diff --git a/core/selection.js b/core/selection.js index 5487cb3da4..24348ed32b 100644 --- a/core/selection.js +++ b/core/selection.js @@ -39,6 +39,7 @@ class Selection { this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => { let native = this.getNativeRange(); if (native == null) return; + if (native.start.node === this.cursor.textNode) return; // cursor.restore() will handle // TODO unclear if this has negative side effects this.emitter.once(Emitter.events.SCROLL_UPDATE, () => { try {