Skip to content

Add addListeners / removeListeners with configurable keyboard shortcuts#14

Open
math-krish wants to merge 1 commit intorocicorp:mainfrom
math-krish:feature/add-keypress-listeners
Open

Add addListeners / removeListeners with configurable keyboard shortcuts#14
math-krish wants to merge 1 commit intorocicorp:mainfrom
math-krish:feature/add-keypress-listeners

Conversation

@math-krish
Copy link
Copy Markdown

@math-krish math-krish commented Mar 29, 2026

Closes #2

I just want to say, I love your implementation! I'm not entirely sure if this works in replicache, but I tested it with a plain react application. Please let me know if there's anything you'd like me to change

What’s Changed

Adds optional keyboard shortcut support to UndoManager via two new methods:

undoManager.addListeners(window)
undoManager.removeListeners(window)


New Constructor Options

Option Type Default Description
enableShortcuts boolean false Opt-in to keyboard shortcut handling
shortcuts ShortcutOptions see below Per-platform key binding overrides


Key Binding Type

KeyBinding is typed as a subset of the browser’s native KeyboardEventInit:

  • key

  • shiftKey

  • altKey

  • ctrlKey

  • metaKey

No extra dependencies — fully standard.


Platform Detection

Uses event.metaKey and event.ctrlKey directly from the event.

No deprecated navigator.platform.


Example

const undoManager = new UndoManager({
  enableShortcuts: true,
  shortcuts: {
    other: {
      undo: { key: 'z' },
      redo: [{ key: 'y' }, { key: 'z', shiftKey: true }],
    },
    mac: {
      undo: { key: 'z' },
      redo: [{ key: 'z', shiftKey: true }],
    },
  },
})

undoManager.addListeners(window)

// later...
undoManager.removeListeners(window)


Testing

  • 11 new unit tests covering:

    • Default shortcuts

    • Custom bindings

    • enableShortcuts: false

    • metaKey vs ctrlKey paths

    • removeListeners cleanup


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add keypress event handlers

1 participant