-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix crash when a void node deletes itself on click #4616
Fix crash when a void node deletes itself on click #4616
Conversation
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.
🦋 Changeset detectedLatest commit: a7cbe9b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Note: I think an alternative fix could use |
(An alternative "fix" would be to remove the feature that selects void nodes when they're clicked. It's not clear to me that this is actually useful, and devs can always add this behavior themselves.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the simpler option (compared to the other possible options you proposed), so let's go with this and see if it works sufficiently.
Sorry, I broke this test in ianstormtaylor#4616 by adding a second image to the example!
Sorry, I broke this test in #4616 by adding a second image to the example!
Description
Here's a reproduction of the bug that this fixes. Here's what's happening:
n
is rendered to a DOM elementel
with anonClick
event that deletesn
from the Slate document.el
is clicked, theonClick
handler runs, and the noden
is deleted.click
event then propagates to this top-levelComponent.onClick
handler, which wants to implement "if the user clicked on a void node, select it".Component.onClick
wants to know whether we clicked on a void node, so it consults a reverse mapping from DOM elements back to Slate nodes, looking up the clicked DOM elementel
.el
, because its customonClick
handler has run already, which deleted its own Slate node (and in general, could have arbitrarily changed the Slate document).This PR fixes the issue by adding some checks that the node is still at that path, before selecting that node.
This PR also adds an 'image delete' feature to the examples. 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.
Issue
Fixes: #4240
Example
slate-delete-void.mp4
Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)