API reference · generated from source
Function: createUndoHistory()#
function createUndoHistory(root, options?): IRetreeUndoHistory;Defined in: undoHistory.ts:135
Record every change under root into an undo/redo history built on
attributable change records.
Parameters#
| Parameter | Type | Description |
|---|---|---|
root | object | Retree-managed node to record; typically a Retree.root result. Only changes to this node and its descendants are recorded. |
options | RetreeUndoHistoryOptions | Optional RetreeUndoHistoryOptions. |
Returns#
An IRetreeUndoHistory controller.
Remarks#
The history subscribes to treeChanged on root and records each change
batch as one step: everything flushed by one Retree.runTransaction
is one step, and each discrete write outside a transaction is its own step
(see RetreeUndoHistoryOptions.coalesce to merge discrete writes).
undo() applies a step's inverse with Retree.applyInverse and
redo() replays it with Retree.applyChanges; both emit normally —
listeners and React re-render — without being recorded as new steps.
New writes recorded after an undo truncate the redo stack, exactly like a
text editor. Steps beyond limit (default 100) drop oldest-first.
Call dispose() during teardown to remove the treeChanged subscription.
Example#
const project = Retree.root({ tasks: [{ title: "Docs", done: false }] });
const history = createUndoHistory(project);
project.tasks[0].done = true;
project.tasks.push({ title: "Tests", done: false });
history.undo(); // ✅ push undone; tasks has one entry
history.undo(); // ✅ done back to false
history.redo(); // ✅ done true again
history.dispose();