Line data Source code
1 2 : export const Dirty = {
2 : DRAWABLE: 1 << 0,
3 : STRUCTURE: 1 << 1,
4 : SELECTION: 1 << 2,
5 : META: 1 << 3,
6 : CHANNEL: 1 << 4
7 : } as const
8 :
9 : export const DIRTY_ALL = ~0
10 :
11 : export type Direction = 'undo' | 'redo'
12 :
13 : export interface Command {
14 : readonly label: string
15 : readonly dirtyMask: number
16 :
17 : apply(dir: Direction): void
18 : sizeBytes(): number
19 :
20 : tryMerge?(prev: Command): boolean
21 : contentRefs?(): string[]
22 : }
23 :
24 2 : export class CommandGroup implements Command {
25 : readonly children: Command[] = []
26 0 : constructor(readonly label: string) {}
27 :
28 : get dirtyMask(): number {
29 0 : return this.children.reduce((m, c) => m | c.dirtyMask, 0)
30 : }
31 :
32 : apply(dir: Direction): void {
33 0 : const order = dir === 'redo' ? this.children : [...this.children].reverse()
34 0 : for (const c of order) c.apply(dir)
35 : }
36 :
37 : sizeBytes(): number {
38 0 : return this.children.reduce((n, c) => n + c.sizeBytes(), 0)
39 : }
40 :
41 : contentRefs(): string[] {
42 0 : return this.children.flatMap((c) => c.contentRefs?.() ?? [])
43 : }
44 :
45 : get empty(): boolean {
46 0 : return this.children.length === 0
47 : }
48 : }
49 :
50 : export interface HistoryOptions {
51 : byteBudget?: number
52 : minSteps?: number
53 : }
54 :
55 2 : export class History {
56 : private undoStack: Command[] = []
57 : private redoStack: Command[] = []
58 : private groupStack: CommandGroup[] = []
59 : private dirtyCount = 0
60 : private cleanReachable = true
61 : private mergeBarrier: Command | null = null
62 : private readonly byteBudget: number
63 : private readonly minSteps: number
64 : private listeners = new Set<(mask: number) => void>()
65 : private undoBytes = 0
66 : private sizes = new WeakMap<Command, number>()
67 :
68 : constructor(opts: HistoryOptions = {}) {
69 2 : this.byteBudget = opts.byteBudget ?? 256 * 1024 * 1024
70 2 : this.minSteps = opts.minSteps ?? 8
71 : }
72 :
73 : onChange(fn: (mask: number) => void): () => void {
74 2 : this.listeners.add(fn)
75 2 : return () => this.listeners.delete(fn)
76 : }
77 :
78 : private emit(mask: number): void {
79 13 : for (const l of this.listeners) l(mask)
80 : }
81 :
82 : beginGroup(label: string): void {
83 0 : this.groupStack.push(new CommandGroup(label))
84 : }
85 :
86 : endGroup(): void {
87 0 : const group = this.groupStack.pop()
88 0 : if (!group) return
89 0 : if (group.empty) return
90 0 : this.commit(group)
91 : }
92 :
93 : push(cmd: Command): void {
94 7 : if (this.groupStack.length > 0) {
95 0 : this.groupStack[this.groupStack.length - 1].children.push(cmd)
96 0 : return
97 : }
98 :
99 7 : const top = this.undoStack.at(-1)
100 7 : if (
101 : top &&
102 : top !== this.mergeBarrier &&
103 : this.redoStack.length === 0 &&
104 : cmd.tryMerge?.(top)
105 : ) {
106 0 : this.undoBytes += top.sizeBytes() - (this.sizes.get(top) ?? 0)
107 0 : this.sizes.set(top, top.sizeBytes())
108 : // Merged edits share the first commit's dirty step - no bumpDirty() here.
109 0 : this.emit(cmd.dirtyMask)
110 0 : return
111 : }
112 7 : this.commit(cmd)
113 : }
114 :
115 : private commit(cmd: Command): void {
116 7 : this.undoStack.push(cmd)
117 7 : this.sizes.set(cmd, cmd.sizeBytes())
118 7 : this.undoBytes += this.sizes.get(cmd) ?? 0
119 7 : if (this.redoStack.length > 0 && this.dirtyCount < 0) {
120 0 : this.cleanReachable = false
121 : }
122 7 : this.redoStack = []
123 7 : this.bumpDirty()
124 7 : this.evict()
125 7 : this.emit(cmd.dirtyMask)
126 : }
127 :
128 : undo(): void {
129 1 : const cmd = this.undoStack.pop()
130 1 : if (!cmd) return
131 1 : cmd.apply('undo')
132 1 : this.undoBytes -= this.sizes.get(cmd) ?? 0
133 1 : this.redoStack.push(cmd)
134 1 : this.dirtyCount -= 1
135 1 : this.emit(cmd.dirtyMask)
136 : }
137 :
138 : redo(): void {
139 1 : const cmd = this.redoStack.pop()
140 1 : if (!cmd) return
141 1 : cmd.apply('redo')
142 1 : this.undoStack.push(cmd)
143 1 : this.undoBytes += this.sizes.get(cmd) ?? 0
144 1 : this.dirtyCount += 1
145 1 : this.emit(cmd.dirtyMask)
146 : }
147 :
148 : canUndo(): boolean {
149 9 : return this.undoStack.length > 0
150 : }
151 :
152 : clear(): void {
153 4 : this.undoStack = []
154 4 : this.redoStack = []
155 4 : this.groupStack = []
156 4 : this.dirtyCount = 0
157 4 : this.undoBytes = 0
158 4 : this.cleanReachable = true
159 4 : this.mergeBarrier = null
160 4 : this.emit(DIRTY_ALL)
161 : }
162 :
163 : canRedo(): boolean {
164 9 : return this.redoStack.length > 0
165 : }
166 :
167 : contentRefs(): Set<string> {
168 13 : const refs = new Set<string>()
169 13 : for (const stack of [this.undoStack, this.redoStack, this.groupStack]) {
170 39 : for (const cmd of stack) {
171 11 : for (const id of cmd.contentRefs?.() ?? []) refs.add(id)
172 : }
173 : }
174 13 : return refs
175 : }
176 :
177 : dirty(): boolean {
178 0 : return !this.cleanReachable || this.dirtyCount !== 0
179 : }
180 :
181 : markSaved(): void {
182 0 : this.dirtyCount = 0
183 0 : this.cleanReachable = true
184 0 : this.mergeBarrier = this.undoStack[this.undoStack.length - 1] ?? null
185 : }
186 :
187 : private bumpDirty(): void {
188 7 : this.dirtyCount += 1
189 : }
190 :
191 : private evict(): void {
192 7 : while (
193 : this.undoStack.length > this.minSteps &&
194 : this.undoBytes > this.byteBudget
195 : ) {
196 0 : const dropped = this.undoStack.shift()
197 0 : if (!dropped) break
198 0 : if (dropped === this.mergeBarrier) this.mergeBarrier = null
199 0 : this.undoBytes -= this.sizes.get(dropped) ?? 0
200 0 : this.cleanReachable = false
201 : }
202 : }
203 :
204 : labels(): { undo: string[]; redo: string[] } {
205 0 : return {
206 : undo: this.undoStack.map((c) => c.label),
207 : redo: this.redoStack.map((c) => c.label)
208 : }
209 : }
210 : }
|