Add scope to UndoManager


I’m adding new Y.XmlFragments to a Y.Doc as a user creates new notes. I’d like to add these to a single Y.UndoManager which is tracking all changes across these notes.

Right now the scope for UndoManager is fixed at instantiation. It would be great to have, addScope & removeScope methods to adjust these as needed as new notes are created or deleted.

Without methods like this, it seems like the closest thing to make the above work would be to have a custom undo manager that tracks several separate undo managers and their stacks in one place.

What if I nest the XmlFragments for each note inside a central Map and just track that via the UndoManager, will it work?

1 Like

Hi Namit,

You are probably adding new notes like so, right? ydoc.getXmlFragment(RandomID()).

I suggest that you maintain notes in a Y.Array instead. Y.Map has some unnecessary overhead for each key created, but it is also certainly a valid option.

Then you can add the root-level Y.Array/Y.Map to the UndoManager.

I advise against defining dynamic keys on the root-level ydoc. Ideally, there is a fixed number of expected root-level types. One reason (there are many) is that you can’t delete root-level types. I.e. there is no method like ydoc.deleteXmlFragment(randomId).

1 Like

Thanks, yes, that’s what I was doing. I’ll move to use an Array in that case.

1 Like