From 1cc0797f53f22e650198c83192ba5fc35c525a15 Mon Sep 17 00:00:00 2001 From: Eric Meier Date: Sat, 23 Jul 2022 01:27:10 +0200 Subject: [PATCH] Fix toSlatePoint in void nodes with nested editors if children are rendered as the last child (#5054) --- .changeset/five-snails-travel.md | 5 +++++ packages/slate-react/src/plugin/react-editor.ts | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 .changeset/five-snails-travel.md diff --git a/.changeset/five-snails-travel.md b/.changeset/five-snails-travel.md new file mode 100644 index 0000000000..a1bff1668e --- /dev/null +++ b/.changeset/five-snails-travel.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fix toSlatePoint in void nodes with nested editors if children are rendered as the last child diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 636d9a2efd..ea1ea31384 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -553,8 +553,16 @@ export const ReactEditor = { } } else if (voidNode) { // For void nodes, the element with the offset key will be a cousin, not an - // ancestor, so find it by going down from the nearest void parent. - leafNode = voidNode.querySelector('[data-slate-leaf]')! + // ancestor, so find it by going down from the nearest void parent and taking the + // first one that isn't inside a nested editor. + const leafNodes = voidNode.querySelectorAll('[data-slate-leaf]') + for (let index = 0; index < leafNodes.length; index++) { + const current = leafNodes[index] + if (ReactEditor.hasDOMNode(editor, current)) { + leafNode = current + break + } + } // COMPAT: In read-only editors the leaf is not rendered. if (!leafNode) {