1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 02:34:05 +02:00

Fix crash when a void node deletes itself on click (#4616)

* Fix crash when a void node deletes itself on click

Fixes https://github.com/ianstormtaylor/slate/issues/4240

* Add 'image delete' feature to example

My immediate motivation is to demonstrate the bug that this fixes. But
this is also a very common editor feature, and I think it's valuable
to show how to achieve it.

* add changeset

* fix:eslint

* revert changes to mentions.tsx
This commit is contained in:
Jim Fisher
2021-10-24 15:31:00 +01:00
committed by GitHub
parent b50a772136
commit 77d9f60ab5
3 changed files with 63 additions and 12 deletions

View File

@@ -706,19 +706,29 @@ export const Editable = (props: EditableProps) => {
) {
const node = ReactEditor.toSlateNode(editor, event.target)
const path = ReactEditor.findPath(editor, node)
const start = Editor.start(editor, path)
const end = Editor.end(editor, path)
const startVoid = Editor.void(editor, { at: start })
const endVoid = Editor.void(editor, { at: end })
// At this time, the Slate document may be arbitrarily different,
// because onClick handlers can change the document before we get here.
// Therefore we must check that this path actually exists,
// and that it still refers to the same node.
if (Editor.hasPath(editor, path)) {
const lookupNode = Node.get(editor, path)
if (lookupNode === node) {
const start = Editor.start(editor, path)
const end = Editor.end(editor, path)
if (
startVoid &&
endVoid &&
Path.equals(startVoid[1], endVoid[1])
) {
const range = Editor.range(editor, start)
Transforms.select(editor, range)
const startVoid = Editor.void(editor, { at: start })
const endVoid = Editor.void(editor, { at: end })
if (
startVoid &&
endVoid &&
Path.equals(startVoid[1], endVoid[1])
) {
const range = Editor.range(editor, start)
Transforms.select(editor, range)
}
}
}
}
},