UNPKG

mind-elixir

Version:

Mind elixir is a free open source mind map core.

62 lines (61 loc) • 2.69 kB
/** * A document-agnostic undo/redo journal. * * Multiple "documents" (e.g. a MindElixir instance and an Outliner instance) * can share ONE HistoryStack. Every entry records which document it belongs to * plus the state before/after the operation, so undo/redo walks a single * linear timeline and dispatches the restore to the owning document. This is * what makes interleaved edits across documents undo seamlessly: * * push(map, ...) push(outline, ...) Ctrl+Z -> outline steps back * Ctrl+Z -> map steps back Ctrl+Shift+Z -> outline steps forward ... * * Each document registers a restorer via `register()`. The restorer receives * the entry and the direction and applies the corresponding snapshot to its * own document (mind-elixir uses `mei.refresh`, the outliner re-renders). */ export type HistoryDirection = 'undo' | 'redo'; /** Apply the entry's snapshot (before for undo, after for redo) to the owning document. */ export type DocRestorer = (entry: HistoryEntry, direction: HistoryDirection) => void; export interface HistoryEntry<S = any> { /** Identifier of the document this entry belongs to. */ doc: string; /** Snapshot before the operation. Treat as immutable — the stack keeps owning it. */ before: S; /** Snapshot after the operation. */ after: S; /** Free-form payload the owner uses to restore selection etc. */ meta?: any; time: number; } export declare class HistoryStack { private entries; /** Number of applied entries; `entries[index]` is the next redo target. */ private index; private restorers; private listeners; /** Attach a document to the stack. Returns an unregister function. */ register(doc: string, restore: DocRestorer): () => void; /** Subscribe to stack changes (push/undo/redo/clear), e.g. for an inspector UI. */ subscribe(fn: () => void): () => void; /** * Record an operation. `before`/`after` must be snapshots (clones) of the * document state; the stack never mutates them. * * Pushing while undone entries exist truncates the redo branch, matching the * classic linear-history behavior. */ push<S>(doc: string, before: S, after: S, meta?: any): void; get canUndo(): boolean; get canRedo(): boolean; get size(): number; get currentIndex(): number; getEntries(): readonly HistoryEntry[]; /** Step back one entry and restore the owning document. */ undo(): HistoryEntry | null; /** Step forward one entry and restore the owning document. */ redo(): HistoryEntry | null; /** Drop every entry. Document states stay as they are. */ clear(): void; private emit; }