Document: getDocument, setDocument, toJSON, getState, subscribe, and destroy.
Pages: list, read, add, update, remove, reorder, and activate.
Elements: list, read, update, move between pages, and remove.
Editing: typed product dispatch, transaction, undo, redo, history status, and clearHistory.
Product controllers share these verbs but accept separate command unions and protect foreign elements.
import { createCanvasController } from "@pixxl-tools/cangraph";
const canvas = createCanvasController();
try {
canvas.transaction(() => {
canvas.dispatch({
type: "cangraph/node/add",
data: { label: "Start" },
transform: { x: 0, y: 0, width: 160, height: 80 },
options: { id: "start" },
});
}, { label: "Add start node" });
const json = canvas.toJSON();
} finally {
canvas.destroy();
}One runtime owns history. React UI reads status from that runtime instead of maintaining a second undo ledger.
One outer transaction creates at most one complete-document before/after entry; a thrown transaction rolls back atomically.
New edits truncate redo. History is bounded and defaults to 100 entries.
setDocument resets history unless history: record is explicitly requested.
Selection, hover, active page, and viewport-only changes are not semantic history entries.
Subscribe once per host lifecycle and release the listener before controller destruction.
Do not call history control from inside an active document transaction.
Use controller events as invalidation signals; query current state instead of retaining stale mutable references.
const unsubscribe = canvas.subscribe((event) => {
if (event.type === "history") {
updateUndoButtons({
canUndo: canvas.canUndo(),
canRedo: canvas.canRedo(),
});
}
});
// Host teardown
unsubscribe();
canvas.destroy();