UNPKG

@typescript-package/history

Version:

A lightweight TypeScript package for tracking the history of values.

241 lines (240 loc) 7.89 kB
import { DataCore, DataConstructorInput, ImmutableArray } from '@typescript-package/data'; import { HistoryCore, HistoryCurrent } from '../core'; import { HistoryCurrentConstructor, HistoryCoreConstructor } from '../type'; /** * @description The base abstract class to manage history. * @export * @abstract * @class HistoryBase * @template Value * @template {number} [Size=number] * @template {DataCore<readonly Value[]>} [DataType=Data<readonly Value[]>] */ export declare abstract class HistoryBase<Value, Size extends number, DataType extends DataCore<readonly Value[]>, CurrentType extends HistoryCurrent<Value, DataType>, RedoType extends HistoryCore<Value, Size, DataType>, UndoType extends HistoryCore<Value, Size, DataType>> { #private; /** * @description The max size for undo history. * @public * @static * @type {number} */ static size: number; /** * @description Returns the `string` tag representation of the `HistoryBase` class when used in `Object.prototype.toString.call(instance)`. * @public * @readonly * @type {string} */ get [Symbol.toStringTag](): string; /** * @description Gets the current value stored in history. * @public * @readonly * @type {Value} */ get current(): Value; /** * @description Returns the current history of `CurrentHistory`. * @public * @readonly * @type {CurrentType} */ get currentHistory(): CurrentType; /** * @description * @public * @readonly * @type {{ * current: DataType, * redo: DataType, * undo: DataType * }} */ get data(): { current: DataType; redo: DataType; undo: DataType; }; /** * @description Returns the redo history. * @public * @readonly * @type {RedoType} */ get redoHistory(): RedoType; /** * @description Returns the undo history. * @public * @readonly * @type {UndoType} */ get undoHistory(): UndoType; /** * Creates an instance of `HistoryBase` child class. * @constructor * @param {{ value?: Value, size?: Size}} [param0={}] * @param {Value} param0.value * @param {Size} param0.size * @param {?DataConstructorInput<readonly Value[], DataType>} [data] * @param {{ * current?: HistoryCurrentConstructor<Value, DataType, CurrentType>, * redo?: HistoryCoreConstructor<Value, Size, DataType, RedoType>, * undo?: HistoryCoreConstructor<Value, Size, DataType, UndoType>, * }} [param1={}] * @param {HistoryCurrentConstructor<Value, DataType, CurrentType>} param1.current * @param {HistoryCoreConstructor<Value, Size, DataType, RedoType>} param1.redo * @param {HistoryCoreConstructor<Value, Size, DataType, UndoType>} param1.undo */ constructor({ value, size }?: { value?: Value; size?: Size; }, data?: DataConstructorInput<readonly Value[], DataType>, { current, redo, undo }?: { current?: HistoryCurrentConstructor<Value, DataType, CurrentType>; redo?: HistoryCoreConstructor<Value, Size, DataType, RedoType>; undo?: HistoryCoreConstructor<Value, Size, DataType, UndoType>; }); /** * @description Clears the `current`, `undo` and `redo` history. * @public * @returns {this} The `this` current instance. */ clear(): this; /** * @description Destroys the history of this instance. * @public * @returns {this} The `this` current instance. */ destroy(): this; /** * @description Gets the current, undo and redo history. * @public * @returns {{ current: Readonly<Value>, undo: ImmutableArray<Value>; redo: ImmutableArray<Value> }} */ get(): { current: Readonly<Value>; undo: ImmutableArray<Value>; redo: ImmutableArray<Value>; }; /** * @description The instance method returns read-only redo history. * @public * @template Type * @returns {(ImmutableArray<Value>)} */ getRedo(): ImmutableArray<Value>; /** * @description The instance method returns read-only undo history. * @public * @template Type * @returns {(ImmutableArray<Value>)} */ getUndo(): ImmutableArray<Value>; /** * @description Checks whether the current value is set. * @public * @returns {boolean} */ hasCurrent(): boolean; /** * @description Checks whether the history is enabled by checking undo size. * @public * @returns {boolean} */ isEnabled(): boolean; /** * @description Sets the callback function invoked on redo. * @public * @param {(value: Value) => void} callbackFn The callback function to invoke. * @returns {this} */ onRedo(callbackFn: (value: Value) => void): this; /** * @description Sets the callback function invoked on undo. * @public * @param {(value: Value) => void} callbackFn The callback function to invoke. * @returns {this} */ onUndo(callbackFn: (value: Value) => void): this; /** * @description Returns the specified by index value from redo history. * @public * @param {number} [index=0] * @returns {(Value | undefined)} */ redoAt(index?: number): Value | undefined; /** * @description Returns the specified by index value from undo history. * @public * @param {number} [index=this.#undo.length - 1] * @returns {(Value | undefined)} */ undoAt(index?: number): Value | undefined; /** * @description Returns the first value that would be redone without modifying history. * @public * @returns {Value | undefined} The first redo value. */ firstRedo(): Value | undefined; /** * @description Returns the first value that would be undone without modifying history. * @public * @returns {Value | undefined} The first undo value. */ firstUndo(): Value | undefined; /** * @description Returns the last value that would be redone without modifying history. * @public * @returns {Value | undefined} The last redo value. */ lastRedo(): Value | undefined; /** * @description Returns the last value that would be undone without modifying history. * @public * @returns {Value | undefined} The last undo value. */ lastUndo(): Value | undefined; /** * @description Returns the next value that would be redone without modifying history. * @public * @returns {Value | undefined} The next redo value. */ nextRedo(): Value | undefined; /** * @description Returns the next value that would be undone without modifying history. * @public * @returns {Value | undefined} The next undo value. */ nextUndo(): Value | undefined; /** * @description Pick the current, redo or undo history. * @public * @param {('current' | 'redo' | 'undo')} type * @returns {ImmutableArray<Value>} */ pick(type: 'current' | 'redo' | 'undo'): ImmutableArray<Value>; /** * @description Redoes the last undone action. * @public * @returns {this} The current instance. */ redo(): this; /** * @description Sets a new value and updates the undo history. * @public * @param {Value} value * @returns {this} The `this` current instance. */ set(value: Value): this; /** * @description Sets the size of undo history. * @public * @param {Size} size The maximum size for undo history. */ setSize(size: Size): this; /** * @description Undoes the last action and moves it to redo history. * @public * @returns {this} The current instance. */ undo(): this; }