An operation-based history implementation for Slate editors providing undo/redo functionality.
—
The withHistory plugin is the core function that transforms a standard Slate editor into a history-enabled editor, adding comprehensive undo/redo functionality through operation tracking and intelligent batch management.
Creates a history-enabled editor by adding undo/redo functionality to any Slate editor.
/**
* Transforms a Slate editor into a history-enabled editor with undo/redo capabilities
* @param editor - Any Slate editor instance
* @returns Editor with HistoryEditor interface and history functionality
*/
function withHistory<T extends Editor>(editor: T): T & HistoryEditor;Usage Examples:
import { createEditor } from "slate";
import { withHistory } from "slate-history";
// Basic usage
const editor = withHistory(createEditor());
// With React
import { withReact } from "slate-react";
const reactEditor = withReact(withHistory(createEditor()));
// The editor now has history functionality
editor.undo(); // Undo last operation batch
editor.redo(); // Redo last undone batch
// Access history state
console.log(editor.history.undos.length); // Number of undo operations
console.log(editor.history.redos.length); // Number of redo operationsThe withHistory plugin enhances the editor with the following behavior:
Operation Tracking:
Intelligent Batching:
History Management:
Added Methods:
undo(): Reverts the last batch of operationsredo(): Reapplies the last undone batch of operationswriteHistory(): Manually adds operation batches to history stacksAdded Properties:
history: Contains the undo and redo operation stacksThe plugin includes intelligent operation merging to create logical undo/redo units:
Text Insertion Merging:
insert_text operations at the same path and offset are mergedText Deletion Merging:
remove_text operations at adjacent positions are mergedNon-mergeable Operations:
set_selection operations are never saved to historyPlugin Order:
withReact, apply withHistory first: withReact(withHistory(editor))Performance:
State Preservation:
Install with Tessl CLI
npx tessl i tessl/npm-slate-history