@macfja/svelte-undoable
Version:
Memento design pattern in Svelte
69 lines (68 loc) • 2.71 kB
TypeScript
import { Writable } from "svelte/store";
/**
* Undoable store.
*/
export interface UndoableStore<T> extends Writable<T> {
/**
* Change the value to the previous saved state
*/
undo(): void;
/**
* Change the value to the next saved state
*/
redo(): void;
/**
* Indicate if the store value can be revert to a previous state
*/
canUndo(): boolean;
/**
* Indicate if the store value can be change to a next state
*/
canRedo(): boolean;
/**
* Revert the value of the store to the oldest state.
* If the parameter is `true`, then the store state history is cleared
* @param {boolean?} clear If `true` the history is cleared
*/
reset(clear?: boolean): void;
length(): number;
}
/**
* Create a store with undo/redo feature
* @param {*} initial The initial value of the store
* @param {number?} capacity The maximum number of entry to remember (any number lower than 2 is considered as infinite size)
* @param {function(*: newValue): boolean} accept The validation function to accept a value to be save in memory.<br/>
* Take the new store value as parameter, should return a boolean (`true` to save the value, `false` to dismiss it).<br/>
* If ignore, it will accept all value
* @return {UndoableStore<*>}
*/
export declare function undoable<T>(initial: T, capacity?: number, accept?: (newValue: T) => boolean): UndoableStore<T>;
/**
* Change the value to the previous saved state
* @param {UndoableStore<*>} undoableStore The store to use
*/
export declare function undo<T>(undoableStore: UndoableStore<T>): void;
/**
* Change the value to the next saved state
* @param {UndoableStore<*>} undoableStore The store to use
*/
export declare function redo<T>(undoableStore: UndoableStore<T>): void;
/**
* Indicate if the store value can be revert to a previous state
* @param {UndoableStore<*>} undoableStore The store to use
* @return {boolean}
*/
export declare function canUndo<T>(undoableStore: UndoableStore<T>): boolean;
/**
* Indicate if the store value can be change to a next state
* @param {UndoableStore<*>} undoableStore The store to use
* @return {boolean}
*/
export declare function canRedo<T>(undoableStore: UndoableStore<T>): boolean;
/**
* Revert the value of the store to the oldest state.
* If the second parameter is `true`, then the store state history is cleared
* @param {UndoableStore<*>} undoableStore The store to use
* @param {boolean} clear If `true` the history is cleared
*/
export declare function reset<T>(undoableStore: UndoableStore<T>, clear?: boolean): void;