@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,1255 @@
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.mediaOut <= clip.mediaIn) {
116
+ errors.push(invalidResult(
117
+ "INVALID_MEDIA_BOUNDS",
118
+ `Clip media out (${clip.mediaOut}) must be greater than media in (${clip.mediaIn})`,
119
+ { clipId: clip.id, mediaIn: clip.mediaIn, mediaOut: clip.mediaOut }
120
+ ));
121
+ }
122
+ if (clip.mediaOut > asset.duration) {
123
+ errors.push(invalidResult(
124
+ "MEDIA_EXCEEDS_ASSET",
125
+ `Clip media out (${clip.mediaOut}) exceeds asset duration (${asset.duration})`,
126
+ { clipId: clip.id, mediaOut: clip.mediaOut, assetDuration: asset.duration }
127
+ ));
128
+ }
129
+ const timelineDuration = getClipDuration(clip);
130
+ const mediaDuration = getClipMediaDuration(clip);
131
+ if (timelineDuration !== mediaDuration) {
132
+ errors.push(invalidResult(
133
+ "DURATION_MISMATCH",
134
+ `Clip timeline duration (${timelineDuration}) must match media duration (${mediaDuration}) in Phase 1`,
135
+ { clipId: clip.id, timelineDuration, mediaDuration }
136
+ ));
137
+ }
138
+ return combineResults(...errors);
139
+ }
140
+ function validateTrack(state, track) {
141
+ const errors = [];
142
+ for (const clip of track.clips) {
143
+ const clipResult = validateClip(state, clip);
144
+ if (!clipResult.valid) {
145
+ errors.push(clipResult);
146
+ }
147
+ }
148
+ for (let i = 0; i < track.clips.length; i++) {
149
+ for (let j = i + 1; j < track.clips.length; j++) {
150
+ const clip1 = track.clips[i];
151
+ const clip2 = track.clips[j];
152
+ if (!clip1 || !clip2) {
153
+ continue;
154
+ }
155
+ if (clipsOverlap(clip1, clip2)) {
156
+ errors.push(invalidResult(
157
+ "CLIPS_OVERLAP",
158
+ `Clips '${clip1.id}' and '${clip2.id}' overlap on track '${track.id}'`,
159
+ {
160
+ trackId: track.id,
161
+ clip1Id: clip1.id,
162
+ clip2Id: clip2.id,
163
+ clip1Start: clip1.timelineStart,
164
+ clip1End: clip1.timelineEnd,
165
+ clip2Start: clip2.timelineStart,
166
+ clip2End: clip2.timelineEnd
167
+ }
168
+ ));
169
+ }
170
+ }
171
+ }
172
+ return combineResults(...errors);
173
+ }
174
+ function validateTimeline(state) {
175
+ const errors = [];
176
+ if (state.timeline.fps <= 0) {
177
+ errors.push(invalidResult(
178
+ "INVALID_FPS",
179
+ `Timeline FPS must be positive, got ${state.timeline.fps}`,
180
+ { fps: state.timeline.fps }
181
+ ));
182
+ }
183
+ if (state.timeline.duration <= 0) {
184
+ errors.push(invalidResult(
185
+ "INVALID_DURATION",
186
+ `Timeline duration must be positive, got ${state.timeline.duration}`,
187
+ { duration: state.timeline.duration }
188
+ ));
189
+ }
190
+ for (const track of state.timeline.tracks) {
191
+ const trackResult = validateTrack(state, track);
192
+ if (!trackResult.valid) {
193
+ errors.push(trackResult);
194
+ }
195
+ }
196
+ return combineResults(...errors);
197
+ }
198
+ function validateNoOverlap(track, clip) {
199
+ for (const existingClip of track.clips) {
200
+ if (existingClip.id === clip.id) {
201
+ continue;
202
+ }
203
+ if (clipsOverlap(existingClip, clip)) {
204
+ return invalidResult(
205
+ "CLIPS_OVERLAP",
206
+ `Clip '${clip.id}' would overlap with existing clip '${existingClip.id}' on track '${track.id}'`,
207
+ {
208
+ trackId: track.id,
209
+ newClipId: clip.id,
210
+ existingClipId: existingClip.id,
211
+ newClipStart: clip.timelineStart,
212
+ newClipEnd: clip.timelineEnd,
213
+ existingClipStart: existingClip.timelineStart,
214
+ existingClipEnd: existingClip.timelineEnd
215
+ }
216
+ );
217
+ }
218
+ }
219
+ return validResult();
220
+ }
221
+ function validateTrackTypeMatch(state, clip, targetTrack) {
222
+ const asset = getAsset(state, clip.assetId);
223
+ if (!asset) {
224
+ return invalidResult(
225
+ "ASSET_NOT_FOUND",
226
+ `Asset '${clip.assetId}' not found in registry`,
227
+ { clipId: clip.id, assetId: clip.assetId }
228
+ );
229
+ }
230
+ if (asset.type === "video" && targetTrack.type !== "video") {
231
+ return invalidResult(
232
+ "TRACK_TYPE_MISMATCH",
233
+ `Cannot place video clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
234
+ {
235
+ clipId: clip.id,
236
+ assetType: asset.type,
237
+ trackType: targetTrack.type,
238
+ trackId: targetTrack.id
239
+ }
240
+ );
241
+ }
242
+ if (asset.type === "audio" && targetTrack.type !== "audio") {
243
+ return invalidResult(
244
+ "TRACK_TYPE_MISMATCH",
245
+ `Cannot place audio clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
246
+ {
247
+ clipId: clip.id,
248
+ assetType: asset.type,
249
+ trackType: targetTrack.type,
250
+ trackId: targetTrack.id
251
+ }
252
+ );
253
+ }
254
+ if (asset.type === "image" && targetTrack.type !== "video") {
255
+ return invalidResult(
256
+ "TRACK_TYPE_MISMATCH",
257
+ `Cannot place image clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
258
+ {
259
+ clipId: clip.id,
260
+ assetType: asset.type,
261
+ trackType: targetTrack.type,
262
+ trackId: targetTrack.id
263
+ }
264
+ );
265
+ }
266
+ return validResult();
267
+ }
268
+
269
+ // src/systems/queries.ts
270
+ function findClipById(state, clipId) {
271
+ for (const track of state.timeline.tracks) {
272
+ const clip = track.clips.find((c) => c.id === clipId);
273
+ if (clip) {
274
+ return clip;
275
+ }
276
+ }
277
+ return void 0;
278
+ }
279
+ function findTrackById(state, trackId) {
280
+ return state.timeline.tracks.find((t) => t.id === trackId);
281
+ }
282
+ function getClipsOnTrack(state, trackId) {
283
+ const track = findTrackById(state, trackId);
284
+ return track ? track.clips : [];
285
+ }
286
+ function getClipsAtFrame(state, frame2) {
287
+ const clips = [];
288
+ for (const track of state.timeline.tracks) {
289
+ for (const clip of track.clips) {
290
+ if (clipContainsFrame(clip, frame2)) {
291
+ clips.push(clip);
292
+ }
293
+ }
294
+ }
295
+ return clips;
296
+ }
297
+ function getClipsInRange(state, start, end) {
298
+ const clips = [];
299
+ for (const track of state.timeline.tracks) {
300
+ for (const clip of track.clips) {
301
+ if (clip.timelineStart < end && clip.timelineEnd > start) {
302
+ clips.push(clip);
303
+ }
304
+ }
305
+ }
306
+ return clips;
307
+ }
308
+ function getAllClips(state) {
309
+ const clips = [];
310
+ for (const track of state.timeline.tracks) {
311
+ clips.push(...track.clips);
312
+ }
313
+ return clips;
314
+ }
315
+ function getAllTracks(state) {
316
+ return state.timeline.tracks;
317
+ }
318
+ function findTrackIndex(state, trackId) {
319
+ return state.timeline.tracks.findIndex((t) => t.id === trackId);
320
+ }
321
+
322
+ // src/types/track.ts
323
+ function createTrack(params) {
324
+ const track = {
325
+ id: params.id,
326
+ name: params.name,
327
+ type: params.type,
328
+ clips: params.clips ?? [],
329
+ locked: params.locked ?? false,
330
+ muted: params.muted ?? false
331
+ };
332
+ if (params.metadata !== void 0) {
333
+ track.metadata = params.metadata;
334
+ }
335
+ return track;
336
+ }
337
+ function sortTrackClips(track) {
338
+ return {
339
+ ...track,
340
+ clips: [...track.clips].sort((a, b) => a.timelineStart - b.timelineStart)
341
+ };
342
+ }
343
+
344
+ // src/operations/clip-operations.ts
345
+ function addClip(state, trackId, clip) {
346
+ const trackIndex = state.timeline.tracks.findIndex((t) => t.id === trackId);
347
+ if (trackIndex === -1) {
348
+ return state;
349
+ }
350
+ const track = state.timeline.tracks[trackIndex];
351
+ if (!track) {
352
+ return state;
353
+ }
354
+ const newTrack = sortTrackClips({
355
+ ...track,
356
+ clips: [...track.clips, clip]
357
+ });
358
+ const newTracks = [...state.timeline.tracks];
359
+ newTracks[trackIndex] = newTrack;
360
+ return {
361
+ ...state,
362
+ timeline: {
363
+ ...state.timeline,
364
+ tracks: newTracks
365
+ }
366
+ };
367
+ }
368
+ function removeClip(state, clipId) {
369
+ const newTracks = state.timeline.tracks.map((track) => ({
370
+ ...track,
371
+ clips: track.clips.filter((c) => c.id !== clipId)
372
+ }));
373
+ return {
374
+ ...state,
375
+ timeline: {
376
+ ...state.timeline,
377
+ tracks: newTracks
378
+ }
379
+ };
380
+ }
381
+ function moveClip(state, clipId, newStart) {
382
+ const clip = findClipById(state, clipId);
383
+ if (!clip) {
384
+ return state;
385
+ }
386
+ const duration = clip.timelineEnd - clip.timelineStart;
387
+ const newEnd = newStart + duration;
388
+ return updateClip(state, clipId, {
389
+ timelineStart: newStart,
390
+ timelineEnd: newEnd
391
+ });
392
+ }
393
+ function resizeClip(state, clipId, newStart, newEnd) {
394
+ return updateClip(state, clipId, {
395
+ timelineStart: newStart,
396
+ timelineEnd: newEnd
397
+ });
398
+ }
399
+ function trimClip(state, clipId, newMediaIn, newMediaOut) {
400
+ return updateClip(state, clipId, {
401
+ mediaIn: newMediaIn,
402
+ mediaOut: newMediaOut
403
+ });
404
+ }
405
+ function updateClip(state, clipId, updates) {
406
+ const newTracks = state.timeline.tracks.map((track) => {
407
+ const clipIndex = track.clips.findIndex((c) => c.id === clipId);
408
+ if (clipIndex === -1) {
409
+ return track;
410
+ }
411
+ const newClips = [...track.clips];
412
+ const existingClip = newClips[clipIndex];
413
+ if (!existingClip) {
414
+ return track;
415
+ }
416
+ newClips[clipIndex] = {
417
+ ...existingClip,
418
+ ...updates
419
+ };
420
+ return sortTrackClips({
421
+ ...track,
422
+ clips: newClips
423
+ });
424
+ });
425
+ return {
426
+ ...state,
427
+ timeline: {
428
+ ...state.timeline,
429
+ tracks: newTracks
430
+ }
431
+ };
432
+ }
433
+ function moveClipToTrack(state, clipId, targetTrackId) {
434
+ const clip = findClipById(state, clipId);
435
+ if (!clip) {
436
+ return state;
437
+ }
438
+ const targetTrack = findTrackById(state, targetTrackId);
439
+ if (!targetTrack) {
440
+ return state;
441
+ }
442
+ const validationResult = validateTrackTypeMatch(state, clip, targetTrack);
443
+ if (!validationResult.valid) {
444
+ return state;
445
+ }
446
+ if (clip.trackId === targetTrackId) {
447
+ return state;
448
+ }
449
+ let newState = removeClip(state, clipId);
450
+ const updatedClip = { ...clip, trackId: targetTrackId };
451
+ newState = addClip(newState, targetTrackId, updatedClip);
452
+ return newState;
453
+ }
454
+
455
+ // src/operations/track-operations.ts
456
+ function addTrack(state, track) {
457
+ return {
458
+ ...state,
459
+ timeline: {
460
+ ...state.timeline,
461
+ tracks: [...state.timeline.tracks, track]
462
+ }
463
+ };
464
+ }
465
+ function removeTrack(state, trackId) {
466
+ return {
467
+ ...state,
468
+ timeline: {
469
+ ...state.timeline,
470
+ tracks: state.timeline.tracks.filter((t) => t.id !== trackId)
471
+ }
472
+ };
473
+ }
474
+ function moveTrack(state, trackId, newIndex) {
475
+ const currentIndex = findTrackIndex(state, trackId);
476
+ if (currentIndex === -1) {
477
+ return state;
478
+ }
479
+ const newTracks = [...state.timeline.tracks];
480
+ const [track] = newTracks.splice(currentIndex, 1);
481
+ if (!track) {
482
+ return state;
483
+ }
484
+ newTracks.splice(newIndex, 0, track);
485
+ return {
486
+ ...state,
487
+ timeline: {
488
+ ...state.timeline,
489
+ tracks: newTracks
490
+ }
491
+ };
492
+ }
493
+ function updateTrack(state, trackId, updates) {
494
+ const trackIndex = findTrackIndex(state, trackId);
495
+ if (trackIndex === -1) {
496
+ return state;
497
+ }
498
+ const newTracks = [...state.timeline.tracks];
499
+ const existingTrack = newTracks[trackIndex];
500
+ if (!existingTrack) {
501
+ return state;
502
+ }
503
+ newTracks[trackIndex] = {
504
+ ...existingTrack,
505
+ ...updates
506
+ };
507
+ return {
508
+ ...state,
509
+ timeline: {
510
+ ...state.timeline,
511
+ tracks: newTracks
512
+ }
513
+ };
514
+ }
515
+ function toggleTrackMute(state, trackId) {
516
+ const trackIndex = findTrackIndex(state, trackId);
517
+ if (trackIndex === -1) {
518
+ return state;
519
+ }
520
+ const track = state.timeline.tracks[trackIndex];
521
+ if (!track) {
522
+ return state;
523
+ }
524
+ return updateTrack(state, trackId, { muted: !track.muted });
525
+ }
526
+ function toggleTrackLock(state, trackId) {
527
+ const trackIndex = findTrackIndex(state, trackId);
528
+ if (trackIndex === -1) {
529
+ return state;
530
+ }
531
+ const track = state.timeline.tracks[trackIndex];
532
+ if (!track) {
533
+ return state;
534
+ }
535
+ return updateTrack(state, trackId, { locked: !track.locked });
536
+ }
537
+
538
+ // src/operations/timeline-operations.ts
539
+ function setTimelineDuration(state, duration) {
540
+ return {
541
+ ...state,
542
+ timeline: {
543
+ ...state.timeline,
544
+ duration
545
+ }
546
+ };
547
+ }
548
+ function setTimelineName(state, name) {
549
+ return {
550
+ ...state,
551
+ timeline: {
552
+ ...state.timeline,
553
+ name
554
+ }
555
+ };
556
+ }
557
+ function updateTimelineMetadata(state, metadata) {
558
+ return {
559
+ ...state,
560
+ timeline: {
561
+ ...state.timeline,
562
+ metadata: {
563
+ ...state.timeline.metadata,
564
+ ...metadata
565
+ }
566
+ }
567
+ };
568
+ }
569
+
570
+ // src/engine/history.ts
571
+ function createHistory(initialState, limit = 50) {
572
+ return {
573
+ past: [],
574
+ present: initialState,
575
+ future: [],
576
+ limit
577
+ };
578
+ }
579
+ function pushHistory(history, newState) {
580
+ const newPast = [...history.past, history.present];
581
+ if (newPast.length > history.limit) {
582
+ newPast.shift();
583
+ }
584
+ return {
585
+ ...history,
586
+ past: newPast,
587
+ present: newState,
588
+ future: []
589
+ // Clear future on new action
590
+ };
591
+ }
592
+ function undo(history) {
593
+ if (history.past.length === 0) {
594
+ return history;
595
+ }
596
+ const newPast = [...history.past];
597
+ const previous = newPast.pop();
598
+ return {
599
+ ...history,
600
+ past: newPast,
601
+ present: previous,
602
+ future: [history.present, ...history.future]
603
+ };
604
+ }
605
+ function redo(history) {
606
+ if (history.future.length === 0) {
607
+ return history;
608
+ }
609
+ const newFuture = [...history.future];
610
+ const next = newFuture.shift();
611
+ return {
612
+ ...history,
613
+ past: [...history.past, history.present],
614
+ present: next,
615
+ future: newFuture
616
+ };
617
+ }
618
+ function canUndo(history) {
619
+ return history.past.length > 0;
620
+ }
621
+ function canRedo(history) {
622
+ return history.future.length > 0;
623
+ }
624
+ function getCurrentState(history) {
625
+ return history.present;
626
+ }
627
+
628
+ // src/engine/dispatcher.ts
629
+ function dispatch(history, operation) {
630
+ const currentState = getCurrentState(history);
631
+ let newState;
632
+ try {
633
+ newState = operation(currentState);
634
+ } catch (error) {
635
+ const errorMessage = error instanceof Error ? error.message : String(error);
636
+ return {
637
+ success: false,
638
+ history,
639
+ // Return unchanged history
640
+ errors: [{
641
+ code: "OPERATION_ERROR",
642
+ message: errorMessage
643
+ }]
644
+ };
645
+ }
646
+ const validationResult = validateTimeline(newState);
647
+ if (!validationResult.valid) {
648
+ return {
649
+ success: false,
650
+ history,
651
+ // Return unchanged history
652
+ errors: validationResult.errors
653
+ };
654
+ }
655
+ const newHistory = pushHistory(history, newState);
656
+ return {
657
+ success: true,
658
+ history: newHistory
659
+ };
660
+ }
661
+
662
+ // src/engine/timeline-engine.ts
663
+ var TimelineEngine = class {
664
+ history;
665
+ listeners = /* @__PURE__ */ new Set();
666
+ /**
667
+ * Create a new timeline engine
668
+ *
669
+ * @param initialState - Initial timeline state
670
+ * @param historyLimit - Maximum number of undo steps (default: 50)
671
+ */
672
+ constructor(initialState, historyLimit = 50) {
673
+ this.history = createHistory(initialState, historyLimit);
674
+ }
675
+ // ===== SUBSCRIPTION =====
676
+ /**
677
+ * Subscribe to state changes
678
+ *
679
+ * The listener will be called whenever the timeline state changes.
680
+ * This is used by framework adapters (e.g., React) to trigger re-renders.
681
+ *
682
+ * @param listener - Function to call on state changes
683
+ * @returns Unsubscribe function
684
+ *
685
+ * @example
686
+ * ```typescript
687
+ * const unsubscribe = engine.subscribe(() => {
688
+ * console.log('State changed:', engine.getState());
689
+ * });
690
+ *
691
+ * // Later...
692
+ * unsubscribe();
693
+ * ```
694
+ */
695
+ subscribe(listener) {
696
+ this.listeners.add(listener);
697
+ return () => {
698
+ this.listeners.delete(listener);
699
+ };
700
+ }
701
+ /**
702
+ * Notify all subscribers of a state change
703
+ *
704
+ * This is called internally after any operation that modifies state.
705
+ * Framework adapters use this to trigger re-renders.
706
+ */
707
+ notify() {
708
+ this.listeners.forEach((listener) => listener());
709
+ }
710
+ // ===== STATE ACCESS =====
711
+ /**
712
+ * Get the current timeline state
713
+ *
714
+ * @returns Current timeline state
715
+ */
716
+ getState() {
717
+ return getCurrentState(this.history);
718
+ }
719
+ // ===== ASSET OPERATIONS =====
720
+ /**
721
+ * Register an asset
722
+ *
723
+ * @param asset - Asset to register
724
+ * @returns Dispatch result
725
+ */
726
+ registerAsset(asset) {
727
+ const result = dispatch(
728
+ this.history,
729
+ (state) => registerAsset(state, asset)
730
+ );
731
+ if (result.success) {
732
+ this.history = result.history;
733
+ this.notify();
734
+ }
735
+ return result;
736
+ }
737
+ /**
738
+ * Get an asset by ID
739
+ *
740
+ * @param assetId - Asset ID
741
+ * @returns The asset, or undefined if not found
742
+ */
743
+ getAsset(assetId) {
744
+ return getAsset(this.getState(), assetId);
745
+ }
746
+ // ===== CLIP OPERATIONS =====
747
+ /**
748
+ * Add a clip to a track
749
+ *
750
+ * @param trackId - ID of the track to add to
751
+ * @param clip - Clip to add
752
+ * @returns Dispatch result
753
+ */
754
+ addClip(trackId, clip) {
755
+ const result = dispatch(
756
+ this.history,
757
+ (state) => addClip(state, trackId, clip)
758
+ );
759
+ if (result.success) {
760
+ this.history = result.history;
761
+ this.notify();
762
+ }
763
+ return result;
764
+ }
765
+ /**
766
+ * Remove a clip
767
+ *
768
+ * @param clipId - ID of the clip to remove
769
+ * @returns Dispatch result
770
+ */
771
+ removeClip(clipId) {
772
+ const result = dispatch(
773
+ this.history,
774
+ (state) => removeClip(state, clipId)
775
+ );
776
+ if (result.success) {
777
+ this.history = result.history;
778
+ this.notify();
779
+ }
780
+ return result;
781
+ }
782
+ /**
783
+ * Move a clip to a new timeline position
784
+ *
785
+ * @param clipId - ID of the clip to move
786
+ * @param newStart - New timeline start frame
787
+ * @returns Dispatch result
788
+ */
789
+ moveClip(clipId, newStart) {
790
+ const result = dispatch(
791
+ this.history,
792
+ (state) => moveClip(state, clipId, newStart)
793
+ );
794
+ if (result.success) {
795
+ this.history = result.history;
796
+ this.notify();
797
+ }
798
+ return result;
799
+ }
800
+ /**
801
+ * Resize a clip
802
+ *
803
+ * @param clipId - ID of the clip to resize
804
+ * @param newStart - New timeline start frame
805
+ * @param newEnd - New timeline end frame
806
+ * @returns Dispatch result
807
+ */
808
+ resizeClip(clipId, newStart, newEnd) {
809
+ const result = dispatch(
810
+ this.history,
811
+ (state) => resizeClip(state, clipId, newStart, newEnd)
812
+ );
813
+ if (result.success) {
814
+ this.history = result.history;
815
+ this.notify();
816
+ }
817
+ return result;
818
+ }
819
+ /**
820
+ * Trim a clip (change media bounds)
821
+ *
822
+ * @param clipId - ID of the clip to trim
823
+ * @param newMediaIn - New media in frame
824
+ * @param newMediaOut - New media out frame
825
+ * @returns Dispatch result
826
+ */
827
+ trimClip(clipId, newMediaIn, newMediaOut) {
828
+ const result = dispatch(
829
+ this.history,
830
+ (state) => trimClip(state, clipId, newMediaIn, newMediaOut)
831
+ );
832
+ if (result.success) {
833
+ this.history = result.history;
834
+ this.notify();
835
+ }
836
+ return result;
837
+ }
838
+ /**
839
+ * Move a clip to a different track
840
+ *
841
+ * @param clipId - ID of the clip to move
842
+ * @param targetTrackId - ID of the target track
843
+ * @returns Dispatch result
844
+ */
845
+ moveClipToTrack(clipId, targetTrackId) {
846
+ const result = dispatch(
847
+ this.history,
848
+ (state) => moveClipToTrack(state, clipId, targetTrackId)
849
+ );
850
+ if (result.success) {
851
+ this.history = result.history;
852
+ this.notify();
853
+ }
854
+ return result;
855
+ }
856
+ // ===== TRACK OPERATIONS =====
857
+ /**
858
+ * Add a track
859
+ *
860
+ * @param track - Track to add
861
+ * @returns Dispatch result
862
+ */
863
+ addTrack(track) {
864
+ const result = dispatch(
865
+ this.history,
866
+ (state) => addTrack(state, track)
867
+ );
868
+ if (result.success) {
869
+ this.history = result.history;
870
+ this.notify();
871
+ }
872
+ return result;
873
+ }
874
+ /**
875
+ * Remove a track
876
+ *
877
+ * @param trackId - ID of the track to remove
878
+ * @returns Dispatch result
879
+ */
880
+ removeTrack(trackId) {
881
+ const result = dispatch(
882
+ this.history,
883
+ (state) => removeTrack(state, trackId)
884
+ );
885
+ if (result.success) {
886
+ this.history = result.history;
887
+ this.notify();
888
+ }
889
+ return result;
890
+ }
891
+ /**
892
+ * Move a track to a new position
893
+ *
894
+ * @param trackId - ID of the track to move
895
+ * @param newIndex - New index position
896
+ * @returns Dispatch result
897
+ */
898
+ moveTrack(trackId, newIndex) {
899
+ const result = dispatch(
900
+ this.history,
901
+ (state) => moveTrack(state, trackId, newIndex)
902
+ );
903
+ if (result.success) {
904
+ this.history = result.history;
905
+ this.notify();
906
+ }
907
+ return result;
908
+ }
909
+ /**
910
+ * Toggle track mute
911
+ *
912
+ * @param trackId - ID of the track
913
+ * @returns Dispatch result
914
+ */
915
+ toggleTrackMute(trackId) {
916
+ const result = dispatch(
917
+ this.history,
918
+ (state) => toggleTrackMute(state, trackId)
919
+ );
920
+ if (result.success) {
921
+ this.history = result.history;
922
+ this.notify();
923
+ }
924
+ return result;
925
+ }
926
+ /**
927
+ * Toggle track lock
928
+ *
929
+ * @param trackId - ID of the track
930
+ * @returns Dispatch result
931
+ */
932
+ toggleTrackLock(trackId) {
933
+ const result = dispatch(
934
+ this.history,
935
+ (state) => toggleTrackLock(state, trackId)
936
+ );
937
+ if (result.success) {
938
+ this.history = result.history;
939
+ this.notify();
940
+ }
941
+ return result;
942
+ }
943
+ // ===== TIMELINE OPERATIONS =====
944
+ /**
945
+ * Set timeline duration
946
+ *
947
+ * @param duration - New duration in frames
948
+ * @returns Dispatch result
949
+ */
950
+ setTimelineDuration(duration) {
951
+ const result = dispatch(
952
+ this.history,
953
+ (state) => setTimelineDuration(state, duration)
954
+ );
955
+ if (result.success) {
956
+ this.history = result.history;
957
+ this.notify();
958
+ }
959
+ return result;
960
+ }
961
+ /**
962
+ * Set timeline name
963
+ *
964
+ * @param name - New timeline name
965
+ * @returns Dispatch result
966
+ */
967
+ setTimelineName(name) {
968
+ const result = dispatch(
969
+ this.history,
970
+ (state) => setTimelineName(state, name)
971
+ );
972
+ if (result.success) {
973
+ this.history = result.history;
974
+ this.notify();
975
+ }
976
+ return result;
977
+ }
978
+ // ===== HISTORY OPERATIONS =====
979
+ /**
980
+ * Undo the last action
981
+ *
982
+ * @returns true if undo was performed
983
+ */
984
+ undo() {
985
+ if (!this.canUndo()) {
986
+ return false;
987
+ }
988
+ this.history = undo(this.history);
989
+ this.notify();
990
+ return true;
991
+ }
992
+ /**
993
+ * Redo the last undone action
994
+ *
995
+ * @returns true if redo was performed
996
+ */
997
+ redo() {
998
+ if (!this.canRedo()) {
999
+ return false;
1000
+ }
1001
+ this.history = redo(this.history);
1002
+ this.notify();
1003
+ return true;
1004
+ }
1005
+ /**
1006
+ * Check if undo is available
1007
+ *
1008
+ * @returns true if undo is available
1009
+ */
1010
+ canUndo() {
1011
+ return canUndo(this.history);
1012
+ }
1013
+ /**
1014
+ * Check if redo is available
1015
+ *
1016
+ * @returns true if redo is available
1017
+ */
1018
+ canRedo() {
1019
+ return canRedo(this.history);
1020
+ }
1021
+ // ===== QUERY OPERATIONS =====
1022
+ /**
1023
+ * Find a clip by ID
1024
+ *
1025
+ * @param clipId - Clip ID
1026
+ * @returns The clip, or undefined if not found
1027
+ */
1028
+ findClipById(clipId) {
1029
+ return findClipById(this.getState(), clipId);
1030
+ }
1031
+ /**
1032
+ * Find a track by ID
1033
+ *
1034
+ * @param trackId - Track ID
1035
+ * @returns The track, or undefined if not found
1036
+ */
1037
+ findTrackById(trackId) {
1038
+ return findTrackById(this.getState(), trackId);
1039
+ }
1040
+ /**
1041
+ * Get all clips on a track
1042
+ *
1043
+ * @param trackId - Track ID
1044
+ * @returns Array of clips on the track
1045
+ */
1046
+ getClipsOnTrack(trackId) {
1047
+ return getClipsOnTrack(this.getState(), trackId);
1048
+ }
1049
+ /**
1050
+ * Get all clips at a specific frame
1051
+ *
1052
+ * @param frame - Frame to check
1053
+ * @returns Array of clips at that frame
1054
+ */
1055
+ getClipsAtFrame(frame2) {
1056
+ return getClipsAtFrame(this.getState(), frame2);
1057
+ }
1058
+ /**
1059
+ * Get all clips in a frame range
1060
+ *
1061
+ * @param start - Start frame
1062
+ * @param end - End frame
1063
+ * @returns Array of clips in the range
1064
+ */
1065
+ getClipsInRange(start, end) {
1066
+ return getClipsInRange(this.getState(), start, end);
1067
+ }
1068
+ /**
1069
+ * Get all clips in the timeline
1070
+ *
1071
+ * @returns Array of all clips
1072
+ */
1073
+ getAllClips() {
1074
+ return getAllClips(this.getState());
1075
+ }
1076
+ /**
1077
+ * Get all tracks in the timeline
1078
+ *
1079
+ * @returns Array of all tracks
1080
+ */
1081
+ getAllTracks() {
1082
+ return getAllTracks(this.getState());
1083
+ }
1084
+ };
1085
+
1086
+ // src/types/timeline.ts
1087
+ function createTimeline(params) {
1088
+ const timeline = {
1089
+ id: params.id,
1090
+ name: params.name,
1091
+ fps: params.fps,
1092
+ duration: params.duration,
1093
+ tracks: params.tracks ?? []
1094
+ };
1095
+ if (params.metadata !== void 0) {
1096
+ timeline.metadata = params.metadata;
1097
+ }
1098
+ return timeline;
1099
+ }
1100
+
1101
+ // src/types/asset.ts
1102
+ function createAsset(params) {
1103
+ const asset = {
1104
+ id: params.id,
1105
+ type: params.type,
1106
+ duration: params.duration,
1107
+ sourceUrl: params.sourceUrl
1108
+ };
1109
+ if (params.metadata !== void 0) {
1110
+ asset.metadata = params.metadata;
1111
+ }
1112
+ return asset;
1113
+ }
1114
+
1115
+ // src/types/state.ts
1116
+ function createTimelineState(params) {
1117
+ const state = {
1118
+ timeline: params.timeline,
1119
+ assets: params.assets ?? /* @__PURE__ */ new Map(),
1120
+ linkGroups: params.linkGroups ?? /* @__PURE__ */ new Map(),
1121
+ groups: params.groups ?? /* @__PURE__ */ new Map(),
1122
+ markers: params.markers ?? {
1123
+ timeline: [],
1124
+ clips: [],
1125
+ regions: []
1126
+ }
1127
+ };
1128
+ if (params.workArea !== void 0) {
1129
+ state.workArea = params.workArea;
1130
+ }
1131
+ return state;
1132
+ }
1133
+
1134
+ // src/types/frame.ts
1135
+ function frame(value) {
1136
+ const rounded = Math.round(value);
1137
+ if (rounded < 0) {
1138
+ throw new Error(`Frame value must be non-negative, got: ${value}`);
1139
+ }
1140
+ return rounded;
1141
+ }
1142
+ function frameRate(value) {
1143
+ if (value <= 0) {
1144
+ throw new Error(`FrameRate must be positive, got: ${value}`);
1145
+ }
1146
+ return value;
1147
+ }
1148
+ function isValidFrame(value) {
1149
+ return Number.isInteger(value) && value >= 0;
1150
+ }
1151
+ function isValidFrameRate(value) {
1152
+ return value > 0;
1153
+ }
1154
+
1155
+ // src/utils/frame.ts
1156
+ function framesToSeconds(frames, fps) {
1157
+ return frames / fps;
1158
+ }
1159
+ function secondsToFrames(seconds, fps) {
1160
+ return frame(seconds * fps);
1161
+ }
1162
+ function framesToTimecode(frames, fps) {
1163
+ const totalFrames = frames;
1164
+ const framesPart = totalFrames % fps;
1165
+ const totalSeconds = Math.floor(totalFrames / fps);
1166
+ const secondsPart = totalSeconds % 60;
1167
+ const totalMinutes = Math.floor(totalSeconds / 60);
1168
+ const minutesPart = totalMinutes % 60;
1169
+ const hoursPart = Math.floor(totalMinutes / 60);
1170
+ return `${pad(hoursPart)}:${pad(minutesPart)}:${pad(secondsPart)}:${pad(framesPart)}`;
1171
+ }
1172
+ function framesToMinutesSeconds(frames, fps) {
1173
+ const totalSeconds = Math.floor(frames / fps);
1174
+ const minutes = Math.floor(totalSeconds / 60);
1175
+ const seconds = totalSeconds % 60;
1176
+ return `${minutes}:${pad(seconds)}`;
1177
+ }
1178
+ function clampFrame(value, min, max) {
1179
+ return frame(Math.max(min, Math.min(max, value)));
1180
+ }
1181
+ function addFrames(a, b) {
1182
+ return frame(a + b);
1183
+ }
1184
+ function subtractFrames(a, b) {
1185
+ return frame(Math.max(0, a - b));
1186
+ }
1187
+ function frameDuration(start, end) {
1188
+ return frame(end - start);
1189
+ }
1190
+ function pad(num, width = 2) {
1191
+ return num.toString().padStart(width, "0");
1192
+ }
1193
+
1194
+ export {
1195
+ createClip,
1196
+ getClipDuration,
1197
+ getClipMediaDuration,
1198
+ clipContainsFrame,
1199
+ clipsOverlap,
1200
+ validResult,
1201
+ invalidResult,
1202
+ invalidResults,
1203
+ combineResults,
1204
+ registerAsset,
1205
+ getAsset,
1206
+ hasAsset,
1207
+ getAllAssets,
1208
+ unregisterAsset,
1209
+ validateClip,
1210
+ validateTrack,
1211
+ validateTimeline,
1212
+ validateNoOverlap,
1213
+ findClipById,
1214
+ findTrackById,
1215
+ getClipsOnTrack,
1216
+ getClipsAtFrame,
1217
+ getClipsInRange,
1218
+ getAllClips,
1219
+ getAllTracks,
1220
+ findTrackIndex,
1221
+ createTrack,
1222
+ sortTrackClips,
1223
+ addClip,
1224
+ removeClip,
1225
+ moveClip,
1226
+ resizeClip,
1227
+ trimClip,
1228
+ updateClip,
1229
+ moveClipToTrack,
1230
+ addTrack,
1231
+ removeTrack,
1232
+ moveTrack,
1233
+ updateTrack,
1234
+ toggleTrackMute,
1235
+ toggleTrackLock,
1236
+ setTimelineDuration,
1237
+ setTimelineName,
1238
+ updateTimelineMetadata,
1239
+ TimelineEngine,
1240
+ createTimeline,
1241
+ createAsset,
1242
+ createTimelineState,
1243
+ frame,
1244
+ frameRate,
1245
+ isValidFrame,
1246
+ isValidFrameRate,
1247
+ framesToSeconds,
1248
+ secondsToFrames,
1249
+ framesToTimecode,
1250
+ framesToMinutesSeconds,
1251
+ clampFrame,
1252
+ addFrames,
1253
+ subtractFrames,
1254
+ frameDuration
1255
+ };