resig.js
Version:
Universal reactive signal library with complete platform features: signals, animations, CRDTs, scheduling, DOM integration. Works identically across React, SolidJS, Svelte, Vue, and Qwik.
76 lines (75 loc) • 2.57 kB
TypeScript
/**
* Undo/Redo System
* Uses Memento<State> with coalgebraic time-travel patterns
*/
import { Signal } from '../core/signal';
export interface Memento<T> {
readonly state: T;
readonly timestamp: number;
readonly id: string;
readonly metadata?: Record<string, any>;
}
export interface Command<T> {
readonly id: string;
readonly name: string;
readonly timestamp: number;
execute(state: T): T;
undo(state: T): T;
canMerge?(other: Command<T>): boolean;
merge?(other: Command<T>): Command<T>;
}
export interface TimeTravel<T> {
readonly currentIndex: number;
readonly history: Memento<T>[];
readonly future: Memento<T>[];
readonly canUndo: boolean;
readonly canRedo: boolean;
readonly totalStates: number;
}
export interface UndoRedoConfig {
maxHistorySize?: number;
autoMerge?: boolean;
mergeTimeout?: number;
persistKey?: string;
compressHistory?: boolean;
snapshotInterval?: number;
}
export declare class UndoRedoManager<T> {
private config;
private history;
private future;
private currentIndex;
private currentState;
private commands;
private lastCommand;
private mergeTimer;
constructor(initialState: T, config?: UndoRedoConfig);
private createMemento;
private deepClone;
private setupAutoMerge;
private loadPersistedHistory;
private persistHistory;
private compressHistory;
execute(command: Command<T>): void;
private executeInternal;
undo(): boolean;
redo(): boolean;
jumpTo(index: number): boolean;
createSnapshot(id?: string): void;
getTimeTravel(): Signal<TimeTravel<T>>;
getCurrentState(): Signal<T>;
getHistory(): Signal<Memento<T>[]>;
getCommands(): Signal<Command<T>[]>;
clearHistory(): void;
getStateAt(index: number): T | null;
canUndo(): boolean;
canRedo(): boolean;
destroy(): void;
}
export declare const createUndoRedoManager: <T>(initialState: T, config?: UndoRedoConfig) => UndoRedoManager<T>;
export declare const createCommand: <T>(id: string, name: string, execute: (state: T) => T, undo: (state: T) => T, options?: {
canMerge?: (other: Command<T>) => boolean;
merge?: (other: Command<T>) => Command<T>;
}) => Command<T>;
export declare const createTextCommand: (id: string, type: 'insert' | 'delete' | 'replace', position: number, text: string, previousText?: string) => Command<string>;
export declare const createArrayCommand: <T>(type: 'push' | 'pop' | 'shift' | 'unshift' | 'splice', ...args: any[]) => Command<T[]>;