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) {