Skip to content
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: invalidate node maps when nodes change in-between paints #5694. #5746

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

DustinMackintosh
Copy link
Contributor

@DustinMackintosh DustinMackintosh commented Oct 18, 2024

Description
This fixes an editor crash where the user types quickly after earlier nodes have been added, split or deleted, but the node maps have not yet been regenerated on React render.

Issue
Fixes: (link to issue)

Example
Recording (from issue linked above)
https://github.com/user-attachments/assets/a68453b1-df47-4324-a542-9313569958ca

Sandbox (from issue linked above)
https://codesandbox.io/s/slate-selection-change-repro-ztssmp

Context
The NODE_TO_INDEX WeakMap is built on each React render as part of the useChildren hook. Both onDOMBeforeInput and onDOMSelectionChange use these maps to set the selection. This change introduces a new WeakMap called IS_NODE_MAP_DIRTY to track whether the underlying nodes have changed (eg: remove_node or insert_node), allowing those methods to skip attempts to setSelection that could crash the component.

Recommend you compare with "Ignore white space" selected.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

Copy link

changeset-bot bot commented Oct 18, 2024

🦋 Changeset detected

Latest commit: dcebdba

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

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

@Calyhre
Copy link
Collaborator

Calyhre commented Oct 23, 2024

We have the same issue on our side, and this patch seems to solve it 👍

Copy link
Collaborator

@dylans dylans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this looks reasonable. Obviously if it causes any major regressions we may need to revert it.

@dylans dylans merged commit e97a9f8 into ianstormtaylor:main Oct 24, 2024
11 checks passed
@github-actions github-actions bot mentioned this pull request Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

onDOMSelectionChange causes crash during concurrent updates
3 participants