From f4ee75d13d219214fd12c1b10448098f4c070b77 Mon Sep 17 00:00:00 2001 From: Cameron Ackerman Date: Tue, 5 May 2020 10:31:01 -0700 Subject: [PATCH] fix issue #3651 where initial click on a void block can cause the editor to crash (#3654) * fix issue #3651 where initial click on a void block can cause the editor to crash * simplify non null cast for editor to element weakmap retrieve * get prettier to shut up * fixed comment --- packages/slate-react/src/components/editable.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index cf5424a7e..08b028230 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -145,9 +145,20 @@ export const Editable = (props: EditableProps) => { return } - // If the DOM selection is already correct, we're done. + // verify that the dom selection is in the editor + const editorElement = EDITOR_TO_ELEMENT.get(editor)! + let hasDomSelectionInEditor = false + if ( + editorElement.contains(domSelection.anchorNode) && + editorElement.contains(domSelection.focusNode) + ) { + hasDomSelectionInEditor = true + } + + // If the DOM selection is in the editor and the editor selection is already correct, we're done. if ( hasDomSelection && + hasDomSelectionInEditor && selection && Range.equals(ReactEditor.toSlateRange(editor, domSelection), selection) ) {