From 4c3e737dda95d5ab67a94af903b25512d926c7fe Mon Sep 17 00:00:00 2001 From: Wayne Leroux Date: Wed, 16 Sep 2020 23:48:12 -0400 Subject: [PATCH] Fixed issue where virtual keyboard closes when resynchronizing selection in Chrome on Android (#3866) * Fixed issue where virtual keyboard closes when resynchronizing selection * Fixed lint issue * Added inversion of start-end to end-start when range is backwards --- .../slate-react/src/components/editable.tsx | 18 ++++++++++++---- packages/slate-react/src/utils/dom.ts | 21 ------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index e6fbc0e71..9f132b636 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -33,7 +33,6 @@ import { isDOMText, DOMStaticRange, isPlainTextOnlyPaste, - setReverseDomSelection, } from '../utils/dom' import { EDITOR_TO_ELEMENT, @@ -180,21 +179,32 @@ export const Editable = (props: EditableProps) => { // Otherwise the DOM selection is out of sync, so update it. const el = ReactEditor.toDOMNode(editor, editor) state.isUpdatingSelection = true - domSelection.removeAllRanges() const newDomRange = selection && ReactEditor.toDOMRange(editor, selection) if (newDomRange) { if (Range.isBackward(selection!)) { - setReverseDomSelection(newDomRange, domSelection) + domSelection.setBaseAndExtent( + newDomRange.endContainer, + newDomRange.endOffset, + newDomRange.startContainer, + newDomRange.startOffset + ) } else { - domSelection.addRange(newDomRange!) + domSelection.setBaseAndExtent( + newDomRange.startContainer, + newDomRange.startOffset, + newDomRange.endContainer, + newDomRange.endOffset + ) } const leafEl = newDomRange.startContainer.parentElement! scrollIntoView(leafEl, { scrollMode: 'if-needed', boundary: el, }) + } else { + domSelection.removeAllRanges() } setTimeout(() => { diff --git a/packages/slate-react/src/utils/dom.ts b/packages/slate-react/src/utils/dom.ts index 77557f715..0736bd4d1 100644 --- a/packages/slate-react/src/utils/dom.ts +++ b/packages/slate-react/src/utils/dom.ts @@ -174,24 +174,3 @@ export const getPlainText = (domNode: DOMNode) => { return text } - -/** - * there is no way to create a reverse DOM Range using Range.setStart/setEnd - * according to https://dom.spec.whatwg.org/#concept-range-bp-set. - * Luckily it's possible to create a reverse selection via Selection.extend - * - * Note: Selection.extend is not implement in any version of IE (up to and including version 11) - */ - -export const setReverseDomSelection = ( - domRange: DOMRange, - domSelection: Selection -) => { - const newRange = domRange.cloneRange() - // collapses the range to end - newRange.collapse() - // set both anchor and focus of the selection to domRange's focus - domSelection.addRange(newRange) - // moves the focus of the selection to domRange's anchor - domSelection.extend(domRange.startContainer, domRange.startOffset) -}