Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/kristianbinau/SimpleHistory. A simple history library for Deno, Node.js and the browser. Build with Deno & Typescript. www.npmjs.com/package/@kristianbinau/simple-history
Something went wrong. Try again.
1.9 kB · 106 lines
TypeScript
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107/** * A simple History class * * Stores the current value and keeps a stack of previous values for undo/redo */class History<T> { private _value: T; private _undo: T[] = []; private _redo: T[] = [];
private _originalValue: T;
constructor(value: T) { this._value = value; this._originalValue = value; }
get value(): T { return this._value; }
set value(value: T) { this.update(value); }
/** * Undo the last change * * @returns boolean */ public undo(): boolean { if (this._undo.length === 0) { return false; }
this._redo.push(this._value); this._value = this._undo.pop() as T;
return true; }
/** * Redo the last undo * * @returns boolean */ public redo(): boolean { if (this._redo.length === 0) { return false; }
this._undo.push(this._value); this._value = this._redo.pop() as T;
return true; }
/** * Has the value changed from the original value? * * @returns boolean */ public hasChanged(): boolean { if (this._undo.length === 0) { return false; }
return JSON.stringify(this._originalValue) !== JSON.stringify(this._value); }
/** * Reset the history * * @returns void */ public reset(): void { this._value = this._originalValue; this._undo = []; this._redo = []; }
/** * Set a new original value and reset the history * * @param value The new original value * @returns void */ public setOriginal(value: T): void { this._originalValue = value; this.reset(); }
/** * Update the value and push the old value to the undo stack * * @param value The new value */ private update(value: T) { this._undo.push(this._value); this._value = value; this._redo = []; }}
export default History;export { History };