undo-peasy
Version:
undo/redo for easy peasy
38 lines (37 loc) • 1.6 kB
TypeScript
import { UndoOptions } from "./Actions";
import { AnyObject } from "./Utils";
export declare const keyPrefix = "undo-redo-";
export declare const currentKey: string;
export declare const oldestKey: string;
/** Store a stack of undo/redo state objects in localStorage.
*
* Each undo redo state is stored in a separate key/value.
* The oldest undo state is stored with the key: "undo-redo-0",
* more recent states are "-1", "-2", etc.
*
* The current state is stored in the key "undo-redo-state-current"
* The oldest state is stored in the key "undo-redo-state-oldest"
*
* If the number of saved states would exceed maxHistory, the oldest
* state is dropped. (e.g. after dropping one state, the oldest state
* becomes "undo-redo-1".)
*
* keys with indices smaller than the current state hold undo states,
* keys with larger indices hold redo states.
*/
export interface HistoryStore {
save(state: AnyObject, prevState: AnyObject | undefined): void;
reset(state: AnyObject): void;
undo(state: AnyObject): AnyObject | undefined;
redo(state: AnyObject): AnyObject | undefined;
initialized(): boolean;
_currentIndex(): number | undefined;
_oldestIndex(): number | undefined;
_allSaved(): AnyObject[];
_erase(): void;
_getState(index: number): AnyObject | undefined;
}
/** return a persistent store that holds undo/redo history
* @param toPlainState remove computed and view fields from a state object
*/
export declare function historyStore<M extends AnyObject>(toPlainState: (state: AnyObject) => AnyObject, options?: UndoOptions<M>): HistoryStore;