From 9d803071753e2264ad63acf01916320de5d5edbf Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Tue, 17 Sep 2019 16:41:05 -0400 Subject: [PATCH 1/4] Commit the selection to the value on selection event when possible --- .../plugins/android/composition-manager.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/slate-react/src/plugins/android/composition-manager.js b/packages/slate-react/src/plugins/android/composition-manager.js index b2f321a87e..5765f8012d 100644 --- a/packages/slate-react/src/plugins/android/composition-manager.js +++ b/packages/slate-react/src/plugins/android/composition-manager.js @@ -582,6 +582,36 @@ function CompositionManager(editor) { clearAction() } + const isPointsEqual = (point1, point2) => { + if (point1.path.size !== point2.path.size) { + return false + } + + if (point1.offset !== point2.offset) { + return false + } + + return point1.key === point2.key + } + + const isRangesEqual = (range1, range2) => { + return ( + isPointsEqual(range1.anchor, range2.anchor) && + isPointsEqual(range1.focus, range2.focus) + ) + } + + // It's important that we don't run the select middleware or commit the + // selection to the value when the diff has a value. When the diff is + // non-null, the text structure in the value is different compared to the DOM. + if ( + range.isSet && + last.diff === null && + !isRangesEqual(editor.value.selection, range) + ) { + editor.select(range) + } + last.range = range last.node = domSelection.anchorNode }) From 5a9b6f19d70175191f0287348da59ef2c4bfc242 Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Tue, 17 Sep 2019 16:49:40 -0400 Subject: [PATCH 2/4] Simplify range comparison --- .../src/plugins/android/composition-manager.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/slate-react/src/plugins/android/composition-manager.js b/packages/slate-react/src/plugins/android/composition-manager.js index 5765f8012d..e5dc288182 100644 --- a/packages/slate-react/src/plugins/android/composition-manager.js +++ b/packages/slate-react/src/plugins/android/composition-manager.js @@ -583,15 +583,7 @@ function CompositionManager(editor) { } const isPointsEqual = (point1, point2) => { - if (point1.path.size !== point2.path.size) { - return false - } - - if (point1.offset !== point2.offset) { - return false - } - - return point1.key === point2.key + return point1.offset === point2.offset && point1.key === point2.key } const isRangesEqual = (range1, range2) => { From 305720eb053efdf194cc6c4d7629cf711317db8e Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Wed, 18 Sep 2019 10:14:34 -0400 Subject: [PATCH 3/4] Send selection to the select command instead of a range --- .../plugins/android/composition-manager.js | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/slate-react/src/plugins/android/composition-manager.js b/packages/slate-react/src/plugins/android/composition-manager.js index e5dc288182..29a331c7cf 100644 --- a/packages/slate-react/src/plugins/android/composition-manager.js +++ b/packages/slate-react/src/plugins/android/composition-manager.js @@ -30,6 +30,20 @@ function renderSync(editor, fn) { }) } +/** + * Takes a the range and the editor. It will take the selection from the editor + * and apply the range's anchor and focus on it. + * + * @param {Editor} editor + * @param {Range} range + */ + +function selectionFromRange(editor, range) { + return editor.value.selection + .moveAnchorTo(range.anchor) + .moveFocusTo(range.focus) +} + /** * Takes text from a dom node and an offset within that text and returns an * object with fixed text and fixed offset which removes zero width spaces @@ -191,7 +205,7 @@ function CompositionManager(editor) { applyDiff() if (last.range) { - editor.select(last.range) + editor.select(selectionFromRange(editor, last.range)) } else { debug('splitBlock:NO-SELECTION') } @@ -232,7 +246,7 @@ function CompositionManager(editor) { applyDiff() editor - .select(last.range) + .select(selectionFromRange(editor, last.range)) .deleteBackward() .focus() .restoreDOM() @@ -308,7 +322,7 @@ function CompositionManager(editor) { if (last.range && !last.range.isCollapsed) { renderSync(editor, () => { editor - .select(last.range) + .select(selectionFromRange(editor, last.range)) .deleteBackward() .focus() .restoreDOM() @@ -437,7 +451,7 @@ function CompositionManager(editor) { renderSync(editor, () => { editor - .select(nodeSelection) + .select(selectionFromRange(editor, nodeSelection)) .delete() .restoreDOM() }) @@ -504,7 +518,7 @@ function CompositionManager(editor) { */ editor - .select(range) + .select(selectionFromRange(editor, range)) .focus() .restoreDOM() }) @@ -601,7 +615,7 @@ function CompositionManager(editor) { last.diff === null && !isRangesEqual(editor.value.selection, range) ) { - editor.select(range) + editor.select(selectionFromRange(editor, range)) } last.range = range From a383a074d0f7c5045450d00d909b11dc5d653811 Mon Sep 17 00:00:00 2001 From: Guillaume Racicot Date: Wed, 18 Sep 2019 14:59:37 -0400 Subject: [PATCH 4/4] Using the correct setters for anchor and focus --- .../slate-react/src/plugins/android/composition-manager.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/slate-react/src/plugins/android/composition-manager.js b/packages/slate-react/src/plugins/android/composition-manager.js index 29a331c7cf..fc0f8b5efe 100644 --- a/packages/slate-react/src/plugins/android/composition-manager.js +++ b/packages/slate-react/src/plugins/android/composition-manager.js @@ -39,9 +39,7 @@ function renderSync(editor, fn) { */ function selectionFromRange(editor, range) { - return editor.value.selection - .moveAnchorTo(range.anchor) - .moveFocusTo(range.focus) + return editor.value.selection.setAnchor(range.anchor).setFocus(range.focus) } /**