Undo Manager

Undo Manager of the DocNode

DocNode includes an undo manager for local transactions. Configure it on the Doc; it is disabled by default.

import { Doc } from "@docukit/docnode";

const doc = new Doc({
  type: "root",
  extensions: [MyExtension],
  undoManager: { maxUndoSteps: 100, mergeInterval: 500 },
});

doc.undoManager.undo(); // undo the last transaction
doc.undoManager.redo(); // redo the last undone transaction
doc.undoManager.canUndo(); // true if there are undo steps available
doc.undoManager.canRedo(); // true if there are redo steps available
doc.undoManager.isEnabled; // true when maxUndoSteps is greater than 0

If undoManager is omitted, maxUndoSteps defaults to 0.

Options

Prop

Type

Skipping Undo History

Use skipUndo for changes that should not become undoable, such as initial content, imports, or remote operations.

doc.forceCommit(() => seedInitialContent(doc), { skipUndo: true });
doc.applyOperations(remoteOperations, { skipUndo: true });

The first transaction created in the same microtask as a new Doc is skipped by the undo manager. This keeps initial document setup from becoming the first undo step.

Advanced: onPush and onPop

Most apps will not need these methods. They’re useful when binding data structures that need to restore external state, like selections in a rich text editor.

const offPush = doc.undoManager.onPush(({ meta }) => {
  meta.set("selection", currentSelection);
});

const offPop = doc.undoManager.onPop(({ meta }) => {
  const selection = meta.get("selection");
  restoreSelection(selection);
});

meta is an opaque Map owned by the stack item. Both callbacks also receive type, either "undo" or "redo".

On this page

Edit on GitHub