Skip to content

Undo/Redo Plugin

The Undo/Redo plugin tracks all cell edits and lets users revert or replay changes with familiar keyboard shortcuts (Ctrl+Z / Ctrl+Y). It 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 '@toolbox-web/grid/features/editing';
import '@toolbox-web/grid/features/undo-redo';

Enable both features and history tracking is automatic—every cell edit is recorded, and users can navigate back and forth through the edit history.

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name', editable: true },
{ field: 'price', header: 'Price', type: 'number', editable: true },
{ field: 'quantity', header: 'Quantity', type: 'number', editable: true },
],
features: {
editing: 'dblclick',
undoRedo: { maxHistorySize: 50 },
},
};

Edit cells by double-clicking, then use Ctrl+Z / Ctrl+Y to undo/redo. Adjust the max history size with the control.

Max history size Maximum undo steps to keep

See UndoRedoConfig for the full list of options and defaults.

ShortcutAction
Ctrl+Z / Cmd+ZUndo
Ctrl+Y / Cmd+Y / Cmd+Shift+ZRedo
const plugin = grid.getPluginByName('undoRedo');
plugin.undo();
plugin.redo();
const canUndo = plugin.canUndo();
const canRedo = plugin.canRedo();
plugin.clearHistory();

When a user edit cascades programmatic changes to other fields, group them into a single undo step so Ctrl+Z reverts everything at once:

grid.on('cell-commit', () => {
const undoRedo = grid.getPluginByName('undoRedo');
undoRedo.beginTransaction();
// Record cascaded updates manually
const oldTotal = row.total;
undoRedo.recordEdit(rowIndex, 'total', oldTotal, newTotal);
grid.updateRow(rowId, { total: newTotal });
// End after the auto-recorded original edit is captured
queueMicrotask(() => undoRedo.endTransaction());
});
MethodDescription
beginTransaction()Start buffering edits into a compound group
endTransaction()Finalize and push the compound as a single undo step
recordEdit()Manually record a cell edit (buffered during txn)
Double-click a cell to edit, then undo/redo.
Event Log:

Fired after an undo or redo action is applied:

grid.on('undo', ({ action, type }) => {
console.log(`${type}: reverted ${action.field} on row ${action.rowIndex}`);
});
grid.on('redo', ({ action, type }) => {
console.log(`${type}: reapplied ${action.field} on row ${action.rowIndex}`);
});

See UndoRedoDetail for the full event payload type.

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