What is the browser undoing when the editor is unfocused?

In the example editor on https://codemirror.net:

  1. Type some text, then press Cmd/Ctrl+z to undo. The original text returns.
  2. Type some text, click outside the editor to focus somewhere else, then press Cmd/Ctrl+z a few times to undo. The last typed character will be removed (if there’s more than one character), then it reappears, then the original text returns.

What steps is the browser running through when the user presses Undo while the editor isn’t focused?

Chrome (and possibly Safari? you didn’t mention what you’re testing with) has some weird page-global undo history concept, which as far as I know no one uses or expects, but that seems to be what’s kicking in here. Because focus isn’t on the editor, keys are handled by the native browser history feature, which then mutates the editor content. To make things worse, it inexplicably doesn’t fire a beforeInput the way it normally does for history actions, so I haven’t found a way for the library to recognize or capture these.

1 Like