lexical icon indicating copy to clipboard operation
lexical copied to clipboard

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

Open ghost opened this issue 1 year ago • 10 comments

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

ghost avatar Apr 15 '23 04:04 ghost

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

acywatson avatar Apr 16 '23 00:04 acywatson

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

ghost avatar Apr 16 '23 01:04 ghost

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.

joshdavenport avatar Apr 20 '23 20:04 joshdavenport

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?

Nantris avatar Apr 20 '23 22:04 Nantris

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.

zurfyx avatar Apr 24 '23 13:04 zurfyx

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?

Nantris avatar Apr 25 '23 20:04 Nantris

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.

acywatson avatar Apr 25 '23 20:04 acywatson

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.

Nantris avatar Apr 25 '23 21:04 Nantris

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

jiangyuhang0506 avatar Mar 13 '24 01:03 jiangyuhang0506

Has there been any progress :D

mayneyao avatar Apr 19 '24 04:04 mayneyao