createCommandStack()
createCommandStack():
object
Creates a command stack with undo/redo support.
This stores actions (execute/undo pairs). When you undo, it calls the command's undo function. Use this when you have reversible operations.
For storing states (snapshots) instead of actions, see the Memento pattern's
createHistory which stores state snapshots and restores them on undo.
Returnsβ
Stack with execute, undo, redo, canUndo, canRedo, clear
execute(): (cmd) => voidβ
Execute a command and push it to the undo stack. Clears redo stack.
cmd:
UndoableCommand
Returns:void
undo(): () => booleanβ
Undo the last command. Returns false if nothing to undo.
Check canUndo first if the result matters to your logic.
Returns: booleanβ
redo(): () => booleanβ
Redo the last undone command. Returns false if nothing to redo.
Check canRedo first if the result matters to your logic.
Returnsβ
boolean
canUndoβ
Get Signature: > *get canUndo*(): booleanβ
Returnsβ
boolean
canRedoβ
Get Signature: > *get canRedo*(): booleanβ
Returns: booleanβ
clear(): () => voidβ
Clear all history.
Returns: voidβ
Sinceβ
2.4.0
Exampleβ
let value = 0;
const stack = createCommandStack();
stack.execute(undoable(() => value += 10, () => value -= 10));
stack.execute(undoable(() => value *= 2, () => value /= 2));
// value = 20
stack.undo(); // value = 10
stack.undo(); // value = 0
stack.redo(); // value = 10