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 }