Undo and history
By the end of this page you have undo and redo buttons, and changes your code makes undo in one step.
Each thing a person does on the board is one undo step: a stroke, a drag, a resize, an erase. mod+Z undoes, and mod+Y or mod+Shift+Z redoes, where mod is Ctrl or ⌘.
Undo and redo buttons
viewport.undo() and viewport.redo() return false when there is nothing to undo or redo. viewport.history.canUndo and canRedo say whether there is, and history.onChange fires after every new step, undo, redo and clear:
const undo = document.querySelector<HTMLButtonElement>('#undo')!;
const redo = document.querySelector<HTMLButtonElement>('#redo')!;
undo.addEventListener('click', () => viewport.undo());
redo.addEventListener('click', () => viewport.redo());
const update = (): void => {
undo.disabled = !viewport.history.canUndo;
redo.disabled = !viewport.history.canRedo;
};
update();
viewport.history.onChange(update);
The board keeps the last 100 undo steps. loadJSON and loadState clear them, and a saved board doesn’t include them. The undo and redo keys do nothing while a pointer gesture is in progress.
What counts as one step
- A pointer gesture that changes the board, from press to release, under any tool. When a second finger cancels the gesture, the pencil, shape, arrow, note and text tools drop it and leave no step. The select and eraser tools finish it as a normal gesture, so what it did stays as one step.
- A finished note or text edit, including the resize that fits the text. Leaving the editor without a change records nothing.
- A run of arrow-key nudges, each less than 400ms after the one before.
- Each call of a viewport method that changes elements:
addShape,addImage,addHtmlElement,removeElements,removeLayer,applyStyleToSelection,groupSelection,ungroupSelection,alignSelection,distributeSelection,rotateSelectionandtoggleLockSelection, and the delete, duplicate and paste actions. - Each layer change, as Layers lists.
A store.add, store.update or store.remove call outside a transaction is a step of its own.
Group your own changes
Store and layer changes made inside viewport.transaction(fn) are one undo step, built-in methods included:
viewport.transaction(() => {
viewport.addShape({ position: { x: 0, y: 0 }, size: { w: 240, h: 160 } });
viewport.store.add(
createNote({
position: { x: 20, y: 20 },
text: 'Room 1',
layerId: viewport.layerManager.activeLayerId,
}),
);
});
- Only changes made before
fnreturns count. Changes after anawaitinside it are outside the transaction. - A transaction started inside another one, or during a pointer gesture, joins the open one.
- If
fnthrows, the changes it already made stay on the board as one undo step, and the error is thrown on. transactionreturns whatfnreturns.
Changes that skip undo
A store change whose origin is anything other than 'local' is not recorded. Edits from other people on a shared board arrive this way, so mod+Z undoes only your own. Use an origin for changes your app applies from its own data:
const note = createNote({
position: { x: 0, y: -140 },
text: 'Saved on the server',
layerId: viewport.layerManager.activeLayerId,
});
viewport.store.add(note, { origin: 'remote' });
Store listeners receive { origin } as their second argument, so your sync code can skip sending such changes back.