facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
19.4k stars 1.64k forks source link

how to save all nodes with their unique ids in lexical file? #4330

Open ghost opened 1 year ago

ghost commented 1 year ago

The screenshot belongs to lexical file node tree which is downloaded from playground.lexical.dev. Nodes are not saved with their unique ids.

How can we make that all nodes are saved with their unique ids?

image

acywatson commented 1 year ago

Why do you want to do this? Those keys are not supposed to be stable across editors or sessions.

ghost commented 1 year ago

If user adds a checkbox item inside of a note, I want to be able to show them as tasks in other section of the app as well. And whenever the user edits, mark/unmark it as checked in one section(either task or note), it should synced across both sections.

So basically, I would like to create kind of synced block experience like Notion has (https://www.notion.so/help/synced-blocks). Actually Evernote has this feature as well which is called Tasks.

Would be happy to know how I could achieve this result.

image

joshdavenport commented 1 year ago

This is also useful for saving and restoring the editor state (to JSON, or whatever) and an associated selection, using the keys that where there when the content was created and saved.

Nantris commented 1 year ago

I'd be interested to know about this too. I assume there's some concept of storing data on a node which a user could then serialize and later deserialize, but I'm not sure what that might be based on my reading of the docs.

I hope such a concept exists?

zurfyx commented 1 year ago

In this case, you may want to extend or override the Node to have a unique ID of your own. The current __key is just meant to be unique within the page, retaining them can cause key collision when restoring the state onto another editor as @acywatson pointed out above.

For the same reason, if you introduce your own identifier you will have to resolve the merge conflicts yourself.

Nantris commented 1 year ago

Thanks for your reply @zurfyx. I guess in my mind I was thinking more about suggesting a way to add an additional, intentionally persistent ID to nodes rather than overriding the __key - but would doing so still result in the possibility of nodes in the editor with duplicated persistent IDs?

acywatson commented 1 year ago

but would doing so still result in the possibility of nodes in the editor with duplicated persistent IDs?

No, this is fine to do with something like a UUID.

Nantris commented 1 year ago

Awesome. So in createDOM devs could just use element.dataset.persistentid = uuidGen() - or does that also need to go in updateDOM? Or both? I'm not entirely clear on when each is invoked.

jiangyuhang0506 commented 6 months ago

Is there a solution to this problem? How to add a unique ID? I want to know how to add a unique ID. After the JSON with the ID goes to parseEditorState, it can then be toJSON, and this ID should be retained and returned to the backend interface

mayneyao commented 5 months ago

Has there been any progress :D