Skip to content

Commit

Permalink
Fix error when trying to grab selection of a void node while read only (
Browse files Browse the repository at this point in the history
#3894)

* fix(react-editor): text node spacer can be non-existant if editor is in readonly mode

* v0.59.1

* Revert "v0.59.1"

This reverts commit 58cb7f1.

* Update react-editor.ts

* Update react-editor.ts

* Create rude-lemons-crash.md

Co-authored-by: Ian Storm Taylor <ian@ianstormtaylor.com>
  • Loading branch information
msc117 and ianstormtaylor authored Mar 31, 2021
1 parent e344f47 commit 7fe41f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-lemons-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

Fixed an error that happened when selecting void nodes in a read-only editor.
19 changes: 12 additions & 7 deletions packages/slate-react/src/plugin/react-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,19 @@ 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]')!
textNode = leafNode.closest('[data-slate-node="text"]')!
domNode = leafNode
offset = domNode.textContent!.length
domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
offset -= el.textContent!.length
})

// COMPAT: In read-only editors the leaf is not rendered.
if (!leafNode) {
offset = 1
} else {
textNode = leafNode.closest('[data-slate-node="text"]')!
domNode = leafNode
offset = domNode.textContent!.length
domNode.querySelectorAll('[data-slate-zero-width]').forEach(el => {
offset -= el.textContent!.length
})
}
}

// COMPAT: If the parent node is a Slate zero-width space, editor is
Expand Down

0 comments on commit 7fe41f1

Please sign in to comment.