@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,1864 @@
1
+ // src/types/clip.ts
2
+ function createClip(params) {
3
+ const clip = {
4
+ id: params.id,
5
+ assetId: params.assetId,
6
+ trackId: params.trackId,
7
+ timelineStart: params.timelineStart,
8
+ timelineEnd: params.timelineEnd,
9
+ mediaIn: params.mediaIn,
10
+ mediaOut: params.mediaOut
11
+ };
12
+ if (params.label !== void 0) {
13
+ clip.label = params.label;
14
+ }
15
+ if (params.metadata !== void 0) {
16
+ clip.metadata = params.metadata;
17
+ }
18
+ return clip;
19
+ }
20
+ function getClipDuration(clip) {
21
+ return clip.timelineEnd - clip.timelineStart;
22
+ }
23
+ function getClipMediaDuration(clip) {
24
+ return clip.mediaOut - clip.mediaIn;
25
+ }
26
+ function clipContainsFrame(clip, frame2) {
27
+ return frame2 >= clip.timelineStart && frame2 < clip.timelineEnd;
28
+ }
29
+ function clipsOverlap(clip1, clip2) {
30
+ return clip1.timelineStart < clip2.timelineEnd && clip2.timelineStart < clip1.timelineEnd;
31
+ }
32
+
33
+ // src/types/validation.ts
34
+ function validResult() {
35
+ return {
36
+ valid: true,
37
+ errors: []
38
+ };
39
+ }
40
+ function invalidResult(code, message, context) {
41
+ const error = { code, message };
42
+ if (context !== void 0) {
43
+ error.context = context;
44
+ }
45
+ return {
46
+ valid: false,
47
+ errors: [error]
48
+ };
49
+ }
50
+ function invalidResults(errors) {
51
+ return {
52
+ valid: false,
53
+ errors
54
+ };
55
+ }
56
+ function combineResults(...results) {
57
+ const allErrors = [];
58
+ for (const result of results) {
59
+ if (!result.valid) {
60
+ allErrors.push(...result.errors);
61
+ }
62
+ }
63
+ if (allErrors.length > 0) {
64
+ return invalidResults(allErrors);
65
+ }
66
+ return validResult();
67
+ }
68
+
69
+ // src/systems/asset-registry.ts
70
+ function registerAsset(state, asset) {
71
+ const newAssets = new Map(state.assets);
72
+ newAssets.set(asset.id, asset);
73
+ return {
74
+ ...state,
75
+ assets: newAssets
76
+ };
77
+ }
78
+ function getAsset(state, assetId) {
79
+ return state.assets.get(assetId);
80
+ }
81
+ function hasAsset(state, assetId) {
82
+ return state.assets.has(assetId);
83
+ }
84
+ function getAllAssets(state) {
85
+ return Array.from(state.assets.values());
86
+ }
87
+ function unregisterAsset(state, assetId) {
88
+ const newAssets = new Map(state.assets);
89
+ newAssets.delete(assetId);
90
+ return {
91
+ ...state,
92
+ assets: newAssets
93
+ };
94
+ }
95
+
96
+ // src/systems/validation.ts
97
+ function validateClip(state, clip) {
98
+ const errors = [];
99
+ const asset = getAsset(state, clip.assetId);
100
+ if (!asset) {
101
+ errors.push(invalidResult(
102
+ "ASSET_NOT_FOUND",
103
+ `Asset '${clip.assetId}' not found in registry`,
104
+ { clipId: clip.id, assetId: clip.assetId }
105
+ ));
106
+ return combineResults(...errors);
107
+ }
108
+ if (clip.timelineEnd <= clip.timelineStart) {
109
+ errors.push(invalidResult(
110
+ "INVALID_TIMELINE_BOUNDS",
111
+ `Clip timeline end (${clip.timelineEnd}) must be greater than start (${clip.timelineStart})`,
112
+ { clipId: clip.id, timelineStart: clip.timelineStart, timelineEnd: clip.timelineEnd }
113
+ ));
114
+ }
115
+ if (clip.mediaIn < 0) {
116
+ errors.push(invalidResult(
117
+ "INVALID_MEDIA_IN",
118
+ `Clip media in (${clip.mediaIn}) must be >= 0`,
119
+ { clipId: clip.id, mediaIn: clip.mediaIn }
120
+ ));
121
+ }
122
+ if (clip.mediaOut <= clip.mediaIn) {
123
+ errors.push(invalidResult(
124
+ "INVALID_MEDIA_BOUNDS",
125
+ `Clip media out (${clip.mediaOut}) must be greater than media in (${clip.mediaIn})`,
126
+ { clipId: clip.id, mediaIn: clip.mediaIn, mediaOut: clip.mediaOut }
127
+ ));
128
+ }
129
+ if (clip.mediaOut > asset.duration) {
130
+ errors.push(invalidResult(
131
+ "MEDIA_EXCEEDS_ASSET",
132
+ `Clip media out (${clip.mediaOut}) exceeds asset duration (${asset.duration})`,
133
+ { clipId: clip.id, mediaOut: clip.mediaOut, assetDuration: asset.duration }
134
+ ));
135
+ }
136
+ const timelineDuration = getClipDuration(clip);
137
+ const mediaDuration = getClipMediaDuration(clip);
138
+ if (timelineDuration !== mediaDuration) {
139
+ errors.push(invalidResult(
140
+ "DURATION_MISMATCH",
141
+ `Clip timeline duration (${timelineDuration}) must match media duration (${mediaDuration}) in Phase 1`,
142
+ { clipId: clip.id, timelineDuration, mediaDuration }
143
+ ));
144
+ }
145
+ return combineResults(...errors);
146
+ }
147
+ function validateTrack(state, track) {
148
+ const errors = [];
149
+ for (const clip of track.clips) {
150
+ const clipResult = validateClip(state, clip);
151
+ if (!clipResult.valid) {
152
+ errors.push(clipResult);
153
+ }
154
+ }
155
+ for (let i = 0; i < track.clips.length; i++) {
156
+ for (let j = i + 1; j < track.clips.length; j++) {
157
+ const clip1 = track.clips[i];
158
+ const clip2 = track.clips[j];
159
+ if (!clip1 || !clip2) {
160
+ continue;
161
+ }
162
+ if (clipsOverlap(clip1, clip2)) {
163
+ errors.push(invalidResult(
164
+ "CLIPS_OVERLAP",
165
+ `Clips '${clip1.id}' and '${clip2.id}' overlap on track '${track.id}'`,
166
+ {
167
+ trackId: track.id,
168
+ clip1Id: clip1.id,
169
+ clip2Id: clip2.id,
170
+ clip1Start: clip1.timelineStart,
171
+ clip1End: clip1.timelineEnd,
172
+ clip2Start: clip2.timelineStart,
173
+ clip2End: clip2.timelineEnd
174
+ }
175
+ ));
176
+ }
177
+ }
178
+ }
179
+ return combineResults(...errors);
180
+ }
181
+ function validateTimeline(state) {
182
+ const errors = [];
183
+ if (state.timeline.fps <= 0) {
184
+ errors.push(invalidResult(
185
+ "INVALID_FPS",
186
+ `Timeline FPS must be positive, got ${state.timeline.fps}`,
187
+ { fps: state.timeline.fps }
188
+ ));
189
+ }
190
+ if (state.timeline.duration <= 0) {
191
+ errors.push(invalidResult(
192
+ "INVALID_DURATION",
193
+ `Timeline duration must be positive, got ${state.timeline.duration}`,
194
+ { duration: state.timeline.duration }
195
+ ));
196
+ }
197
+ for (const track of state.timeline.tracks) {
198
+ const trackResult = validateTrack(state, track);
199
+ if (!trackResult.valid) {
200
+ errors.push(trackResult);
201
+ }
202
+ }
203
+ return combineResults(...errors);
204
+ }
205
+ function validateNoOverlap(track, clip) {
206
+ for (const existingClip of track.clips) {
207
+ if (existingClip.id === clip.id) {
208
+ continue;
209
+ }
210
+ if (clipsOverlap(existingClip, clip)) {
211
+ return invalidResult(
212
+ "CLIPS_OVERLAP",
213
+ `Clip '${clip.id}' would overlap with existing clip '${existingClip.id}' on track '${track.id}'`,
214
+ {
215
+ trackId: track.id,
216
+ newClipId: clip.id,
217
+ existingClipId: existingClip.id,
218
+ newClipStart: clip.timelineStart,
219
+ newClipEnd: clip.timelineEnd,
220
+ existingClipStart: existingClip.timelineStart,
221
+ existingClipEnd: existingClip.timelineEnd
222
+ }
223
+ );
224
+ }
225
+ }
226
+ return validResult();
227
+ }
228
+ function validateTrackTypeMatch(state, clip, targetTrack) {
229
+ const asset = getAsset(state, clip.assetId);
230
+ if (!asset) {
231
+ return invalidResult(
232
+ "ASSET_NOT_FOUND",
233
+ `Asset '${clip.assetId}' not found in registry`,
234
+ { clipId: clip.id, assetId: clip.assetId }
235
+ );
236
+ }
237
+ if (asset.type === "video" && targetTrack.type !== "video") {
238
+ return invalidResult(
239
+ "TRACK_TYPE_MISMATCH",
240
+ `Cannot place video clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
241
+ {
242
+ clipId: clip.id,
243
+ assetType: asset.type,
244
+ trackType: targetTrack.type,
245
+ trackId: targetTrack.id
246
+ }
247
+ );
248
+ }
249
+ if (asset.type === "audio" && targetTrack.type !== "audio") {
250
+ return invalidResult(
251
+ "TRACK_TYPE_MISMATCH",
252
+ `Cannot place audio clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
253
+ {
254
+ clipId: clip.id,
255
+ assetType: asset.type,
256
+ trackType: targetTrack.type,
257
+ trackId: targetTrack.id
258
+ }
259
+ );
260
+ }
261
+ if (asset.type === "image" && targetTrack.type !== "video") {
262
+ return invalidResult(
263
+ "TRACK_TYPE_MISMATCH",
264
+ `Cannot place image clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
265
+ {
266
+ clipId: clip.id,
267
+ assetType: asset.type,
268
+ trackType: targetTrack.type,
269
+ trackId: targetTrack.id
270
+ }
271
+ );
272
+ }
273
+ return validResult();
274
+ }
275
+
276
+ // src/systems/queries.ts
277
+ function findClipById(state, clipId) {
278
+ for (const track of state.timeline.tracks) {
279
+ const clip = track.clips.find((c) => c.id === clipId);
280
+ if (clip) {
281
+ return clip;
282
+ }
283
+ }
284
+ return void 0;
285
+ }
286
+ function findTrackById(state, trackId) {
287
+ return state.timeline.tracks.find((t) => t.id === trackId);
288
+ }
289
+ function getClipsOnTrack(state, trackId) {
290
+ const track = findTrackById(state, trackId);
291
+ return track ? track.clips : [];
292
+ }
293
+ function getClipsAtFrame(state, frame2) {
294
+ const clips = [];
295
+ for (const track of state.timeline.tracks) {
296
+ for (const clip of track.clips) {
297
+ if (clipContainsFrame(clip, frame2)) {
298
+ clips.push(clip);
299
+ }
300
+ }
301
+ }
302
+ return clips;
303
+ }
304
+ function getClipsInRange(state, start, end) {
305
+ const clips = [];
306
+ for (const track of state.timeline.tracks) {
307
+ for (const clip of track.clips) {
308
+ if (clip.timelineStart < end && clip.timelineEnd > start) {
309
+ clips.push(clip);
310
+ }
311
+ }
312
+ }
313
+ return clips;
314
+ }
315
+ function getAllClips(state) {
316
+ const clips = [];
317
+ for (const track of state.timeline.tracks) {
318
+ clips.push(...track.clips);
319
+ }
320
+ return clips;
321
+ }
322
+ function getAllTracks(state) {
323
+ return state.timeline.tracks;
324
+ }
325
+ function findTrackIndex(state, trackId) {
326
+ return state.timeline.tracks.findIndex((t) => t.id === trackId);
327
+ }
328
+
329
+ // src/types/track.ts
330
+ function createTrack(params) {
331
+ const track = {
332
+ id: params.id,
333
+ name: params.name,
334
+ type: params.type,
335
+ clips: params.clips ?? [],
336
+ locked: params.locked ?? false,
337
+ muted: params.muted ?? false,
338
+ solo: params.solo ?? false,
339
+ height: params.height ?? 56
340
+ };
341
+ if (params.metadata !== void 0) {
342
+ track.metadata = params.metadata;
343
+ }
344
+ return track;
345
+ }
346
+ function sortTrackClips(track) {
347
+ return {
348
+ ...track,
349
+ clips: [...track.clips].sort((a, b) => a.timelineStart - b.timelineStart)
350
+ };
351
+ }
352
+
353
+ // src/operations/clip-operations.ts
354
+ function addClip(state, trackId, clip) {
355
+ const trackIndex = state.timeline.tracks.findIndex((t) => t.id === trackId);
356
+ if (trackIndex === -1) {
357
+ return state;
358
+ }
359
+ const track = state.timeline.tracks[trackIndex];
360
+ if (!track) {
361
+ return state;
362
+ }
363
+ const newTrack = sortTrackClips({
364
+ ...track,
365
+ clips: [...track.clips, clip]
366
+ });
367
+ const newTracks = [...state.timeline.tracks];
368
+ newTracks[trackIndex] = newTrack;
369
+ return {
370
+ ...state,
371
+ timeline: {
372
+ ...state.timeline,
373
+ tracks: newTracks
374
+ }
375
+ };
376
+ }
377
+ function removeClip(state, clipId) {
378
+ const newTracks = state.timeline.tracks.map((track) => ({
379
+ ...track,
380
+ clips: track.clips.filter((c) => c.id !== clipId)
381
+ }));
382
+ return {
383
+ ...state,
384
+ timeline: {
385
+ ...state.timeline,
386
+ tracks: newTracks
387
+ }
388
+ };
389
+ }
390
+ function moveClip(state, clipId, newStart) {
391
+ const clip = findClipById(state, clipId);
392
+ if (!clip) {
393
+ return state;
394
+ }
395
+ const duration = clip.timelineEnd - clip.timelineStart;
396
+ const newEnd = newStart + duration;
397
+ return updateClip(state, clipId, {
398
+ timelineStart: newStart,
399
+ timelineEnd: newEnd
400
+ });
401
+ }
402
+ function resizeClip(state, clipId, newStart, newEnd) {
403
+ const clip = findClipById(state, clipId);
404
+ if (!clip) {
405
+ return state;
406
+ }
407
+ const startDelta = newStart - clip.timelineStart;
408
+ const endDelta = newEnd - clip.timelineEnd;
409
+ let newMediaIn = clip.mediaIn;
410
+ let newMediaOut = clip.mediaOut;
411
+ if (startDelta !== 0) {
412
+ newMediaIn = clip.mediaIn + startDelta;
413
+ }
414
+ if (endDelta !== 0) {
415
+ newMediaOut = clip.mediaOut + endDelta;
416
+ }
417
+ return updateClip(state, clipId, {
418
+ timelineStart: newStart,
419
+ timelineEnd: newEnd,
420
+ mediaIn: newMediaIn,
421
+ mediaOut: newMediaOut
422
+ });
423
+ }
424
+ function trimClip(state, clipId, newMediaIn, newMediaOut) {
425
+ return updateClip(state, clipId, {
426
+ mediaIn: newMediaIn,
427
+ mediaOut: newMediaOut
428
+ });
429
+ }
430
+ function updateClip(state, clipId, updates) {
431
+ const newTracks = state.timeline.tracks.map((track) => {
432
+ const clipIndex = track.clips.findIndex((c) => c.id === clipId);
433
+ if (clipIndex === -1) {
434
+ return track;
435
+ }
436
+ const newClips = [...track.clips];
437
+ const existingClip = newClips[clipIndex];
438
+ if (!existingClip) {
439
+ return track;
440
+ }
441
+ newClips[clipIndex] = {
442
+ ...existingClip,
443
+ ...updates
444
+ };
445
+ return sortTrackClips({
446
+ ...track,
447
+ clips: newClips
448
+ });
449
+ });
450
+ return {
451
+ ...state,
452
+ timeline: {
453
+ ...state.timeline,
454
+ tracks: newTracks
455
+ }
456
+ };
457
+ }
458
+ function moveClipToTrack(state, clipId, targetTrackId) {
459
+ const clip = findClipById(state, clipId);
460
+ if (!clip) {
461
+ return state;
462
+ }
463
+ const targetTrack = findTrackById(state, targetTrackId);
464
+ if (!targetTrack) {
465
+ return state;
466
+ }
467
+ const validationResult = validateTrackTypeMatch(state, clip, targetTrack);
468
+ if (!validationResult.valid) {
469
+ return state;
470
+ }
471
+ if (clip.trackId === targetTrackId) {
472
+ return state;
473
+ }
474
+ let newState = removeClip(state, clipId);
475
+ const updatedClip = { ...clip, trackId: targetTrackId };
476
+ newState = addClip(newState, targetTrackId, updatedClip);
477
+ return newState;
478
+ }
479
+
480
+ // src/operations/track-operations.ts
481
+ function addTrack(state, track) {
482
+ return {
483
+ ...state,
484
+ timeline: {
485
+ ...state.timeline,
486
+ tracks: [...state.timeline.tracks, track]
487
+ }
488
+ };
489
+ }
490
+ function removeTrack(state, trackId) {
491
+ return {
492
+ ...state,
493
+ timeline: {
494
+ ...state.timeline,
495
+ tracks: state.timeline.tracks.filter((t) => t.id !== trackId)
496
+ }
497
+ };
498
+ }
499
+ function moveTrack(state, trackId, newIndex) {
500
+ const currentIndex = findTrackIndex(state, trackId);
501
+ if (currentIndex === -1) {
502
+ return state;
503
+ }
504
+ const newTracks = [...state.timeline.tracks];
505
+ const [track] = newTracks.splice(currentIndex, 1);
506
+ if (!track) {
507
+ return state;
508
+ }
509
+ newTracks.splice(newIndex, 0, track);
510
+ return {
511
+ ...state,
512
+ timeline: {
513
+ ...state.timeline,
514
+ tracks: newTracks
515
+ }
516
+ };
517
+ }
518
+ function updateTrack(state, trackId, updates) {
519
+ const trackIndex = findTrackIndex(state, trackId);
520
+ if (trackIndex === -1) {
521
+ return state;
522
+ }
523
+ const newTracks = [...state.timeline.tracks];
524
+ const existingTrack = newTracks[trackIndex];
525
+ if (!existingTrack) {
526
+ return state;
527
+ }
528
+ newTracks[trackIndex] = {
529
+ ...existingTrack,
530
+ ...updates
531
+ };
532
+ return {
533
+ ...state,
534
+ timeline: {
535
+ ...state.timeline,
536
+ tracks: newTracks
537
+ }
538
+ };
539
+ }
540
+ function toggleTrackMute(state, trackId) {
541
+ const trackIndex = findTrackIndex(state, trackId);
542
+ if (trackIndex === -1) {
543
+ return state;
544
+ }
545
+ const track = state.timeline.tracks[trackIndex];
546
+ if (!track) {
547
+ return state;
548
+ }
549
+ return updateTrack(state, trackId, { muted: !track.muted });
550
+ }
551
+ function toggleTrackLock(state, trackId) {
552
+ const trackIndex = findTrackIndex(state, trackId);
553
+ if (trackIndex === -1) {
554
+ return state;
555
+ }
556
+ const track = state.timeline.tracks[trackIndex];
557
+ if (!track) {
558
+ return state;
559
+ }
560
+ return updateTrack(state, trackId, { locked: !track.locked });
561
+ }
562
+ function toggleTrackSolo(state, trackId) {
563
+ const trackIndex = findTrackIndex(state, trackId);
564
+ if (trackIndex === -1) {
565
+ return state;
566
+ }
567
+ const track = state.timeline.tracks[trackIndex];
568
+ if (!track) {
569
+ return state;
570
+ }
571
+ return updateTrack(state, trackId, { solo: !track.solo });
572
+ }
573
+ function setTrackHeight(state, trackId, height) {
574
+ return updateTrack(state, trackId, { height: Math.max(40, Math.min(200, height)) });
575
+ }
576
+
577
+ // src/operations/timeline-operations.ts
578
+ function setTimelineDuration(state, duration) {
579
+ return {
580
+ ...state,
581
+ timeline: {
582
+ ...state.timeline,
583
+ duration
584
+ }
585
+ };
586
+ }
587
+ function setTimelineName(state, name) {
588
+ return {
589
+ ...state,
590
+ timeline: {
591
+ ...state.timeline,
592
+ name
593
+ }
594
+ };
595
+ }
596
+ function updateTimelineMetadata(state, metadata) {
597
+ return {
598
+ ...state,
599
+ timeline: {
600
+ ...state.timeline,
601
+ metadata: {
602
+ ...state.timeline.metadata,
603
+ ...metadata
604
+ }
605
+ }
606
+ };
607
+ }
608
+
609
+ // src/engine/transactions.ts
610
+ function beginTransaction(state) {
611
+ return {
612
+ initialState: state,
613
+ currentState: state,
614
+ operations: [],
615
+ finalized: false
616
+ };
617
+ }
618
+ function applyOperation(tx, operation) {
619
+ if (tx.finalized) {
620
+ throw new Error("Cannot apply operation to finalized transaction");
621
+ }
622
+ const newState = operation(tx.currentState);
623
+ return {
624
+ ...tx,
625
+ currentState: newState,
626
+ operations: [...tx.operations, operation]
627
+ };
628
+ }
629
+ function commitTransaction(tx) {
630
+ if (tx.finalized) {
631
+ throw new Error("Transaction already finalized");
632
+ }
633
+ tx.finalized = true;
634
+ return tx.currentState;
635
+ }
636
+ function rollbackTransaction(tx) {
637
+ if (tx.finalized) {
638
+ throw new Error("Transaction already finalized");
639
+ }
640
+ tx.finalized = true;
641
+ return tx.initialState;
642
+ }
643
+ function getOperationCount(tx) {
644
+ return tx.operations.length;
645
+ }
646
+
647
+ // src/operations/ripple.ts
648
+ function rippleDelete(state, clipId) {
649
+ const clip = findClipById(state, clipId);
650
+ if (!clip) {
651
+ throw new Error(`Clip not found: ${clipId}`);
652
+ }
653
+ const track = findTrackById(state, clip.trackId);
654
+ if (!track) {
655
+ throw new Error(`Track not found: ${clip.trackId}`);
656
+ }
657
+ const clipDuration = getClipDuration(clip);
658
+ const deleteEnd = clip.timelineEnd;
659
+ const clipsToShift = track.clips.filter(
660
+ (c) => c.id !== clipId && c.timelineStart >= deleteEnd
661
+ );
662
+ let tx = beginTransaction(state);
663
+ tx = applyOperation(tx, (s) => removeClip(s, clipId));
664
+ for (const clipToShift of clipsToShift) {
665
+ const newStart = clipToShift.timelineStart - clipDuration;
666
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
667
+ }
668
+ return commitTransaction(tx);
669
+ }
670
+ function rippleTrim(state, clipId, newEnd) {
671
+ const clip = findClipById(state, clipId);
672
+ if (!clip) {
673
+ throw new Error(`Clip not found: ${clipId}`);
674
+ }
675
+ const track = findTrackById(state, clip.trackId);
676
+ if (!track) {
677
+ throw new Error(`Track not found: ${clip.trackId}`);
678
+ }
679
+ if (newEnd <= clip.timelineStart) {
680
+ throw new Error("New end must be after clip start");
681
+ }
682
+ const delta = newEnd - clip.timelineEnd;
683
+ const clipsToShift = track.clips.filter(
684
+ (c) => c.id !== clipId && c.timelineStart >= clip.timelineEnd
685
+ );
686
+ let tx = beginTransaction(state);
687
+ tx = applyOperation(tx, (s) => resizeClip(s, clipId, clip.timelineStart, newEnd));
688
+ for (const clipToShift of clipsToShift) {
689
+ const newStart = clipToShift.timelineStart + delta;
690
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
691
+ }
692
+ return commitTransaction(tx);
693
+ }
694
+ function insertEdit(state, trackId, clip, atFrame) {
695
+ const track = findTrackById(state, trackId);
696
+ if (!track) {
697
+ throw new Error(`Track not found: ${trackId}`);
698
+ }
699
+ const clipDuration = getClipDuration(clip);
700
+ const clipsToShift = track.clips.filter((c) => c.timelineStart >= atFrame);
701
+ const adjustedClip = {
702
+ ...clip,
703
+ timelineStart: atFrame,
704
+ timelineEnd: atFrame + clipDuration
705
+ };
706
+ let tx = beginTransaction(state);
707
+ for (const clipToShift of clipsToShift) {
708
+ const newStart = clipToShift.timelineStart + clipDuration;
709
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
710
+ }
711
+ tx = applyOperation(tx, (s) => addClip(s, trackId, adjustedClip));
712
+ return commitTransaction(tx);
713
+ }
714
+ function rippleMove(state, clipId, newStart) {
715
+ const clip = findClipById(state, clipId);
716
+ if (!clip) {
717
+ throw new Error(`Clip not found: ${clipId}`);
718
+ }
719
+ const track = findTrackById(state, clip.trackId);
720
+ if (!track) {
721
+ throw new Error(`Track not found: ${clip.trackId}`);
722
+ }
723
+ const clipDuration = getClipDuration(clip);
724
+ const newEnd = newStart + clipDuration;
725
+ if (newStart < 0) {
726
+ throw new Error("Cannot move clip before timeline start (frame 0)");
727
+ }
728
+ if (newEnd > state.timeline.duration) {
729
+ throw new Error(`Cannot move clip beyond timeline duration (${state.timeline.duration} frames)`);
730
+ }
731
+ const originalStart = clip.timelineStart;
732
+ const originalEnd = clip.timelineEnd;
733
+ if (newStart === originalStart) {
734
+ return state;
735
+ }
736
+ let tx = beginTransaction(state);
737
+ const movingRight = newStart > originalStart;
738
+ if (movingRight) {
739
+ const clipsToShiftLeft = track.clips.filter(
740
+ (c) => c.id !== clipId && c.timelineStart >= originalEnd && c.timelineStart < newStart
741
+ );
742
+ for (const clipToShift of clipsToShiftLeft) {
743
+ const shiftedStart = clipToShift.timelineStart - clipDuration;
744
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, shiftedStart));
745
+ }
746
+ const adjustedNewStart = newStart - clipDuration;
747
+ tx = applyOperation(tx, (s) => moveClip(s, clipId, adjustedNewStart));
748
+ } else {
749
+ const clipsToShiftRight = track.clips.filter(
750
+ (c) => c.id !== clipId && c.timelineStart >= newStart && c.timelineStart < originalStart
751
+ );
752
+ for (const clipToShift of clipsToShiftRight) {
753
+ const shiftedStart = clipToShift.timelineStart + clipDuration;
754
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, shiftedStart));
755
+ }
756
+ tx = applyOperation(tx, (s) => moveClip(s, clipId, newStart));
757
+ }
758
+ return commitTransaction(tx);
759
+ }
760
+ function insertMove(state, clipId, newStart) {
761
+ const clip = findClipById(state, clipId);
762
+ if (!clip) {
763
+ throw new Error(`Clip not found: ${clipId}`);
764
+ }
765
+ const track = findTrackById(state, clip.trackId);
766
+ if (!track) {
767
+ throw new Error(`Track not found: ${clip.trackId}`);
768
+ }
769
+ const clipDuration = getClipDuration(clip);
770
+ const newEnd = newStart + clipDuration;
771
+ if (newStart < 0) {
772
+ throw new Error("Cannot move clip before timeline start (frame 0)");
773
+ }
774
+ if (newEnd > state.timeline.duration) {
775
+ throw new Error(`Cannot move clip beyond timeline duration (${state.timeline.duration} frames)`);
776
+ }
777
+ if (newStart === clip.timelineStart) {
778
+ return state;
779
+ }
780
+ let tx = beginTransaction(state);
781
+ const clipsToShift = track.clips.filter(
782
+ (c) => c.id !== clipId && c.timelineStart >= newStart
783
+ );
784
+ for (const clipToShift of clipsToShift) {
785
+ const shiftedStart = clipToShift.timelineStart + clipDuration;
786
+ tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, shiftedStart));
787
+ }
788
+ tx = applyOperation(tx, (s) => moveClip(s, clipId, newStart));
789
+ return commitTransaction(tx);
790
+ }
791
+
792
+ // src/operations/marker-operations.ts
793
+ function addTimelineMarker(state, marker) {
794
+ return {
795
+ ...state,
796
+ markers: {
797
+ ...state.markers,
798
+ timeline: [...state.markers.timeline, marker]
799
+ }
800
+ };
801
+ }
802
+ function addClipMarker(state, marker) {
803
+ const clip = findClipById(state, marker.clipId);
804
+ if (!clip) {
805
+ throw new Error(`Clip not found: ${marker.clipId}`);
806
+ }
807
+ return {
808
+ ...state,
809
+ markers: {
810
+ ...state.markers,
811
+ clips: [...state.markers.clips, marker]
812
+ }
813
+ };
814
+ }
815
+ function addRegionMarker(state, marker) {
816
+ if (marker.startFrame >= marker.endFrame) {
817
+ throw new Error("Region marker start must be before end");
818
+ }
819
+ return {
820
+ ...state,
821
+ markers: {
822
+ ...state.markers,
823
+ regions: [...state.markers.regions, marker]
824
+ }
825
+ };
826
+ }
827
+ function removeMarker(state, markerId) {
828
+ return {
829
+ ...state,
830
+ markers: {
831
+ timeline: state.markers.timeline.filter((m) => m.id !== markerId),
832
+ clips: state.markers.clips.filter((m) => m.id !== markerId),
833
+ regions: state.markers.regions.filter((m) => m.id !== markerId)
834
+ }
835
+ };
836
+ }
837
+ function removeClipMarkers(state, clipId) {
838
+ return {
839
+ ...state,
840
+ markers: {
841
+ ...state.markers,
842
+ clips: state.markers.clips.filter((m) => m.clipId !== clipId)
843
+ }
844
+ };
845
+ }
846
+ function setWorkArea(state, workArea) {
847
+ if (workArea.startFrame >= workArea.endFrame) {
848
+ throw new Error("Work area start must be before end");
849
+ }
850
+ return {
851
+ ...state,
852
+ workArea
853
+ };
854
+ }
855
+ function clearWorkArea(state) {
856
+ const { workArea: _, ...stateWithoutWorkArea } = state;
857
+ return stateWithoutWorkArea;
858
+ }
859
+ function updateTimelineMarker(state, markerId, updates) {
860
+ return {
861
+ ...state,
862
+ markers: {
863
+ ...state.markers,
864
+ timeline: state.markers.timeline.map(
865
+ (m) => m.id === markerId ? { ...m, ...updates } : m
866
+ )
867
+ }
868
+ };
869
+ }
870
+ function updateRegionMarker(state, markerId, updates) {
871
+ return {
872
+ ...state,
873
+ markers: {
874
+ ...state.markers,
875
+ regions: state.markers.regions.map(
876
+ (m) => m.id === markerId ? { ...m, ...updates } : m
877
+ )
878
+ }
879
+ };
880
+ }
881
+
882
+ // src/engine/history.ts
883
+ function createHistory(initialState, limit = 50) {
884
+ return {
885
+ past: [],
886
+ present: initialState,
887
+ future: [],
888
+ limit
889
+ };
890
+ }
891
+ function pushHistory(history, newState) {
892
+ const newPast = [...history.past, history.present];
893
+ if (newPast.length > history.limit) {
894
+ newPast.shift();
895
+ }
896
+ return {
897
+ ...history,
898
+ past: newPast,
899
+ present: newState,
900
+ future: []
901
+ // Clear future on new action
902
+ };
903
+ }
904
+ function undo(history) {
905
+ if (history.past.length === 0) {
906
+ return history;
907
+ }
908
+ const newPast = [...history.past];
909
+ const previous = newPast.pop();
910
+ return {
911
+ ...history,
912
+ past: newPast,
913
+ present: previous,
914
+ future: [history.present, ...history.future]
915
+ };
916
+ }
917
+ function redo(history) {
918
+ if (history.future.length === 0) {
919
+ return history;
920
+ }
921
+ const newFuture = [...history.future];
922
+ const next = newFuture.shift();
923
+ return {
924
+ ...history,
925
+ past: [...history.past, history.present],
926
+ present: next,
927
+ future: newFuture
928
+ };
929
+ }
930
+ function canUndo(history) {
931
+ return history.past.length > 0;
932
+ }
933
+ function canRedo(history) {
934
+ return history.future.length > 0;
935
+ }
936
+ function getCurrentState(history) {
937
+ return history.present;
938
+ }
939
+
940
+ // src/engine/dispatcher.ts
941
+ function dispatch(history, operation) {
942
+ const currentState = getCurrentState(history);
943
+ let newState;
944
+ try {
945
+ newState = operation(currentState);
946
+ } catch (error) {
947
+ const errorMessage = error instanceof Error ? error.message : String(error);
948
+ return {
949
+ success: false,
950
+ history,
951
+ // Return unchanged history
952
+ errors: [{
953
+ code: "OPERATION_ERROR",
954
+ message: errorMessage
955
+ }]
956
+ };
957
+ }
958
+ const validationResult = validateTimeline(newState);
959
+ if (!validationResult.valid) {
960
+ return {
961
+ success: false,
962
+ history,
963
+ // Return unchanged history
964
+ errors: validationResult.errors
965
+ };
966
+ }
967
+ const newHistory = pushHistory(history, newState);
968
+ return {
969
+ success: true,
970
+ history: newHistory
971
+ };
972
+ }
973
+
974
+ // src/engine/timeline-engine.ts
975
+ var TimelineEngine = class {
976
+ history;
977
+ listeners = /* @__PURE__ */ new Set();
978
+ /**
979
+ * Create a new timeline engine
980
+ *
981
+ * @param initialState - Initial timeline state
982
+ * @param historyLimit - Maximum number of undo steps (default: 50)
983
+ */
984
+ constructor(initialState, historyLimit = 50) {
985
+ this.history = createHistory(initialState, historyLimit);
986
+ }
987
+ // ===== SUBSCRIPTION =====
988
+ /**
989
+ * Subscribe to state changes
990
+ *
991
+ * The listener will be called whenever the timeline state changes,
992
+ * with the new state passed as an argument.
993
+ * This is used by framework adapters (e.g., React) to trigger re-renders.
994
+ *
995
+ * @param listener - Function to call on state changes, receives new state
996
+ * @returns Unsubscribe function
997
+ *
998
+ * @example
999
+ * ```typescript
1000
+ * const unsubscribe = engine.subscribe((state) => {
1001
+ * console.log('State changed:', state);
1002
+ * });
1003
+ *
1004
+ * // Later...
1005
+ * unsubscribe();
1006
+ * ```
1007
+ */
1008
+ subscribe(listener) {
1009
+ this.listeners.add(listener);
1010
+ return () => {
1011
+ this.listeners.delete(listener);
1012
+ };
1013
+ }
1014
+ /**
1015
+ * Notify all subscribers of a state change
1016
+ *
1017
+ * This is called internally after any operation that modifies state.
1018
+ * Framework adapters use this to trigger re-renders.
1019
+ */
1020
+ notify() {
1021
+ const state = this.getState();
1022
+ this.listeners.forEach((listener) => listener(state));
1023
+ }
1024
+ // ===== STATE ACCESS =====
1025
+ /**
1026
+ * Get the current timeline state
1027
+ *
1028
+ * @returns Current timeline state
1029
+ */
1030
+ getState() {
1031
+ return getCurrentState(this.history);
1032
+ }
1033
+ // ===== ASSET OPERATIONS =====
1034
+ /**
1035
+ * Register an asset
1036
+ *
1037
+ * @param asset - Asset to register
1038
+ * @returns Dispatch result
1039
+ */
1040
+ registerAsset(asset) {
1041
+ const result = dispatch(
1042
+ this.history,
1043
+ (state) => registerAsset(state, asset)
1044
+ );
1045
+ if (result.success) {
1046
+ this.history = result.history;
1047
+ this.notify();
1048
+ }
1049
+ return result;
1050
+ }
1051
+ /**
1052
+ * Get an asset by ID
1053
+ *
1054
+ * @param assetId - Asset ID
1055
+ * @returns The asset, or undefined if not found
1056
+ */
1057
+ getAsset(assetId) {
1058
+ return getAsset(this.getState(), assetId);
1059
+ }
1060
+ // ===== CLIP OPERATIONS =====
1061
+ /**
1062
+ * Add a clip to a track
1063
+ *
1064
+ * @param trackId - ID of the track to add to
1065
+ * @param clip - Clip to add
1066
+ * @returns Dispatch result
1067
+ */
1068
+ addClip(trackId, clip) {
1069
+ const result = dispatch(
1070
+ this.history,
1071
+ (state) => addClip(state, trackId, clip)
1072
+ );
1073
+ if (result.success) {
1074
+ this.history = result.history;
1075
+ this.notify();
1076
+ }
1077
+ return result;
1078
+ }
1079
+ /**
1080
+ * Remove a clip
1081
+ *
1082
+ * @param clipId - ID of the clip to remove
1083
+ * @returns Dispatch result
1084
+ */
1085
+ removeClip(clipId) {
1086
+ const result = dispatch(
1087
+ this.history,
1088
+ (state) => removeClip(state, clipId)
1089
+ );
1090
+ if (result.success) {
1091
+ this.history = result.history;
1092
+ this.notify();
1093
+ }
1094
+ return result;
1095
+ }
1096
+ /**
1097
+ * Move a clip to a new timeline position
1098
+ *
1099
+ * @param clipId - ID of the clip to move
1100
+ * @param newStart - New timeline start frame
1101
+ * @returns Dispatch result
1102
+ */
1103
+ moveClip(clipId, newStart) {
1104
+ const result = dispatch(
1105
+ this.history,
1106
+ (state) => moveClip(state, clipId, newStart)
1107
+ );
1108
+ if (result.success) {
1109
+ this.history = result.history;
1110
+ this.notify();
1111
+ }
1112
+ return result;
1113
+ }
1114
+ /**
1115
+ * Resize a clip
1116
+ *
1117
+ * @param clipId - ID of the clip to resize
1118
+ * @param newStart - New timeline start frame
1119
+ * @param newEnd - New timeline end frame
1120
+ * @returns Dispatch result
1121
+ */
1122
+ resizeClip(clipId, newStart, newEnd) {
1123
+ const result = dispatch(
1124
+ this.history,
1125
+ (state) => resizeClip(state, clipId, newStart, newEnd)
1126
+ );
1127
+ if (result.success) {
1128
+ this.history = result.history;
1129
+ this.notify();
1130
+ }
1131
+ return result;
1132
+ }
1133
+ /**
1134
+ * Trim a clip (change media bounds)
1135
+ *
1136
+ * @param clipId - ID of the clip to trim
1137
+ * @param newMediaIn - New media in frame
1138
+ * @param newMediaOut - New media out frame
1139
+ * @returns Dispatch result
1140
+ */
1141
+ trimClip(clipId, newMediaIn, newMediaOut) {
1142
+ const result = dispatch(
1143
+ this.history,
1144
+ (state) => trimClip(state, clipId, newMediaIn, newMediaOut)
1145
+ );
1146
+ if (result.success) {
1147
+ this.history = result.history;
1148
+ this.notify();
1149
+ }
1150
+ return result;
1151
+ }
1152
+ /**
1153
+ * Move a clip to a different track
1154
+ *
1155
+ * @param clipId - ID of the clip to move
1156
+ * @param targetTrackId - ID of the target track
1157
+ * @returns Dispatch result
1158
+ */
1159
+ moveClipToTrack(clipId, targetTrackId) {
1160
+ const result = dispatch(
1161
+ this.history,
1162
+ (state) => moveClipToTrack(state, clipId, targetTrackId)
1163
+ );
1164
+ if (result.success) {
1165
+ this.history = result.history;
1166
+ this.notify();
1167
+ }
1168
+ return result;
1169
+ }
1170
+ // ===== TRACK OPERATIONS =====
1171
+ /**
1172
+ * Add a track
1173
+ *
1174
+ * @param track - Track to add
1175
+ * @returns Dispatch result
1176
+ */
1177
+ addTrack(track) {
1178
+ const result = dispatch(
1179
+ this.history,
1180
+ (state) => addTrack(state, track)
1181
+ );
1182
+ if (result.success) {
1183
+ this.history = result.history;
1184
+ this.notify();
1185
+ }
1186
+ return result;
1187
+ }
1188
+ /**
1189
+ * Remove a track
1190
+ *
1191
+ * @param trackId - ID of the track to remove
1192
+ * @returns Dispatch result
1193
+ */
1194
+ removeTrack(trackId) {
1195
+ const result = dispatch(
1196
+ this.history,
1197
+ (state) => removeTrack(state, trackId)
1198
+ );
1199
+ if (result.success) {
1200
+ this.history = result.history;
1201
+ this.notify();
1202
+ }
1203
+ return result;
1204
+ }
1205
+ /**
1206
+ * Move a track to a new position
1207
+ *
1208
+ * @param trackId - ID of the track to move
1209
+ * @param newIndex - New index position
1210
+ * @returns Dispatch result
1211
+ */
1212
+ moveTrack(trackId, newIndex) {
1213
+ const result = dispatch(
1214
+ this.history,
1215
+ (state) => moveTrack(state, trackId, newIndex)
1216
+ );
1217
+ if (result.success) {
1218
+ this.history = result.history;
1219
+ this.notify();
1220
+ }
1221
+ return result;
1222
+ }
1223
+ /**
1224
+ * Toggle track mute
1225
+ *
1226
+ * @param trackId - ID of the track
1227
+ * @returns Dispatch result
1228
+ */
1229
+ toggleTrackMute(trackId) {
1230
+ const result = dispatch(
1231
+ this.history,
1232
+ (state) => toggleTrackMute(state, trackId)
1233
+ );
1234
+ if (result.success) {
1235
+ this.history = result.history;
1236
+ this.notify();
1237
+ }
1238
+ return result;
1239
+ }
1240
+ /**
1241
+ * Toggle track lock
1242
+ *
1243
+ * @param trackId - ID of the track
1244
+ * @returns Dispatch result
1245
+ */
1246
+ toggleTrackLock(trackId) {
1247
+ const result = dispatch(
1248
+ this.history,
1249
+ (state) => toggleTrackLock(state, trackId)
1250
+ );
1251
+ if (result.success) {
1252
+ this.history = result.history;
1253
+ this.notify();
1254
+ }
1255
+ return result;
1256
+ }
1257
+ /**
1258
+ * Toggle track solo
1259
+ *
1260
+ * @param trackId - ID of the track
1261
+ * @returns Dispatch result
1262
+ */
1263
+ toggleTrackSolo(trackId) {
1264
+ const result = dispatch(
1265
+ this.history,
1266
+ (state) => toggleTrackSolo(state, trackId)
1267
+ );
1268
+ if (result.success) {
1269
+ this.history = result.history;
1270
+ this.notify();
1271
+ }
1272
+ return result;
1273
+ }
1274
+ /**
1275
+ * Set track height
1276
+ *
1277
+ * @param trackId - ID of the track
1278
+ * @param height - New height in pixels
1279
+ * @returns Dispatch result
1280
+ */
1281
+ setTrackHeight(trackId, height) {
1282
+ const result = dispatch(
1283
+ this.history,
1284
+ (state) => setTrackHeight(state, trackId, height)
1285
+ );
1286
+ if (result.success) {
1287
+ this.history = result.history;
1288
+ this.notify();
1289
+ }
1290
+ return result;
1291
+ }
1292
+ // ===== TIMELINE OPERATIONS =====
1293
+ /**
1294
+ * Set timeline duration
1295
+ *
1296
+ * @param duration - New duration in frames
1297
+ * @returns Dispatch result
1298
+ */
1299
+ setTimelineDuration(duration) {
1300
+ const result = dispatch(
1301
+ this.history,
1302
+ (state) => setTimelineDuration(state, duration)
1303
+ );
1304
+ if (result.success) {
1305
+ this.history = result.history;
1306
+ this.notify();
1307
+ }
1308
+ return result;
1309
+ }
1310
+ /**
1311
+ * Set timeline name
1312
+ *
1313
+ * @param name - New timeline name
1314
+ * @returns Dispatch result
1315
+ */
1316
+ setTimelineName(name) {
1317
+ const result = dispatch(
1318
+ this.history,
1319
+ (state) => setTimelineName(state, name)
1320
+ );
1321
+ if (result.success) {
1322
+ this.history = result.history;
1323
+ this.notify();
1324
+ }
1325
+ return result;
1326
+ }
1327
+ // ===== HISTORY OPERATIONS =====
1328
+ /**
1329
+ * Undo the last action
1330
+ *
1331
+ * @returns true if undo was performed
1332
+ */
1333
+ undo() {
1334
+ if (!this.canUndo()) {
1335
+ return false;
1336
+ }
1337
+ this.history = undo(this.history);
1338
+ this.notify();
1339
+ return true;
1340
+ }
1341
+ /**
1342
+ * Redo the last undone action
1343
+ *
1344
+ * @returns true if redo was performed
1345
+ */
1346
+ redo() {
1347
+ if (!this.canRedo()) {
1348
+ return false;
1349
+ }
1350
+ this.history = redo(this.history);
1351
+ this.notify();
1352
+ return true;
1353
+ }
1354
+ /**
1355
+ * Check if undo is available
1356
+ *
1357
+ * @returns true if undo is available
1358
+ */
1359
+ canUndo() {
1360
+ return canUndo(this.history);
1361
+ }
1362
+ /**
1363
+ * Check if redo is available
1364
+ *
1365
+ * @returns true if redo is available
1366
+ */
1367
+ canRedo() {
1368
+ return canRedo(this.history);
1369
+ }
1370
+ // ===== QUERY OPERATIONS =====
1371
+ /**
1372
+ * Find a clip by ID
1373
+ *
1374
+ * @param clipId - Clip ID
1375
+ * @returns The clip, or undefined if not found
1376
+ */
1377
+ findClipById(clipId) {
1378
+ return findClipById(this.getState(), clipId);
1379
+ }
1380
+ /**
1381
+ * Find a track by ID
1382
+ *
1383
+ * @param trackId - Track ID
1384
+ * @returns The track, or undefined if not found
1385
+ */
1386
+ findTrackById(trackId) {
1387
+ return findTrackById(this.getState(), trackId);
1388
+ }
1389
+ /**
1390
+ * Get all clips on a track
1391
+ *
1392
+ * @param trackId - Track ID
1393
+ * @returns Array of clips on the track
1394
+ */
1395
+ getClipsOnTrack(trackId) {
1396
+ return getClipsOnTrack(this.getState(), trackId);
1397
+ }
1398
+ /**
1399
+ * Get all clips at a specific frame
1400
+ *
1401
+ * @param frame - Frame to check
1402
+ * @returns Array of clips at that frame
1403
+ */
1404
+ getClipsAtFrame(frame2) {
1405
+ return getClipsAtFrame(this.getState(), frame2);
1406
+ }
1407
+ /**
1408
+ * Get all clips in a frame range
1409
+ *
1410
+ * @param start - Start frame
1411
+ * @param end - End frame
1412
+ * @returns Array of clips in the range
1413
+ */
1414
+ getClipsInRange(start, end) {
1415
+ return getClipsInRange(this.getState(), start, end);
1416
+ }
1417
+ /**
1418
+ * Get all clips in the timeline
1419
+ *
1420
+ * @returns Array of all clips
1421
+ */
1422
+ getAllClips() {
1423
+ return getAllClips(this.getState());
1424
+ }
1425
+ /**
1426
+ * Get all tracks in the timeline
1427
+ *
1428
+ * @returns Array of all tracks
1429
+ */
1430
+ getAllTracks() {
1431
+ return getAllTracks(this.getState());
1432
+ }
1433
+ // ===== RIPPLE OPERATIONS =====
1434
+ /**
1435
+ * Ripple delete - delete clip and shift subsequent clips left
1436
+ *
1437
+ * @param clipId - ID of the clip to delete
1438
+ * @returns Dispatch result
1439
+ */
1440
+ rippleDelete(clipId) {
1441
+ const result = dispatch(
1442
+ this.history,
1443
+ (state) => rippleDelete(state, clipId)
1444
+ );
1445
+ if (result.success) {
1446
+ this.history = result.history;
1447
+ this.notify();
1448
+ }
1449
+ return result;
1450
+ }
1451
+ /**
1452
+ * Ripple trim - trim clip end and shift subsequent clips
1453
+ *
1454
+ * @param clipId - ID of the clip to trim
1455
+ * @param newEnd - New end frame for the clip
1456
+ * @returns Dispatch result
1457
+ */
1458
+ rippleTrim(clipId, newEnd) {
1459
+ const result = dispatch(
1460
+ this.history,
1461
+ (state) => rippleTrim(state, clipId, newEnd)
1462
+ );
1463
+ if (result.success) {
1464
+ this.history = result.history;
1465
+ this.notify();
1466
+ }
1467
+ return result;
1468
+ }
1469
+ /**
1470
+ * Insert edit - insert clip and shift subsequent clips right
1471
+ *
1472
+ * @param trackId - ID of the track to insert into
1473
+ * @param clip - Clip to insert
1474
+ * @param atFrame - Frame to insert at
1475
+ * @returns Dispatch result
1476
+ */
1477
+ insertEdit(trackId, clip, atFrame) {
1478
+ const result = dispatch(
1479
+ this.history,
1480
+ (state) => insertEdit(state, trackId, clip, atFrame)
1481
+ );
1482
+ if (result.success) {
1483
+ this.history = result.history;
1484
+ this.notify();
1485
+ }
1486
+ return result;
1487
+ }
1488
+ /**
1489
+ * Ripple move - move clip and shift surrounding clips to accommodate
1490
+ *
1491
+ * This moves a clip to a new position while maintaining timeline continuity:
1492
+ * - Closes the gap at the source position
1493
+ * - Makes space at the destination position
1494
+ * - All operations are atomic (single undo entry)
1495
+ *
1496
+ * @param clipId - ID of the clip to move
1497
+ * @param newStart - New start frame for the clip
1498
+ * @returns Dispatch result
1499
+ */
1500
+ rippleMove(clipId, newStart) {
1501
+ const result = dispatch(
1502
+ this.history,
1503
+ (state) => rippleMove(state, clipId, newStart)
1504
+ );
1505
+ if (result.success) {
1506
+ this.history = result.history;
1507
+ this.notify();
1508
+ }
1509
+ return result;
1510
+ }
1511
+ /**
1512
+ * Insert move - move clip and shift destination clips right
1513
+ *
1514
+ * This moves a clip to a new position without closing the gap at source:
1515
+ * - Leaves gap at the source position
1516
+ * - Pushes all clips at destination right to make space
1517
+ * - All operations are atomic (single undo entry)
1518
+ *
1519
+ * @param clipId - ID of the clip to move
1520
+ * @param newStart - New start frame for the clip
1521
+ * @returns Dispatch result
1522
+ */
1523
+ insertMove(clipId, newStart) {
1524
+ const result = dispatch(
1525
+ this.history,
1526
+ (state) => insertMove(state, clipId, newStart)
1527
+ );
1528
+ if (result.success) {
1529
+ this.history = result.history;
1530
+ this.notify();
1531
+ }
1532
+ return result;
1533
+ }
1534
+ // ===== MARKER OPERATIONS =====
1535
+ /**
1536
+ * Add a timeline marker
1537
+ *
1538
+ * @param marker - Timeline marker to add
1539
+ * @returns Dispatch result
1540
+ */
1541
+ addTimelineMarker(marker) {
1542
+ const result = dispatch(
1543
+ this.history,
1544
+ (state) => addTimelineMarker(state, marker)
1545
+ );
1546
+ if (result.success) {
1547
+ this.history = result.history;
1548
+ this.notify();
1549
+ }
1550
+ return result;
1551
+ }
1552
+ /**
1553
+ * Add a clip marker
1554
+ *
1555
+ * @param marker - Clip marker to add
1556
+ * @returns Dispatch result
1557
+ */
1558
+ addClipMarker(marker) {
1559
+ const result = dispatch(
1560
+ this.history,
1561
+ (state) => addClipMarker(state, marker)
1562
+ );
1563
+ if (result.success) {
1564
+ this.history = result.history;
1565
+ this.notify();
1566
+ }
1567
+ return result;
1568
+ }
1569
+ /**
1570
+ * Add a region marker
1571
+ *
1572
+ * @param marker - Region marker to add
1573
+ * @returns Dispatch result
1574
+ */
1575
+ addRegionMarker(marker) {
1576
+ const result = dispatch(
1577
+ this.history,
1578
+ (state) => addRegionMarker(state, marker)
1579
+ );
1580
+ if (result.success) {
1581
+ this.history = result.history;
1582
+ this.notify();
1583
+ }
1584
+ return result;
1585
+ }
1586
+ /**
1587
+ * Remove a marker by ID
1588
+ *
1589
+ * @param markerId - ID of the marker to remove
1590
+ * @returns Dispatch result
1591
+ */
1592
+ removeMarker(markerId) {
1593
+ const result = dispatch(
1594
+ this.history,
1595
+ (state) => removeMarker(state, markerId)
1596
+ );
1597
+ if (result.success) {
1598
+ this.history = result.history;
1599
+ this.notify();
1600
+ }
1601
+ return result;
1602
+ }
1603
+ /**
1604
+ * Update a timeline marker
1605
+ *
1606
+ * @param markerId - ID of the marker to update
1607
+ * @param updates - Partial marker updates
1608
+ * @returns Dispatch result
1609
+ */
1610
+ updateTimelineMarker(markerId, updates) {
1611
+ const result = dispatch(
1612
+ this.history,
1613
+ (state) => updateTimelineMarker(state, markerId, updates)
1614
+ );
1615
+ if (result.success) {
1616
+ this.history = result.history;
1617
+ this.notify();
1618
+ }
1619
+ return result;
1620
+ }
1621
+ /**
1622
+ * Update a region marker
1623
+ *
1624
+ * @param markerId - ID of the marker to update
1625
+ * @param updates - Partial marker updates
1626
+ * @returns Dispatch result
1627
+ */
1628
+ updateRegionMarker(markerId, updates) {
1629
+ const result = dispatch(
1630
+ this.history,
1631
+ (state) => updateRegionMarker(state, markerId, updates)
1632
+ );
1633
+ if (result.success) {
1634
+ this.history = result.history;
1635
+ this.notify();
1636
+ }
1637
+ return result;
1638
+ }
1639
+ // ===== WORK AREA OPERATIONS =====
1640
+ /**
1641
+ * Set work area
1642
+ *
1643
+ * @param start - Start frame
1644
+ * @param end - End frame
1645
+ * @returns Dispatch result
1646
+ */
1647
+ setWorkArea(start, end) {
1648
+ const result = dispatch(
1649
+ this.history,
1650
+ (state) => setWorkArea(state, { startFrame: start, endFrame: end })
1651
+ );
1652
+ if (result.success) {
1653
+ this.history = result.history;
1654
+ this.notify();
1655
+ }
1656
+ return result;
1657
+ }
1658
+ /**
1659
+ * Clear work area
1660
+ *
1661
+ * @returns Dispatch result
1662
+ */
1663
+ clearWorkArea() {
1664
+ const result = dispatch(
1665
+ this.history,
1666
+ (state) => clearWorkArea(state)
1667
+ );
1668
+ if (result.success) {
1669
+ this.history = result.history;
1670
+ this.notify();
1671
+ }
1672
+ return result;
1673
+ }
1674
+ };
1675
+
1676
+ // src/types/timeline.ts
1677
+ function createTimeline(params) {
1678
+ const timeline = {
1679
+ id: params.id,
1680
+ name: params.name,
1681
+ fps: params.fps,
1682
+ duration: params.duration,
1683
+ tracks: params.tracks ?? []
1684
+ };
1685
+ if (params.metadata !== void 0) {
1686
+ timeline.metadata = params.metadata;
1687
+ }
1688
+ return timeline;
1689
+ }
1690
+
1691
+ // src/types/asset.ts
1692
+ function createAsset(params) {
1693
+ const asset = {
1694
+ id: params.id,
1695
+ type: params.type,
1696
+ duration: params.duration,
1697
+ sourceUrl: params.sourceUrl
1698
+ };
1699
+ if (params.metadata !== void 0) {
1700
+ asset.metadata = params.metadata;
1701
+ }
1702
+ return asset;
1703
+ }
1704
+
1705
+ // src/types/state.ts
1706
+ function createTimelineState(params) {
1707
+ const state = {
1708
+ timeline: params.timeline,
1709
+ assets: params.assets ?? /* @__PURE__ */ new Map(),
1710
+ linkGroups: params.linkGroups ?? /* @__PURE__ */ new Map(),
1711
+ groups: params.groups ?? /* @__PURE__ */ new Map(),
1712
+ markers: params.markers ?? {
1713
+ timeline: [],
1714
+ clips: [],
1715
+ regions: []
1716
+ }
1717
+ };
1718
+ if (params.workArea !== void 0) {
1719
+ state.workArea = params.workArea;
1720
+ }
1721
+ return state;
1722
+ }
1723
+
1724
+ // src/types/frame.ts
1725
+ function frame(value) {
1726
+ const rounded = Math.round(value);
1727
+ if (rounded < 0) {
1728
+ throw new Error(`Frame value must be non-negative, got: ${value}`);
1729
+ }
1730
+ return rounded;
1731
+ }
1732
+ function frameRate(value) {
1733
+ if (value <= 0) {
1734
+ throw new Error(`FrameRate must be positive, got: ${value}`);
1735
+ }
1736
+ return value;
1737
+ }
1738
+ function isValidFrame(value) {
1739
+ return Number.isInteger(value) && value >= 0;
1740
+ }
1741
+ function isValidFrameRate(value) {
1742
+ return value > 0;
1743
+ }
1744
+
1745
+ // src/utils/frame.ts
1746
+ function framesToSeconds(frames, fps) {
1747
+ return frames / fps;
1748
+ }
1749
+ function secondsToFrames(seconds, fps) {
1750
+ return frame(seconds * fps);
1751
+ }
1752
+ function framesToTimecode(frames, fps) {
1753
+ const totalFrames = frames;
1754
+ const framesPart = totalFrames % fps;
1755
+ const totalSeconds = Math.floor(totalFrames / fps);
1756
+ const secondsPart = totalSeconds % 60;
1757
+ const totalMinutes = Math.floor(totalSeconds / 60);
1758
+ const minutesPart = totalMinutes % 60;
1759
+ const hoursPart = Math.floor(totalMinutes / 60);
1760
+ return `${pad(hoursPart)}:${pad(minutesPart)}:${pad(secondsPart)}:${pad(framesPart)}`;
1761
+ }
1762
+ function framesToMinutesSeconds(frames, fps) {
1763
+ const totalSeconds = Math.floor(frames / fps);
1764
+ const minutes = Math.floor(totalSeconds / 60);
1765
+ const seconds = totalSeconds % 60;
1766
+ return `${minutes}:${pad(seconds)}`;
1767
+ }
1768
+ function clampFrame(value, min, max) {
1769
+ return frame(Math.max(min, Math.min(max, value)));
1770
+ }
1771
+ function addFrames(a, b) {
1772
+ return frame(a + b);
1773
+ }
1774
+ function subtractFrames(a, b) {
1775
+ return frame(Math.max(0, a - b));
1776
+ }
1777
+ function frameDuration(start, end) {
1778
+ return frame(end - start);
1779
+ }
1780
+ function pad(num, width = 2) {
1781
+ return num.toString().padStart(width, "0");
1782
+ }
1783
+
1784
+ export {
1785
+ createClip,
1786
+ getClipDuration,
1787
+ getClipMediaDuration,
1788
+ clipContainsFrame,
1789
+ clipsOverlap,
1790
+ validResult,
1791
+ invalidResult,
1792
+ invalidResults,
1793
+ combineResults,
1794
+ registerAsset,
1795
+ getAsset,
1796
+ hasAsset,
1797
+ getAllAssets,
1798
+ unregisterAsset,
1799
+ validateClip,
1800
+ validateTrack,
1801
+ validateTimeline,
1802
+ validateNoOverlap,
1803
+ findClipById,
1804
+ findTrackById,
1805
+ getClipsOnTrack,
1806
+ getClipsAtFrame,
1807
+ getClipsInRange,
1808
+ getAllClips,
1809
+ getAllTracks,
1810
+ findTrackIndex,
1811
+ createTrack,
1812
+ sortTrackClips,
1813
+ addClip,
1814
+ removeClip,
1815
+ moveClip,
1816
+ resizeClip,
1817
+ trimClip,
1818
+ updateClip,
1819
+ moveClipToTrack,
1820
+ addTrack,
1821
+ removeTrack,
1822
+ moveTrack,
1823
+ updateTrack,
1824
+ toggleTrackMute,
1825
+ toggleTrackLock,
1826
+ setTimelineDuration,
1827
+ setTimelineName,
1828
+ updateTimelineMetadata,
1829
+ beginTransaction,
1830
+ applyOperation,
1831
+ commitTransaction,
1832
+ rollbackTransaction,
1833
+ getOperationCount,
1834
+ rippleDelete,
1835
+ rippleTrim,
1836
+ insertEdit,
1837
+ rippleMove,
1838
+ insertMove,
1839
+ addTimelineMarker,
1840
+ addClipMarker,
1841
+ addRegionMarker,
1842
+ removeMarker,
1843
+ removeClipMarkers,
1844
+ setWorkArea,
1845
+ clearWorkArea,
1846
+ updateTimelineMarker,
1847
+ updateRegionMarker,
1848
+ TimelineEngine,
1849
+ createTimeline,
1850
+ createAsset,
1851
+ createTimelineState,
1852
+ frame,
1853
+ frameRate,
1854
+ isValidFrame,
1855
+ isValidFrameRate,
1856
+ framesToSeconds,
1857
+ secondsToFrames,
1858
+ framesToTimecode,
1859
+ framesToMinutesSeconds,
1860
+ clampFrame,
1861
+ addFrames,
1862
+ subtractFrames,
1863
+ frameDuration
1864
+ };