@webpacked-timeline/core 1.0.0-beta.1

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.
Files changed (77) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/LICENSE +21 -0
  3. package/README.md +162 -0
  4. package/dist/chunk-27XCNVPR.js +5969 -0
  5. package/dist/chunk-6PDBJDHM.js +2263 -0
  6. package/dist/chunk-BWPS6NQT.js +7465 -0
  7. package/dist/chunk-FBOYSUYV.js +1280 -0
  8. package/dist/chunk-FR632TZX.js +1870 -0
  9. package/dist/chunk-HW4Z7YLJ.js +1242 -0
  10. package/dist/chunk-HWW62IFH.js +5424 -0
  11. package/dist/chunk-I2GZXRH4.js +4790 -0
  12. package/dist/chunk-JQZE3OK4.js +1255 -0
  13. package/dist/chunk-KF7JNK2F.js +1864 -0
  14. package/dist/chunk-KR3P2DYK.js +5655 -0
  15. package/dist/chunk-MO5DSFSW.js +2214 -0
  16. package/dist/chunk-MQAW33RJ.js +5530 -0
  17. package/dist/chunk-N4WUWZZX.js +2833 -0
  18. package/dist/chunk-NRJV7I4C.js +1331 -0
  19. package/dist/chunk-NXG52532.js +2230 -0
  20. package/dist/chunk-PVXF67CN.js +1278 -0
  21. package/dist/chunk-QSB6DHIF.js +5429 -0
  22. package/dist/chunk-QYWJT7HR.js +5837 -0
  23. package/dist/chunk-SWBRCMW7.js +7466 -0
  24. package/dist/chunk-TAT3ULSV.js +2214 -0
  25. package/dist/chunk-TTDP5JUM.js +2228 -0
  26. package/dist/chunk-UAGP4VPG.js +1739 -0
  27. package/dist/chunk-WIG6SY7A.js +1183 -0
  28. package/dist/chunk-YJ2K5N2R.js +6187 -0
  29. package/dist/index-3Lr_vKBd.d.cts +2810 -0
  30. package/dist/index-3Lr_vKBd.d.ts +2810 -0
  31. package/dist/index-7IPJn1yM.d.cts +1146 -0
  32. package/dist/index-7IPJn1yM.d.ts +1146 -0
  33. package/dist/index-B0xOv0V0.d.cts +3259 -0
  34. package/dist/index-B0xOv0V0.d.ts +3259 -0
  35. package/dist/index-B2m3zwg7.d.cts +1381 -0
  36. package/dist/index-B2m3zwg7.d.ts +1381 -0
  37. package/dist/index-B3sUrU_X.d.cts +1249 -0
  38. package/dist/index-B3sUrU_X.d.ts +1249 -0
  39. package/dist/index-B6wla7ZJ.d.cts +2751 -0
  40. package/dist/index-B6wla7ZJ.d.ts +2751 -0
  41. package/dist/index-BIv8RWWT.d.cts +1574 -0
  42. package/dist/index-BIv8RWWT.d.ts +1574 -0
  43. package/dist/index-BJv6hDHL.d.cts +3255 -0
  44. package/dist/index-BJv6hDHL.d.ts +3255 -0
  45. package/dist/index-BUCimS2e.d.cts +1393 -0
  46. package/dist/index-BUCimS2e.d.ts +1393 -0
  47. package/dist/index-Bw_nvNcG.d.cts +1275 -0
  48. package/dist/index-Bw_nvNcG.d.ts +1275 -0
  49. package/dist/index-ByG0gOtd.d.cts +1167 -0
  50. package/dist/index-ByG0gOtd.d.ts +1167 -0
  51. package/dist/index-CDGd2XXv.d.cts +2492 -0
  52. package/dist/index-CDGd2XXv.d.ts +2492 -0
  53. package/dist/index-CznAVeJ6.d.cts +1145 -0
  54. package/dist/index-CznAVeJ6.d.ts +1145 -0
  55. package/dist/index-DQD9IMh7.d.cts +2534 -0
  56. package/dist/index-DQD9IMh7.d.ts +2534 -0
  57. package/dist/index-Dl3qtJEI.d.cts +2178 -0
  58. package/dist/index-Dl3qtJEI.d.ts +2178 -0
  59. package/dist/index-DnE2A-Nz.d.cts +2603 -0
  60. package/dist/index-DnE2A-Nz.d.ts +2603 -0
  61. package/dist/index-DrOA6QmW.d.cts +2492 -0
  62. package/dist/index-DrOA6QmW.d.ts +2492 -0
  63. package/dist/index-Vpa3rPEM.d.cts +1402 -0
  64. package/dist/index-Vpa3rPEM.d.ts +1402 -0
  65. package/dist/index-jP6BomSd.d.cts +2640 -0
  66. package/dist/index-jP6BomSd.d.ts +2640 -0
  67. package/dist/index-wiGRwVyY.d.cts +3259 -0
  68. package/dist/index-wiGRwVyY.d.ts +3259 -0
  69. package/dist/index.cjs +7386 -0
  70. package/dist/index.d.cts +1 -0
  71. package/dist/index.d.ts +1 -0
  72. package/dist/index.js +263 -0
  73. package/dist/internal.cjs +7721 -0
  74. package/dist/internal.d.cts +704 -0
  75. package/dist/internal.d.ts +704 -0
  76. package/dist/internal.js +405 -0
  77. package/package.json +58 -0
