Undo / Redo
ReoGrid Web tracks edit history and supports undo/redo through keyboard shortcuts and a programmatic API.
Keyboard shortcuts
| Shortcut | Operation |
|---|---|
Ctrl+Z / ⌘+Z | Undo |
Ctrl+Y / ⌘+Y | Redo — or, with nothing to redo, repeat the last action |
F4 | Same as Ctrl+Y |
Ctrl+Shift+Z / ⌘+Shift+Z | Redo only |
While a cell is being edited, Ctrl+Z, Ctrl+Y and Ctrl+Shift+Z undo and redo the text of that edit only — a typing run, a deletion, a paste and an IME conversion are one step each — back to the text the editor opened with. Once the edit is committed, Ctrl+Z undoes it as one step. (Before v1.7.2 the editor kept the browser’s own history, so Ctrl+Z in one cell could bring back text deleted in another.)
Repeating the last action (F4 / Ctrl+Y)
Since v1.7.2, as in Excel, F4 (or Ctrl+Y / ⌘+Y, which does exactly the same) does the last action again on whatever is selected now — make one cell bold, select another range, press F4. A Ctrl+click selection of several ranges gets it on every range, and the repeat is an ordinary step that Ctrl+Z undoes. Available in both editions.
- What follows the new selection is the operation, not the old shape: an outside border outlines the new range, an inserted row count is the number of rows now selected, a paste lands on the selection’s top-left cell.
- Right after an undo,
F4/Ctrl+Yredo; they repeat only once there is nothing left to redo.Ctrl+Shift+Znever repeats. - Only the newest action counts. If it cannot be repeated,
F4does nothing.
| Repeats | Does not repeat |
|---|---|
| Style, number format, borders | Typing a value into a cell |
| Merge / unmerge | Auto-fill |
| Column width, row height | Moving cells, rows or columns by dragging |
| Insert / delete rows and columns | Sort, replace |
| Hide / show rows and columns | Image and page-break edits |
| Group / ungroup rows and columns | Cut & paste |
| Clear (Delete), paste |
From code:
actionManager.canRepeat(); // boolean — something to redo or repeat
actionManager.repeatLastAction(); // on the current selection
// or on a range of your choice
actionManager.repeatLastAction({
ranges: [{ topRow: 4, leftColumn: 1, bottomRow: 8, rightColumn: 3 }],
});
repeatLastAction() returns true when something was redone or repeated.
An ActionGroup repeats when it is one operation spread over cells or ranges (every child of the same class with the same settings — what createSetRangeStyleAction() builds). To make your own action repeatable, implement repeat(target) and return a new action for target.ranges (never this — it holds the undo state of the run already on the stack; return null when the operation is not possible there). Add repeatKey() — the settings without the location — if it should also repeat from inside a group.
ActionManager API
Control undo/redo programmatically through the ActionManager.
const { actionManager } = grid;
// Undo
actionManager.undo();
// Redo
actionManager.redo();
// Check state
actionManager.canUndo(); // boolean
actionManager.canRedo(); // boolean
// Clear history
actionManager.clear();
Monitoring undo/redo state
actionManager.onUndoRedoChange(() => {
console.log('canUndo:', actionManager.canUndo());
console.log('canRedo:', actionManager.canRedo());
});
Cell operations via Actions
When operations are executed through the ActionManager, they automatically support undo/redo.
Since v1.7.1
@reogrid/liteexports the actions for the features Lite has — cell values, styles, column widths and row heights, merges, borders, number formats, auto-fill, paste, clear and replace — along withActionManagerandActionGroup. The examples below import from@reogrid/pro; with Lite, import the same names from@reogrid/lite. The insert/delete, hide, sort and grouping actions are Pro only.
Changing cell values
import { SetCellInputAction } from '@reogrid/pro';
actionManager.execute(
new SetCellInputAction(0, 0, 'Hello')
);
Changing cell styles
import { SetCellStyleAction, createSetRangeStyleAction } from '@reogrid/pro';
// Single cell
actionManager.execute(
new SetCellStyleAction(0, 0, { bold: true })
);
// Range — the factory takes a name and an explicit cell list
const cells: Array<{ row: number; column: number }> = [];
for (let row = 0; row <= 4; row++) {
for (let column = 0; column <= 3; column++) {
cells.push({ row, column });
}
}
actionManager.execute(
createSetRangeStyleAction('Set range style', cells, { backgroundColor: '#dbeafe' })
);
Changing column widths / row heights
import { SetColumnWidthAction, SetRowHeightAction } from '@reogrid/pro';
actionManager.execute(new SetColumnWidthAction(0, 200));
actionManager.execute(new SetRowHeightAction(0, 40));
Cell merging
import { MergeCellsAction, UnmergeCellsAction } from '@reogrid/pro';
actionManager.execute(new MergeCellsAction(0, 0, 0, 4));
actionManager.execute(new UnmergeCellsAction(0, 0, 0, 4));
Borders
import { SetRangeBorderAction } from '@reogrid/pro';
actionManager.execute(
new SetRangeBorderAction(0, 0, 4, 3, {
style: 'solid',
color: '#000',
})
);
Inserting / deleting rows and columns
import {
InsertRowsAction, DeleteRowsAction,
InsertColumnsAction, DeleteColumnsAction,
} from '@reogrid/pro';
actionManager.execute(new InsertRowsAction(2, 3));
actionManager.execute(new DeleteRowsAction(2, 3));
actionManager.execute(new InsertColumnsAction(1, 2));
actionManager.execute(new DeleteColumnsAction(1, 2));
Showing / hiding rows and columns
import { SetRowHiddenAction, SetColumnHiddenAction } from '@reogrid/pro';
actionManager.execute(new SetRowHiddenAction([2, 3], true));
actionManager.execute(new SetColumnHiddenAction([1], false));
Sorting
import { SortColumnAction } from '@reogrid/pro';
actionManager.execute(new SortColumnAction({ column: 0, order: 'asc' }));
Grouping
import {
GroupRowsAction, UngroupRowsAction,
GroupColumnsAction, UngroupColumnsAction,
} from '@reogrid/pro';
actionManager.execute(new GroupRowsAction(1, 4));
actionManager.execute(new UngroupRowsAction(1, 4));
Paste and clear
import { PasteAction, ClearRangeAction } from '@reogrid/pro';
actionManager.execute(new ClearRangeAction(0, 0, 4, 3));
Available Actions
| Action | Description |
|---|---|
SetCellInputAction | Change cell value |
SetCellStyleAction | Change cell style |
createSetRangeStyleAction() | Change range style |
SetColumnWidthAction | Change column width |
SetRowHeightAction | Change row height |
SetRangeBorderAction | Change range border |
SetCellNumberFormatAction | Change number format |
MergeCellsAction | Merge cells |
UnmergeCellsAction | Unmerge cells |
InsertRowsAction | Insert rows |
DeleteRowsAction | Delete rows |
InsertColumnsAction | Insert columns |
DeleteColumnsAction | Delete columns |
SetRowHiddenAction | Show/hide rows |
SetColumnHiddenAction | Show/hide columns |
SortColumnAction | Sort column |
GroupRowsAction | Group rows |
UngroupRowsAction | Ungroup rows |
GroupColumnsAction | Group columns |
UngroupColumnsAction | Ungroup columns |
ToggleOutlineAction | Toggle outline collapse/expand |
CollapseToLevelAction | Collapse to level |
PasteAction | Paste |
ClearRangeAction | Clear range |
Protected cell checking
actionManager.execute() automatically checks cell protection. Changes to protected cells are rejected and false is returned.
const success = actionManager.execute(
new SetCellInputAction(0, 0, 'new value')
);
if (!success) {
console.log('Operation blocked by cell protection');
}
Undo capacity
By default, up to 30 undo steps are retained. This can be changed with the undoCapacity option in createReogrid().
const grid = createReogrid({
workspace: '#grid',
undoCapacity: 100,
});