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

[lexical] Feature: Add mutatedNodes to UpdateListener payload #7321

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

etrepum
Copy link
Collaborator

@etrepum etrepum commented Mar 12, 2025

Description

In order to better support NodeState and other use cases (e.g. some sort of "createdDOM/updateDOM" middleware) where it's useful to know about all nodes that were mutated in the DOM this adds the mutatedNodes property to UpdateListenerPayload and adds some API docs.

With this change it would be possible to implement mutation listeners as an updateListener so we could reduce some surface area in the future with a reduced core.

Test plan

Includes unit tests for expected behavior of the payload.

@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Mar 12, 2025
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 12, 2025
Copy link

vercel bot commented Mar 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 12, 2025 6:42pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 12, 2025 6:42pm

Comment on lines +134 to +142
for (const [klass, m] of mutatedNodes) {
if ($isElementNode(klass.prototype)) {
for (const [nodeKey, value] of m) {
if (value === 'destroyed') {
dirtyElements.set(nodeKey, true);
}
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems weird, in the case when we do $getRoot().clear() the key of the destroyed ParagraphNode appears in here even though it's not in the editor state. The same behavior doesn't happen for destroyed leaves. IMO we should probably have an efficient way to see which nodes were deleted between prevEditorState and editorState without any DOM dependency (without including ephemeral nodes that were created and then GC'd in the same reconciliation cycle).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks like this behavior is intentional-ish but there's no way to know about deleted leaves https://github.com/facebook/lexical/blob/main/packages/lexical/src/LexicalGC.ts#L103-L107

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My preference would be to leave this alone for now. I don't really have a use case for dirtyElements or dirtyLeaves and you can't really do anything sound with dirtyElements+dirtyLeaves because they aren't coherent when setEditorState has happened (historic events).

Everything on my plate is better solved by using this new mutatedNodes prop. Just figured I'd point it out while I was in here writing the test coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants