lexical: Bug: Undo hotkey stops working in a specific case

Lexical version: lexical: 0.17.1

Steps To Reproduce

  1. Switch to a non-Latin keyboard layout (e.g. Greek layout)
  2. Open Lexical editor with a non-empty content (playground is a good variant)
  3. Select all content
  4. Press the Backspace (or Delete) key to clear all content
  5. Press the Ctrl+Z(undo) hotkey

Link to code example: https://playground.lexical.dev/

The current behavior

Nothing happens

The expected behavior

The editor restores the last state from the undoStack field of the history when you press the Ctrl + Z hotkey.

By default, in the LexicalEvents.ts file there are two points that dispatch the UNDO_COMMAND command:

  • onKeyDown listener (by checking whether event.ctrlKey is true and event.key equals to z character)
  • onBeforeInput listener (by checking whether event.inputType equals to historyUndo)

When the content has been cleared as described above (in the Steps To Reproduce section) only the first listener (onKeyDown) is called and never the second one.

For Latin layout, it works correctly because it passes the isUndo function test. But it doesn’t work for other layouts, since the event.key value is not the ‘z’ character.

However, if we modify the content (e.g. type in any character) before step 3 and then execute steps 3-5 everything will work as expected. The onBeforeInput event is called and the previous state is restored.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (11 by maintainers)

Most upvoted comments

@etrepum Sound good, I should have some time next week and can take this ticket. Please feel free to assign to me if it’s ok.

The contributors spent some time discussing this issue and we are open to accepting a PR that changes the hotkey logic to work more like it did before #6110 - even better if it lifts this logic out a bit so that it’s easier to customize

If you do have a systematic way of solving this, a PR would be appreciated. Similar issues may affect other built-in keyboard shortcuts.

The fix would be to update isUndo in LexicalEvents.ts with whatever the correct code is to detect that specific shortcut with all supported keyboards and platforms.

For a workaround, you would add a KEY_DOWN_COMMAND listener that handles the event how you want to, something like:

const unreg = editor.registerCommand(KEY_DOWN_COMMAND, (event) => {
  if (isUndo(event)) {
    event.preventDefault();
    dispatchCommand(editor, UNDO_COMMAND, undefined);
    return true;
  }
  return false;
});