From a9da0eb6d1f4c28fcef39c285e252c10ca84841f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claud=C3=A9ric=20Demers?= Date: Tue, 21 Feb 2023 22:41:34 +0000 Subject: [PATCH] Avoid selection reconciliation on Android when there are pending changes --- .changeset/bugfix-android-selection.md | 5 +++++ packages/slate-react/src/components/editable.tsx | 14 +++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 .changeset/bugfix-android-selection.md diff --git a/.changeset/bugfix-android-selection.md b/.changeset/bugfix-android-selection.md new file mode 100644 index 000000000..18e1c5db8 --- /dev/null +++ b/.changeset/bugfix-android-selection.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fixed a bug on Android where Slate would attempt to reconcile the DOM selection with Slate's internal selection while there are pending diffs or while the pending diffs are being flushed, which could result in an error being thrown because of a mismatch between the state of the DOM and Slate's internal representation if they were out of sync. diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 7e8a2e8e4..a521349f4 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -266,16 +266,20 @@ export const Editable = (props: EditableProps) => { NODE_TO_ELEMENT.delete(editor) } + if ( + !ReactEditor.isFocused(editor) || + androidInputManager?.hasPendingChanges() || + androidInputManager?.isFlushing() + ) { + return + } + // Make sure the DOM selection state is in sync. const { selection } = editor const root = ReactEditor.findDocumentOrShadowRoot(editor) const domSelection = root.getSelection() - if ( - !domSelection || - !ReactEditor.isFocused(editor) || - androidInputManager?.hasPendingAction() - ) { + if (!domSelection) { return }