@weasel-js/history 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +48 -1
- package/dist/index.js +85 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -101,6 +101,8 @@ interface SerializedHistoryEntry {
|
|
|
101
101
|
baseOps: SerializedOp[];
|
|
102
102
|
/** See `HistoryEntry.pushes`. Absent in snapshots that predate it; read as 1. */
|
|
103
103
|
pushes?: number;
|
|
104
|
+
/** See `HistoryEntry.timestamp`. Absent in snapshots that predate it; read as 0. */
|
|
105
|
+
timestamp?: number;
|
|
104
106
|
selectionBefore?: readonly string[];
|
|
105
107
|
selectionAfter?: readonly string[];
|
|
106
108
|
}
|
|
@@ -114,6 +116,12 @@ interface SerializedHistory {
|
|
|
114
116
|
* history matches the original's `entries().redo` ordering. */
|
|
115
117
|
redoStack: SerializedHistoryEntry[];
|
|
116
118
|
nextEntryId: number;
|
|
119
|
+
/** Futures kept by a branching history, each under the id of the entry it
|
|
120
|
+
* forks from (0 for the start), entries in time order. Absent when none. */
|
|
121
|
+
branches?: {
|
|
122
|
+
at: number;
|
|
123
|
+
entries: SerializedHistoryEntry[];
|
|
124
|
+
}[];
|
|
117
125
|
/** Entries dropped because at least one of their ops lacked a `name`
|
|
118
126
|
* and therefore couldn't round-trip through the op-factory registry.
|
|
119
127
|
* Always present (zero when nothing was dropped) so callers can detect
|
|
@@ -151,6 +159,18 @@ interface HistoryEntry {
|
|
|
151
159
|
/** Selection restored when this entry is redone. */
|
|
152
160
|
selectionAfter?: readonly string[];
|
|
153
161
|
}
|
|
162
|
+
/** One future at the current entry, as `History.branches()` lists it. */
|
|
163
|
+
interface HistoryBranch {
|
|
164
|
+
/** The id of the branch's first entry; what `switchBranch` takes. */
|
|
165
|
+
id: number;
|
|
166
|
+
/** The first entry's label and timestamp. */
|
|
167
|
+
label: string;
|
|
168
|
+
timestamp: number;
|
|
169
|
+
/** How many entries run along it from here, nested branches not counted. */
|
|
170
|
+
length: number;
|
|
171
|
+
/** Whether `redo()` would follow it. */
|
|
172
|
+
current: boolean;
|
|
173
|
+
}
|
|
154
174
|
/** Op-batched undo/redo controller returned by `createHistory`. */
|
|
155
175
|
interface History {
|
|
156
176
|
apply(op: Op, label?: string): void;
|
|
@@ -179,6 +199,27 @@ interface History {
|
|
|
179
199
|
* Equivalent to repeated `undo()`/`redo()` calls but doesn't bother
|
|
180
200
|
* rebuilding entry snapshots between steps. No-op if already at `n`. */
|
|
181
201
|
goto(n: number): void;
|
|
202
|
+
/** The `timestamp` of entry `i`, counting in time order through the undo
|
|
203
|
+
* stack and on into the redo stack, so `timestampAt(undoDepth())` is the
|
|
204
|
+
* next redo's. `undefined` outside the entries. Allocates nothing. */
|
|
205
|
+
timestampAt(i: number): number | undefined;
|
|
206
|
+
/** The undo depth at which every entry stamped at or before `t` is applied
|
|
207
|
+
* and none after it: `goto(depthAt(t))` seeks the history to `t`, either
|
|
208
|
+
* way. Assumes stamps never decrease in time order, which holds while
|
|
209
|
+
* `now()` does not run backward between pushes. */
|
|
210
|
+
depthAt(t: number): number;
|
|
211
|
+
/** Evict the oldest undo entries stamped before `t`, reporting each through
|
|
212
|
+
* `onEvict` — retention by age, beside `historyLimit`'s by count. Redo
|
|
213
|
+
* entries are never pruned: they are a future still reachable. */
|
|
214
|
+
prune(t: number): void;
|
|
215
|
+
/** The futures that fork from the current entry, oldest first: the redo
|
|
216
|
+
* stack, marked `current`, and any a branching history kept. Empty when
|
|
217
|
+
* there is nothing to redo and nothing was kept. */
|
|
218
|
+
branches(): readonly HistoryBranch[];
|
|
219
|
+
/** Make the branch `id` the redo future, keeping the one it replaces. Applies
|
|
220
|
+
* nothing — both start from where the history stands. Throws when no branch
|
|
221
|
+
* of that id forks from the current entry. */
|
|
222
|
+
switchBranch(id: number): void;
|
|
182
223
|
/** Monotonic counter bumped on every push/undo/redo/clear/coalesce.
|
|
183
224
|
* Cheap to read; callers use it as a React dep to detect changes. */
|
|
184
225
|
getVersion(): number;
|
|
@@ -274,6 +315,12 @@ interface CreateHistoryOptions {
|
|
|
274
315
|
* `historyLimit` either: a restored snapshot may exceed the cap, which
|
|
275
316
|
* re-applies (evicting via `onEvict`) on the next push. */
|
|
276
317
|
onEvict?: (entry: EvictedEntry) => void;
|
|
318
|
+
/** Keep the redo future when a push is made while there is one, as a branch
|
|
319
|
+
* `branches()` lists and `switchBranch` returns to, instead of dropping it.
|
|
320
|
+
* Undo and redo still walk one line, the current branch. An entry evicted by
|
|
321
|
+
* `historyLimit` or `prune` takes the branches forking from it along.
|
|
322
|
+
* Default `false`. */
|
|
323
|
+
branching?: boolean;
|
|
277
324
|
/** Custom op rebuilder consulted by `restore()` before the global
|
|
278
325
|
* op-factory registry. Return `null` to fall through (global registry,
|
|
279
326
|
* then a no-op placeholder). Lets an owner rebuild ops whose handlers
|
|
@@ -302,4 +349,4 @@ interface HistoryLogger {
|
|
|
302
349
|
/** Build an op-batched undo/redo `History`. The adapter is passed to each op's `apply`/`invert`. */
|
|
303
350
|
declare function createHistory(adapter: unknown, options?: CreateHistoryOptions): History;
|
|
304
351
|
|
|
305
|
-
export { type BeginJournalOptions, type CreateHistoryOptions, type EvictedEntry, type History, type HistoryEntry, type HistoryLogger, type HistorySelection, type Journal, type Op, type RecordEntryOptions, type SerializedHistory, type SerializedHistoryEntry, type SerializedOp, createHistory };
|
|
352
|
+
export { type BeginJournalOptions, type CreateHistoryOptions, type EvictedEntry, type History, type HistoryBranch, type HistoryEntry, type HistoryLogger, type HistorySelection, type Journal, type Op, type RecordEntryOptions, type SerializedHistory, type SerializedHistoryEntry, type SerializedOp, createHistory };
|
package/dist/index.js
CHANGED
|
@@ -105,6 +105,8 @@ function createHistory(adapter, options = {}) {
|
|
|
105
105
|
const now = options.now ?? (() => Date.now());
|
|
106
106
|
const historyLimit = Math.max(0, options.historyLimit ?? Infinity);
|
|
107
107
|
const onEvict = options.onEvict;
|
|
108
|
+
const branching = options.branching ?? false;
|
|
109
|
+
const forks = /* @__PURE__ */ new Map();
|
|
108
110
|
const customRebuild = options.rebuildOp;
|
|
109
111
|
const selection = options.selection;
|
|
110
112
|
const logger = options.debug ?? SILENT;
|
|
@@ -126,13 +128,29 @@ function createHistory(adapter, options = {}) {
|
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
}
|
|
129
|
-
function
|
|
131
|
+
function evict(entries) {
|
|
132
|
+
reportEvicted(entries);
|
|
133
|
+
for (const e of entries) {
|
|
134
|
+
const kept = forks.get(e.id);
|
|
135
|
+
if (!kept) continue;
|
|
136
|
+
forks.delete(e.id);
|
|
137
|
+
for (const branch of kept) evict(branch);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function forkPoint() {
|
|
141
|
+
return undoStack[undoStack.length - 1]?.id ?? 0;
|
|
142
|
+
}
|
|
143
|
+
function retireRedo(at) {
|
|
130
144
|
if (redoStack.length === 0) return;
|
|
131
|
-
|
|
145
|
+
const future = redoStack.splice(0);
|
|
146
|
+
if (!branching) return evict(future);
|
|
147
|
+
const kept = forks.get(at);
|
|
148
|
+
if (kept) kept.push(future.reverse());
|
|
149
|
+
else forks.set(at, [future.reverse()]);
|
|
132
150
|
}
|
|
133
151
|
function enforceLimit() {
|
|
134
152
|
while (undoStack.length > historyLimit) {
|
|
135
|
-
|
|
153
|
+
evict([undoStack.shift()]);
|
|
136
154
|
}
|
|
137
155
|
}
|
|
138
156
|
function applyOps(ops) {
|
|
@@ -212,6 +230,7 @@ function createHistory(adapter, options = {}) {
|
|
|
212
230
|
return;
|
|
213
231
|
}
|
|
214
232
|
logger.log(`push '${label}' (${ops.length} ops)`);
|
|
233
|
+
const at = forkPoint();
|
|
215
234
|
undoStack.push({
|
|
216
235
|
id: nextEntryId++,
|
|
217
236
|
forwardOps: ops,
|
|
@@ -222,7 +241,7 @@ function createHistory(adapter, options = {}) {
|
|
|
222
241
|
touchedIds: incoming,
|
|
223
242
|
...selectionBefore ? { selectionBefore } : {}
|
|
224
243
|
});
|
|
225
|
-
|
|
244
|
+
retireRedo(at);
|
|
226
245
|
enforceLimit();
|
|
227
246
|
bump();
|
|
228
247
|
coalesceAnchorVersion = version;
|
|
@@ -256,9 +275,10 @@ function createHistory(adapter, options = {}) {
|
|
|
256
275
|
undoDepth: () => undoStack.length,
|
|
257
276
|
redoDepth: () => redoStack.length,
|
|
258
277
|
clear: () => {
|
|
259
|
-
const had = undoStack.length > 0 || redoStack.length > 0;
|
|
278
|
+
const had = undoStack.length > 0 || redoStack.length > 0 || forks.size > 0;
|
|
260
279
|
undoStack.length = 0;
|
|
261
280
|
redoStack.length = 0;
|
|
281
|
+
forks.clear();
|
|
262
282
|
if (had) bump();
|
|
263
283
|
},
|
|
264
284
|
entries() {
|
|
@@ -278,7 +298,7 @@ function createHistory(adapter, options = {}) {
|
|
|
278
298
|
},
|
|
279
299
|
goto(n) {
|
|
280
300
|
const total = undoStack.length + redoStack.length;
|
|
281
|
-
if (n < 0 || n > total) return;
|
|
301
|
+
if (n < 0 || n > total || n === undoStack.length) return;
|
|
282
302
|
while (undoStack.length > n) {
|
|
283
303
|
const entry = undoStack.pop();
|
|
284
304
|
stepBack(entry);
|
|
@@ -292,6 +312,47 @@ function createHistory(adapter, options = {}) {
|
|
|
292
312
|
}
|
|
293
313
|
bump();
|
|
294
314
|
},
|
|
315
|
+
timestampAt(i) {
|
|
316
|
+
if (i < 0) return void 0;
|
|
317
|
+
if (i < undoStack.length) return undoStack[i].timestamp;
|
|
318
|
+
return redoStack[redoStack.length - 1 - (i - undoStack.length)]?.timestamp;
|
|
319
|
+
},
|
|
320
|
+
depthAt(t) {
|
|
321
|
+
let n = undoStack.length;
|
|
322
|
+
while (n > 0 && undoStack[n - 1].timestamp > t) n--;
|
|
323
|
+
if (n < undoStack.length) return n;
|
|
324
|
+
for (let r = redoStack.length - 1; r >= 0 && redoStack[r].timestamp <= t; r--) n++;
|
|
325
|
+
return n;
|
|
326
|
+
},
|
|
327
|
+
prune(t) {
|
|
328
|
+
let k = 0;
|
|
329
|
+
while (k < undoStack.length && undoStack[k].timestamp < t) k++;
|
|
330
|
+
if (k === 0) return;
|
|
331
|
+
evict(undoStack.splice(0, k));
|
|
332
|
+
bump();
|
|
333
|
+
},
|
|
334
|
+
branches() {
|
|
335
|
+
const view = (entries, current) => {
|
|
336
|
+
const first = entries[0];
|
|
337
|
+
return { id: first.id, label: first.label, timestamp: first.timestamp, length: entries.length, current };
|
|
338
|
+
};
|
|
339
|
+
const out = (forks.get(forkPoint()) ?? []).map((b) => view(b, false));
|
|
340
|
+
if (redoStack.length > 0) out.push(view([...redoStack].reverse(), true));
|
|
341
|
+
return out.sort((a, b) => a.id - b.id);
|
|
342
|
+
},
|
|
343
|
+
switchBranch(id) {
|
|
344
|
+
if (redoStack[redoStack.length - 1]?.id === id) return;
|
|
345
|
+
const at = forkPoint();
|
|
346
|
+
const kept = forks.get(at) ?? [];
|
|
347
|
+
const i = kept.findIndex((b) => b[0].id === id);
|
|
348
|
+
if (i < 0) throw new Error(`No branch ${id} forks from entry ${at}`);
|
|
349
|
+
const [chosen] = kept.splice(i, 1);
|
|
350
|
+
if (redoStack.length > 0) kept.push(redoStack.splice(0).reverse());
|
|
351
|
+
if (kept.length > 0) forks.set(at, kept);
|
|
352
|
+
else forks.delete(at);
|
|
353
|
+
redoStack.push(...[...chosen].reverse());
|
|
354
|
+
bump();
|
|
355
|
+
},
|
|
295
356
|
getVersion: () => version,
|
|
296
357
|
subscribe(listener) {
|
|
297
358
|
listeners.add(listener);
|
|
@@ -310,12 +371,19 @@ function createHistory(adapter, options = {}) {
|
|
|
310
371
|
version: 1,
|
|
311
372
|
undoStack: undoStack.map(project).filter((e) => e !== null),
|
|
312
373
|
redoStack: redoStack.map(project).filter((e) => e !== null),
|
|
374
|
+
...forks.size > 0 ? {
|
|
375
|
+
branches: [...forks].flatMap(([at, kept]) => kept.map((b) => ({
|
|
376
|
+
at,
|
|
377
|
+
entries: b.map(project).filter((e) => e !== null)
|
|
378
|
+
}))).filter((b) => b.entries.length > 0)
|
|
379
|
+
} : {},
|
|
313
380
|
nextEntryId,
|
|
314
381
|
droppedEntries: dropped
|
|
315
382
|
};
|
|
316
383
|
},
|
|
317
384
|
recordEntry(ops, label, options2 = {}) {
|
|
318
385
|
if (ops.length === 0) return;
|
|
386
|
+
const at = forkPoint();
|
|
319
387
|
undoStack.push({
|
|
320
388
|
id: nextEntryId++,
|
|
321
389
|
forwardOps: ops,
|
|
@@ -326,7 +394,7 @@ function createHistory(adapter, options = {}) {
|
|
|
326
394
|
touchedIds: touchedIdsFromOps(ops),
|
|
327
395
|
...options2.selectionBefore ? { selectionBefore: [...options2.selectionBefore] } : {}
|
|
328
396
|
});
|
|
329
|
-
|
|
397
|
+
retireRedo(at);
|
|
330
398
|
enforceLimit();
|
|
331
399
|
bump();
|
|
332
400
|
},
|
|
@@ -371,9 +439,17 @@ function createHistory(adapter, options = {}) {
|
|
|
371
439
|
for (const se of snapshot.redoStack) {
|
|
372
440
|
redoStack.push(serialToEntry(se, customRebuild, logger));
|
|
373
441
|
}
|
|
442
|
+
forks.clear();
|
|
443
|
+
for (const b of snapshot.branches ?? []) {
|
|
444
|
+
const entries = b.entries.map((se) => serialToEntry(se, customRebuild, logger));
|
|
445
|
+
const kept = forks.get(b.at);
|
|
446
|
+
if (kept) kept.push(entries);
|
|
447
|
+
else forks.set(b.at, [entries]);
|
|
448
|
+
}
|
|
374
449
|
nextEntryId = snapshot.nextEntryId;
|
|
375
450
|
for (const e of undoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;
|
|
376
451
|
for (const e of redoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;
|
|
452
|
+
for (const kept of forks.values()) for (const b of kept) for (const e of b) if (e.id >= nextEntryId) nextEntryId = e.id + 1;
|
|
377
453
|
bump();
|
|
378
454
|
}
|
|
379
455
|
};
|
|
@@ -421,6 +497,7 @@ function entryToSerial(e, logger) {
|
|
|
421
497
|
forwardOps,
|
|
422
498
|
baseOps,
|
|
423
499
|
pushes: e.pushes,
|
|
500
|
+
timestamp: e.timestamp,
|
|
424
501
|
...e.selectionBefore ? { selectionBefore: e.selectionBefore } : {},
|
|
425
502
|
...e.selectionAfter ? { selectionAfter: e.selectionAfter } : {}
|
|
426
503
|
};
|
|
@@ -449,9 +526,7 @@ function serialToEntry(se, custom, logger) {
|
|
|
449
526
|
label: se.label,
|
|
450
527
|
forwardOps,
|
|
451
528
|
baseOps,
|
|
452
|
-
|
|
453
|
-
// coalesce anchor, so its timestamp only has to be non-null.
|
|
454
|
-
timestamp: 0,
|
|
529
|
+
timestamp: se.timestamp ?? 0,
|
|
455
530
|
pushes: se.pushes ?? 1,
|
|
456
531
|
// Re-derive touchedIds from the rebuilt ops rather than trying to
|
|
457
532
|
// round-trip the Set through the serialized form (Sets aren't JSON-safe).
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/journal.ts","../src/history.ts"],"names":["options"],"mappings":";AAGA,IAAM,QAAA,uBAAe,OAAA,EAA6B;AAG3C,SAAS,uBAAuB,CAAA,EAAkB;AACvD,EAAA,MAAM,CAAA,GAAI,QAAA,CAAS,GAAA,CAAI,CAAC,CAAA;AACxB,EAAA,IAAI,CAAC,CAAA,EAAG,MAAM,IAAI,MAAM,2DAA2D,CAAA;AACnF,EAAA,CAAA,EAAE;AACJ;AA4DO,SAAS,sBACd,MAAA,EACA,OAAA,EACA,MACA,OAAA,EACA,SAAA,GAA4F,EAAC,EACpF;AACT,EAAA,MAAM,KAAA,GAAQ,cAAc,OAAA,EAAS;AAAA,IACnC,GAAG,SAAA;AAAA,IACH,GAAI,KAAK,gBAAA,KAAqB,MAAA,GAAY,EAAE,gBAAA,EAAkB,IAAA,CAAK,gBAAA,EAAiB,GAAI;AAAC,GAC1F,CAAA;AACD,EAAA,MAAM,YAAY,SAAA,CAAU,SAAA;AAC5B,EAAA,IAAI,O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AAO,eAAA,CAAgB,EAAA,EAAI,EAAA,CAAG,KAAA,EAAO,MAAA,EAAQ,MAAM,CAAC,CAAA;AAC1F,EAAA,MAAM,OAAA,GAAU,EAAA,CAAG,OAAA,CAAQ,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,EAAA,EAAI,EAAA,CAAG,KAAA,EAAO,MAAA,EAAQ,MAAM,CAAC,CAAA;AACpF,EAAA,OAAO;AAAA,IACL,IAAI,EAAA,CAAG,EAAA;AAAA,IACP,OAAO,EAAA,CAAG,KAAA;AAAA,IACV,UAAA;AAAA,IACA,OAAA;AAAA;AAAA;AAAA,IAGA,SAAA,EAAW,CAAA;AAAA,IACX,MAAA,EAAQ,GAAG,MAAA,IAAU,CAAA;AAAA;AAAA;AAAA,IAGrB,UAAA,EAAY,kBAAkB,UAAU,CAAA;AAAA,IACxC,GAAI,EAAA,CAAG,eAAA,GAAkB,EAAE,eAAA,EAAiB,CAAC,GAAG,EAAA,CAAG,eAAe,CAAA,EAAE,GAAI,EAAC;AAAA,IACzE,GAAI,EAAA,CAAG,cAAA,GAAiB,EAAE,cAAA,EAAgB,CAAC,GAAG,EAAA,CAAG,cAAc,CAAA,EAAE,GAAI;AAAC,GACxE;AACF","file":"index.js","sourcesContent":["import type { Op } from './op';\nimport { createHistory, type CreateHistoryOptions, type History, type HistoryEntry } from './history';\n\nconst RESUMERS = new WeakMap<Journal, () => void>();\n\n/** Called by `history.resumeJournal`. Not part of the public API. */\nexport function _resumeJournalInternal(j: Journal): void {\n const r = RESUMERS.get(j);\n if (!r) throw new Error('Journal is not resumable (already committed or cancelled)');\n r();\n}\n\n/** Options for `history.beginJournal()`. */\nexport interface BeginJournalOptions {\n /** Label for the single parent-history entry the journal flushes on commit. */\n label: string;\n /** Caller-supplied tag naming what this journal is scoped to — typically the\n * id of the node being edited. The history layer only carries it; callers\n * read it back off the journal to decide whether a suspended journal\n * matches what they are about to edit. */\n targetId?: string;\n /** Coalesce window for the journal's own entries, as\n * `CreateHistoryOptions.coalesceWindowMs`. Defaults to the parent's. */\n coalesceWindowMs?: number;\n}\n\n/**\n * A scoped sub-history forked from a `History`, opened by\n * `history.beginJournal()`. Applies, undoes and redoes against the same\n * adapter as its parent, but keeps its entries to itself: `commit` flushes the\n * journal's net forward ops to the parent as one entry, `cancel` rewinds them\n * and contributes nothing. Use it when a self-contained editing session (a\n * text edit, a modal drag) should collapse to a single step in the parent's\n * undo stack while still offering undo *within* the session.\n *\n * A journal is active, suspended or closed. `commit` and `cancel` are\n * terminal; `suspend` lets the parent be used again and can be reversed with\n * `history.resumeJournal()`. Every mutating method throws when the journal is\n * not active.\n */\nexport interface Journal {\n readonly targetId: string | undefined;\n readonly forkedAtEntryId: number;\n\n // Same operational surface as History\n applyBatch(ops: Op[], label: string): void;\n undo(): void;\n redo(): void;\n canUndo(): boolean;\n canRedo(): boolean;\n entries(): { undo: readonly HistoryEntry[]; redo: readonly HistoryEntry[] };\n /** As `History.seal`, for the journal's own entries. */\n seal(): void;\n /** Counter bumped on every change a reader of this journal could see: an\n * apply, undo, redo or coalesce inside it, and every lifecycle transition\n * (`isActive` changes). Pairs with `subscribe` for `useSyncExternalStore`. */\n getVersion(): number;\n /** Fires whenever `getVersion` bumps. Returns an unsubscribe fn. */\n subscribe(listener: () => void): () => void;\n\n // Lifecycle\n commit(label: string): void;\n cancel(): void;\n suspend(): void;\n isActive(): boolean;\n}\n\n/** Internal factory used by `createHistory`'s `beginJournal` method.\n * Not exported via the package's `index.ts` — callers go through\n * `history.beginJournal()`. */\nexport function createJournalInternal(\n parent: History,\n adapter: unknown,\n opts: BeginJournalOptions,\n onClose?: () => void,\n inherited: Pick<CreateHistoryOptions, 'coalesceWindowMs' | 'now' | 'debug' | 'selection'> = {},\n): Journal {\n const inner = createHistory(adapter, {\n ...inherited,\n ...(opts.coalesceWindowMs !== undefined ? { coalesceWindowMs: opts.coalesceWindowMs } : {}),\n });\n const selection = inherited.selection;\n let version = 0;\n const listeners = new Set<() => void>();\n function bump(): void {\n version++;\n for (const l of listeners) l();\n }\n inner.subscribe(bump);\n const forkedAtEntryId = parent.currentEntryId();\n // The whole session collapses to one parent entry, so the selection that\n // entry restores is the one the session opened under — not whatever the\n // last keystroke left.\n const selectionBefore = selection ? [...selection.get()] : undefined;\n type State = 'active' | 'suspended' | 'closed';\n let state: State = 'active';\n const targetId = opts.targetId;\n\n const journal: Journal = {\n targetId,\n forkedAtEntryId,\n\n applyBatch(ops: Op[], label: string): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.applyOps(ops, label);\n },\n undo(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.undo();\n },\n redo(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.redo();\n },\n canUndo(): boolean {\n return inner.canUndo();\n },\n canRedo(): boolean {\n return inner.canRedo();\n },\n entries() {\n return inner.entries();\n },\n seal(): void {\n inner.seal();\n },\n getVersion: () => version,\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n commit(label: string): void {\n if (state !== 'active') throw new Error('Journal is not active');\n const netOps = inner.allForwardOps();\n if (netOps.length > 0) {\n parent.recordEntry(netOps, label, selectionBefore ? { selectionBefore } : undefined);\n }\n state = 'closed';\n RESUMERS.delete(journal);\n onClose?.();\n bump();\n },\n cancel(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.goto(0);\n state = 'closed';\n RESUMERS.delete(journal);\n onClose?.();\n bump();\n },\n suspend(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n state = 'suspended';\n onClose?.();\n bump();\n },\n isActive(): boolean {\n return state === 'active';\n },\n };\n\n RESUMERS.set(journal, () => {\n if (state !== 'suspended') throw new Error('Journal is not suspended');\n state = 'active';\n bump();\n });\n\n return journal;\n}\n","import type { Op } from './op';\nimport { createJournalInternal, _resumeJournalInternal, type Journal, type BeginJournalOptions } from './journal';\n\ninterface Entry {\n /** Monotonic id assigned at first push. Stable across coalesce merges\n * (a merged entry keeps the original id) so UI lists keyed on `id` don't\n * flicker when the underlying entry mutates. */\n id: number;\n /** Forward ops — applied on redo, reflect the latest to-state after any\n * coalescing. Diverges from `baseOps` only after a coalesce. */\n forwardOps: Op[];\n /** Original ops at first push — their `.invert()` is what undo replays.\n * Preserved across coalesces so undo always returns to the original\n * pre-edit state, no matter how many coalesces happened. */\n baseOps: Op[];\n label: string;\n /** ms timestamp at last push or coalesce; used to gate the coalesce window. */\n timestamp: number;\n /** See `HistoryEntry.pushes`. */\n pushes: number;\n /** Node ids touched by ops in this entry. See `HistoryEntry.touchedIds`. */\n touchedIds: ReadonlySet<string>;\n /** Selection as of just before this entry's ops ran; restored by undo.\n * Undefined when nothing supplied one (no `selection` option, or a\n * `recordEntry` call that captured none). */\n selectionBefore?: readonly string[];\n /** Selection as of just before this entry was last undone; restored by\n * redo. Written on the way past, so it reflects where the user actually\n * was rather than where the entry's ops left them. */\n selectionAfter?: readonly string[];\n}\n\n/** Wire form of a single op inside a serialized history. The pair\n * `(name, args)` reconstructs a live `Op` via the op-factory registry. */\nexport interface SerializedOp {\n name: string;\n args: unknown;\n}\n\n/** Wire form of one history entry. `forwardOps` / `baseOps` mirror the\n * in-memory entry's fields (see `Entry` above) but only carry the\n * serializable `(name, args)` projection of each op. */\nexport interface SerializedHistoryEntry {\n id: number;\n label: string;\n forwardOps: SerializedOp[];\n baseOps: SerializedOp[];\n /** See `HistoryEntry.pushes`. Absent in snapshots that predate it; read as 1. */\n pushes?: number;\n selectionBefore?: readonly string[];\n selectionAfter?: readonly string[];\n}\n\n/** Snapshot of an entire `History` instance. Designed to live alongside the\n * scene snapshot in IDB so a reload restores the undo / redo stacks to\n * exactly where they were. */\nexport interface SerializedHistory {\n version: 1;\n undoStack: SerializedHistoryEntry[];\n /** Stored newest-first, mirroring the in-memory stack so a deserialized\n * history matches the original's `entries().redo` ordering. */\n redoStack: SerializedHistoryEntry[];\n nextEntryId: number;\n /** Entries dropped because at least one of their ops lacked a `name`\n * and therefore couldn't round-trip through the op-factory registry.\n * Always present (zero when nothing was dropped) so callers can detect\n * loss without parsing the debug log. */\n droppedEntries: number;\n}\n\n/** Snapshot of an entry handed to `onEvict` when it permanently leaves the\n * reachable stacks. Ops are live references — read `name`/`args`, don't\n * mutate. */\nexport interface EvictedEntry {\n id: number;\n label: string;\n forwardOps: readonly Op[];\n baseOps: readonly Op[];\n}\n\n/** Read-only view of a history entry exposed via `History.entries()`. */\nexport interface HistoryEntry {\n /** Stable monotonic id (preserved across coalesce merges). */\n id: number;\n /** Human-readable label (the `label` arg passed to `applyOps`). */\n label: string;\n /** Push/last-coalesce timestamp (ms). */\n timestamp: number;\n /** How many pushes this entry holds: 1 when it was pushed once, plus one\n * for every later push coalesced into it. */\n pushes: number;\n /** Set of node ids touched by any op in this entry. Populated from ops\n * whose `args` carry an `id` field (transform, setPath, reparent) or a\n * `node.id` field (insert, delete). Ops without a recognisable id field\n * contribute nothing. May be `undefined` for deserialized entries\n * restored from an older snapshot that predates this field. */\n touchedIds?: ReadonlySet<string>;\n /** Selection restored when this entry is undone. */\n selectionBefore?: readonly string[];\n /** Selection restored when this entry is redone. */\n selectionAfter?: readonly string[];\n}\n\n/** Op-batched undo/redo controller returned by `createHistory`. */\nexport interface History {\n apply(op: Op, label?: string): void;\n applyOps(ops: Op[], label: string): void;\n undo(): void;\n redo(): void;\n canUndo(): boolean;\n canRedo(): boolean;\n /** Number of entries on the undo stack (O(1); `entries().undo.length`\n * without materializing the views). */\n undoDepth(): number;\n /** Number of entries on the redo stack (O(1)). */\n redoDepth(): number;\n clear(): void;\n /** Snapshot of the current undo + redo stacks. `undo` is oldest→newest\n * (i.e. the last element is what `undo()` would pop next); `redo` is\n * also oldest→newest from the user's perspective (i.e. the *first*\n * element is what `redo()` would pop next — see implementation note).\n * Freshly built on each call, and readonly so that stays true. */\n entries(): { undo: readonly HistoryEntry[]; redo: readonly HistoryEntry[] };\n /** Walk the history forward/back until exactly `n` entries are on the\n * undo stack (0 ≤ n ≤ entries().undo.length + entries().redo.length).\n * Equivalent to repeated `undo()`/`redo()` calls but doesn't bother\n * rebuilding entry snapshots between steps. No-op if already at `n`. */\n goto(n: number): void;\n /** Monotonic counter bumped on every push/undo/redo/clear/coalesce.\n * Cheap to read; callers use it as a React dep to detect changes. */\n getVersion(): number;\n /** End the current coalescing run: the next entry is pushed fresh even if\n * its keys match the top entry and it lands inside the window. For a\n * caller that changed state the stack does not know about, so an entry\n * coalesced across that change would undo past it. */\n seal(): void;\n /** Subscribe to history changes. Fires after every push/undo/redo/\n * clear/coalesce. Returns an unsubscribe fn. */\n subscribe(listener: () => void): () => void;\n /** Snapshot the undo + redo stacks in a structured-clone-safe form.\n * Entries whose ops aren't all kit-registered (i.e. any op missing a\n * `name`) are dropped from the snapshot with a debug-level log — they\n * can't round-trip, so we omit them rather than emit a half-restorable\n * entry. The in-memory stacks aren't modified. */\n serialize(): SerializedHistory;\n /** Replace the current undo + redo stacks with the deserialized contents\n * of `snapshot`. Ops are rebuilt via the `rebuildOp` option when\n * provided, then the global registry; unknown names become no-op\n * placeholders so stack ordering survives across kit-version skew.\n * Bumps `version` and notifies subscribers exactly once. */\n restore(snapshot: SerializedHistory): void;\n /** Push an entry whose ops have already been applied to the adapter.\n * Unlike `applyOps`, does NOT call `op.apply()`. Used by Journal.commit\n * to flush a session's net forward ops to the parent as one entry without\n * re-mutating the scene. */\n recordEntry(ops: Op[], label: string, options?: RecordEntryOptions): void;\n /** Concatenated forwardOps of every undo-stack entry, in order. Snapshot\n * of \"what changes are currently applied via this history\" — useful for\n * Journal.commit to flush to a parent, and for any caller that wants to\n * diff against a baseline. */\n allForwardOps(): Op[];\n /** The id that will be assigned to the *next* pushed entry. Stable\n * monotonic counter; callers use it to tag a fork point (see Journal). */\n currentEntryId(): number;\n /** Open a scoped sub-history. All apply/undo/redo on the returned Journal\n * affect the same adapter; on commit, the Journal's net forward ops are\n * flushed to this History as one entry. See spec docs/superpowers/specs/\n * 2026-05-24-modality-design.md for the full lifecycle. */\n beginJournal(opts: BeginJournalOptions): Journal;\n /** Re-activate a suspended journal. Throws if the journal was committed or\n * cancelled (those are terminal), or if a different journal is currently\n * active — at most one journal writes to the adapter at a time, on resume\n * as well as on open. Staleness checking is the caller's\n * responsibility — consult `journal.forkedAtEntryId` against\n * `currentEntryId()` and your own op-semantic rules to decide whether\n * to resume or discard before calling this. */\n resumeJournal(journal: Journal): void;\n}\n\n/** Read/write access to whatever holds the caller's selection. The engine\n * stores the ids it is handed and hands them back on the way past; it\n * attaches no meaning to them and never records a selection change as an\n * entry of its own. */\nexport interface HistorySelection {\n get(): readonly string[];\n set(ids: readonly string[]): void;\n}\n\n/** Options for `recordEntry`. */\nexport interface RecordEntryOptions {\n /** Selection as of before the already-applied ops ran. `recordEntry` is\n * called after the fact, so the live selection has moved on by then and\n * the engine cannot sample it — a caller that wants undo to restore the\n * selection captures it when the batch opens and passes it here. */\n selectionBefore?: readonly string[];\n}\n\n/** Options for `createHistory`. */\nexport interface CreateHistoryOptions {\n /** Window (ms) within which a new entry may merge into the previous one\n * via matching `Op.coalesceKey`. Defaults to `0` (no coalescing — every\n * `applyOps` pushes a discrete entry). Recommended: ~500ms for typical\n * rapid-input UX (nudge, per-keystroke text edits). The window resets on\n * each successful coalesce, so a sustained burst keeps merging, and closes\n * on any other history operation — only the entry the last push created is\n * ever a merge target. */\n coalesceWindowMs?: number;\n /** Clock injection point for tests. Defaults to `Date.now`. */\n now?: () => number;\n /** Where the caller's selection lives. Supplied, each entry records the\n * selection around it and undo / redo / goto restore it; omitted, the\n * engine never reads or writes selection at all. */\n selection?: HistorySelection;\n /** Maximum undo-stack depth. When a push overflows the cap the oldest\n * entry is evicted (reported via `onEvict`) and can no longer be undone.\n * `0` disables the undo stack entirely — every push is evicted\n * synchronously (negative values are clamped to `0`). Default: unbounded. */\n historyLimit?: number;\n /** Fired once per entry that permanently leaves the reachable stacks:\n * redo entries dropped by a branch edit (a new push or `recordEntry`\n * after undo) and undo entries evicted by `historyLimit`.\n * NOT fired by `clear()` or `restore()` — those wholesale-replace the\n * history and the caller already knows. Note `restore()` does not enforce\n * `historyLimit` either: a restored snapshot may exceed the cap, which\n * re-applies (evicting via `onEvict`) on the next push. */\n onEvict?: (entry: EvictedEntry) => void;\n /** Custom op rebuilder consulted by `restore()` before the global\n * op-factory registry. Return `null` to fall through (global registry,\n * then a no-op placeholder). Lets an owner rebuild ops whose handlers\n * live in per-instance state the global registry can't reach (e.g. a\n * Scene's registered op kinds).\n *\n * May be invoked more than once per entry with the same `(name, args)` —\n * once per op for `forwardOps` and again for `baseOps` (the same array\n * until a coalesce splits them). Unlike `onEvict`, a throwing hook is\n * NOT caught: it aborts `restore()` mid-rebuild and can leave the\n * stacks partially rebuilt. */\n rebuildOp?: (name: string, args: unknown) => Op | null;\n /** Diagnostics sink. Omitted, the engine is silent — it deliberately owns no\n * logging utility, so that this package depends on nothing. `@weasel-js/core`'s\n * `createHistory` wrapper routes these into its `debug/flag` namespace, which\n * is what makes `DEBUG=history` work for kit consumers. */\n debug?: HistoryLogger;\n}\n\n/** Diagnostics sink for {@link CreateHistoryOptions.debug}. Messages arrive\n * pre-formatted and unconditional; deciding whether to emit them is the\n * caller's job. */\nexport interface HistoryLogger {\n log(message: string): void;\n warn(message: string): void;\n}\n\n/** Silent default — keeps every call site unconditional. */\nconst SILENT: HistoryLogger = { log: () => {}, warn: () => {} };\n\n/** Build an op-batched undo/redo `History`. The adapter is passed to each op's `apply`/`invert`. */\nexport function createHistory(adapter: unknown, options: CreateHistoryOptions = {}): History {\n const undoStack: Entry[] = [];\n const redoStack: Entry[] = [];\n let activeJournal: Journal | null = null;\n const coalesceWindowMs = options.coalesceWindowMs ?? 0;\n const now = options.now ?? (() => Date.now());\n const historyLimit = Math.max(0, options.historyLimit ?? Infinity);\n const onEvict = options.onEvict;\n const customRebuild = options.rebuildOp;\n const selection = options.selection;\n const logger = options.debug ?? SILENT;\n let nextEntryId = 1;\n let version = 0;\n /** `version` as of the last push or coalesce. A batch may only merge into\n * the top entry while this still matches: every other operation bumps\n * `version`, so undo, redo, goto, clear, restore and `recordEntry` all\n * expire the merge window without having to remember to. Without that, an\n * edit made after stepping back rewrites whatever entry the step left on\n * top, and one undo then jumps past it. */\n let coalesceAnchorVersion = -1;\n const listeners = new Set<() => void>();\n function bump(): void {\n version++;\n for (const l of listeners) l();\n }\n\n /** Report entries that just became permanently unreachable. A throwing\n * callback must not desync the stacks mid-mutation, so failures are\n * contained and surfaced via the debug flag. */\n function reportEvicted(entries: Entry[]): void {\n if (!onEvict) return;\n for (const e of entries) {\n try {\n onEvict({ id: e.id, label: e.label, forwardOps: e.forwardOps, baseOps: e.baseOps });\n } catch (err) {\n logger.warn(`onEvict callback threw for entry id=${e.id} \"${e.label}\": ${String(err)}`);\n }\n }\n }\n\n /** Clear the redo stack (branch-on-edit), reporting dropped entries. */\n function dropRedo(): void {\n if (redoStack.length === 0) return;\n reportEvicted(redoStack.splice(0));\n }\n\n /** Evict the oldest undo entries past `historyLimit`, reporting each. */\n function enforceLimit(): void {\n while (undoStack.length > historyLimit) {\n reportEvicted([undoStack.shift()!]);\n }\n }\n\n function applyOps(ops: Op[]): void {\n for (const op of ops) op.apply(adapter);\n }\n\n /** Apply each op and collect whether any reported a real mutation.\n * Returns true iff at least one op did NOT explicitly return `false` /\n * `'noop'`. Used by `pushOrCoalesce` to skip pushing entries when every\n * op in the batch was a silent no-op (e.g. reorder where the order\n * already matched). Existing ops that return `undefined`/`void` count\n * as \"mutated\" — the default — so this is backwards-compatible. */\n function applyOpsAndDetectMutation(ops: Op[]): boolean {\n let anyMutated = false;\n for (const op of ops) {\n const r = op.apply(adapter);\n if (r !== false && r !== 'noop') anyMutated = true;\n }\n return anyMutated;\n }\n\n function invertEntry(entry: Entry): Op[] {\n return [...entry.baseOps].reverse().map((op) => op.invert());\n }\n\n /** Copy on read: the caller's array is theirs to mutate. */\n function readSelection(): readonly string[] | undefined {\n return selection ? [...selection.get()] : undefined;\n }\n\n /** Walk one entry backwards. The selection is restored *after* the\n * inverted ops apply, so an entry that also carries a `setSelection`-style\n * op can't overwrite the snapshot. */\n function stepBack(entry: Entry): void {\n const leaving = readSelection();\n if (leaving) entry.selectionAfter = leaving;\n applyOps(invertEntry(entry));\n if (selection && entry.selectionBefore) selection.set(entry.selectionBefore);\n }\n\n /** Walk one entry forwards. Mirror of `stepBack`. */\n function stepForward(entry: Entry): void {\n const leaving = readSelection();\n if (leaving) entry.selectionBefore = leaving;\n applyOps(entry.forwardOps);\n if (selection && entry.selectionAfter) selection.set(entry.selectionAfter);\n }\n\n /** Coalesce eligibility: every op on both sides has a `coalesceKey`, and\n * the multisets of keys match (order-independent). Match by multiset\n * rather than positional index so a multi-id selection can re-emit ops in\n * any order between batches without breaking the merge. */\n function canCoalesce(top: Entry, incoming: Op[]): boolean {\n if (coalesceWindowMs <= 0) return false;\n if (version !== coalesceAnchorVersion) return false;\n if (now() - top.timestamp > coalesceWindowMs) return false;\n if (top.forwardOps.length === 0 || incoming.length === 0) return false;\n if (top.forwardOps.length !== incoming.length) return false;\n const counts = new Map<string, number>();\n for (const op of top.forwardOps) {\n const k = op.coalesceKey;\n if (k === undefined) return false;\n counts.set(k, (counts.get(k) ?? 0) + 1);\n }\n for (const op of incoming) {\n const k = op.coalesceKey;\n if (k === undefined) return false;\n const c = counts.get(k);\n if (!c) return false;\n counts.set(k, c - 1);\n }\n return true;\n }\n\n function pushOrCoalesce(ops: Op[], label: string): void {\n if (ops.length === 0) return;\n const selectionBefore = readSelection();\n const anyMutated = applyOpsAndDetectMutation(ops);\n if (!anyMutated) {\n // Every op reported `false`/`'noop'`. Skip the push so undo stays\n // tied to real state changes. Surfaced through the kit's debug\n // flag so the upstream caller can consider avoiding the dispatch\n // entirely. Hidden by default; enable via\n // `localStorage.setItem('weasel.debug', '1')`.\n logger.warn(\n `'${label}' batch was a no-op — every op reported false/'noop'. ` +\n `Skipping the undo entry; consider gating the dispatch upstream to avoid the wasted work.`,\n );\n return;\n }\n const incoming = touchedIdsFromOps(ops);\n const top = undoStack[undoStack.length - 1];\n if (top && canCoalesce(top, ops)) {\n top.forwardOps = ops;\n top.timestamp = now();\n top.pushes++;\n // Merge incoming touched ids into the coalesced entry's set.\n if (incoming.size > 0) {\n const merged = new Set(top.touchedIds);\n for (const id of incoming) merged.add(id);\n top.touchedIds = merged;\n }\n // baseOps + label + id intentionally preserved — undo returns to the\n // pre-edit state, the original label sticks, and the entry id stays\n // stable so React lists keyed on id don't flicker. No dropRedo() here:\n // the anchor only survives until the stack is stepped, and every step\n // clears it, so a live anchor implies an empty redo stack.\n logger.log(`coalesce '${label}' into entry id=${top.id} (${ops.length} ops)`);\n bump();\n coalesceAnchorVersion = version;\n return;\n }\n logger.log(`push '${label}' (${ops.length} ops)`);\n undoStack.push({\n id: nextEntryId++, forwardOps: ops, baseOps: ops, label, timestamp: now(), pushes: 1, touchedIds: incoming,\n ...(selectionBefore ? { selectionBefore } : {}),\n });\n dropRedo();\n enforceLimit();\n bump();\n coalesceAnchorVersion = version;\n }\n\n return {\n apply(op, label) {\n pushOrCoalesce([op], label ?? op.label ?? '');\n },\n applyOps(ops, label) {\n pushOrCoalesce(ops, label);\n },\n undo() {\n const entry = undoStack.pop();\n if (!entry) return;\n stepBack(entry);\n redoStack.push(entry);\n bump();\n },\n redo() {\n const entry = redoStack.pop();\n if (!entry) return;\n stepForward(entry);\n undoStack.push(entry);\n bump();\n },\n seal() {\n coalesceAnchorVersion = -1;\n },\n canUndo: () => undoStack.length > 0,\n canRedo: () => redoStack.length > 0,\n undoDepth: () => undoStack.length,\n redoDepth: () => redoStack.length,\n clear: () => {\n const had = undoStack.length > 0 || redoStack.length > 0;\n undoStack.length = 0;\n redoStack.length = 0;\n if (had) bump();\n },\n entries() {\n const toView = (e: Entry): HistoryEntry => ({\n id: e.id, label: e.label, timestamp: e.timestamp, pushes: e.pushes, touchedIds: e.touchedIds,\n ...(e.selectionBefore ? { selectionBefore: e.selectionBefore } : {}),\n ...(e.selectionAfter ? { selectionAfter: e.selectionAfter } : {}),\n });\n // redoStack is internally stored newest-on-top (so `pop()` redoes the\n // next-most-recent undo). Reverse on the way out so callers see the\n // entries in chronological order — the user's next redo is the first\n // element, matching `entries().redo[0]` semantics.\n return {\n undo: undoStack.map(toView),\n redo: [...redoStack].reverse().map(toView),\n };\n },\n goto(n) {\n // Total length stays constant during this walk (we only shuffle\n // entries between undo and redo stacks).\n const total = undoStack.length + redoStack.length;\n if (n < 0 || n > total) return;\n while (undoStack.length > n) {\n const entry = undoStack.pop()!;\n stepBack(entry);\n redoStack.push(entry);\n }\n while (undoStack.length < n) {\n const entry = redoStack.pop();\n if (!entry) break; // defensive — shouldn't fire given the bounds check above\n stepForward(entry);\n undoStack.push(entry);\n }\n bump();\n },\n getVersion: () => version,\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n serialize(): SerializedHistory {\n let dropped = 0;\n const project = (e: Entry): SerializedHistoryEntry | null => {\n const s = entryToSerial(e, logger);\n if (s === null) dropped++;\n return s;\n };\n return {\n version: 1,\n undoStack: undoStack.map(project).filter((e): e is SerializedHistoryEntry => e !== null),\n redoStack: redoStack.map(project).filter((e): e is SerializedHistoryEntry => e !== null),\n nextEntryId,\n droppedEntries: dropped,\n };\n },\n recordEntry(ops: Op[], label: string, options: RecordEntryOptions = {}): void {\n if (ops.length === 0) return;\n undoStack.push({\n id: nextEntryId++, forwardOps: ops, baseOps: ops, label, timestamp: now(), pushes: 1,\n touchedIds: touchedIdsFromOps(ops),\n ...(options.selectionBefore ? { selectionBefore: [...options.selectionBefore] } : {}),\n });\n dropRedo();\n enforceLimit();\n bump();\n },\n allForwardOps(): Op[] {\n const out: Op[] = [];\n for (const e of undoStack) {\n for (const op of e.forwardOps) out.push(op);\n }\n return out;\n },\n currentEntryId(): number {\n return nextEntryId;\n },\n beginJournal(opts: BeginJournalOptions): Journal {\n if (activeJournal !== null && activeJournal.isActive()) {\n throw new Error('A journal is already active — commit, cancel, or suspend it first');\n }\n // `adapter` is the closure-captured adapter passed to createHistory.\n // The returned History object's `this` doesn't carry it, so we pass\n // it through to the factory directly.\n const j = createJournalInternal(this, adapter, opts, () => { activeJournal = null; }, {\n coalesceWindowMs, now, debug: logger,\n ...(selection ? { selection } : {}),\n });\n activeJournal = j;\n return j;\n },\n resumeJournal(journal: Journal): void {\n if (activeJournal !== null && activeJournal !== journal && activeJournal.isActive()) {\n throw new Error('A journal is already active — commit, cancel, or suspend it first');\n }\n _resumeJournalInternal(journal);\n activeJournal = journal;\n },\n restore(snapshot: SerializedHistory): void {\n undoStack.length = 0;\n redoStack.length = 0;\n for (const se of snapshot.undoStack) {\n undoStack.push(serialToEntry(se, customRebuild, logger));\n }\n for (const se of snapshot.redoStack) {\n redoStack.push(serialToEntry(se, customRebuild, logger));\n }\n // Seed nextEntryId from the snapshot, then defensively bump past any\n // restored id — a malformed snapshot with duplicate or out-of-range\n // ids should never produce a collision with future entries.\n nextEntryId = snapshot.nextEntryId;\n for (const e of undoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;\n for (const e of redoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;\n bump();\n },\n };\n}\n\n/** Extract node ids from an op's `args`. Handles the common patterns:\n * - `args.id` (string) — transform, setPath, reparent\n * - `args.node.id` (string) — insert, delete\n * Ops that don't match either pattern contribute nothing.\n * Exported for in-package test use; not part of the published package API. */\nexport function touchedIdsFromOps(ops: Op[]): ReadonlySet<string> {\n const ids = new Set<string>();\n for (const op of ops) {\n if (op.args === null || typeof op.args !== 'object') continue;\n const a = op.args as Record<string, unknown>;\n if (typeof a['id'] === 'string') {\n ids.add(a['id']);\n } else if (a['node'] !== null && typeof a['node'] === 'object') {\n const n = a['node'] as Record<string, unknown>;\n if (typeof n['id'] === 'string') ids.add(n['id']);\n }\n }\n return ids;\n}\n\n/** Project an `Op` to its `(name, args)` wire form. Returns `null` for ops\n * missing `name` — the caller drops the containing entry. */\nfunction opToSerial(op: Op): SerializedOp | null {\n if (typeof op.name !== 'string') return null;\n return { name: op.name, args: op.args };\n}\n\n/** Project a runtime entry to its serialized form, or `null` if any op in\n * the entry can't be serialized (we drop the whole entry then — a partially\n * serializable entry would invert against the wrong baseline on undo). */\nfunction entryToSerial(e: Entry, logger: HistoryLogger): SerializedHistoryEntry | null {\n const forwardOps: SerializedOp[] = [];\n for (const op of e.forwardOps) {\n const s = opToSerial(op);\n if (s === null) {\n logger.log(`serialize: dropping entry id=${e.id} \"${e.label}\" — forwardOp without name`);\n return null;\n }\n forwardOps.push(s);\n }\n const baseOps: SerializedOp[] = [];\n for (const op of e.baseOps) {\n const s = opToSerial(op);\n if (s === null) {\n logger.log(`serialize: dropping entry id=${e.id} \"${e.label}\" — baseOp without name`);\n return null;\n }\n baseOps.push(s);\n }\n return {\n id: e.id, label: e.label, forwardOps, baseOps, pushes: e.pushes,\n ...(e.selectionBefore ? { selectionBefore: e.selectionBefore } : {}),\n ...(e.selectionAfter ? { selectionAfter: e.selectionAfter } : {}),\n };\n}\n\n/** Placeholder op used when the registry lacks the requested name. Stable\n * identity (each placeholder is its own invert) keeps undo/redo plumbing\n * happy without performing any adapter mutation. */\nfunction placeholderOp(name: string, args: unknown, label?: string): Op {\n const op: Op = {\n name,\n args,\n label,\n apply: () => 'noop' as const,\n invert: () => op,\n };\n return op;\n}\n\n/** Signature of a per-instance op rebuilder (`CreateHistoryOptions.rebuildOp`). */\ntype CustomRebuild = (name: string, args: unknown) => Op | null;\n\n/** Rebuild a single serialized op via `custom` (if provided), falling back to\n * a no-op placeholder.\n *\n * This engine deliberately knows nothing about any op registry: hydrating a\n * `(name, args)` pair back into an op is the caller's concern, injected\n * through `CreateHistoryOptions.rebuildOp`. `@weasel-js/core`'s\n * `createHistory` wrapper supplies its global op-factory registry as that\n * hook, so core consumers see unchanged behavior. */\nfunction rebuildSerialOp(so: SerializedOp, label: string, custom: CustomRebuild | undefined, logger: HistoryLogger): Op {\n const viaCustom = custom ? custom(so.name, so.args) : null;\n if (viaCustom !== null) return viaCustom;\n logger.log(`restore: unknown op name \"${so.name}\" — substituting no-op placeholder`);\n return placeholderOp(so.name, so.args, label);\n}\n\n/** Rebuild a runtime entry from its serialized form. Unknown op names become\n * no-op placeholders so the entry still occupies its slot in the stack. */\nfunction serialToEntry(se: SerializedHistoryEntry, custom: CustomRebuild | undefined, logger: HistoryLogger): Entry {\n const forwardOps = se.forwardOps.map((so) => rebuildSerialOp(so, se.label, custom, logger));\n const baseOps = se.baseOps.map((so) => rebuildSerialOp(so, se.label, custom, logger));\n return {\n id: se.id,\n label: se.label,\n forwardOps,\n baseOps,\n // Coalescing is a within-session concept; a restored entry is never a\n // coalesce anchor, so its timestamp only has to be non-null.\n timestamp: 0,\n pushes: se.pushes ?? 1,\n // Re-derive touchedIds from the rebuilt ops rather than trying to\n // round-trip the Set through the serialized form (Sets aren't JSON-safe).\n touchedIds: touchedIdsFromOps(forwardOps),\n ...(se.selectionBefore ? { selectionBefore: [...se.selectionBefore] } : {}),\n ...(se.selectionAfter ? { selectionAfter: [...se.selectionAfter] } : {}),\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/journal.ts","../src/history.ts"],"names":["options"],"mappings":";AAGA,IAAM,QAAA,uBAAe,OAAA,EAA6B;AAG3C,SAAS,uBAAuB,CAAA,EAAkB;AACvD,EAAA,MAAM,CAAA,GAAI,QAAA,CAAS,GAAA,CAAI,CAAC,CAAA;AACxB,EAAA,IAAI,CAAC,CAAA,EAAG,MAAM,IAAI,MAAM,2DAA2D,CAAA;AACnF,EAAA,CAAA,EAAE;AACJ;AA4DO,SAAS,sBACd,MAAA,EACA,OAAA,EACA,MACA,OAAA,EACA,SAAA,GAA4F,EAAC,EACpF;AACT,EAAA,MAAM,KAAA,GAAQ,cAAc,OAAA,EAAS;AAAA,IACnC,GAAG,SAAA;AAAA,IACH,GAAI,KAAK,gBAAA,KAAqB,MAAA,GAAY,EAAE,gBAAA,EAAkB,IAAA,CAAK,gBAAA,EAAiB,GAAI;AAAC,GAC1F,CAAA;AACD,EAAA,MAAM,YAAY,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AAS,aAAA,CAAc,EAAA,EAA4B,MAAA,EAAmC,MAAA,EAA8B;AAClH,EAAA,MAAM,UAAA,GAAa,EAAA,CAAG,UAAA,CAAW,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,EAAA,EAAI,EAAA,CAAG,KAAA,EAAO,MAAA,EAAQ,MAAM,CAAC,CAAA;AAC1F,EAAA,MAAM,OAAA,GAAU,EAAA,CAAG,OAAA,CAAQ,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,EAAA,EAAI,EAAA,CAAG,KAAA,EAAO,MAAA,EAAQ,MAAM,CAAC,CAAA;AACpF,EAAA,OAAO;AAAA,IACL,IAAI,EAAA,CAAG,EAAA;AAAA,IACP,OAAO,EAAA,CAAG,KAAA;AAAA,IACV,UAAA;AAAA,IACA,OAAA;AAAA,IACA,SAAA,EAAW,GAAG,SAAA,IAAa,CAAA;AAAA,IAC3B,MAAA,EAAQ,GAAG,MAAA,IAAU,CAAA;AAAA;AAAA;AAAA,IAGrB,UAAA,EAAY,kBAAkB,UAAU,CAAA;AAAA,IACxC,GAAI,EAAA,CAAG,eAAA,GAAkB,EAAE,eAAA,EAAiB,CAAC,GAAG,EAAA,CAAG,eAAe,CAAA,EAAE,GAAI,EAAC;AAAA,IACzE,GAAI,EAAA,CAAG,cAAA,GAAiB,EAAE,cAAA,EAAgB,CAAC,GAAG,EAAA,CAAG,cAAc,CAAA,EAAE,GAAI;AAAC,GACxE;AACF","file":"index.js","sourcesContent":["import type { Op } from './op';\nimport { createHistory, type CreateHistoryOptions, type History, type HistoryEntry } from './history';\n\nconst RESUMERS = new WeakMap<Journal, () => void>();\n\n/** Called by `history.resumeJournal`. Not part of the public API. */\nexport function _resumeJournalInternal(j: Journal): void {\n const r = RESUMERS.get(j);\n if (!r) throw new Error('Journal is not resumable (already committed or cancelled)');\n r();\n}\n\n/** Options for `history.beginJournal()`. */\nexport interface BeginJournalOptions {\n /** Label for the single parent-history entry the journal flushes on commit. */\n label: string;\n /** Caller-supplied tag naming what this journal is scoped to — typically the\n * id of the node being edited. The history layer only carries it; callers\n * read it back off the journal to decide whether a suspended journal\n * matches what they are about to edit. */\n targetId?: string;\n /** Coalesce window for the journal's own entries, as\n * `CreateHistoryOptions.coalesceWindowMs`. Defaults to the parent's. */\n coalesceWindowMs?: number;\n}\n\n/**\n * A scoped sub-history forked from a `History`, opened by\n * `history.beginJournal()`. Applies, undoes and redoes against the same\n * adapter as its parent, but keeps its entries to itself: `commit` flushes the\n * journal's net forward ops to the parent as one entry, `cancel` rewinds them\n * and contributes nothing. Use it when a self-contained editing session (a\n * text edit, a modal drag) should collapse to a single step in the parent's\n * undo stack while still offering undo *within* the session.\n *\n * A journal is active, suspended or closed. `commit` and `cancel` are\n * terminal; `suspend` lets the parent be used again and can be reversed with\n * `history.resumeJournal()`. Every mutating method throws when the journal is\n * not active.\n */\nexport interface Journal {\n readonly targetId: string | undefined;\n readonly forkedAtEntryId: number;\n\n // Same operational surface as History\n applyBatch(ops: Op[], label: string): void;\n undo(): void;\n redo(): void;\n canUndo(): boolean;\n canRedo(): boolean;\n entries(): { undo: readonly HistoryEntry[]; redo: readonly HistoryEntry[] };\n /** As `History.seal`, for the journal's own entries. */\n seal(): void;\n /** Counter bumped on every change a reader of this journal could see: an\n * apply, undo, redo or coalesce inside it, and every lifecycle transition\n * (`isActive` changes). Pairs with `subscribe` for `useSyncExternalStore`. */\n getVersion(): number;\n /** Fires whenever `getVersion` bumps. Returns an unsubscribe fn. */\n subscribe(listener: () => void): () => void;\n\n // Lifecycle\n commit(label: string): void;\n cancel(): void;\n suspend(): void;\n isActive(): boolean;\n}\n\n/** Internal factory used by `createHistory`'s `beginJournal` method.\n * Not exported via the package's `index.ts` — callers go through\n * `history.beginJournal()`. */\nexport function createJournalInternal(\n parent: History,\n adapter: unknown,\n opts: BeginJournalOptions,\n onClose?: () => void,\n inherited: Pick<CreateHistoryOptions, 'coalesceWindowMs' | 'now' | 'debug' | 'selection'> = {},\n): Journal {\n const inner = createHistory(adapter, {\n ...inherited,\n ...(opts.coalesceWindowMs !== undefined ? { coalesceWindowMs: opts.coalesceWindowMs } : {}),\n });\n const selection = inherited.selection;\n let version = 0;\n const listeners = new Set<() => void>();\n function bump(): void {\n version++;\n for (const l of listeners) l();\n }\n inner.subscribe(bump);\n const forkedAtEntryId = parent.currentEntryId();\n // The whole session collapses to one parent entry, so the selection that\n // entry restores is the one the session opened under — not whatever the\n // last keystroke left.\n const selectionBefore = selection ? [...selection.get()] : undefined;\n type State = 'active' | 'suspended' | 'closed';\n let state: State = 'active';\n const targetId = opts.targetId;\n\n const journal: Journal = {\n targetId,\n forkedAtEntryId,\n\n applyBatch(ops: Op[], label: string): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.applyOps(ops, label);\n },\n undo(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.undo();\n },\n redo(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.redo();\n },\n canUndo(): boolean {\n return inner.canUndo();\n },\n canRedo(): boolean {\n return inner.canRedo();\n },\n entries() {\n return inner.entries();\n },\n seal(): void {\n inner.seal();\n },\n getVersion: () => version,\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n commit(label: string): void {\n if (state !== 'active') throw new Error('Journal is not active');\n const netOps = inner.allForwardOps();\n if (netOps.length > 0) {\n parent.recordEntry(netOps, label, selectionBefore ? { selectionBefore } : undefined);\n }\n state = 'closed';\n RESUMERS.delete(journal);\n onClose?.();\n bump();\n },\n cancel(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n inner.goto(0);\n state = 'closed';\n RESUMERS.delete(journal);\n onClose?.();\n bump();\n },\n suspend(): void {\n if (state !== 'active') throw new Error('Journal is not active');\n state = 'suspended';\n onClose?.();\n bump();\n },\n isActive(): boolean {\n return state === 'active';\n },\n };\n\n RESUMERS.set(journal, () => {\n if (state !== 'suspended') throw new Error('Journal is not suspended');\n state = 'active';\n bump();\n });\n\n return journal;\n}\n","import type { Op } from './op';\nimport { createJournalInternal, _resumeJournalInternal, type Journal, type BeginJournalOptions } from './journal';\n\ninterface Entry {\n /** Monotonic id assigned at first push. Stable across coalesce merges\n * (a merged entry keeps the original id) so UI lists keyed on `id` don't\n * flicker when the underlying entry mutates. */\n id: number;\n /** Forward ops — applied on redo, reflect the latest to-state after any\n * coalescing. Diverges from `baseOps` only after a coalesce. */\n forwardOps: Op[];\n /** Original ops at first push — their `.invert()` is what undo replays.\n * Preserved across coalesces so undo always returns to the original\n * pre-edit state, no matter how many coalesces happened. */\n baseOps: Op[];\n label: string;\n /** `now()` at last push or coalesce: gates the coalesce window, and is the\n * entry's place in time for `depthAt` and `prune`. */\n timestamp: number;\n /** See `HistoryEntry.pushes`. */\n pushes: number;\n /** Node ids touched by ops in this entry. See `HistoryEntry.touchedIds`. */\n touchedIds: ReadonlySet<string>;\n /** Selection as of just before this entry's ops ran; restored by undo.\n * Undefined when nothing supplied one (no `selection` option, or a\n * `recordEntry` call that captured none). */\n selectionBefore?: readonly string[];\n /** Selection as of just before this entry was last undone; restored by\n * redo. Written on the way past, so it reflects where the user actually\n * was rather than where the entry's ops left them. */\n selectionAfter?: readonly string[];\n}\n\n/** Wire form of a single op inside a serialized history. The pair\n * `(name, args)` reconstructs a live `Op` via the op-factory registry. */\nexport interface SerializedOp {\n name: string;\n args: unknown;\n}\n\n/** Wire form of one history entry. `forwardOps` / `baseOps` mirror the\n * in-memory entry's fields (see `Entry` above) but only carry the\n * serializable `(name, args)` projection of each op. */\nexport interface SerializedHistoryEntry {\n id: number;\n label: string;\n forwardOps: SerializedOp[];\n baseOps: SerializedOp[];\n /** See `HistoryEntry.pushes`. Absent in snapshots that predate it; read as 1. */\n pushes?: number;\n /** See `HistoryEntry.timestamp`. Absent in snapshots that predate it; read as 0. */\n timestamp?: number;\n selectionBefore?: readonly string[];\n selectionAfter?: readonly string[];\n}\n\n/** Snapshot of an entire `History` instance. Designed to live alongside the\n * scene snapshot in IDB so a reload restores the undo / redo stacks to\n * exactly where they were. */\nexport interface SerializedHistory {\n version: 1;\n undoStack: SerializedHistoryEntry[];\n /** Stored newest-first, mirroring the in-memory stack so a deserialized\n * history matches the original's `entries().redo` ordering. */\n redoStack: SerializedHistoryEntry[];\n nextEntryId: number;\n /** Futures kept by a branching history, each under the id of the entry it\n * forks from (0 for the start), entries in time order. Absent when none. */\n branches?: { at: number; entries: SerializedHistoryEntry[] }[];\n /** Entries dropped because at least one of their ops lacked a `name`\n * and therefore couldn't round-trip through the op-factory registry.\n * Always present (zero when nothing was dropped) so callers can detect\n * loss without parsing the debug log. */\n droppedEntries: number;\n}\n\n/** Snapshot of an entry handed to `onEvict` when it permanently leaves the\n * reachable stacks. Ops are live references — read `name`/`args`, don't\n * mutate. */\nexport interface EvictedEntry {\n id: number;\n label: string;\n forwardOps: readonly Op[];\n baseOps: readonly Op[];\n}\n\n/** Read-only view of a history entry exposed via `History.entries()`. */\nexport interface HistoryEntry {\n /** Stable monotonic id (preserved across coalesce merges). */\n id: number;\n /** Human-readable label (the `label` arg passed to `applyOps`). */\n label: string;\n /** Push/last-coalesce timestamp (ms). */\n timestamp: number;\n /** How many pushes this entry holds: 1 when it was pushed once, plus one\n * for every later push coalesced into it. */\n pushes: number;\n /** Set of node ids touched by any op in this entry. Populated from ops\n * whose `args` carry an `id` field (transform, setPath, reparent) or a\n * `node.id` field (insert, delete). Ops without a recognisable id field\n * contribute nothing. May be `undefined` for deserialized entries\n * restored from an older snapshot that predates this field. */\n touchedIds?: ReadonlySet<string>;\n /** Selection restored when this entry is undone. */\n selectionBefore?: readonly string[];\n /** Selection restored when this entry is redone. */\n selectionAfter?: readonly string[];\n}\n\n/** One future at the current entry, as `History.branches()` lists it. */\nexport interface HistoryBranch {\n /** The id of the branch's first entry; what `switchBranch` takes. */\n id: number;\n /** The first entry's label and timestamp. */\n label: string;\n timestamp: number;\n /** How many entries run along it from here, nested branches not counted. */\n length: number;\n /** Whether `redo()` would follow it. */\n current: boolean;\n}\n\n/** Op-batched undo/redo controller returned by `createHistory`. */\nexport interface History {\n apply(op: Op, label?: string): void;\n applyOps(ops: Op[], label: string): void;\n undo(): void;\n redo(): void;\n canUndo(): boolean;\n canRedo(): boolean;\n /** Number of entries on the undo stack (O(1); `entries().undo.length`\n * without materializing the views). */\n undoDepth(): number;\n /** Number of entries on the redo stack (O(1)). */\n redoDepth(): number;\n clear(): void;\n /** Snapshot of the current undo + redo stacks. `undo` is oldest→newest\n * (i.e. the last element is what `undo()` would pop next); `redo` is\n * also oldest→newest from the user's perspective (i.e. the *first*\n * element is what `redo()` would pop next — see implementation note).\n * Freshly built on each call, and readonly so that stays true. */\n entries(): { undo: readonly HistoryEntry[]; redo: readonly HistoryEntry[] };\n /** Walk the history forward/back until exactly `n` entries are on the\n * undo stack (0 ≤ n ≤ entries().undo.length + entries().redo.length).\n * Equivalent to repeated `undo()`/`redo()` calls but doesn't bother\n * rebuilding entry snapshots between steps. No-op if already at `n`. */\n goto(n: number): void;\n /** The `timestamp` of entry `i`, counting in time order through the undo\n * stack and on into the redo stack, so `timestampAt(undoDepth())` is the\n * next redo's. `undefined` outside the entries. Allocates nothing. */\n timestampAt(i: number): number | undefined;\n /** The undo depth at which every entry stamped at or before `t` is applied\n * and none after it: `goto(depthAt(t))` seeks the history to `t`, either\n * way. Assumes stamps never decrease in time order, which holds while\n * `now()` does not run backward between pushes. */\n depthAt(t: number): number;\n /** Evict the oldest undo entries stamped before `t`, reporting each through\n * `onEvict` — retention by age, beside `historyLimit`'s by count. Redo\n * entries are never pruned: they are a future still reachable. */\n prune(t: number): void;\n /** The futures that fork from the current entry, oldest first: the redo\n * stack, marked `current`, and any a branching history kept. Empty when\n * there is nothing to redo and nothing was kept. */\n branches(): readonly HistoryBranch[];\n /** Make the branch `id` the redo future, keeping the one it replaces. Applies\n * nothing — both start from where the history stands. Throws when no branch\n * of that id forks from the current entry. */\n switchBranch(id: number): void;\n /** Monotonic counter bumped on every push/undo/redo/clear/coalesce.\n * Cheap to read; callers use it as a React dep to detect changes. */\n getVersion(): number;\n /** End the current coalescing run: the next entry is pushed fresh even if\n * its keys match the top entry and it lands inside the window. For a\n * caller that changed state the stack does not know about, so an entry\n * coalesced across that change would undo past it. */\n seal(): void;\n /** Subscribe to history changes. Fires after every push/undo/redo/\n * clear/coalesce. Returns an unsubscribe fn. */\n subscribe(listener: () => void): () => void;\n /** Snapshot the undo + redo stacks in a structured-clone-safe form.\n * Entries whose ops aren't all kit-registered (i.e. any op missing a\n * `name`) are dropped from the snapshot with a debug-level log — they\n * can't round-trip, so we omit them rather than emit a half-restorable\n * entry. The in-memory stacks aren't modified. */\n serialize(): SerializedHistory;\n /** Replace the current undo + redo stacks with the deserialized contents\n * of `snapshot`. Ops are rebuilt via the `rebuildOp` option when\n * provided, then the global registry; unknown names become no-op\n * placeholders so stack ordering survives across kit-version skew.\n * Bumps `version` and notifies subscribers exactly once. */\n restore(snapshot: SerializedHistory): void;\n /** Push an entry whose ops have already been applied to the adapter.\n * Unlike `applyOps`, does NOT call `op.apply()`. Used by Journal.commit\n * to flush a session's net forward ops to the parent as one entry without\n * re-mutating the scene. */\n recordEntry(ops: Op[], label: string, options?: RecordEntryOptions): void;\n /** Concatenated forwardOps of every undo-stack entry, in order. Snapshot\n * of \"what changes are currently applied via this history\" — useful for\n * Journal.commit to flush to a parent, and for any caller that wants to\n * diff against a baseline. */\n allForwardOps(): Op[];\n /** The id that will be assigned to the *next* pushed entry. Stable\n * monotonic counter; callers use it to tag a fork point (see Journal). */\n currentEntryId(): number;\n /** Open a scoped sub-history. All apply/undo/redo on the returned Journal\n * affect the same adapter; on commit, the Journal's net forward ops are\n * flushed to this History as one entry. See spec docs/superpowers/specs/\n * 2026-05-24-modality-design.md for the full lifecycle. */\n beginJournal(opts: BeginJournalOptions): Journal;\n /** Re-activate a suspended journal. Throws if the journal was committed or\n * cancelled (those are terminal), or if a different journal is currently\n * active — at most one journal writes to the adapter at a time, on resume\n * as well as on open. Staleness checking is the caller's\n * responsibility — consult `journal.forkedAtEntryId` against\n * `currentEntryId()` and your own op-semantic rules to decide whether\n * to resume or discard before calling this. */\n resumeJournal(journal: Journal): void;\n}\n\n/** Read/write access to whatever holds the caller's selection. The engine\n * stores the ids it is handed and hands them back on the way past; it\n * attaches no meaning to them and never records a selection change as an\n * entry of its own. */\nexport interface HistorySelection {\n get(): readonly string[];\n set(ids: readonly string[]): void;\n}\n\n/** Options for `recordEntry`. */\nexport interface RecordEntryOptions {\n /** Selection as of before the already-applied ops ran. `recordEntry` is\n * called after the fact, so the live selection has moved on by then and\n * the engine cannot sample it — a caller that wants undo to restore the\n * selection captures it when the batch opens and passes it here. */\n selectionBefore?: readonly string[];\n}\n\n/** Options for `createHistory`. */\nexport interface CreateHistoryOptions {\n /** Window (ms) within which a new entry may merge into the previous one\n * via matching `Op.coalesceKey`. Defaults to `0` (no coalescing — every\n * `applyOps` pushes a discrete entry). Recommended: ~500ms for typical\n * rapid-input UX (nudge, per-keystroke text edits). The window resets on\n * each successful coalesce, so a sustained burst keeps merging, and closes\n * on any other history operation — only the entry the last push created is\n * ever a merge target. */\n coalesceWindowMs?: number;\n /** Clock injection point for tests. Defaults to `Date.now`. */\n now?: () => number;\n /** Where the caller's selection lives. Supplied, each entry records the\n * selection around it and undo / redo / goto restore it; omitted, the\n * engine never reads or writes selection at all. */\n selection?: HistorySelection;\n /** Maximum undo-stack depth. When a push overflows the cap the oldest\n * entry is evicted (reported via `onEvict`) and can no longer be undone.\n * `0` disables the undo stack entirely — every push is evicted\n * synchronously (negative values are clamped to `0`). Default: unbounded. */\n historyLimit?: number;\n /** Fired once per entry that permanently leaves the reachable stacks:\n * redo entries dropped by a branch edit (a new push or `recordEntry`\n * after undo) and undo entries evicted by `historyLimit`.\n * NOT fired by `clear()` or `restore()` — those wholesale-replace the\n * history and the caller already knows. Note `restore()` does not enforce\n * `historyLimit` either: a restored snapshot may exceed the cap, which\n * re-applies (evicting via `onEvict`) on the next push. */\n onEvict?: (entry: EvictedEntry) => void;\n /** Keep the redo future when a push is made while there is one, as a branch\n * `branches()` lists and `switchBranch` returns to, instead of dropping it.\n * Undo and redo still walk one line, the current branch. An entry evicted by\n * `historyLimit` or `prune` takes the branches forking from it along.\n * Default `false`. */\n branching?: boolean;\n /** Custom op rebuilder consulted by `restore()` before the global\n * op-factory registry. Return `null` to fall through (global registry,\n * then a no-op placeholder). Lets an owner rebuild ops whose handlers\n * live in per-instance state the global registry can't reach (e.g. a\n * Scene's registered op kinds).\n *\n * May be invoked more than once per entry with the same `(name, args)` —\n * once per op for `forwardOps` and again for `baseOps` (the same array\n * until a coalesce splits them). Unlike `onEvict`, a throwing hook is\n * NOT caught: it aborts `restore()` mid-rebuild and can leave the\n * stacks partially rebuilt. */\n rebuildOp?: (name: string, args: unknown) => Op | null;\n /** Diagnostics sink. Omitted, the engine is silent — it deliberately owns no\n * logging utility, so that this package depends on nothing. `@weasel-js/core`'s\n * `createHistory` wrapper routes these into its `debug/flag` namespace, which\n * is what makes `DEBUG=history` work for kit consumers. */\n debug?: HistoryLogger;\n}\n\n/** Diagnostics sink for {@link CreateHistoryOptions.debug}. Messages arrive\n * pre-formatted and unconditional; deciding whether to emit them is the\n * caller's job. */\nexport interface HistoryLogger {\n log(message: string): void;\n warn(message: string): void;\n}\n\n/** Silent default — keeps every call site unconditional. */\nconst SILENT: HistoryLogger = { log: () => {}, warn: () => {} };\n\n/** Build an op-batched undo/redo `History`. The adapter is passed to each op's `apply`/`invert`. */\nexport function createHistory(adapter: unknown, options: CreateHistoryOptions = {}): History {\n const undoStack: Entry[] = [];\n const redoStack: Entry[] = [];\n let activeJournal: Journal | null = null;\n const coalesceWindowMs = options.coalesceWindowMs ?? 0;\n const now = options.now ?? (() => Date.now());\n const historyLimit = Math.max(0, options.historyLimit ?? Infinity);\n const onEvict = options.onEvict;\n const branching = options.branching ?? false;\n /** Futures a push set aside, by the id of the entry they fork from (0 for\n * the start), each in time order. Only a branching history adds to it. */\n const forks = new Map<number, Entry[][]>();\n const customRebuild = options.rebuildOp;\n const selection = options.selection;\n const logger = options.debug ?? SILENT;\n let nextEntryId = 1;\n let version = 0;\n /** `version` as of the last push or coalesce. A batch may only merge into\n * the top entry while this still matches: every other operation bumps\n * `version`, so undo, redo, goto, clear, restore and `recordEntry` all\n * expire the merge window without having to remember to. Without that, an\n * edit made after stepping back rewrites whatever entry the step left on\n * top, and one undo then jumps past it. */\n let coalesceAnchorVersion = -1;\n const listeners = new Set<() => void>();\n function bump(): void {\n version++;\n for (const l of listeners) l();\n }\n\n /** Report entries that just became permanently unreachable. A throwing\n * callback must not desync the stacks mid-mutation, so failures are\n * contained and surfaced via the debug flag. */\n function reportEvicted(entries: Entry[]): void {\n if (!onEvict) return;\n for (const e of entries) {\n try {\n onEvict({ id: e.id, label: e.label, forwardOps: e.forwardOps, baseOps: e.baseOps });\n } catch (err) {\n logger.warn(`onEvict callback threw for entry id=${e.id} \"${e.label}\": ${String(err)}`);\n }\n }\n }\n\n /** Report `entries` evicted, and evict every branch forking from them. */\n function evict(entries: Entry[]): void {\n reportEvicted(entries);\n for (const e of entries) {\n const kept = forks.get(e.id);\n if (!kept) continue;\n forks.delete(e.id);\n for (const branch of kept) evict(branch);\n }\n }\n\n /** The id of the entry the history stands on, 0 at the start. */\n function forkPoint(): number {\n return undoStack[undoStack.length - 1]?.id ?? 0;\n }\n\n /** Clear the redo stack after a push forking from `at`: kept as a branch\n * when branching, else dropped and reported. */\n function retireRedo(at: number): void {\n if (redoStack.length === 0) return;\n const future = redoStack.splice(0);\n if (!branching) return evict(future);\n const kept = forks.get(at);\n if (kept) kept.push(future.reverse());\n else forks.set(at, [future.reverse()]);\n }\n\n /** Evict the oldest undo entries past `historyLimit`, reporting each. */\n function enforceLimit(): void {\n while (undoStack.length > historyLimit) {\n evict([undoStack.shift()!]);\n }\n }\n\n function applyOps(ops: Op[]): void {\n for (const op of ops) op.apply(adapter);\n }\n\n /** Apply each op and collect whether any reported a real mutation.\n * Returns true iff at least one op did NOT explicitly return `false` /\n * `'noop'`. Used by `pushOrCoalesce` to skip pushing entries when every\n * op in the batch was a silent no-op (e.g. reorder where the order\n * already matched). Existing ops that return `undefined`/`void` count\n * as \"mutated\" — the default — so this is backwards-compatible. */\n function applyOpsAndDetectMutation(ops: Op[]): boolean {\n let anyMutated = false;\n for (const op of ops) {\n const r = op.apply(adapter);\n if (r !== false && r !== 'noop') anyMutated = true;\n }\n return anyMutated;\n }\n\n function invertEntry(entry: Entry): Op[] {\n return [...entry.baseOps].reverse().map((op) => op.invert());\n }\n\n /** Copy on read: the caller's array is theirs to mutate. */\n function readSelection(): readonly string[] | undefined {\n return selection ? [...selection.get()] : undefined;\n }\n\n /** Walk one entry backwards. The selection is restored *after* the\n * inverted ops apply, so an entry that also carries a `setSelection`-style\n * op can't overwrite the snapshot. */\n function stepBack(entry: Entry): void {\n const leaving = readSelection();\n if (leaving) entry.selectionAfter = leaving;\n applyOps(invertEntry(entry));\n if (selection && entry.selectionBefore) selection.set(entry.selectionBefore);\n }\n\n /** Walk one entry forwards. Mirror of `stepBack`. */\n function stepForward(entry: Entry): void {\n const leaving = readSelection();\n if (leaving) entry.selectionBefore = leaving;\n applyOps(entry.forwardOps);\n if (selection && entry.selectionAfter) selection.set(entry.selectionAfter);\n }\n\n /** Coalesce eligibility: every op on both sides has a `coalesceKey`, and\n * the multisets of keys match (order-independent). Match by multiset\n * rather than positional index so a multi-id selection can re-emit ops in\n * any order between batches without breaking the merge. */\n function canCoalesce(top: Entry, incoming: Op[]): boolean {\n if (coalesceWindowMs <= 0) return false;\n if (version !== coalesceAnchorVersion) return false;\n if (now() - top.timestamp > coalesceWindowMs) return false;\n if (top.forwardOps.length === 0 || incoming.length === 0) return false;\n if (top.forwardOps.length !== incoming.length) return false;\n const counts = new Map<string, number>();\n for (const op of top.forwardOps) {\n const k = op.coalesceKey;\n if (k === undefined) return false;\n counts.set(k, (counts.get(k) ?? 0) + 1);\n }\n for (const op of incoming) {\n const k = op.coalesceKey;\n if (k === undefined) return false;\n const c = counts.get(k);\n if (!c) return false;\n counts.set(k, c - 1);\n }\n return true;\n }\n\n function pushOrCoalesce(ops: Op[], label: string): void {\n if (ops.length === 0) return;\n const selectionBefore = readSelection();\n const anyMutated = applyOpsAndDetectMutation(ops);\n if (!anyMutated) {\n // Every op reported `false`/`'noop'`. Skip the push so undo stays\n // tied to real state changes. Surfaced through the kit's debug\n // flag so the upstream caller can consider avoiding the dispatch\n // entirely. Hidden by default; enable via\n // `localStorage.setItem('weasel.debug', '1')`.\n logger.warn(\n `'${label}' batch was a no-op — every op reported false/'noop'. ` +\n `Skipping the undo entry; consider gating the dispatch upstream to avoid the wasted work.`,\n );\n return;\n }\n const incoming = touchedIdsFromOps(ops);\n const top = undoStack[undoStack.length - 1];\n if (top && canCoalesce(top, ops)) {\n top.forwardOps = ops;\n top.timestamp = now();\n top.pushes++;\n // Merge incoming touched ids into the coalesced entry's set.\n if (incoming.size > 0) {\n const merged = new Set(top.touchedIds);\n for (const id of incoming) merged.add(id);\n top.touchedIds = merged;\n }\n // baseOps + label + id intentionally preserved — undo returns to the\n // pre-edit state, the original label sticks, and the entry id stays\n // stable so React lists keyed on id don't flicker. No dropRedo() here:\n // the anchor only survives until the stack is stepped, and every step\n // clears it, so a live anchor implies an empty redo stack.\n logger.log(`coalesce '${label}' into entry id=${top.id} (${ops.length} ops)`);\n bump();\n coalesceAnchorVersion = version;\n return;\n }\n logger.log(`push '${label}' (${ops.length} ops)`);\n const at = forkPoint();\n undoStack.push({\n id: nextEntryId++, forwardOps: ops, baseOps: ops, label, timestamp: now(), pushes: 1, touchedIds: incoming,\n ...(selectionBefore ? { selectionBefore } : {}),\n });\n retireRedo(at);\n enforceLimit();\n bump();\n coalesceAnchorVersion = version;\n }\n\n return {\n apply(op, label) {\n pushOrCoalesce([op], label ?? op.label ?? '');\n },\n applyOps(ops, label) {\n pushOrCoalesce(ops, label);\n },\n undo() {\n const entry = undoStack.pop();\n if (!entry) return;\n stepBack(entry);\n redoStack.push(entry);\n bump();\n },\n redo() {\n const entry = redoStack.pop();\n if (!entry) return;\n stepForward(entry);\n undoStack.push(entry);\n bump();\n },\n seal() {\n coalesceAnchorVersion = -1;\n },\n canUndo: () => undoStack.length > 0,\n canRedo: () => redoStack.length > 0,\n undoDepth: () => undoStack.length,\n redoDepth: () => redoStack.length,\n clear: () => {\n const had = undoStack.length > 0 || redoStack.length > 0 || forks.size > 0;\n undoStack.length = 0;\n redoStack.length = 0;\n forks.clear();\n if (had) bump();\n },\n entries() {\n const toView = (e: Entry): HistoryEntry => ({\n id: e.id, label: e.label, timestamp: e.timestamp, pushes: e.pushes, touchedIds: e.touchedIds,\n ...(e.selectionBefore ? { selectionBefore: e.selectionBefore } : {}),\n ...(e.selectionAfter ? { selectionAfter: e.selectionAfter } : {}),\n });\n // redoStack is internally stored newest-on-top (so `pop()` redoes the\n // next-most-recent undo). Reverse on the way out so callers see the\n // entries in chronological order — the user's next redo is the first\n // element, matching `entries().redo[0]` semantics.\n return {\n undo: undoStack.map(toView),\n redo: [...redoStack].reverse().map(toView),\n };\n },\n goto(n) {\n // Total length stays constant during this walk (we only shuffle\n // entries between undo and redo stacks).\n const total = undoStack.length + redoStack.length;\n if (n < 0 || n > total || n === undoStack.length) return;\n while (undoStack.length > n) {\n const entry = undoStack.pop()!;\n stepBack(entry);\n redoStack.push(entry);\n }\n while (undoStack.length < n) {\n const entry = redoStack.pop();\n if (!entry) break; // defensive — shouldn't fire given the bounds check above\n stepForward(entry);\n undoStack.push(entry);\n }\n bump();\n },\n timestampAt(i) {\n if (i < 0) return undefined;\n if (i < undoStack.length) return undoStack[i]!.timestamp;\n // redoStack is newest-on-top, so time order runs from its end.\n return redoStack[redoStack.length - 1 - (i - undoStack.length)]?.timestamp;\n },\n depthAt(t) {\n let n = undoStack.length;\n while (n > 0 && undoStack[n - 1]!.timestamp > t) n--;\n if (n < undoStack.length) return n;\n for (let r = redoStack.length - 1; r >= 0 && redoStack[r]!.timestamp <= t; r--) n++;\n return n;\n },\n prune(t) {\n let k = 0;\n while (k < undoStack.length && undoStack[k]!.timestamp < t) k++;\n if (k === 0) return;\n evict(undoStack.splice(0, k));\n bump();\n },\n branches() {\n const view = (entries: readonly Entry[], current: boolean): HistoryBranch => {\n const first = entries[0]!;\n return { id: first.id, label: first.label, timestamp: first.timestamp, length: entries.length, current };\n };\n const out = (forks.get(forkPoint()) ?? []).map((b) => view(b, false));\n if (redoStack.length > 0) out.push(view([...redoStack].reverse(), true));\n return out.sort((a, b) => a.id - b.id);\n },\n switchBranch(id) {\n if (redoStack[redoStack.length - 1]?.id === id) return;\n const at = forkPoint();\n const kept = forks.get(at) ?? [];\n const i = kept.findIndex((b) => b[0]!.id === id);\n if (i < 0) throw new Error(`No branch ${id} forks from entry ${at}`);\n const [chosen] = kept.splice(i, 1);\n if (redoStack.length > 0) kept.push(redoStack.splice(0).reverse());\n if (kept.length > 0) forks.set(at, kept);\n else forks.delete(at);\n redoStack.push(...[...chosen!].reverse());\n bump();\n },\n getVersion: () => version,\n subscribe(listener) {\n listeners.add(listener);\n return () => { listeners.delete(listener); };\n },\n serialize(): SerializedHistory {\n let dropped = 0;\n const project = (e: Entry): SerializedHistoryEntry | null => {\n const s = entryToSerial(e, logger);\n if (s === null) dropped++;\n return s;\n };\n return {\n version: 1,\n undoStack: undoStack.map(project).filter((e): e is SerializedHistoryEntry => e !== null),\n redoStack: redoStack.map(project).filter((e): e is SerializedHistoryEntry => e !== null),\n ...(forks.size > 0 ? {\n branches: [...forks].flatMap(([at, kept]) => kept.map((b) => ({\n at,\n entries: b.map(project).filter((e): e is SerializedHistoryEntry => e !== null),\n }))).filter((b) => b.entries.length > 0),\n } : {}),\n nextEntryId,\n droppedEntries: dropped,\n };\n },\n recordEntry(ops: Op[], label: string, options: RecordEntryOptions = {}): void {\n if (ops.length === 0) return;\n const at = forkPoint();\n undoStack.push({\n id: nextEntryId++, forwardOps: ops, baseOps: ops, label, timestamp: now(), pushes: 1,\n touchedIds: touchedIdsFromOps(ops),\n ...(options.selectionBefore ? { selectionBefore: [...options.selectionBefore] } : {}),\n });\n retireRedo(at);\n enforceLimit();\n bump();\n },\n allForwardOps(): Op[] {\n const out: Op[] = [];\n for (const e of undoStack) {\n for (const op of e.forwardOps) out.push(op);\n }\n return out;\n },\n currentEntryId(): number {\n return nextEntryId;\n },\n beginJournal(opts: BeginJournalOptions): Journal {\n if (activeJournal !== null && activeJournal.isActive()) {\n throw new Error('A journal is already active — commit, cancel, or suspend it first');\n }\n // `adapter` is the closure-captured adapter passed to createHistory.\n // The returned History object's `this` doesn't carry it, so we pass\n // it through to the factory directly.\n const j = createJournalInternal(this, adapter, opts, () => { activeJournal = null; }, {\n coalesceWindowMs, now, debug: logger,\n ...(selection ? { selection } : {}),\n });\n activeJournal = j;\n return j;\n },\n resumeJournal(journal: Journal): void {\n if (activeJournal !== null && activeJournal !== journal && activeJournal.isActive()) {\n throw new Error('A journal is already active — commit, cancel, or suspend it first');\n }\n _resumeJournalInternal(journal);\n activeJournal = journal;\n },\n restore(snapshot: SerializedHistory): void {\n undoStack.length = 0;\n redoStack.length = 0;\n for (const se of snapshot.undoStack) {\n undoStack.push(serialToEntry(se, customRebuild, logger));\n }\n for (const se of snapshot.redoStack) {\n redoStack.push(serialToEntry(se, customRebuild, logger));\n }\n forks.clear();\n for (const b of snapshot.branches ?? []) {\n const entries = b.entries.map((se) => serialToEntry(se, customRebuild, logger));\n const kept = forks.get(b.at);\n if (kept) kept.push(entries);\n else forks.set(b.at, [entries]);\n }\n // Seed nextEntryId from the snapshot, then defensively bump past any\n // restored id — a malformed snapshot with duplicate or out-of-range\n // ids should never produce a collision with future entries.\n nextEntryId = snapshot.nextEntryId;\n for (const e of undoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;\n for (const e of redoStack) if (e.id >= nextEntryId) nextEntryId = e.id + 1;\n for (const kept of forks.values()) for (const b of kept) for (const e of b) if (e.id >= nextEntryId) nextEntryId = e.id + 1;\n bump();\n },\n };\n}\n\n/** Extract node ids from an op's `args`. Handles the common patterns:\n * - `args.id` (string) — transform, setPath, reparent\n * - `args.node.id` (string) — insert, delete\n * Ops that don't match either pattern contribute nothing.\n * Exported for in-package test use; not part of the published package API. */\nexport function touchedIdsFromOps(ops: Op[]): ReadonlySet<string> {\n const ids = new Set<string>();\n for (const op of ops) {\n if (op.args === null || typeof op.args !== 'object') continue;\n const a = op.args as Record<string, unknown>;\n if (typeof a['id'] === 'string') {\n ids.add(a['id']);\n } else if (a['node'] !== null && typeof a['node'] === 'object') {\n const n = a['node'] as Record<string, unknown>;\n if (typeof n['id'] === 'string') ids.add(n['id']);\n }\n }\n return ids;\n}\n\n/** Project an `Op` to its `(name, args)` wire form. Returns `null` for ops\n * missing `name` — the caller drops the containing entry. */\nfunction opToSerial(op: Op): SerializedOp | null {\n if (typeof op.name !== 'string') return null;\n return { name: op.name, args: op.args };\n}\n\n/** Project a runtime entry to its serialized form, or `null` if any op in\n * the entry can't be serialized (we drop the whole entry then — a partially\n * serializable entry would invert against the wrong baseline on undo). */\nfunction entryToSerial(e: Entry, logger: HistoryLogger): SerializedHistoryEntry | null {\n const forwardOps: SerializedOp[] = [];\n for (const op of e.forwardOps) {\n const s = opToSerial(op);\n if (s === null) {\n logger.log(`serialize: dropping entry id=${e.id} \"${e.label}\" — forwardOp without name`);\n return null;\n }\n forwardOps.push(s);\n }\n const baseOps: SerializedOp[] = [];\n for (const op of e.baseOps) {\n const s = opToSerial(op);\n if (s === null) {\n logger.log(`serialize: dropping entry id=${e.id} \"${e.label}\" — baseOp without name`);\n return null;\n }\n baseOps.push(s);\n }\n return {\n id: e.id, label: e.label, forwardOps, baseOps, pushes: e.pushes, timestamp: e.timestamp,\n ...(e.selectionBefore ? { selectionBefore: e.selectionBefore } : {}),\n ...(e.selectionAfter ? { selectionAfter: e.selectionAfter } : {}),\n };\n}\n\n/** Placeholder op used when the registry lacks the requested name. Stable\n * identity (each placeholder is its own invert) keeps undo/redo plumbing\n * happy without performing any adapter mutation. */\nfunction placeholderOp(name: string, args: unknown, label?: string): Op {\n const op: Op = {\n name,\n args,\n label,\n apply: () => 'noop' as const,\n invert: () => op,\n };\n return op;\n}\n\n/** Signature of a per-instance op rebuilder (`CreateHistoryOptions.rebuildOp`). */\ntype CustomRebuild = (name: string, args: unknown) => Op | null;\n\n/** Rebuild a single serialized op via `custom` (if provided), falling back to\n * a no-op placeholder.\n *\n * This engine deliberately knows nothing about any op registry: hydrating a\n * `(name, args)` pair back into an op is the caller's concern, injected\n * through `CreateHistoryOptions.rebuildOp`. `@weasel-js/core`'s\n * `createHistory` wrapper supplies its global op-factory registry as that\n * hook, so core consumers see unchanged behavior. */\nfunction rebuildSerialOp(so: SerializedOp, label: string, custom: CustomRebuild | undefined, logger: HistoryLogger): Op {\n const viaCustom = custom ? custom(so.name, so.args) : null;\n if (viaCustom !== null) return viaCustom;\n logger.log(`restore: unknown op name \"${so.name}\" — substituting no-op placeholder`);\n return placeholderOp(so.name, so.args, label);\n}\n\n/** Rebuild a runtime entry from its serialized form. Unknown op names become\n * no-op placeholders so the entry still occupies its slot in the stack. */\nfunction serialToEntry(se: SerializedHistoryEntry, custom: CustomRebuild | undefined, logger: HistoryLogger): Entry {\n const forwardOps = se.forwardOps.map((so) => rebuildSerialOp(so, se.label, custom, logger));\n const baseOps = se.baseOps.map((so) => rebuildSerialOp(so, se.label, custom, logger));\n return {\n id: se.id,\n label: se.label,\n forwardOps,\n baseOps,\n timestamp: se.timestamp ?? 0,\n pushes: se.pushes ?? 1,\n // Re-derive touchedIds from the rebuilt ops rather than trying to\n // round-trip the Set through the serialized form (Sets aren't JSON-safe).\n touchedIds: touchedIdsFromOps(forwardOps),\n ...(se.selectionBefore ? { selectionBefore: [...se.selectionBefore] } : {}),\n ...(se.selectionAfter ? { selectionAfter: [...se.selectionAfter] } : {}),\n };\n}\n"]}
|