char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores split refactor (#27240)521a64cd0

char/aliceai-t5-moe
1.4 KiB56 linesraw
1/**
2 * Source-space undo/redo history for the ChatFormInputRich, whose
3 * imperative DOM rebuilds destroy the browser's native undo stack.
4 * Entries record the state BEFORE an edit; edits within `groupWindowMs`
5 * extend the open group so a typing burst undoes as a unit, while
6 * structural edits (paste, mention insert, clear) pass `newGroup`.
7 */
8
9export interface SourceHistoryEntry {
10	value: string;
11	caret: number;
12}
13
14export class SourceHistory {
15	private lastPush = 0;
16	private redoStack: SourceHistoryEntry[] = [];
17	private undoStack: SourceHistoryEntry[] = [];
18
19	constructor(
20		private limit = 100,
21		private groupWindowMs = 800
22	) {}
23
24	push(entry: SourceHistoryEntry, now: number, newGroup = false): void {
25		if (newGroup || now - this.lastPush >= this.groupWindowMs || this.undoStack.length === 0) {
26			this.undoStack.push(entry);
27
28			if (this.undoStack.length > this.limit) this.undoStack.shift();
29		}
30
31		this.lastPush = now;
32		this.redoStack = [];
33	}
34
35	redo(current: SourceHistoryEntry): SourceHistoryEntry | null {
36		const entry = this.redoStack.pop();
37
38		if (!entry) return null;
39
40		this.undoStack.push(current);
41		this.lastPush = 0;
42
43		return entry;
44	}
45
46	undo(current: SourceHistoryEntry): SourceHistoryEntry | null {
47		const entry = this.undoStack.pop();
48
49		if (!entry) return null;
50
51		this.redoStack.push(current);
52		this.lastPush = 0; // the next edit after an undo starts a new group
53
54		return entry;
55	}
56}