-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add node replacement system to core #3367
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
d66b25e
to
689db9c
Compare
Should name be more generic to allow easier extension in future? Eg if we introduce something else than replace/with options $nodeFactory/$createNode/etc |
I'm not sure we'd ever want to introduce $nodeFactory/$createNode as it would be such a large breaking change that it would cause a ton of churn for no real gains. |
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.
Thank you @trueadm! Stamping to unblock but I share the same sentiment as @fantactuka. I feel like a more generic name (just the term) would give us space to expand this in the future with further validation or other stuff without a breaking changes. I am personally for $createNote
@@ -1263,3 +1263,29 @@ export function $copyNode<T extends LexicalNode>(node: T): T { | |||
$setNodeKey(copy, null); | |||
return copy; | |||
} | |||
|
|||
export function $applyNodeReplacement<N extends LexicalNode>( | |||
node: LexicalNode, |
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.
node: LexicalNode, | |
node: N, |
This will guarantee that the return is the same or an extended node?
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.
They aren't the same though. The extended node is a subclass of N, not N.
@zurfyx I don't understand how |
I was thinking that something like |
|
This looks right. Nice work. |
Actually what happens if I override TextNode or something and then copy and paste into a Lexical editor that doesn't have that override? I guess we'll fallback to HTML. Maybe that's the best we can do here. |
* Add node replacement system to core * Add test * Fix missing logic * Fix prettier * Update flow types
@trueadm can you please post a concrete example on how to use this:
E.g. I want to replace the edit: actually, I just found this in the docs and the code in the sandbox is exactly what I was looking for: https://lexical.dev/docs/concepts/node-replacement |
This PR extends on the work from #2924.
Instead of defining a proxy that the user must remember to invoke, we can instead opt nodes into replacement in their factory function
$createXNode
using the new exported function$applyNodeReplacement
. Then when defining the nodes array for editor config, we can supply an object in the form of:This should address the issues on the above PR.