Simple JavaScript undo/redo command manager supporting transactions with no dependencies.

Fork Of JavaScript Undo Manager 1.0.0, by Alexey Grinko, licensed under MIT License.

Changes -

  1. Added presets commands
  2. Port to TypeScript
  3. Remove transactions, see SetValueUndoCommand for a replacement JSUndoManager

Constructors

Properties

limit: number
options: JSUndoManagerOptions = ...
presets: Record<string, (c: JSUndoManagerCommand2) => JSUndoManagerCommand1> = {}

Presets for commands, allows to define command types and their handlers. A preset maps from a JSUndoManagerCommand2 to a JSUndoManagerCommand1.

To add a preset, simply set it on this object, or using Object.assign.

sp: number

Accessors

  • get enabled(): boolean
  • When false, nothing is recorded, undone or redone.

    Returns boolean

  • set enabled(value: boolean): void
  • Parameters

    • value: boolean

    Returns void

  • get hotKeysBound(): boolean
  • Whether the Ctrl/Cmd+Z, Ctrl+Y and Ctrl/Cmd+Shift+Z listener is bound (see bindHotKeys).

    Returns boolean

Methods

  • Be told whenever the stack changes: a step recorded, undone, redone or replaced, the stack reset, the limit changed, or the manager enabled/disabled. An undo history list or a menu's enabled state listens here instead of polling stack and sp. A continuous edit (a slider drag, recordUndoCommand) replaces the last step on each change, so a listener that redraws a list can wait for the next frame. A listener keeps what it closes over alive: remove it (the returned function) when its UI goes; dispose removes all of them.

    Parameters

    • listener: (e: JSUndoManagerChangeEvent) => void

    Returns () => void

    a function that removes the listener

  • Bind 'undo' and 'redo' to the hot keys, on options.hotKeyRoot (default document): Cmd+Z and Cmd+Shift+Z on a Mac, Ctrl+Z, Ctrl+Shift+Z and Ctrl+Y elsewhere. The keys follow the letter printed on the key (on a QWERTZ keyboard, Ctrl + the key labelled Z undoes), and its place when it types no Latin letter (Cyrillic, Greek). A key with Alt (AltGr on Windows types letters) or the other modifier is left alone. Calling it again does nothing. Keys typed in a text field or an editable element are left to it (shouldHandleKeyEvent). An app that handles the keys itself calls unbindHotKeys (at any time) and calls undo / redo.

    Returns JSUndoManager

  • Check whether redoing command is possible

    Returns boolean

  • Check whether undoing previous command is possible

    Returns boolean

  • Removes the hot key listener and clears the stack. The change listeners get the reset, then they are removed too, so a disposed manager keeps no listener (and no UI that it closes over) alive.

    Returns JSUndoManager

  • Get number of commands in memory stack

    Returns number

  • Check whether the commands stack is empty

    Returns boolean

  • Check whether the commands stack size reaches its limit

    Returns boolean

  • The label of a command: its own label, or for a preset command the label of what the preset makes of it. For a preset command without its own label this runs the preset on each call, so a history list of many steps can keep the labels, or give the commands a label.

    Parameters

    Returns undefined | string

  • Parameters

    • msg: string
    • ...args: any[]

    Returns void

  • Parameters

    • listener: (e: JSUndoManagerChangeEvent) => void

    Returns void

  • Whether the hot keys act on this key event. Default: not when it comes from a text field (an input that takes text, a textarea, a select) or an editable element, which keep their own undo (also inside an open shadow root, through composedPath), or when it was already handled (defaultPrevented). A focused checkbox, radio button, range, color input or button does not stop the keys. Override to change it. With nothing to undo or redo the key is not taken either.

    Parameters

    • e: KeyboardEvent

    Returns boolean