@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,1331 @@
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/history.ts
610
+ function createHistory(initialState, limit = 50) {
611
+ return {
612
+ past: [],
613
+ present: initialState,
614
+ future: [],
615
+ limit
616
+ };
617
+ }
618
+ function pushHistory(history, newState) {
619
+ const newPast = [...history.past, history.present];
620
+ if (newPast.length > history.limit) {
621
+ newPast.shift();
622
+ }
623
+ return {
624
+ ...history,
625
+ past: newPast,
626
+ present: newState,
627
+ future: []
628
+ // Clear future on new action
629
+ };
630
+ }
631
+ function undo(history) {
632
+ if (history.past.length === 0) {
633
+ return history;
634
+ }
635
+ const newPast = [...history.past];
636
+ const previous = newPast.pop();
637
+ return {
638
+ ...history,
639
+ past: newPast,
640
+ present: previous,
641
+ future: [history.present, ...history.future]
642
+ };
643
+ }
644
+ function redo(history) {
645
+ if (history.future.length === 0) {
646
+ return history;
647
+ }
648
+ const newFuture = [...history.future];
649
+ const next = newFuture.shift();
650
+ return {
651
+ ...history,
652
+ past: [...history.past, history.present],
653
+ present: next,
654
+ future: newFuture
655
+ };
656
+ }
657
+ function canUndo(history) {
658
+ return history.past.length > 0;
659
+ }
660
+ function canRedo(history) {
661
+ return history.future.length > 0;
662
+ }
663
+ function getCurrentState(history) {
664
+ return history.present;
665
+ }
666
+
667
+ // src/engine/dispatcher.ts
668
+ function dispatch(history, operation) {
669
+ const currentState = getCurrentState(history);
670
+ let newState;
671
+ try {
672
+ newState = operation(currentState);
673
+ } catch (error) {
674
+ const errorMessage = error instanceof Error ? error.message : String(error);
675
+ return {
676
+ success: false,
677
+ history,
678
+ // Return unchanged history
679
+ errors: [{
680
+ code: "OPERATION_ERROR",
681
+ message: errorMessage
682
+ }]
683
+ };
684
+ }
685
+ const validationResult = validateTimeline(newState);
686
+ if (!validationResult.valid) {
687
+ return {
688
+ success: false,
689
+ history,
690
+ // Return unchanged history
691
+ errors: validationResult.errors
692
+ };
693
+ }
694
+ const newHistory = pushHistory(history, newState);
695
+ return {
696
+ success: true,
697
+ history: newHistory
698
+ };
699
+ }
700
+
701
+ // src/engine/timeline-engine.ts
702
+ var TimelineEngine = class {
703
+ history;
704
+ listeners = /* @__PURE__ */ new Set();
705
+ /**
706
+ * Create a new timeline engine
707
+ *
708
+ * @param initialState - Initial timeline state
709
+ * @param historyLimit - Maximum number of undo steps (default: 50)
710
+ */
711
+ constructor(initialState, historyLimit = 50) {
712
+ this.history = createHistory(initialState, historyLimit);
713
+ }
714
+ // ===== SUBSCRIPTION =====
715
+ /**
716
+ * Subscribe to state changes
717
+ *
718
+ * The listener will be called whenever the timeline state changes,
719
+ * with the new state passed as an argument.
720
+ * This is used by framework adapters (e.g., React) to trigger re-renders.
721
+ *
722
+ * @param listener - Function to call on state changes, receives new state
723
+ * @returns Unsubscribe function
724
+ *
725
+ * @example
726
+ * ```typescript
727
+ * const unsubscribe = engine.subscribe((state) => {
728
+ * console.log('State changed:', state);
729
+ * });
730
+ *
731
+ * // Later...
732
+ * unsubscribe();
733
+ * ```
734
+ */
735
+ subscribe(listener) {
736
+ this.listeners.add(listener);
737
+ return () => {
738
+ this.listeners.delete(listener);
739
+ };
740
+ }
741
+ /**
742
+ * Notify all subscribers of a state change
743
+ *
744
+ * This is called internally after any operation that modifies state.
745
+ * Framework adapters use this to trigger re-renders.
746
+ */
747
+ notify() {
748
+ const state = this.getState();
749
+ this.listeners.forEach((listener) => listener(state));
750
+ }
751
+ // ===== STATE ACCESS =====
752
+ /**
753
+ * Get the current timeline state
754
+ *
755
+ * @returns Current timeline state
756
+ */
757
+ getState() {
758
+ return getCurrentState(this.history);
759
+ }
760
+ // ===== ASSET OPERATIONS =====
761
+ /**
762
+ * Register an asset
763
+ *
764
+ * @param asset - Asset to register
765
+ * @returns Dispatch result
766
+ */
767
+ registerAsset(asset) {
768
+ const result = dispatch(
769
+ this.history,
770
+ (state) => registerAsset(state, asset)
771
+ );
772
+ if (result.success) {
773
+ this.history = result.history;
774
+ this.notify();
775
+ }
776
+ return result;
777
+ }
778
+ /**
779
+ * Get an asset by ID
780
+ *
781
+ * @param assetId - Asset ID
782
+ * @returns The asset, or undefined if not found
783
+ */
784
+ getAsset(assetId) {
785
+ return getAsset(this.getState(), assetId);
786
+ }
787
+ // ===== CLIP OPERATIONS =====
788
+ /**
789
+ * Add a clip to a track
790
+ *
791
+ * @param trackId - ID of the track to add to
792
+ * @param clip - Clip to add
793
+ * @returns Dispatch result
794
+ */
795
+ addClip(trackId, clip) {
796
+ const result = dispatch(
797
+ this.history,
798
+ (state) => addClip(state, trackId, clip)
799
+ );
800
+ if (result.success) {
801
+ this.history = result.history;
802
+ this.notify();
803
+ }
804
+ return result;
805
+ }
806
+ /**
807
+ * Remove a clip
808
+ *
809
+ * @param clipId - ID of the clip to remove
810
+ * @returns Dispatch result
811
+ */
812
+ removeClip(clipId) {
813
+ const result = dispatch(
814
+ this.history,
815
+ (state) => removeClip(state, clipId)
816
+ );
817
+ if (result.success) {
818
+ this.history = result.history;
819
+ this.notify();
820
+ }
821
+ return result;
822
+ }
823
+ /**
824
+ * Move a clip to a new timeline position
825
+ *
826
+ * @param clipId - ID of the clip to move
827
+ * @param newStart - New timeline start frame
828
+ * @returns Dispatch result
829
+ */
830
+ moveClip(clipId, newStart) {
831
+ const result = dispatch(
832
+ this.history,
833
+ (state) => moveClip(state, clipId, newStart)
834
+ );
835
+ if (result.success) {
836
+ this.history = result.history;
837
+ this.notify();
838
+ }
839
+ return result;
840
+ }
841
+ /**
842
+ * Resize a clip
843
+ *
844
+ * @param clipId - ID of the clip to resize
845
+ * @param newStart - New timeline start frame
846
+ * @param newEnd - New timeline end frame
847
+ * @returns Dispatch result
848
+ */
849
+ resizeClip(clipId, newStart, newEnd) {
850
+ const result = dispatch(
851
+ this.history,
852
+ (state) => resizeClip(state, clipId, newStart, newEnd)
853
+ );
854
+ if (result.success) {
855
+ this.history = result.history;
856
+ this.notify();
857
+ }
858
+ return result;
859
+ }
860
+ /**
861
+ * Trim a clip (change media bounds)
862
+ *
863
+ * @param clipId - ID of the clip to trim
864
+ * @param newMediaIn - New media in frame
865
+ * @param newMediaOut - New media out frame
866
+ * @returns Dispatch result
867
+ */
868
+ trimClip(clipId, newMediaIn, newMediaOut) {
869
+ const result = dispatch(
870
+ this.history,
871
+ (state) => trimClip(state, clipId, newMediaIn, newMediaOut)
872
+ );
873
+ if (result.success) {
874
+ this.history = result.history;
875
+ this.notify();
876
+ }
877
+ return result;
878
+ }
879
+ /**
880
+ * Move a clip to a different track
881
+ *
882
+ * @param clipId - ID of the clip to move
883
+ * @param targetTrackId - ID of the target track
884
+ * @returns Dispatch result
885
+ */
886
+ moveClipToTrack(clipId, targetTrackId) {
887
+ const result = dispatch(
888
+ this.history,
889
+ (state) => moveClipToTrack(state, clipId, targetTrackId)
890
+ );
891
+ if (result.success) {
892
+ this.history = result.history;
893
+ this.notify();
894
+ }
895
+ return result;
896
+ }
897
+ // ===== TRACK OPERATIONS =====
898
+ /**
899
+ * Add a track
900
+ *
901
+ * @param track - Track to add
902
+ * @returns Dispatch result
903
+ */
904
+ addTrack(track) {
905
+ const result = dispatch(
906
+ this.history,
907
+ (state) => addTrack(state, track)
908
+ );
909
+ if (result.success) {
910
+ this.history = result.history;
911
+ this.notify();
912
+ }
913
+ return result;
914
+ }
915
+ /**
916
+ * Remove a track
917
+ *
918
+ * @param trackId - ID of the track to remove
919
+ * @returns Dispatch result
920
+ */
921
+ removeTrack(trackId) {
922
+ const result = dispatch(
923
+ this.history,
924
+ (state) => removeTrack(state, trackId)
925
+ );
926
+ if (result.success) {
927
+ this.history = result.history;
928
+ this.notify();
929
+ }
930
+ return result;
931
+ }
932
+ /**
933
+ * Move a track to a new position
934
+ *
935
+ * @param trackId - ID of the track to move
936
+ * @param newIndex - New index position
937
+ * @returns Dispatch result
938
+ */
939
+ moveTrack(trackId, newIndex) {
940
+ const result = dispatch(
941
+ this.history,
942
+ (state) => moveTrack(state, trackId, newIndex)
943
+ );
944
+ if (result.success) {
945
+ this.history = result.history;
946
+ this.notify();
947
+ }
948
+ return result;
949
+ }
950
+ /**
951
+ * Toggle track mute
952
+ *
953
+ * @param trackId - ID of the track
954
+ * @returns Dispatch result
955
+ */
956
+ toggleTrackMute(trackId) {
957
+ const result = dispatch(
958
+ this.history,
959
+ (state) => toggleTrackMute(state, trackId)
960
+ );
961
+ if (result.success) {
962
+ this.history = result.history;
963
+ this.notify();
964
+ }
965
+ return result;
966
+ }
967
+ /**
968
+ * Toggle track lock
969
+ *
970
+ * @param trackId - ID of the track
971
+ * @returns Dispatch result
972
+ */
973
+ toggleTrackLock(trackId) {
974
+ const result = dispatch(
975
+ this.history,
976
+ (state) => toggleTrackLock(state, trackId)
977
+ );
978
+ if (result.success) {
979
+ this.history = result.history;
980
+ this.notify();
981
+ }
982
+ return result;
983
+ }
984
+ /**
985
+ * Toggle track solo
986
+ *
987
+ * @param trackId - ID of the track
988
+ * @returns Dispatch result
989
+ */
990
+ toggleTrackSolo(trackId) {
991
+ const result = dispatch(
992
+ this.history,
993
+ (state) => toggleTrackSolo(state, trackId)
994
+ );
995
+ if (result.success) {
996
+ this.history = result.history;
997
+ this.notify();
998
+ }
999
+ return result;
1000
+ }
1001
+ /**
1002
+ * Set track height
1003
+ *
1004
+ * @param trackId - ID of the track
1005
+ * @param height - New height in pixels
1006
+ * @returns Dispatch result
1007
+ */
1008
+ setTrackHeight(trackId, height) {
1009
+ const result = dispatch(
1010
+ this.history,
1011
+ (state) => setTrackHeight(state, trackId, height)
1012
+ );
1013
+ if (result.success) {
1014
+ this.history = result.history;
1015
+ this.notify();
1016
+ }
1017
+ return result;
1018
+ }
1019
+ // ===== TIMELINE OPERATIONS =====
1020
+ /**
1021
+ * Set timeline duration
1022
+ *
1023
+ * @param duration - New duration in frames
1024
+ * @returns Dispatch result
1025
+ */
1026
+ setTimelineDuration(duration) {
1027
+ const result = dispatch(
1028
+ this.history,
1029
+ (state) => setTimelineDuration(state, duration)
1030
+ );
1031
+ if (result.success) {
1032
+ this.history = result.history;
1033
+ this.notify();
1034
+ }
1035
+ return result;
1036
+ }
1037
+ /**
1038
+ * Set timeline name
1039
+ *
1040
+ * @param name - New timeline name
1041
+ * @returns Dispatch result
1042
+ */
1043
+ setTimelineName(name) {
1044
+ const result = dispatch(
1045
+ this.history,
1046
+ (state) => setTimelineName(state, name)
1047
+ );
1048
+ if (result.success) {
1049
+ this.history = result.history;
1050
+ this.notify();
1051
+ }
1052
+ return result;
1053
+ }
1054
+ // ===== HISTORY OPERATIONS =====
1055
+ /**
1056
+ * Undo the last action
1057
+ *
1058
+ * @returns true if undo was performed
1059
+ */
1060
+ undo() {
1061
+ if (!this.canUndo()) {
1062
+ return false;
1063
+ }
1064
+ this.history = undo(this.history);
1065
+ this.notify();
1066
+ return true;
1067
+ }
1068
+ /**
1069
+ * Redo the last undone action
1070
+ *
1071
+ * @returns true if redo was performed
1072
+ */
1073
+ redo() {
1074
+ if (!this.canRedo()) {
1075
+ return false;
1076
+ }
1077
+ this.history = redo(this.history);
1078
+ this.notify();
1079
+ return true;
1080
+ }
1081
+ /**
1082
+ * Check if undo is available
1083
+ *
1084
+ * @returns true if undo is available
1085
+ */
1086
+ canUndo() {
1087
+ return canUndo(this.history);
1088
+ }
1089
+ /**
1090
+ * Check if redo is available
1091
+ *
1092
+ * @returns true if redo is available
1093
+ */
1094
+ canRedo() {
1095
+ return canRedo(this.history);
1096
+ }
1097
+ // ===== QUERY OPERATIONS =====
1098
+ /**
1099
+ * Find a clip by ID
1100
+ *
1101
+ * @param clipId - Clip ID
1102
+ * @returns The clip, or undefined if not found
1103
+ */
1104
+ findClipById(clipId) {
1105
+ return findClipById(this.getState(), clipId);
1106
+ }
1107
+ /**
1108
+ * Find a track by ID
1109
+ *
1110
+ * @param trackId - Track ID
1111
+ * @returns The track, or undefined if not found
1112
+ */
1113
+ findTrackById(trackId) {
1114
+ return findTrackById(this.getState(), trackId);
1115
+ }
1116
+ /**
1117
+ * Get all clips on a track
1118
+ *
1119
+ * @param trackId - Track ID
1120
+ * @returns Array of clips on the track
1121
+ */
1122
+ getClipsOnTrack(trackId) {
1123
+ return getClipsOnTrack(this.getState(), trackId);
1124
+ }
1125
+ /**
1126
+ * Get all clips at a specific frame
1127
+ *
1128
+ * @param frame - Frame to check
1129
+ * @returns Array of clips at that frame
1130
+ */
1131
+ getClipsAtFrame(frame2) {
1132
+ return getClipsAtFrame(this.getState(), frame2);
1133
+ }
1134
+ /**
1135
+ * Get all clips in a frame range
1136
+ *
1137
+ * @param start - Start frame
1138
+ * @param end - End frame
1139
+ * @returns Array of clips in the range
1140
+ */
1141
+ getClipsInRange(start, end) {
1142
+ return getClipsInRange(this.getState(), start, end);
1143
+ }
1144
+ /**
1145
+ * Get all clips in the timeline
1146
+ *
1147
+ * @returns Array of all clips
1148
+ */
1149
+ getAllClips() {
1150
+ return getAllClips(this.getState());
1151
+ }
1152
+ /**
1153
+ * Get all tracks in the timeline
1154
+ *
1155
+ * @returns Array of all tracks
1156
+ */
1157
+ getAllTracks() {
1158
+ return getAllTracks(this.getState());
1159
+ }
1160
+ };
1161
+
1162
+ // src/types/timeline.ts
1163
+ function createTimeline(params) {
1164
+ const timeline = {
1165
+ id: params.id,
1166
+ name: params.name,
1167
+ fps: params.fps,
1168
+ duration: params.duration,
1169
+ tracks: params.tracks ?? []
1170
+ };
1171
+ if (params.metadata !== void 0) {
1172
+ timeline.metadata = params.metadata;
1173
+ }
1174
+ return timeline;
1175
+ }
1176
+
1177
+ // src/types/asset.ts
1178
+ function createAsset(params) {
1179
+ const asset = {
1180
+ id: params.id,
1181
+ type: params.type,
1182
+ duration: params.duration,
1183
+ sourceUrl: params.sourceUrl
1184
+ };
1185
+ if (params.metadata !== void 0) {
1186
+ asset.metadata = params.metadata;
1187
+ }
1188
+ return asset;
1189
+ }
1190
+
1191
+ // src/types/state.ts
1192
+ function createTimelineState(params) {
1193
+ const state = {
1194
+ timeline: params.timeline,
1195
+ assets: params.assets ?? /* @__PURE__ */ new Map(),
1196
+ linkGroups: params.linkGroups ?? /* @__PURE__ */ new Map(),
1197
+ groups: params.groups ?? /* @__PURE__ */ new Map(),
1198
+ markers: params.markers ?? {
1199
+ timeline: [],
1200
+ clips: [],
1201
+ regions: []
1202
+ }
1203
+ };
1204
+ if (params.workArea !== void 0) {
1205
+ state.workArea = params.workArea;
1206
+ }
1207
+ return state;
1208
+ }
1209
+
1210
+ // src/types/frame.ts
1211
+ function frame(value) {
1212
+ const rounded = Math.round(value);
1213
+ if (rounded < 0) {
1214
+ throw new Error(`Frame value must be non-negative, got: ${value}`);
1215
+ }
1216
+ return rounded;
1217
+ }
1218
+ function frameRate(value) {
1219
+ if (value <= 0) {
1220
+ throw new Error(`FrameRate must be positive, got: ${value}`);
1221
+ }
1222
+ return value;
1223
+ }
1224
+ function isValidFrame(value) {
1225
+ return Number.isInteger(value) && value >= 0;
1226
+ }
1227
+ function isValidFrameRate(value) {
1228
+ return value > 0;
1229
+ }
1230
+
1231
+ // src/utils/frame.ts
1232
+ function framesToSeconds(frames, fps) {
1233
+ return frames / fps;
1234
+ }
1235
+ function secondsToFrames(seconds, fps) {
1236
+ return frame(seconds * fps);
1237
+ }
1238
+ function framesToTimecode(frames, fps) {
1239
+ const totalFrames = frames;
1240
+ const framesPart = totalFrames % fps;
1241
+ const totalSeconds = Math.floor(totalFrames / fps);
1242
+ const secondsPart = totalSeconds % 60;
1243
+ const totalMinutes = Math.floor(totalSeconds / 60);
1244
+ const minutesPart = totalMinutes % 60;
1245
+ const hoursPart = Math.floor(totalMinutes / 60);
1246
+ return `${pad(hoursPart)}:${pad(minutesPart)}:${pad(secondsPart)}:${pad(framesPart)}`;
1247
+ }
1248
+ function framesToMinutesSeconds(frames, fps) {
1249
+ const totalSeconds = Math.floor(frames / fps);
1250
+ const minutes = Math.floor(totalSeconds / 60);
1251
+ const seconds = totalSeconds % 60;
1252
+ return `${minutes}:${pad(seconds)}`;
1253
+ }
1254
+ function clampFrame(value, min, max) {
1255
+ return frame(Math.max(min, Math.min(max, value)));
1256
+ }
1257
+ function addFrames(a, b) {
1258
+ return frame(a + b);
1259
+ }
1260
+ function subtractFrames(a, b) {
1261
+ return frame(Math.max(0, a - b));
1262
+ }
1263
+ function frameDuration(start, end) {
1264
+ return frame(end - start);
1265
+ }
1266
+ function pad(num, width = 2) {
1267
+ return num.toString().padStart(width, "0");
1268
+ }
1269
+
1270
+ export {
1271
+ createClip,
1272
+ getClipDuration,
1273
+ getClipMediaDuration,
1274
+ clipContainsFrame,
1275
+ clipsOverlap,
1276
+ validResult,
1277
+ invalidResult,
1278
+ invalidResults,
1279
+ combineResults,
1280
+ registerAsset,
1281
+ getAsset,
1282
+ hasAsset,
1283
+ getAllAssets,
1284
+ unregisterAsset,
1285
+ validateClip,
1286
+ validateTrack,
1287
+ validateTimeline,
1288
+ validateNoOverlap,
1289
+ findClipById,
1290
+ findTrackById,
1291
+ getClipsOnTrack,
1292
+ getClipsAtFrame,
1293
+ getClipsInRange,
1294
+ getAllClips,
1295
+ getAllTracks,
1296
+ findTrackIndex,
1297
+ createTrack,
1298
+ sortTrackClips,
1299
+ addClip,
1300
+ removeClip,
1301
+ moveClip,
1302
+ resizeClip,
1303
+ trimClip,
1304
+ updateClip,
1305
+ moveClipToTrack,
1306
+ addTrack,
1307
+ removeTrack,
1308
+ moveTrack,
1309
+ updateTrack,
1310
+ toggleTrackMute,
1311
+ toggleTrackLock,
1312
+ setTimelineDuration,
1313
+ setTimelineName,
1314
+ updateTimelineMetadata,
1315
+ TimelineEngine,
1316
+ createTimeline,
1317
+ createAsset,
1318
+ createTimelineState,
1319
+ frame,
1320
+ frameRate,
1321
+ isValidFrame,
1322
+ isValidFrameRate,
1323
+ framesToSeconds,
1324
+ secondsToFrames,
1325
+ framesToTimecode,
1326
+ framesToMinutesSeconds,
1327
+ clampFrame,
1328
+ addFrames,
1329
+ subtractFrames,
1330
+ frameDuration
1331
+ };