@@ -0,0 +1,1381 @@
1
+ /**
2
+ * FRAME-BASED TIME REPRESENTATION
3
+ *
4
+ * Phase 0 compliant. All time values in state are TimelineFrame branded integers.
5
+ * FrameRate is a discriminated literal union — never a raw float.
6
+ *
7
+ * THREE INVIOLABLE RULES:
8
+ * 1. Core has ZERO UI framework imports.
9
+ * 2. Every function that changes state returns a NEW object.
10
+ * 3. Every frame value is a branded TimelineFrame integer — never a raw number.
11
+ */
12
+ /**
13
+ * TimelineFrame — A discrete, non-negative integer point in time measured in frames.
14
+ *
15
+ * Branded so TypeScript prevents raw numbers from sneaking into frame positions.
16
+ * The ONLY way to create one is via toFrame().
17
+ */
18
+ type TimelineFrame = number & {
19
+ readonly __brand: "TimelineFrame";
20
+ };
21
+ /** The canonical factory. Use this everywhere instead of casting. */
22
+ declare const toFrame: (n: number) => TimelineFrame;
23
+ /**
24
+ * Legacy alias kept for backward-compat during transition.
25
+ * Prefer toFrame() for new code.
26
+ */
27
+ declare function frame(value: number): TimelineFrame;
28
+ /**
29
+ * FrameRate — The exact set of supported frame rates.
30
+ *
31
+ * RULE: Never pass 29.97 as a plain number. Use the literal type.
32
+ * This is a discriminated union — TypeScript enforces membership at compile time.
33
+ */
34
+ type FrameRate = 23.976 | 24 | 25 | 29.97 | 30 | 50 | 59.94 | 60;
35
+ /**
36
+ * Named constants for the most common rates.
37
+ * Prefer these over raw literals where possible.
38
+ */
39
+ declare const FrameRates: {
40
+ readonly CINEMA: FrameRate;
41
+ readonly PAL: FrameRate;
42
+ readonly NTSC_DF: FrameRate;
43
+ readonly NTSC: FrameRate;
44
+ readonly PAL_HFR: FrameRate;
45
+ readonly NTSC_HFR: FrameRate;
46
+ readonly HFR: FrameRate;
47
+ };
48
+ /**
49
+ * Legacy factory — kept for backward-compat with existing tests.
50
+ * This now validates that the value is a member of the FrameRate union.
51
+ * @throws if the value is not a recognised frame rate.
52
+ */
53
+ declare function frameRate(value: number): FrameRate;
54
+ /**
55
+ * RationalTime — a frame count at a specific rate. Used only at
56
+ * ingest/export boundaries. Never stored in TimelineState.
57
+ */
58
+ type RationalTime = {
59
+ readonly value: number;
60
+ readonly rate: FrameRate;
61
+ };
62
+ /**
63
+ * Timecode — SMPTE timecode string, display-only. Never use for arithmetic.
64
+ */
65
+ type Timecode = string & {
66
+ readonly __brand: "Timecode";
67
+ };
68
+ declare const toTimecode: (s: string) => Timecode;
69
+ /**
70
+ * TimeRange — a start + duration pair, both in TimelineFrame units.
71
+ */
72
+ type TimeRange = {
73
+ readonly startFrame: TimelineFrame;
74
+ readonly duration: TimelineFrame;
75
+ };
76
+ declare function isValidFrame(value: number): boolean;
77
+ declare function isDropFrame(fps: FrameRate): boolean;
78
+
79
+ /**
80
+ * ASSET MODEL — Phase 0 compliant
81
+ *
82
+ * Asset is immutable metadata about a media source file.
83
+ * Multiple Clips can reference the same Asset.
84
+ * Assets never change their intrinsicDuration after registration.
85
+ */
86
+
87
+ type AssetId = string & {
88
+ readonly __brand: 'AssetId';
89
+ };
90
+ type AssetStatus = 'online' | 'offline' | 'proxy-only' | 'missing';
91
+ /**
92
+ * Asset — immutable metadata for a single media source file.
93
+ *
94
+ * RULE: Never store wall-clock seconds or floating-point durations here.
95
+ * intrinsicDuration is always a TimelineFrame measured at the timeline fps.
96
+ */
97
+ type Asset = {
98
+ readonly id: AssetId;
99
+ readonly name: string;
100
+ /**
101
+ * mediaType must match the type of any Track this asset's clips are placed on.
102
+ * Replaces the old 'type: AssetType' field.
103
+ */
104
+ readonly mediaType: TrackType;
105
+ /** Replaces the old 'sourceUrl: string' field. */
106
+ readonly filePath: string;
107
+ /** Total length of the source media, expressed as TimelineFrame at the timeline fps. */
108
+ readonly intrinsicDuration: TimelineFrame;
109
+ /** The native frame rate the file was captured at. */
110
+ readonly nativeFps: FrameRate;
111
+ /** Where in the source the first frame maps to on the timeline. */
112
+ readonly sourceTimecodeOffset: TimelineFrame;
113
+ readonly status: AssetStatus;
114
+ };
115
+ declare function createAsset(params: {
116
+ id: string;
117
+ name: string;
118
+ mediaType: TrackType;
119
+ filePath: string;
120
+ intrinsicDuration: TimelineFrame;
121
+ nativeFps: FrameRate;
122
+ sourceTimecodeOffset: TimelineFrame;
123
+ status?: AssetStatus;
124
+ }): Asset;
125
+
126
+ /**
127
+ * CLIP MODEL — Phase 0 compliant
128
+ *
129
+ * A Clip is a time-bound reference to an Asset placed on a Track.
130
+ * All fields are readonly. Never mutate — always return a new object.
131
+ */
132
+
133
+ type ClipId = string & {
134
+ readonly __brand: 'ClipId';
135
+ };
136
+ /**
137
+ * Clip — a time-bound viewport into an Asset on a Track.
138
+ *
139
+ * TIMELINE BOUNDS: timelineStart / timelineEnd — where it sits on the track.
140
+ * MEDIA BOUNDS: mediaIn / mediaOut — which portion of the asset plays.
141
+ *
142
+ * INVARIANTS (Phase 0, speed=1.0):
143
+ * timelineEnd > timelineStart
144
+ * mediaOut > mediaIn
145
+ * (mediaOut - mediaIn) === (timelineEnd - timelineStart)
146
+ * mediaIn >= 0
147
+ * mediaOut <= asset.intrinsicDuration
148
+ * timelineEnd <= timeline.duration
149
+ * speed > 0
150
+ */
151
+ type Clip = {
152
+ readonly id: ClipId;
153
+ readonly assetId: AssetId;
154
+ readonly trackId: TrackId;
155
+ readonly timelineStart: TimelineFrame;
156
+ readonly timelineEnd: TimelineFrame;
157
+ readonly mediaIn: TimelineFrame;
158
+ readonly mediaOut: TimelineFrame;
159
+ readonly speed: number;
160
+ readonly enabled: boolean;
161
+ readonly reversed: boolean;
162
+ readonly name: string | null;
163
+ readonly color: string | null;
164
+ readonly metadata: Record<string, string>;
165
+ };
166
+ declare function createClip(params: {
167
+ id: string;
168
+ assetId: string;
169
+ trackId: string;
170
+ timelineStart: TimelineFrame;
171
+ timelineEnd: TimelineFrame;
172
+ mediaIn: TimelineFrame;
173
+ mediaOut: TimelineFrame;
174
+ speed?: number;
175
+ enabled?: boolean;
176
+ reversed?: boolean;
177
+ name?: string | null;
178
+ color?: string | null;
179
+ metadata?: Record<string, string>;
180
+ }): Clip;
181
+ declare function getClipDuration(clip: Clip): TimelineFrame;
182
+ declare function getClipMediaDuration(clip: Clip): TimelineFrame;
183
+ declare function clipContainsFrame(clip: Clip, f: TimelineFrame): boolean;
184
+ declare function clipsOverlap(a: Clip, b: Clip): boolean;
185
+
186
+ /**
187
+ * TRACK MODEL — Phase 0 compliant
188
+ *
189
+ * A Track is a horizontal container for Clips, always sorted by timelineStart.
190
+ */
191
+
192
+ type TrackId = string & {
193
+ readonly __brand: 'TrackId';
194
+ };
195
+ type TrackType = 'video' | 'audio' | 'subtitle' | 'title';
196
+ type Track = {
197
+ readonly id: TrackId;
198
+ readonly name: string;
199
+ readonly type: TrackType;
200
+ readonly locked: boolean;
201
+ readonly muted: boolean;
202
+ readonly solo: boolean;
203
+ readonly height: number;
204
+ /** Always sorted ascending by timelineStart — invariant enforced by checkInvariants. */
205
+ readonly clips: readonly Clip[];
206
+ };
207
+ declare function createTrack(params: {
208
+ id: string;
209
+ name: string;
210
+ type: TrackType;
211
+ clips?: readonly Clip[];
212
+ locked?: boolean;
213
+ muted?: boolean;
214
+ solo?: boolean;
215
+ height?: number;
216
+ }): Track;
217
+ /** Returns a new track with clips sorted ascending by timelineStart. */
218
+ declare function sortTrackClips(track: Track): Track;
219
+
220
+ /**
221
+ * TIMELINE MODEL — Phase 0 compliant
222
+ */
223
+
224
+ type SequenceSettings = {
225
+ readonly pixelAspectRatio: number;
226
+ readonly fieldOrder: 'progressive' | 'upper' | 'lower';
227
+ readonly colorSpace: string;
228
+ readonly audioSampleRate: number;
229
+ readonly audioChannelCount: number;
230
+ };
231
+ type Timeline = {
232
+ readonly id: string;
233
+ readonly name: string;
234
+ readonly fps: FrameRate;
235
+ readonly duration: TimelineFrame;
236
+ readonly startTimecode: Timecode;
237
+ readonly tracks: readonly Track[];
238
+ readonly sequenceSettings: SequenceSettings;
239
+ /**
240
+ * Increments by 1 on every successfully committed Transaction.
241
+ * Use this to detect stale references without deep equality checks.
242
+ */
243
+ readonly version: number;
244
+ };
245
+ declare function createTimeline(params: {
246
+ id: string;
247
+ name: string;
248
+ fps: FrameRate;
249
+ duration: TimelineFrame;
250
+ startTimecode?: Timecode;
251
+ tracks?: readonly Track[];
252
+ sequenceSettings?: Partial<SequenceSettings>;
253
+ }): Timeline;
254
+
255
+ /**
256
+ * TIMELINE STATE — Phase 0 compliant
257
+ *
258
+ * TimelineState is the single source of truth for the engine.
259
+ * Phase 0 only: timeline + assetRegistry. No Phase 2 fields.
260
+ *
261
+ * RULE: Every function that changes state returns a NEW TimelineState.
262
+ * Never mutate the existing state.
263
+ */
264
+
265
+ type AssetRegistry = ReadonlyMap<AssetId, Asset>;
266
+ type TimelineState = {
267
+ readonly timeline: Timeline;
268
+ readonly assetRegistry: AssetRegistry;
269
+ };
270
+ declare function createTimelineState(params: {
271
+ timeline: Timeline;
272
+ assetRegistry?: AssetRegistry;
273
+ /** @deprecated use assetRegistry. Kept for test backward-compat only. */
274
+ assets?: Map<string, Asset>;
275
+ }): TimelineState;
276
+
277
+ /**
278
+ * FRAME UTILITIES
279
+ *
280
+ * Pure functions for working with frame-based time values.
281
+ *
282
+ * These utilities handle:
283
+ * - Converting between frames and seconds
284
+ * - Formatting frames as timecode (HH:MM:SS:FF)
285
+ * - Frame arithmetic (clamping, rounding)
286
+ *
287
+ * CRITICAL RULES:
288
+ * - All conversions must quantize to whole frames
289
+ * - No floating-point frame values allowed
290
+ * - Always round/floor/ceil explicitly
291
+ *
292
+ * USAGE:
293
+ * ```typescript
294
+ * const fps = frameRate(30);
295
+ * const frames = secondsToFrames(5.5, fps); // 165 frames
296
+ * const seconds = framesToSeconds(frames, fps); // 5.5 seconds
297
+ * const timecode = framesToTimecode(frames, fps); // "00:00:05:15"
298
+ * ```
299
+ */
300
+
301
+ /**
302
+ * Convert frames to seconds
303
+ *
304
+ * @param frames - Frame number
305
+ * @param fps - Frames per second
306
+ * @returns Time in seconds (may be fractional)
307
+ */
308
+ declare function framesToSeconds(frames: TimelineFrame, fps: FrameRate): number;
309
+ /**
310
+ * Convert seconds to frames
311
+ *
312
+ * IMPORTANT: This rounds to the nearest frame.
313
+ * If you need different rounding behavior, use Math.floor or Math.ceil explicitly.
314
+ *
315
+ * @param seconds - Time in seconds
316
+ * @param fps - Frames per second
317
+ * @returns Frame number (rounded to nearest frame)
318
+ */
319
+ declare function secondsToFrames(seconds: number, fps: FrameRate): TimelineFrame;
320
+ /**
321
+ * Convert frames to timecode format (HH:MM:SS:FF)
322
+ *
323
+ * Example: 3825 frames at 30fps = "00:02:07:15"
324
+ *
325
+ * @param frames - Frame number
326
+ * @param fps - Frames per second
327
+ * @returns Timecode string
328
+ */
329
+ declare function framesToTimecode(frames: TimelineFrame, fps: FrameRate): string;
330
+ /**
331
+ * Convert frames to simple MM:SS format
332
+ *
333
+ * Example: 3825 frames at 30fps = "02:07"
334
+ *
335
+ * @param frames - Frame number
336
+ * @param fps - Frames per second
337
+ * @returns Time string in MM:SS format
338
+ */
339
+ declare function framesToMinutesSeconds(frames: TimelineFrame, fps: FrameRate): string;
340
+ /**
341
+ * Clamp a frame value between min and max
342
+ *
343
+ * @param value - Frame to clamp
344
+ * @param min - Minimum frame (inclusive)
345
+ * @param max - Maximum frame (inclusive)
346
+ * @returns Clamped frame value
347
+ */
348
+ declare function clampFrame(value: TimelineFrame, min: TimelineFrame, max: TimelineFrame): TimelineFrame;
349
+ /**
350
+ * Add two frame values
351
+ *
352
+ * @param a - First frame
353
+ * @param b - Second frame
354
+ * @returns Sum of frames
355
+ */
356
+ declare function addFrames(a: TimelineFrame, b: TimelineFrame): TimelineFrame;
357
+ /**
358
+ * Subtract two frame values
359
+ *
360
+ * @param a - First frame
361
+ * @param b - Second frame (subtracted from a)
362
+ * @returns Difference of frames (clamped to 0 if negative)
363
+ */
364
+ declare function subtractFrames(a: TimelineFrame, b: TimelineFrame): TimelineFrame;
365
+ /**
366
+ * Calculate duration between two frames
367
+ *
368
+ * @param start - Start frame
369
+ * @param end - End frame
370
+ * @returns Duration in frames (end - start)
371
+ */
372
+ declare function frameDuration(start: TimelineFrame, end: TimelineFrame): TimelineFrame;
373
+
374
+ /**
375
+ * TIMELINE ENGINE
376
+ *
377
+ * The main public API for the timeline editing kernel.
378
+ *
379
+ * WHAT IS THE TIMELINE ENGINE?
380
+ * - A thin wrapper around the history and dispatch systems
381
+ * - Provides a convenient, object-oriented API
382
+ * - Manages internal state
383
+ * - Coordinates operations, validation, and history
384
+ *
385
+ * WHY A CLASS?
386
+ * - Encapsulates state management
387
+ * - Provides a clean API for users
388
+ * - Hides complexity of history and dispatch
389
+ * - Familiar OOP interface for most developers
390
+ *
391
+ * USAGE:
392
+ * ```typescript
393
+ * const engine = new TimelineEngine(initialState);
394
+ *
395
+ * // Add a clip
396
+ * const result = engine.addClip(trackId, clip);
397
+ * if (!result.success) {
398
+ * console.error('Failed to add clip:', result.errors);
399
+ * }
400
+ *
401
+ * // Undo/redo
402
+ * engine.undo();
403
+ * engine.redo();
404
+ *
405
+ * // Query state
406
+ * const clip = engine.findClipById('clip_1');
407
+ * const state = engine.getState();
408
+ * ```
409
+ *
410
+ * DESIGN PHILOSOPHY:
411
+ * - Business logic lives in pure modules (operations, validation, etc.)
412
+ * - Engine is just a thin orchestration layer
413
+ * - Easy to test (can test pure functions independently)
414
+ */
415
+
416
+ /**
417
+ * TimelineEngine - The main timeline editing engine
418
+ *
419
+ * Provides a high-level API for timeline editing with built-in
420
+ * undo/redo, validation, and state management.
421
+ */
422
+ declare class TimelineEngine {
423
+ private history;
424
+ private listeners;
425
+ /**
426
+ * Create a new timeline engine
427
+ *
428
+ * @param initialState - Initial timeline state
429
+ * @param historyLimit - Maximum number of undo steps (default: 50)
430
+ */
431
+ constructor(initialState: TimelineState, historyLimit?: number);
432
+ /**
433
+ * Subscribe to state changes
434
+ *
435
+ * The listener will be called whenever the timeline state changes,
436
+ * with the new state passed as an argument.
437
+ * This is used by framework adapters (e.g., React) to trigger re-renders.
438
+ *
439
+ * @param listener - Function to call on state changes, receives new state
440
+ * @returns Unsubscribe function
441
+ *
442
+ * @example
443
+ * ```typescript
444
+ * const unsubscribe = engine.subscribe((state) => {
445
+ * console.log('State changed:', state);
446
+ * });
447
+ *
448
+ * // Later...
449
+ * unsubscribe();
450
+ * ```
451
+ */
452
+ subscribe(listener: (state: TimelineState) => void): () => void;
453
+ /**
454
+ * Notify all subscribers of a state change
455
+ *
456
+ * This is called internally after any operation that modifies state.
457
+ * Framework adapters use this to trigger re-renders.
458
+ */
459
+ private notify;
460
+ /**
461
+ * Get the current timeline state
462
+ *
463
+ * @returns Current timeline state
464
+ */
465
+ getState(): TimelineState;
466
+ /**
467
+ * Register an asset
468
+ *
469
+ * @param asset - Asset to register
470
+ * @returns Dispatch result
471
+ */
472
+ registerAsset(asset: Asset): {
473
+ accepted: boolean;
474
+ errors?: {
475
+ code: string;
476
+ message: string;
477
+ }[];
478
+ };
479
+ /**
480
+ * Get an asset by ID
481
+ *
482
+ * @param assetId - Asset ID
483
+ * @returns The asset, or undefined if not found
484
+ */
485
+ getAsset(assetId: string): Asset | undefined;
486
+ /**
487
+ * Add a clip to a track
488
+ *
489
+ * @param trackId - ID of the track to add to
490
+ * @param clip - Clip to add
491
+ * @returns Dispatch result
492
+ */
493
+ addClip(trackId: string, clip: Clip): {
494
+ accepted: boolean;
495
+ errors?: {
496
+ code: string;
497
+ message: string;
498
+ }[];
499
+ };
500
+ /**
501
+ * Remove a clip
502
+ *
503
+ * @param clipId - ID of the clip to remove
504
+ * @returns Dispatch result
505
+ */
506
+ removeClip(clipId: string): {
507
+ accepted: boolean;
508
+ errors?: {
509
+ code: string;
510
+ message: string;
511
+ }[];
512
+ };
513
+ /**
514
+ * Move a clip to a new timeline position
515
+ *
516
+ * @param clipId - ID of the clip to move
517
+ * @param newStart - New timeline start frame
518
+ * @returns Dispatch result
519
+ */
520
+ moveClip(clipId: string, newStart: TimelineFrame): {
521
+ accepted: boolean;
522
+ errors?: {
523
+ code: string;
524
+ message: string;
525
+ }[];
526
+ };
527
+ /**
528
+ * Resize a clip
529
+ *
530
+ * @param clipId - ID of the clip to resize
531
+ * @param newStart - New timeline start frame
532
+ * @param newEnd - New timeline end frame
533
+ * @returns Dispatch result
534
+ */
535
+ resizeClip(clipId: string, newStart: TimelineFrame, newEnd: TimelineFrame): {
536
+ accepted: boolean;
537
+ errors?: {
538
+ code: string;
539
+ message: string;
540
+ }[];
541
+ };
542
+ /**
543
+ * Trim a clip (change media bounds)
544
+ *
545
+ * @param clipId - ID of the clip to trim
546
+ * @param newMediaIn - New media in frame
547
+ * @param newMediaOut - New media out frame
548
+ * @returns Dispatch result
549
+ */
550
+ trimClip(clipId: string, newMediaIn: TimelineFrame, newMediaOut: TimelineFrame): {
551
+ accepted: boolean;
552
+ errors?: {
553
+ code: string;
554
+ message: string;
555
+ }[];
556
+ };
557
+ /**
558
+ * Move a clip to a different track
559
+ *
560
+ * @param clipId - ID of the clip to move
561
+ * @param targetTrackId - ID of the target track
562
+ * @returns Dispatch result
563
+ */
564
+ moveClipToTrack(clipId: string, targetTrackId: string): {
565
+ accepted: boolean;
566
+ errors?: {
567
+ code: string;
568
+ message: string;
569
+ }[];
570
+ };
571
+ /**
572
+ * Add a track
573
+ *
574
+ * @param track - Track to add
575
+ * @returns Dispatch result
576
+ */
577
+ addTrack(track: Track): {
578
+ accepted: boolean;
579
+ errors?: {
580
+ code: string;
581
+ message: string;
582
+ }[];
583
+ };
584
+ /**
585
+ * Remove a track
586
+ *
587
+ * @param trackId - ID of the track to remove
588
+ * @returns Dispatch result
589
+ */
590
+ removeTrack(trackId: string): {
591
+ accepted: boolean;
592
+ errors?: {
593
+ code: string;
594
+ message: string;
595
+ }[];
596
+ };
597
+ /**
598
+ * Move a track to a new position
599
+ *
600
+ * @param trackId - ID of the track to move
601
+ * @param newIndex - New index position
602
+ * @returns Dispatch result
603
+ */
604
+ moveTrack(trackId: string, newIndex: number): {
605
+ accepted: boolean;
606
+ errors?: {
607
+ code: string;
608
+ message: string;
609
+ }[];
610
+ };
611
+ /**
612
+ * Toggle track mute
613
+ *
614
+ * @param trackId - ID of the track
615
+ * @returns Dispatch result
616
+ */
617
+ toggleTrackMute(trackId: string): {
618
+ accepted: boolean;
619
+ errors?: {
620
+ code: string;
621
+ message: string;
622
+ }[];
623
+ };
624
+ /**
625
+ * Toggle track lock
626
+ *
627
+ * @param trackId - ID of the track
628
+ * @returns Dispatch result
629
+ */
630
+ toggleTrackLock(trackId: string): {
631
+ accepted: boolean;
632
+ errors?: {
633
+ code: string;
634
+ message: string;
635
+ }[];
636
+ };
637
+ /**
638
+ * Toggle track solo
639
+ *
640
+ * @param trackId - ID of the track
641
+ * @returns Dispatch result
642
+ */
643
+ toggleTrackSolo(trackId: string): {
644
+ accepted: boolean;
645
+ errors?: {
646
+ code: string;
647
+ message: string;
648
+ }[];
649
+ };
650
+ /**
651
+ * Set track height
652
+ *
653
+ * @param trackId - ID of the track
654
+ * @param height - New height in pixels
655
+ * @returns Dispatch result
656
+ */
657
+ setTrackHeight(trackId: string, height: number): {
658
+ accepted: boolean;
659
+ errors?: {
660
+ code: string;
661
+ message: string;
662
+ }[];
663
+ };
664
+ /**
665
+ * Set timeline duration
666
+ *
667
+ * @param duration - New duration in frames
668
+ * @returns Dispatch result
669
+ */
670
+ setTimelineDuration(duration: TimelineFrame): {
671
+ accepted: boolean;
672
+ errors?: {
673
+ code: string;
674
+ message: string;
675
+ }[];
676
+ };
677
+ /**
678
+ * Set timeline name
679
+ *
680
+ * @param name - New timeline name
681
+ * @returns Dispatch result
682
+ */
683
+ setTimelineName(name: string): {
684
+ accepted: boolean;
685
+ errors?: {
686
+ code: string;
687
+ message: string;
688
+ }[];
689
+ };
690
+ /**
691
+ * Undo the last action
692
+ *
693
+ * @returns true if undo was performed
694
+ */
695
+ undo(): boolean;
696
+ /**
697
+ * Redo the last undone action
698
+ *
699
+ * @returns true if redo was performed
700
+ */
701
+ redo(): boolean;
702
+ /**
703
+ * Check if undo is available
704
+ *
705
+ * @returns true if undo is available
706
+ */
707
+ canUndo(): boolean;
708
+ /**
709
+ * Check if redo is available
710
+ *
711
+ * @returns true if redo is available
712
+ */
713
+ canRedo(): boolean;
714
+ /**
715
+ * Find a clip by ID
716
+ *
717
+ * @param clipId - Clip ID
718
+ * @returns The clip, or undefined if not found
719
+ */
720
+ findClipById(clipId: string): Clip | undefined;
721
+ /**
722
+ * Find a track by ID
723
+ *
724
+ * @param trackId - Track ID
725
+ * @returns The track, or undefined if not found
726
+ */
727
+ findTrackById(trackId: string): Track | undefined;
728
+ /**
729
+ * Get all clips on a track
730
+ *
731
+ * @param trackId - Track ID
732
+ * @returns Array of clips on the track
733
+ */
734
+ getClipsOnTrack(trackId: string): Clip[];
735
+ /**
736
+ * Get all clips at a specific frame
737
+ *
738
+ * @param frame - Frame to check
739
+ * @returns Array of clips at that frame
740
+ */
741
+ getClipsAtFrame(f: TimelineFrame): Clip[];
742
+ /**
743
+ * Get all clips in a frame range
744
+ *
745
+ * @param start - Start frame
746
+ * @param end - End frame
747
+ * @returns Array of clips in the range
748
+ */
749
+ getClipsInRange(start: TimelineFrame, end: TimelineFrame): Clip[];
750
+ /**
751
+ * Get all clips in the timeline
752
+ *
753
+ * @returns Array of all clips
754
+ */
755
+ getAllClips(): Clip[];
756
+ /**
757
+ * Get all tracks in the timeline
758
+ *
759
+ * @returns Array of all tracks
760
+ */
761
+ getAllTracks(): readonly Track[];
762
+ /**
763
+ * Ripple delete - delete clip and shift subsequent clips left
764
+ *
765
+ * @param clipId - ID of the clip to delete
766
+ * @returns Dispatch result
767
+ */
768
+ rippleDelete(clipId: string): {
769
+ accepted: boolean;
770
+ errors?: {
771
+ code: string;
772
+ message: string;
773
+ }[];
774
+ };
775
+ /**
776
+ * Ripple trim - trim clip end and shift subsequent clips
777
+ *
778
+ * @param clipId - ID of the clip to trim
779
+ * @param newEnd - New end frame for the clip
780
+ * @returns Dispatch result
781
+ */
782
+ rippleTrim(clipId: string, newEnd: TimelineFrame): {
783
+ accepted: boolean;
784
+ errors?: {
785
+ code: string;
786
+ message: string;
787
+ }[];
788
+ };
789
+ /**
790
+ * Insert edit - insert clip and shift subsequent clips right
791
+ *
792
+ * @param trackId - ID of the track to insert into
793
+ * @param clip - Clip to insert
794
+ * @param atFrame - Frame to insert at
795
+ * @returns Dispatch result
796
+ */
797
+ insertEdit(trackId: string, clip: Clip, atFrame: TimelineFrame): {
798
+ accepted: boolean;
799
+ errors?: {
800
+ code: string;
801
+ message: string;
802
+ }[];
803
+ };
804
+ /**
805
+ * Ripple move - move clip and shift surrounding clips to accommodate
806
+ *
807
+ * This moves a clip to a new position while maintaining timeline continuity:
808
+ * - Closes the gap at the source position
809
+ * - Makes space at the destination position
810
+ * - All operations are atomic (single undo entry)
811
+ *
812
+ * @param clipId - ID of the clip to move
813
+ * @param newStart - New start frame for the clip
814
+ * @returns Dispatch result
815
+ */
816
+ rippleMove(clipId: string, newStart: TimelineFrame): {
817
+ accepted: boolean;
818
+ errors?: {
819
+ code: string;
820
+ message: string;
821
+ }[];
822
+ };
823
+ /**
824
+ * Insert move - move clip and shift destination clips right
825
+ *
826
+ * This moves a clip to a new position without closing the gap at source:
827
+ * - Leaves gap at the source position
828
+ * - Pushes all clips at destination right to make space
829
+ * - All operations are atomic (single undo entry)
830
+ *
831
+ * @param clipId - ID of the clip to move
832
+ * @param newStart - New start frame for the clip
833
+ * @returns Dispatch result
834
+ */
835
+ insertMove(clipId: string, newStart: TimelineFrame): {
836
+ accepted: boolean;
837
+ errors?: {
838
+ code: string;
839
+ message: string;
840
+ }[];
841
+ };
842
+ }
843
+
844
+ /**
845
+ * OPERATION PRIMITIVES — Phase 0 compliant
846
+ *
847
+ * The ONLY way to express a mutation in the engine.
848
+ * All mutations flow through: OperationPrimitive[] → Transaction → Dispatcher.
849
+ *
850
+ * RULE: Never add a new mutation function.
851
+ * Add a new type to OperationPrimitive, handle it in the Dispatcher switch,
852
+ * update the InvariantChecker, and update OPERATIONS.md.
853
+ *
854
+ * RULE: Transactions are all-or-nothing.
855
+ * If any primitive fails validation, the entire Transaction is rejected.
856
+ */
857
+
858
+ type OperationPrimitive = {
859
+ type: 'MOVE_CLIP';
860
+ clipId: ClipId;
861
+ newTimelineStart: TimelineFrame;
862
+ targetTrackId?: TrackId;
863
+ } | {
864
+ type: 'RESIZE_CLIP';
865
+ clipId: ClipId;
866
+ edge: 'start' | 'end';
867
+ newFrame: TimelineFrame;
868
+ } | {
869
+ type: 'SLICE_CLIP';
870
+ clipId: ClipId;
871
+ atFrame: TimelineFrame;
872
+ } | {
873
+ type: 'DELETE_CLIP';
874
+ clipId: ClipId;
875
+ } | {
876
+ type: 'INSERT_CLIP';
877
+ clip: Clip;
878
+ trackId: TrackId;
879
+ } | {
880
+ type: 'SET_MEDIA_BOUNDS';
881
+ clipId: ClipId;
882
+ mediaIn: TimelineFrame;
883
+ mediaOut: TimelineFrame;
884
+ } | {
885
+ type: 'SET_CLIP_ENABLED';
886
+ clipId: ClipId;
887
+ enabled: boolean;
888
+ } | {
889
+ type: 'SET_CLIP_REVERSED';
890
+ clipId: ClipId;
891
+ reversed: boolean;
892
+ } | {
893
+ type: 'SET_CLIP_SPEED';
894
+ clipId: ClipId;
895
+ speed: number;
896
+ } | {
897
+ type: 'SET_CLIP_COLOR';
898
+ clipId: ClipId;
899
+ color: string | null;
900
+ } | {
901
+ type: 'SET_CLIP_NAME';
902
+ clipId: ClipId;
903
+ name: string | null;
904
+ } | {
905
+ type: 'ADD_TRACK';
906
+ track: Track;
907
+ } | {
908
+ type: 'DELETE_TRACK';
909
+ trackId: TrackId;
910
+ } | {
911
+ type: 'REORDER_TRACK';
912
+ trackId: TrackId;
913
+ newIndex: number;
914
+ } | {
915
+ type: 'SET_TRACK_HEIGHT';
916
+ trackId: TrackId;
917
+ height: number;
918
+ } | {
919
+ type: 'SET_TRACK_NAME';
920
+ trackId: TrackId;
921
+ name: string;
922
+ } | {
923
+ type: 'REGISTER_ASSET';
924
+ asset: Asset;
925
+ } | {
926
+ type: 'UNREGISTER_ASSET';
927
+ assetId: AssetId;
928
+ } | {
929
+ type: 'SET_ASSET_STATUS';
930
+ assetId: AssetId;
931
+ status: AssetStatus;
932
+ } | {
933
+ type: 'RENAME_TIMELINE';
934
+ name: string;
935
+ } | {
936
+ type: 'SET_TIMELINE_DURATION';
937
+ duration: TimelineFrame;
938
+ } | {
939
+ type: 'SET_TIMELINE_START_TC';
940
+ startTimecode: Timecode;
941
+ } | {
942
+ type: 'SET_SEQUENCE_SETTINGS';
943
+ settings: Partial<SequenceSettings>;
944
+ };
945
+ /**
946
+ * Transaction — an atomic, labeled batch of OperationPrimitives.
947
+ *
948
+ * All primitives in a Transaction are validated before any are applied.
949
+ * If one fails, none are applied. This is the all-or-nothing rule.
950
+ */
951
+ type Transaction = {
952
+ readonly id: string;
953
+ readonly label: string;
954
+ readonly timestamp: number;
955
+ readonly operations: readonly OperationPrimitive[];
956
+ };
957
+ type RejectionReason = 'OVERLAP' | 'LOCKED_TRACK' | 'ASSET_MISSING' | 'TYPE_MISMATCH' | 'OUT_OF_BOUNDS' | 'MEDIA_BOUNDS_INVALID' | 'ASSET_IN_USE' | 'TRACK_NOT_EMPTY' | 'SPEED_INVALID' | 'INVARIANT_VIOLATED';
958
+ type DispatchResult = {
959
+ accepted: true;
960
+ nextState: TimelineState;
961
+ } | {
962
+ accepted: false;
963
+ reason: RejectionReason;
964
+ message: string;
965
+ };
966
+ type ViolationType = 'OVERLAP' | 'MEDIA_BOUNDS_INVALID' | 'ASSET_MISSING' | 'TRACK_TYPE_MISMATCH' | 'CLIP_BEYOND_TIMELINE' | 'TRACK_NOT_SORTED' | 'DURATION_MISMATCH' | 'SPEED_INVALID';
967
+ type InvariantViolation = {
968
+ readonly type: ViolationType;
969
+ readonly entityId: string;
970
+ readonly message: string;
971
+ };
972
+
973
+ /**
974
+ * DISPATCHER — Phase 0 compliant
975
+ *
976
+ * The ONLY entry point for mutating TimelineState.
977
+ * Validates first, applies atomically, checks invariants.
978
+ *
979
+ * Algorithm:
980
+ * 1. For each operation: run per-primitive validator → reject immediately on failure
981
+ * 2. Apply all operations sequentially to get proposedState
982
+ * 3. Run checkInvariants(proposedState) → reject on any violation
983
+ * 4. Bump timeline.version by 1 and return accepted
984
+ *
985
+ * RULE: If one primitive fails, zero primitives are applied.
986
+ */
987
+
988
+ declare function dispatch(state: TimelineState, transaction: Transaction): DispatchResult;
989
+
990
+ /**
991
+ * INVARIANT CHECKER — Phase 0 compliant
992
+ *
993
+ * The most critical file in the engine.
994
+ * checkInvariants() runs after every proposed state change inside the Dispatcher.
995
+ * Zero violations is the only acceptable result in tests and at commit time.
996
+ *
997
+ * RULE: Run checkInvariants in EVERY test after every state mutation.
998
+ */
999
+
1000
+ declare function checkInvariants(state: TimelineState): InvariantViolation[];
1001
+
1002
+ /**
1003
+ * HISTORY ENGINE
1004
+ *
1005
+ * Snapshot-based undo/redo system for timeline state.
1006
+ *
1007
+ * WHAT IS THE HISTORY ENGINE?
1008
+ * - Stores immutable snapshots of timeline state
1009
+ * - Provides undo/redo functionality
1010
+ * - Prevents state corruption
1011
+ *
1012
+ * HOW IT WORKS:
1013
+ * - past: Array of previous states
1014
+ * - present: Current state
1015
+ * - future: Array of states that can be redone
1016
+ *
1017
+ * WHY SNAPSHOTS?
1018
+ * - Simple and reliable (no complex diffing)
1019
+ * - Guaranteed to restore exact state
1020
+ * - No risk of partial corruption
1021
+ * - Easy to implement and test
1022
+ *
1023
+ * USAGE:
1024
+ * ```typescript
1025
+ * let history = createHistory(initialState);
1026
+ * history = pushHistory(history, newState);
1027
+ * history = undo(history);
1028
+ * history = redo(history);
1029
+ * ```
1030
+ *
1031
+ * ALL FUNCTIONS ARE PURE:
1032
+ * - Take history as input
1033
+ * - Return new history as output
1034
+ * - Never mutate input
1035
+ */
1036
+
1037
+ /**
1038
+ * HistoryState - The history container
1039
+ *
1040
+ * Contains:
1041
+ * - past: Array of previous states (oldest first)
1042
+ * - present: Current state
1043
+ * - future: Array of states that can be redone (newest first)
1044
+ * - limit: Maximum number of past states to keep
1045
+ */
1046
+ interface HistoryState {
1047
+ past: TimelineState[];
1048
+ present: TimelineState;
1049
+ future: TimelineState[];
1050
+ limit: number;
1051
+ }
1052
+ /**
1053
+ * Create a new history state
1054
+ *
1055
+ * @param initialState - Initial timeline state
1056
+ * @param limit - Maximum number of past states to keep (default: 50)
1057
+ * @returns A new HistoryState
1058
+ */
1059
+ declare function createHistory(initialState: TimelineState, limit?: number): HistoryState;
1060
+ /**
1061
+ * Push a new state to history
1062
+ *
1063
+ * Moves current state to past, sets new state as present,
1064
+ * and clears future (can't redo after new action).
1065
+ *
1066
+ * @param history - Current history state
1067
+ * @param newState - New timeline state to push
1068
+ * @returns New history state with new state pushed
1069
+ */
1070
+ declare function pushHistory(history: HistoryState, newState: TimelineState): HistoryState;
1071
+ /**
1072
+ * Undo the last action
1073
+ *
1074
+ * Moves current state to future, pops last state from past
1075
+ * and sets it as present.
1076
+ *
1077
+ * @param history - Current history state
1078
+ * @returns New history state with undo applied
1079
+ */
1080
+ declare function undo(history: HistoryState): HistoryState;
1081
+ /**
1082
+ * Redo the last undone action
1083
+ *
1084
+ * Moves current state to past, pops first state from future
1085
+ * and sets it as present.
1086
+ *
1087
+ * @param history - Current history state
1088
+ * @returns New history state with redo applied
1089
+ */
1090
+ declare function redo(history: HistoryState): HistoryState;
1091
+ /**
1092
+ * Check if undo is available
1093
+ *
1094
+ * @param history - Current history state
1095
+ * @returns true if undo is available
1096
+ */
1097
+ declare function canUndo(history: HistoryState): boolean;
1098
+ /**
1099
+ * Check if redo is available
1100
+ *
1101
+ * @param history - Current history state
1102
+ * @returns true if redo is available
1103
+ */
1104
+ declare function canRedo(history: HistoryState): boolean;
1105
+ /**
1106
+ * Get the current state from history
1107
+ *
1108
+ * @param history - Current history state
1109
+ * @returns The current timeline state
1110
+ */
1111
+ declare function getCurrentState(history: HistoryState): TimelineState;
1112
+
1113
+ /**
1114
+ * SNAP INDEX — Phase 1
1115
+ *
1116
+ * Pure functions. Zero React/DOM imports. Zero mutation.
1117
+ *
1118
+ * Phase 1 snap sources: ClipStart, ClipEnd, Playhead.
1119
+ * Phase 2 will add: Marker, InPoint, OutPoint.
1120
+ * Phase 3 will add: BeatGrid.
1121
+ *
1122
+ * Priority table (do not change values):
1123
+ * Marker: 100
1124
+ * InPoint: 90
1125
+ * OutPoint: 90
1126
+ * ClipStart: 80
1127
+ * ClipEnd: 80
1128
+ * Playhead: 70
1129
+ * BeatGrid: 50
1130
+ */
1131
+
1132
+ /**
1133
+ * All snap point sources across phases.
1134
+ * Defined in full now so SnapPoint & allowedTypes filters are stable.
1135
+ */
1136
+ type SnapPointType = 'ClipStart' | 'ClipEnd' | 'Playhead' | 'Marker' | 'InPoint' | 'OutPoint' | 'BeatGrid';
1137
+ type SnapPoint = {
1138
+ readonly frame: TimelineFrame;
1139
+ readonly type: SnapPointType;
1140
+ readonly priority: number;
1141
+ readonly trackId: TrackId | null;
1142
+ readonly sourceId: string;
1143
+ };
1144
+ type SnapIndex = {
1145
+ readonly points: readonly SnapPoint[];
1146
+ readonly builtAt: number;
1147
+ readonly enabled: boolean;
1148
+ };
1149
+ /**
1150
+ * Build a SnapIndex from committed state + playhead position.
1151
+ *
1152
+ * RULE: Call via queueMicrotask after accepted dispatch.
1153
+ * Never call during a drag (pointer move).
1154
+ *
1155
+ * Phase 1 sources pulled (in order):
1156
+ * 1. ClipStart + ClipEnd from every clip on every track
1157
+ * 2. Playhead position (trackId = null)
1158
+ */
1159
+ declare function buildSnapIndex(state: TimelineState, playheadFrame: TimelineFrame, enabled?: boolean): SnapIndex;
1160
+ /**
1161
+ * Find the highest-priority snap candidate within radiusFrames.
1162
+ *
1163
+ * Returns null when:
1164
+ * - index.enabled is false
1165
+ * - no point is within radiusFrames of frame
1166
+ *
1167
+ * Tiebreak (equidistant candidates): highest priority wins.
1168
+ * Second tiebreak (equal priority): first in sorted order.
1169
+ *
1170
+ * @param exclude sourceIds to skip (e.g. the clip being dragged)
1171
+ * @param allowedTypes if provided, only consider points of these types
1172
+ */
1173
+ declare function nearest(index: SnapIndex, frame: TimelineFrame, radiusFrames: number, exclude?: readonly string[], allowedTypes?: readonly SnapPointType[]): SnapPoint | null;
1174
+ /**
1175
+ * Return a new SnapIndex with enabled toggled.
1176
+ * Does NOT rebuild points — pure field update.
1177
+ */
1178
+ declare function toggleSnap(index: SnapIndex, enabled: boolean): SnapIndex;
1179
+
1180
+ /**
1181
+ * TOOL CONTRACT TYPES — Phase 1
1182
+ *
1183
+ * Zero implementation. Zero imports from React or DOM.
1184
+ * Every ITool must satisfy this interface exactly.
1185
+ *
1186
+ * RULES (from ITOOL_CONTRACT.md):
1187
+ * - onPointerMove NEVER calls dispatch
1188
+ * - onPointerUp NEVER mutates instance state
1189
+ * - onKeyDown, onKeyUp, onCancel are REQUIRED — implement as no-ops if unused
1190
+ */
1191
+
1192
+ type ToolId = string & {
1193
+ readonly __brand: 'ToolId';
1194
+ };
1195
+ declare function toToolId(s: string): ToolId;
1196
+ /** Keyboard modifier state — available on ToolContext so getCursor() can
1197
+ * react to held keys even when no pointer event is firing. */
1198
+ type Modifiers = {
1199
+ readonly shift: boolean;
1200
+ readonly alt: boolean;
1201
+ readonly ctrl: boolean;
1202
+ readonly meta: boolean;
1203
+ };
1204
+ /** Normalised pointer event in frame-space.
1205
+ * ToolRouter populates clipId via hit-test — tools never recompute it. */
1206
+ type TimelinePointerEvent = {
1207
+ readonly frame: TimelineFrame;
1208
+ readonly trackId: TrackId | null;
1209
+ readonly clipId: ClipId | null;
1210
+ readonly x: number;
1211
+ readonly y: number;
1212
+ readonly buttons: number;
1213
+ readonly shiftKey: boolean;
1214
+ readonly altKey: boolean;
1215
+ readonly metaKey: boolean;
1216
+ };
1217
+ type TimelineKeyEvent = {
1218
+ readonly key: string;
1219
+ readonly code: string;
1220
+ readonly shiftKey: boolean;
1221
+ readonly altKey: boolean;
1222
+ readonly metaKey: boolean;
1223
+ };
1224
+ /** Ghost state produced by onPointerMove.
1225
+ * isProvisional: true is a compile-time discriminant so resolveClip()
1226
+ * can distinguish provisional from committed Clip[] arrays. */
1227
+ type ProvisionalState = {
1228
+ readonly clips: readonly Clip[];
1229
+ readonly isProvisional: true;
1230
+ };
1231
+ /** Injected by TimelineEngine on every event call.
1232
+ * Tools never import TimelineEngine. They never call dispatch() directly. */
1233
+ type ToolContext = {
1234
+ readonly state: TimelineState;
1235
+ readonly snapIndex: SnapIndex;
1236
+ readonly pixelsPerFrame: number;
1237
+ /** Current modifier key state — updates on every pointer/key event. */
1238
+ readonly modifiers: Modifiers;
1239
+ /** Convert a client-pixel x-position to a TimelineFrame. */
1240
+ readonly frameAtX: (x: number) => TimelineFrame;
1241
+ /** Return the TrackId whose row contains client-pixel y, or null. */
1242
+ readonly trackAtY: (y: number) => TrackId | null;
1243
+ /** Query snap and return the snapped frame (or original if no hit).
1244
+ * Handles enabled/disabled, radius, exclusion, and type filter internally.
1245
+ * Tools never see radiusFrames or the enabled flag. */
1246
+ readonly snap: (frame: TimelineFrame, exclude?: readonly string[], allowedTypes?: readonly SnapPointType[]) => TimelineFrame;
1247
+ };
1248
+ interface ITool {
1249
+ readonly id: ToolId;
1250
+ /** Single-character keyboard shortcut, e.g. 'v', 'b', 'r'. Empty string = no shortcut. */
1251
+ readonly shortcutKey: string;
1252
+ /** Return the CSS cursor string for the current tool + modifier state.
1253
+ * Called on every pointermove — must be cheap. */
1254
+ getCursor(ctx: ToolContext): string;
1255
+ /** Return the SnapPointType categories this tool snaps to.
1256
+ * Used by ctx.snap() to filter the snap index automatically. */
1257
+ getSnapCandidateTypes(): readonly SnapPointType[];
1258
+ onPointerDown(event: TimelinePointerEvent, ctx: ToolContext): void;
1259
+ /** Return ProvisionalState for ghost rendering.
1260
+ * MUST NOT call dispatch. MUST NOT call engine methods. */
1261
+ onPointerMove(event: TimelinePointerEvent, ctx: ToolContext): ProvisionalState | null;
1262
+ /** Return a Transaction to commit, or null if this gesture produces no edit.
1263
+ * MUST NOT mutate any instance state. */
1264
+ onPointerUp(event: TimelinePointerEvent, ctx: ToolContext): Transaction | null;
1265
+ /** Handle a keydown — return a Transaction or null.
1266
+ * Required — implement as `return null` if unused. */
1267
+ onKeyDown(event: TimelineKeyEvent, ctx: ToolContext): Transaction | null;
1268
+ /** Handle a keyup — no return value.
1269
+ * Required — implement as no-op if unused. */
1270
+ onKeyUp(event: TimelineKeyEvent, ctx: ToolContext): void;
1271
+ /** Called when a gesture is interrupted (Escape, tool switch mid-drag).
1272
+ * Required — implement as no-op if unused. */
1273
+ onCancel(): void;
1274
+ }
1275
+
1276
+ /**
1277
+ * TOOL REGISTRY — Phase 1
1278
+ *
1279
+ * Pure functions. No classes. No React. No state mutation.
1280
+ *
1281
+ * ToolRegistry is immutable data — activateTool returns a NEW registry.
1282
+ * The active tool lives here, not on TimelineEngine, keeping the engine thin.
1283
+ *
1284
+ * RULES:
1285
+ * - activateTool calls outgoing.onCancel() before switching
1286
+ * - activateTool throws on unknown id (programmer error, never user error)
1287
+ * - NoOpTool is the canonical do-nothing ITool (test double + startup default)
1288
+ */
1289
+
1290
+ type ToolRegistry = {
1291
+ readonly tools: ReadonlyMap<ToolId, ITool>;
1292
+ readonly activeToolId: ToolId;
1293
+ };
1294
+ /**
1295
+ * Create an initial registry from an array of tools.
1296
+ *
1297
+ * @throws if defaultId is not present in the tools array
1298
+ */
1299
+ declare function createRegistry(tools: readonly ITool[], defaultId: ToolId): ToolRegistry;
1300
+ /**
1301
+ * Activate a new tool.
1302
+ *
1303
+ * Steps (must run in order):
1304
+ * 1. Call outgoing tool's onCancel() — cleans up any in-progress drag state
1305
+ * 2. Validate that the new id exists in the registry
1306
+ * 3. Return a new ToolRegistry with activeToolId updated
1307
+ *
1308
+ * @throws if id is not registered
1309
+ */
1310
+ declare function activateTool(registry: ToolRegistry, id: ToolId): ToolRegistry;
1311
+ /**
1312
+ * Return the currently active ITool.
1313
+ * Never returns undefined — registry invariant guarantees activeToolId is registered.
1314
+ */
1315
+ declare function getActiveTool(registry: ToolRegistry): ITool;
1316
+ /**
1317
+ * Return a new registry with the tool added.
1318
+ * If a tool with the same id already exists, it is replaced.
1319
+ * activeToolId is unchanged.
1320
+ */
1321
+ declare function registerTool(registry: ToolRegistry, tool: ITool): ToolRegistry;
1322
+ /**
1323
+ * Satisfies ITool with no side effects.
1324
+ *
1325
+ * Use for:
1326
+ * - Test doubles (spread and override only the methods you need)
1327
+ * - Default active tool on engine startup
1328
+ * - ToolRouter smoke tests
1329
+ *
1330
+ * onCancel() is a deliberate no-op: NoOpTool has no drag state to clean up.
1331
+ * Real tools will clear instance variables there.
1332
+ */
1333
+ declare const NoOpTool: ITool;
1334
+
1335
+ /**
1336
+ * PROVISIONAL MANAGER — Phase 1
1337
+ *
1338
+ * Manages ghost state during pointer drags.
1339
+ *
1340
+ * RULES (from ITOOL_CONTRACT.md):
1341
+ * - setProvisional / clearProvisional return NEW objects — never mutate
1342
+ * - resolveClip checks provisional first, then committed state
1343
+ * - The engine calls clearProvisional() BEFORE dispatching onPointerUp's tx
1344
+ * - Provisional updates trigger notify() so ghosts render immediately
1345
+ *
1346
+ * resolveClip priority:
1347
+ * 1. provisional.clips has a clip with this id → return ghost version
1348
+ * 2. clip exists in committed state → return committed
1349
+ * 3. clip absent from both (deleted mid-drag) → return undefined
1350
+ */
1351
+
1352
+ type ProvisionalManager = {
1353
+ readonly current: ProvisionalState | null;
1354
+ };
1355
+ /** Create an empty provisional manager (current = null). */
1356
+ declare function createProvisionalManager(): ProvisionalManager;
1357
+ /** Return a new manager with current set to state.
1358
+ * Pure — never mutates the original manager. */
1359
+ declare function setProvisional(_manager: ProvisionalManager, state: ProvisionalState): ProvisionalManager;
1360
+ /** Return a new manager with current set to null.
1361
+ * Pure — never mutates the original manager. */
1362
+ declare function clearProvisional(_manager: ProvisionalManager): ProvisionalManager;
1363
+ /**
1364
+ * Resolve which version of a clip to render.
1365
+ *
1366
+ * Priority:
1367
+ * 1. If manager.current has a clip with this id → return provisional (ghost)
1368
+ * 2. Otherwise → search committed state
1369
+ * 3. If absent from both (clip deleted mid-drag) → return undefined
1370
+ *
1371
+ * Returns undefined if the clip has been deleted from committed state
1372
+ * and is not in provisional. Components must handle this:
1373
+ * const clip = useClip(id)
1374
+ * if (!clip) return null ← required, not optional
1375
+ *
1376
+ * Call site in useClip selector:
1377
+ * () => resolveClip(id, engine.getSnapshot(), engine.getProvisionalManager())
1378
+ */
1379
+ declare function resolveClip(clipId: ClipId, state: TimelineState, manager: ProvisionalManager): Clip | undefined;
1380
+
1381
+ export { createRegistry as $, type Asset as A, addFrames as B, type Clip as C, type DispatchResult as D, buildSnapIndex as E, type FrameRate as F, canRedo as G, type HistoryState as H, type ITool as I, canUndo as J, checkInvariants as K, clampFrame as L, type Modifiers as M, NoOpTool as N, type OperationPrimitive as O, type ProvisionalManager as P, clearProvisional as Q, type RationalTime as R, type SequenceSettings as S, type TimelineState as T, clipContainsFrame as U, type ViolationType as V, clipsOverlap as W, createAsset as X, createClip as Y, createHistory as Z, createProvisionalManager as _, type Track as a, createTimeline as a0, createTimelineState as a1, createTrack as a2, dispatch as a3, frame as a4, frameDuration as a5, frameRate as a6, framesToMinutesSeconds as a7, framesToSeconds as a8, framesToTimecode as a9, getActiveTool as aa, getClipDuration as ab, getClipMediaDuration as ac, getCurrentState as ad, isDropFrame as ae, isValidFrame as af, nearest as ag, pushHistory as ah, redo as ai, registerTool as aj, resolveClip as ak, secondsToFrames as al, setProvisional as am, sortTrackClips as an, subtractFrames as ao, toFrame as ap, toTimecode as aq, toToolId as ar, toggleSnap as as, undo as at, type TimelineFrame as b, type AssetId as c, type AssetRegistry as d, type AssetStatus as e, type ClipId as f, FrameRates as g, type InvariantViolation as h, type ProvisionalState as i, type RejectionReason as j, type SnapIndex as k, type SnapPoint as l, type SnapPointType as m, type TimeRange as n, type Timecode as o, type Timeline as p, TimelineEngine as q, type TimelineKeyEvent as r, type TimelinePointerEvent as s, type ToolContext as t, type ToolId as u, type ToolRegistry as v, type TrackId as w, type TrackType as x, type Transaction as y, activateTool as z };