Detect history undo

Is it possible to detect wether an update was dispatched via undo (or redo)?

My use case: I want to react to document changes, but not if undo is dispatched.

I was thinking of setting a flag before executing undo(view) and unsetting it when the updateListener runs, but I’m still a codemirror beginner and can’t seem to get it working via a state field.
Any help is much appreciated!

You can use Transaction.isUserEvent for this, passing "undo" or "redo".

1 Like

Thanks a lot!
Just for completeness, here is a StateField that detects updates, which are neither undo nor redo transactions in TypeScript:

const detectChangeWithoutUndoRedo = StateField.define<boolean>({
    create: (_state: EditorState) => false,
    update: (v: boolean, tr: Transaction) => {
        if (tr.docChanged && !tr.isUserEvent("undo") && !tr.isUserEvent("redo")) {
            console.log("changed");
        }
        return v;
    }
});
// use `detectChangeWithoutUndoRedo.extension`