An operation-based history implementation for Slate editors providing undo/redo functionality.
—
The History object is the core data structure that manages undo and redo operation stacks. It contains batched operations and provides utilities for validating history objects.
The main data structure for storing operation history in undo and redo stacks.
interface History {
/** Array of operation batches for redo functionality */
redos: Batch[];
/** Array of operation batches for undo functionality */
undos: Batch[];
}Internal structure representing a group of related operations that should be undone/redone together.
interface Batch {
/** Array of Slate operations in this batch */
operations: Operation[];
/** Selection state before these operations were applied */
selectionBefore: Range | null;
}Batch Characteristics:
Utility for validating History objects.
/**
* Check if a value is a valid History object
* @param value - Value to check
* @returns True if value is a properly structured History object
*/
function isHistory(value: any): value is History;Usage Examples:
import { History } from "slate-history";
// Check if an object is a valid history
const historyData = { redos: [], undos: [] };
if (History.isHistory(historyData)) {
console.log("Valid history object");
}
// Access history from a history-enabled editor
if (HistoryEditor.isHistoryEditor(editor)) {
const history = editor.history;
// Check undo availability
const canUndo = history.undos.length > 0;
console.log(`Can undo: ${canUndo}`);
// Check redo availability
const canRedo = history.redos.length > 0;
console.log(`Can redo: ${canRedo}`);
// Get operation counts
console.log(`Undo batches: ${history.undos.length}`);
console.log(`Redo batches: ${history.redos.length}`);
}Undos Stack:
Redos Stack:
// When operations are applied:
// 1. New batch is added to undos stack
// 2. Redos stack is cleared
// When undo is called:
// 1. Last batch is removed from undos stack
// 2. Inverse operations are applied
// 3. Original batch is added to redos stack
// When redo is called:
// 1. Last batch is removed from redos stack
// 2. Original operations are reapplied
// 3. Batch is added back to undos stackBatch Creation:
Batch Limits:
The History.isHistory function performs comprehensive validation:
redos and undos propertiesOperation.isOperationList to validate operation structureExample Validation:
import { History } from "slate-history";
// Valid history objects
History.isHistory({ redos: [], undos: [] }); // true
History.isHistory({
redos: [{ operations: [], selectionBefore: null }],
undos: []
}); // true
// Invalid history objects
History.isHistory(null); // false
History.isHistory({ redos: "not array" }); // false
History.isHistory({ redos: [], undos: [{ invalid: "batch" }] }); // falseThe History object is automatically managed by the withHistory plugin:
import { withHistory, HistoryEditor } from "slate-history";
import { createEditor } from "slate";
const editor = withHistory(createEditor());
// History is automatically initialized
console.log(editor.history); // { redos: [], undos: [] }
// History is updated as operations are applied
editor.insertText("Hello");
console.log(editor.history.undos.length); // 1
// History stacks change with undo/redo
editor.undo();
console.log(editor.history.undos.length); // 0
console.log(editor.history.redos.length); // 1Install with Tessl CLI
npx tessl i tessl/npm-slate-history