History Editor API

PreviousNext

Read history state, run undo/redo transactions, and control history batches.

History adds read and transaction groups instead of mutating the root editor object.

State API

state.history(): History

Read the full history value.

state.history.undos(): Batch[]

Read the undo stack.

state.history.redos(): Batch[]

Read the redo stack.

Transaction API

tx.history.undo(): void

Undo the previous history batch.

tx.history.redo(): void

Redo the next history batch.

tx.history.skip(): void

Do not save the current transaction to history.

tx.history.merge(): void

Merge the current transaction into the previous compatible undo batch.

tx.history.newBatch(): void

Start a fresh undo batch for the current transaction.

Update Policy

editor.update({ history: "skip" }, fn): void

Run one update without saving it to history.

editor.update({ history: "merge" }, fn): void

Run one update that merges into the previous compatible undo batch.

editor.update({ history: "new-batch" }, fn): void

Run one update where the first operation starts a fresh history batch, then the rest of the callback merges into that batch.

Configure a direct write by calling editor.update(policy) first.

editor.update({ history: "skip" }).text.insert("draft");
editor.update({ history: "skip" }).text.insert("draft");

editor.update.history.undo(): void

Undo the previous history batch outside a larger transaction.

editor.update.history.redo(): void

Redo the next history batch outside a larger transaction.

Types

import type { EditorStatePatch, Operation, Range } from "@platejs/plite";
 
type History = {
  redos: Batch[];
  undos: Batch[];
};
 
type Batch = {
  operations: Operation[];
  selectionBefore: Range | null;
  selectionBeforeRoot?: string;
  statePatches: EditorStatePatch[];
};
import type { EditorStatePatch, Operation, Range } from "@platejs/plite";
 
type History = {
  redos: Batch[];
  undos: Batch[];
};
 
type Batch = {
  operations: Operation[];
  selectionBefore: Range | null;
  selectionBeforeRoot?: string;
  statePatches: EditorStatePatch[];
};