Skip to content

UndoRedoPlugin

Undo/Redo Plugin for tbw-grid

Tracks all cell edits and lets users revert or replay changes with familiar keyboard shortcuts (Ctrl+Z / Ctrl+Y). Maintains an in-memory history stack with configurable depth—perfect for data entry workflows where mistakes happen.

> Required Dependency: This plugin requires EditingPlugin to be loaded first. > UndoRedo tracks the edit history that EditingPlugin creates.

import { EditingPlugin } from '@toolbox-web/grid/plugins/editing';
import { UndoRedoPlugin } from '@toolbox-web/grid/plugins/undo-redo';
OptionTypeDefaultDescription
maxHistorySizenumber100Maximum actions in history stack
ShortcutAction
Ctrl+Z / Cmd+ZUndo last edit
Ctrl+Y / Cmd+Shift+ZRedo last undone edit
MethodSignatureDescription
undo() => UndoRedoAction | nullUndo the last edit (or compound)
redo() => UndoRedoAction | nullRedo the last undone edit (or compound)
canUndo() => booleanCheck if undo is available
canRedo() => booleanCheck if redo is available
clearHistory() => voidClear the entire history stack
recordEdit(rowIndex, field, old, new) => voidManually record a cell edit
beginTransaction() => voidStart grouping edits into a compound
endTransaction() => voidFinalize and push the compound action
import { queryGrid } from '@toolbox-web/grid';
import { EditingPlugin } from '@toolbox-web/grid/plugins/editing';
import { UndoRedoPlugin } from '@toolbox-web/grid/plugins/undo-redo';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name', editable: true },
{ field: 'price', header: 'Price', type: 'number', editable: true },
],
plugins: [
new EditingPlugin({ editOn: 'dblclick' }), // Required - must be first
new UndoRedoPlugin({ maxHistorySize: 50 }),
],
};

Extends BaseGridPlugin

Inherited methods like attach(), detach(), afterRender(), etc. are documented in the base class.

Record a cell edit for undo/redo tracking. Call this when a cell value changes.

recordEdit(rowIndex: number, field: string, oldValue: unknown, newValue: unknown): void
NameTypeDescription
rowIndexnumberThe row index where the edit occurred
fieldstringThe field (column key) that was edited
oldValueunknownThe value before the edit
newValueunknownThe value after the edit

Begin grouping subsequent edits into a single compound action.

While a transaction is active, all recordEdit() calls (both manual and auto-recorded from cell-edit-committed) are buffered instead of pushed to the undo stack. Call endTransaction() to finalize the group.

Typical usage — group a user edit with its cascaded side-effects:

grid.on('cell-commit', () => {
const undoRedo = grid.getPluginByName('undoRedo');
undoRedo.beginTransaction();
// Record cascaded updates (these won't auto-record)
const oldB = row.fieldB;
undoRedo.recordEdit(rowIndex, 'fieldB', oldB, computedB);
grid.updateRow(rowId, { fieldB: computedB });
// End after the auto-recorded original edit is captured
queueMicrotask(() => undoRedo.endTransaction());
});
beginTransaction(): void

Finalize the current transaction, wrapping all buffered edits into a single compound action on the undo stack.

  • If the buffer contains multiple edits, they are wrapped in a CompoundEditAction.
  • If the buffer contains a single edit, it is pushed as a regular EditAction.
  • If the buffer is empty, this is a no-op.

Undoing a compound action reverts all edits in reverse order; redoing replays them in forward order.

endTransaction(): void

Programmatically undo the last action.

undo(): UndoRedoAction | null

UndoRedoAction | null - The undone action, or null if nothing to undo


Programmatically redo the last undone action.

redo(): UndoRedoAction | null

UndoRedoAction | null - The redone action, or null if nothing to redo


Check if there are any actions that can be undone.

canUndo(): boolean

Check if there are any actions that can be redone.

canRedo(): boolean

Clear all undo/redo history.

clearHistory(): void

Get a copy of the current undo stack.

getUndoStack(): UndoRedoAction[]

Get a copy of the current redo stack.

getRedoStack(): UndoRedoAction[]

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt