|
| 1 | +import type { ColorSettings, SeedSettings } from "../settings/types.js"; |
| 2 | +import { restoreSource } from "./commands/image.js"; |
| 3 | +import { signals } from "./signals.js"; |
| 4 | +import { store } from "./store.js"; |
| 5 | +import type { StackEntry } from "./types.js"; |
| 6 | + |
| 7 | +// In-session undo/redo over the document SOURCE only (image, seed, settings, stack). |
| 8 | +// The architecture makes this cheap: derived geometry is recomputed from source, so a |
| 9 | +// snapshot never needs to store points or render buffers. History is driven off |
| 10 | +// `signals.document` (the universal "an edit committed" signal): we keep a `baseline` |
| 11 | +// source snapshot and, on each commit, push the previous baseline onto the undo stack. |
| 12 | +// Continuous canvas drags fire `signals.document` every frame, so they are bracketed by |
| 13 | +// beginGesture/endGesture to collapse into a single step. Image swaps and project loads |
| 14 | +// clear history (see commands/image.ts), so within a session the image is invariant - |
| 15 | +// snapshots omit it (the data URL is large) and restore reattaches the live image. |
| 16 | + |
| 17 | +const HISTORY_LIMIT = 100; |
| 18 | + |
| 19 | +interface SourceSnapshot { |
| 20 | + seed: number; |
| 21 | + seedSettings: SeedSettings; |
| 22 | + colorSettings: ColorSettings; |
| 23 | + stack: StackEntry[]; |
| 24 | +} |
| 25 | + |
| 26 | +const clone = <T>(value: T): T => |
| 27 | + typeof structuredClone === "function" |
| 28 | + ? structuredClone(value) |
| 29 | + : JSON.parse(JSON.stringify(value)); |
| 30 | + |
| 31 | +function snapshot(): SourceSnapshot { |
| 32 | + const data = store.data(); |
| 33 | + return clone({ |
| 34 | + seed: data.seed, |
| 35 | + seedSettings: data.seedSettings, |
| 36 | + colorSettings: data.colorSettings, |
| 37 | + stack: data.stack, |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +// Group `collapsed` is a view flag (the app even discards it on load), so toggling a |
| 42 | +// folder must not create an undo step. Compare snapshots with every `collapsed` |
| 43 | +// normalized out; `muted` and everything else stays significant. |
| 44 | +function comparisonKey(snap: SourceSnapshot): string { |
| 45 | + const stack = snap.stack.map((entry) => |
| 46 | + entry.type === "group" ? { ...entry, group: { ...entry.group, collapsed: false } } : entry, |
| 47 | + ); |
| 48 | + return JSON.stringify({ ...snap, stack }); |
| 49 | +} |
| 50 | + |
| 51 | +const undoStack: SourceSnapshot[] = []; |
| 52 | +const redoStack: SourceSnapshot[] = []; |
| 53 | +let baseline: SourceSnapshot = snapshot(); |
| 54 | +let gestureDepth = 0; |
| 55 | +const listeners = new Set<() => void>(); |
| 56 | + |
| 57 | +function notify(): void { |
| 58 | + for (const cb of listeners) cb(); |
| 59 | +} |
| 60 | + |
| 61 | +// Push `baseline` onto the undo stack if the live source differs, then adopt the live |
| 62 | +// source as the new baseline. A no-op when nothing changed (early-returning commands, |
| 63 | +// or a pure folder collapse/expand). |
| 64 | +function reconcile(): void { |
| 65 | + const current = snapshot(); |
| 66 | + if (comparisonKey(current) === comparisonKey(baseline)) { |
| 67 | + baseline = current; |
| 68 | + return; |
| 69 | + } |
| 70 | + undoStack.push(baseline); |
| 71 | + if (undoStack.length > HISTORY_LIMIT) undoStack.shift(); |
| 72 | + redoStack.length = 0; |
| 73 | + baseline = current; |
| 74 | + notify(); |
| 75 | +} |
| 76 | + |
| 77 | +export function initHistory(): void { |
| 78 | + baseline = snapshot(); |
| 79 | + signals.document.on(() => { |
| 80 | + if (gestureDepth > 0) return; |
| 81 | + reconcile(); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +export function beginGesture(): void { |
| 86 | + gestureDepth++; |
| 87 | +} |
| 88 | + |
| 89 | +export function endGesture(): void { |
| 90 | + if (gestureDepth === 0) return; |
| 91 | + gestureDepth--; |
| 92 | + if (gestureDepth === 0) reconcile(); |
| 93 | +} |
| 94 | + |
| 95 | +export function canUndo(): boolean { |
| 96 | + return undoStack.length > 0; |
| 97 | +} |
| 98 | + |
| 99 | +export function canRedo(): boolean { |
| 100 | + return redoStack.length > 0; |
| 101 | +} |
| 102 | + |
| 103 | +export function undo(): void { |
| 104 | + const target = undoStack.pop(); |
| 105 | + if (!target) return; |
| 106 | + redoStack.push(baseline); |
| 107 | + apply(target); |
| 108 | +} |
| 109 | + |
| 110 | +export function redo(): void { |
| 111 | + const target = redoStack.pop(); |
| 112 | + if (!target) return; |
| 113 | + undoStack.push(baseline); |
| 114 | + apply(target); |
| 115 | +} |
| 116 | + |
| 117 | +// Restore a snapshot. The user's current folder expansion is preserved across undo |
| 118 | +// (collapsed is not historical), so live `collapsed` flags are overlaid onto the |
| 119 | +// restored stack by group uuid. |
| 120 | +function apply(target: SourceSnapshot): void { |
| 121 | + baseline = target; |
| 122 | + const collapsedByUuid = new Map<string, boolean>(); |
| 123 | + for (const entry of store.data().stack) { |
| 124 | + if (entry.type === "group") collapsedByUuid.set(entry.group.uuid, entry.group.collapsed); |
| 125 | + } |
| 126 | + const stack = clone(target.stack); |
| 127 | + for (const entry of stack) { |
| 128 | + if (entry.type !== "group") continue; |
| 129 | + const collapsed = collapsedByUuid.get(entry.group.uuid); |
| 130 | + if (collapsed !== undefined) entry.group.collapsed = collapsed; |
| 131 | + } |
| 132 | + restoreSource({ |
| 133 | + seed: target.seed, |
| 134 | + seedSettings: clone(target.seedSettings), |
| 135 | + colorSettings: clone(target.colorSettings), |
| 136 | + stack, |
| 137 | + }); |
| 138 | + notify(); |
| 139 | +} |
| 140 | + |
| 141 | +export function clearHistory(): void { |
| 142 | + undoStack.length = 0; |
| 143 | + redoStack.length = 0; |
| 144 | + baseline = snapshot(); |
| 145 | + notify(); |
| 146 | +} |
| 147 | + |
| 148 | +export function onHistoryChange(listener: () => void): void { |
| 149 | + listeners.add(listener); |
| 150 | +} |
0 commit comments