Global document undo manager

I’d like to reset a Y.Doc to a previous state. My understanding is that I have to use an undo manager, but it seems to always be scoped to some shared types. In my case, I don’t know anything about the structure of the document. Is there a way for the undo manager to be globally scoped to the document?

I recommend that you know about the structure of the Y.Doc. Having “root-level” types with non-static names almost always has bad side effects. They should be know at “compile-time” (as far as that applies to JavaScript).

Nevertheless, you can implement what you are looking for. You could extract all the available types by doing Array.from(ydoc.share.keys()). Note that any root-level type added after initializing the undo-manager must be added manually.

Also, note that you can’t remove root-level types. Once they are initialized, you can only delete all their content. Root-level types are meant to be static.

The structure of the Y.Doc is static and well defined, I just want a mechanism to reset the document to a previous state, which is agnostic to the document. To give some context, I’m working on this suggestion system, where a fork of a document is created. If the suggestions are not merged back to the root document, I want to reset the document to its pre-fork state.
Thanks for the solution, I’ll try that. I’m wondering if that will work with nested shared types? Also, do shared types support multiple undo managers at the same time?

@dmonad It seems that ydoc.share is only available in Yjs, right? I couldn’t find something equivalent in Yrs.

In Yjs you do ydoc.getType(..) in Yrs you’d do ydoc.get_or_insert_type(..), where type is one of the supported types.

It shouldn’t be necessary to use ydoc.share, as you should know what types exist on a Y* document. You should access them using ydoc.getType(..). I should have kept this property hidden. Yrs doesn’t expose an equivalent.

Since you mentioned ydoc.share, I thought I’d give it a try. Or do you mean to remove this from the public API at some point?
Just to remind what I’m trying to do, I just want an undo manager to undo changes from a YDoc, in a document-agnostic way. I’d be happy even with the undo manager accepting a YDoc instead of shared types.