# How to detect whether an update comes from history

**URL:** <https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393>\
**Category:** v6\
**Created:** [July 25, 2021, 10:10am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393 "2021-07-25T10:10:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmlenz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/cmlenz/32/1271_2.png) [@cmlenz](https://discuss.codemirror.net/u/cmlenz)\
**Post date:** [July 25, 2021, 10:10am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/1 "2021-07-25T10:10:24Z")

</div>

I am trying to integrate the CM6 history with an outer app undo stack.

For that purpose, I am looking for a way to detect whether a view update is coming from an undo/redo command. Here’s some stripped down code showing the approach:

```auto
const noop = () => true;

const state = EditorState.create({
  doc,
  extensions: [
    // Override the default keybindings, as these are handled by the app undo stack
    keymap.of([
      {key: 'Mod-z', run: noop},
      {key: 'Mod-y', mac: 'Mod-Shift-z', run: noop},
    ]),
    basicSetup,
    EditorView.updateListener.of(update => {
      // Can we determine whether the update was done by a undo/redo command?
      if (update.docChanged) {
        undoStack.push('Template Change',
          () => undo(update.view),
          () => redo(update.view)
        );
      }
    })
  ]
}));

```

(I know that the undo stack architecture is suboptimal, but this is an old code base)

The history module has the `fromHistory` annotation, but that is not exported.

Is there a way to handle this situation?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [July 26, 2021, 9:09am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/2 "2021-07-26T09:09:12Z")

</div>

Not currently, but it would probably be a good idea to make the history annotate the transactions it produces. Would having a [`userEvent`](https://codemirror.net/6/docs/ref/#state.Transaction%5EuserEvent) annotation for this be useful for you?

---

<div class="post-metadata">

**Author:** ![cmlenz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/cmlenz/32/1271_2.png) [@cmlenz](https://discuss.codemirror.net/u/cmlenz)\
**Post date:** [July 26, 2021, 9:25am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/3 "2021-07-26T09:25:25Z")

</div>

So the view would automatically set the `userEvent` to type `"undo"` or `"redo"`, right? That would make a lot of sense, and solve the problem I am facing.

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/marijn/32/136_2.png) [@marijn](https://discuss.codemirror.net/u/marijn)\
**Post date:** [August 9, 2021, 9:55am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/4 "2021-08-09T09:55:39Z")

</div>

Great. It’ll do this in the next major release (which I’m working on and should drop sometime this week).

---

<div class="post-metadata">

**Author:** ![cmlenz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/cmlenz/32/1271_2.png) [@cmlenz](https://discuss.codemirror.net/u/cmlenz)\
**Post date:** [August 17, 2021, 6:05am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/5 "2021-08-17T06:05:46Z")

</div>

I saw that this change landed in 0.19, so thanks a lot!

(I still have more work to do to properly integrate with the app undo stack, but this is a good step)

By the way, I wonder why you opted not to prefix the `undo` and `redo` events with `history`, so that we could more easily match using `transaction.isUserEvent('history')`. It works well as is, but a prefixed version would’ve been a bit more convenient.

---

<div class="post-metadata">

**Author:** ![cmlenz](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/cmlenz/32/1271_2.png) [@cmlenz](https://discuss.codemirror.net/u/cmlenz)\
**Post date:** [August 17, 2021, 7:24am UTC](https://discuss.codemirror.net/t/how-to-detect-whether-an-update-comes-from-history/3393/6 "2021-08-17T07:24:26Z")

</div>

I just saw that you also added `select.undo` and `select.redo`, which would explain why there’s no `history.` prefix.
