From f7f02a8b23f16a3f3103302343e3326549917892 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Tue, 7 Mar 2023 17:15:59 +0000 Subject: [PATCH] Fix triple-click `contenteditable="false"` error (#5343) --- .changeset/lucky-falcons-reflect.md | 5 +++++ packages/slate-react/src/plugin/react-editor.ts | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 .changeset/lucky-falcons-reflect.md diff --git a/.changeset/lucky-falcons-reflect.md b/.changeset/lucky-falcons-reflect.md new file mode 100644 index 0000000000..c9626aab7b --- /dev/null +++ b/.changeset/lucky-falcons-reflect.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fix error when triple-clicking a word preceding a `contenteditable="false"` DOM node in Chrome diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 783b99e28a..f930db4678 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -754,6 +754,17 @@ export const ReactEditor = { ) } + // COMPAT: Triple-clicking a word in chrome will sometimes place the focus + // inside a `contenteditable="false"` DOM node following the word, which + // will cause `toSlatePoint` to throw an error. (2023/03/07) + if ( + 'getAttribute' in focusNode && + (focusNode as HTMLElement).getAttribute('contenteditable') === 'false' + ) { + focusNode = anchorNode + focusOffset = anchorNode.textContent?.length || 0 + } + let anchor = ReactEditor.toSlatePoint(editor, [anchorNode, anchorOffset], { exactMatch, suppressThrow,