Unique block IDs - avoiding duplicates

A common pattern is to assign unique ID attributes to block nodes, for database persistence.

It would be a problem of course if an ID got duplicated, e.g. via copy/paste or a command like splitBlock.

The copy/paste problem can be avoided in toDOM/parseDOM. But what about commands and transforms that might duplicate attributes?

Right now I am using a plugin to assign IDs to new nodes, and it first finds and clears any duplicates. However as my app supports embedding references to other nodes, there are situations where a duplicate is allowed, and the logic is getting quite complicated.

An alternative approach would be to take care of this at the command level, and insure that invalid duplicates can’t happen in the first place. That seems cleaner, but there’s potentially a large surface area to cover.

Just wondering if others have run into this issue and what solutions you’ve found.

There are also other complications, like old IDs coming back via undo, and correctly detecting deletes.



1 Like