web-ui-pack
Version:
Web package with UI elements
86 lines (85 loc) • 3.16 kB
TypeScript
declare const enum InputTypes {
/** Same as insert but possible with update last snapshot to merge several append to single */
append = 0,
insert = 1,
deleteAfter = 2,
deleteBefore = 3,
replace = 4
}
interface InputState {
pos1: number;
pos2: number;
inserted: string | null;
action: InputTypes;
value: string;
}
interface Snapshot {
/** Action applied to input */
action: InputTypes;
/** selectionStart before input change */
pos1: number;
/** selectionEnd before input change */
pos2: number;
/** position where text changed */
posIns: number;
/** Removed part of text */
removed?: string;
/** Inserted part of text */
inserted?: string;
}
/** Implements custom history undo/redo for input-text */
export default class TextHistory {
#private;
refInput: HTMLInputElement | (HTMLElement & Pick<HTMLInputElement, "value" | "selectionStart" | "selectionEnd" | "setSelectionRange">);
/** Convert values to history-snapshot; required for undo/redo logic of input */
static historyToSnapshot(s: string, pos: number): string;
/** Parse history-snapshot; required for undo/redo logic of input */
static historyFromSnapshot(h: string): {
v: string;
pos: number;
};
/** Returns simple changes for 2 strings */
static findDiff(prev: string, next: string): {
inserted: {
v: string;
pos: number;
} | null;
removed: {
v: string;
pos: number;
} | null;
};
/** Used to separate history when user types text in ordinary way */
static delimiters: string;
constructor(refInput: HTMLInputElement | (HTMLElement & Pick<HTMLInputElement, "value" | "selectionStart" | "selectionEnd" | "setSelectionRange">));
/** Converts snapshot to string representation */
snapshotEncode(snap: Snapshot): string;
/** Converts snapshot from string representation */
snapshotDecode(snap: string): Required<Snapshot>;
/** Call this handler on input.on('keydown');
* @returns "true" if was undo/redo action & history exists */
handleKeyDown(e: KeyboardEvent): boolean;
_stateBeforeInput?: InputState | false;
/** Returns basic input state */
get inputState(): InputState;
/** Call this handler on input.onBefore;
* @returns "true" if was processed undo/redo action */
handleBeforeInput(e: InputEvent): boolean;
/** Call this handler on input.on('input'); */
handleInput(e: InputEvent): void;
_hist: string[];
_histTimeout?: ReturnType<typeof setTimeout> | null;
/** Save to history based on inputState */
save(prev: string, next: string): void;
save(beforeState: InputState): void;
/** Returns last history index */
get lastIndex(): number;
/** Call this to remove last history */
removeLast(): void;
/** Returns whether is possible to process undo/redo actions */
canUndoRedo(isRedo: boolean): boolean;
_histPos?: number | null;
/** Update input value & selection according to history */
undoRedo(isRedo: boolean): boolean;
}
export {};