Detecting a transaction made by undo/redo in y-prosemirror

Is it possible to detect if a prosemirror transaction was caused by an undo/redo in y-prosemirror? I tried to check isChangeOrigin, but it seems to be always true for transactions dispatched by y-prosemirror.

You could try to figure out the origin of the Yjs transaction (transaction.origin). If transaction.origin instanceof Y.UndoManager then the change originates from the undo manager. However, I’m not sure if you have access to the Yjs transaction within the prosemirror event…

An undo operation in prosemirror triggers the Y.UndoManager. Hence this is not a native ProseMirror change anymore. So isChangeOrigin is supposed to be false.

I don’t think I can access the Yjs transaction from the prosemirror transaction. I will try to detect undo/redo by registering an event in the UndoManager, but it would be nice to do everything inside the appendTransaction of prosemirror.

Testing here, even undo transactions have isChangeOrigin as true. Is this right?

No, I think undo-transactions should always be false. I haven’t verified it though.

I think the problem is here: y-prosemirror/sync-plugin.js at master · yjs/y-prosemirror · GitHub. It runs for external and undo transactions.

Right, sorry. Right, it’s the other way around. The expected origin is indeed ‘true’ because the change doesn’t originate from a ProseMirror transaction. ySyncPlugin sees that someone manipulated the Yjs type and creates another ProseMirror transaction with isChangeOrigin: true with the appropriate changes. Now that I look at it, it might be really easy to add another option { isUndoChange: true } that is true iff the change is fired by the local undo manager. Would that help?

If Yjs

It would be really helpful!

Done. isUndoRedoOperation is available in the 1.2.0 release.

1 Like