@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,1242 @@
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
+ throw new Error(`Clip not found: ${clipId}`);
437
+ }
438
+ const targetTrack = findTrackById(state, targetTrackId);
439
+ if (!targetTrack) {
440
+ throw new Error(`Target track not found: ${targetTrackId}`);
441
+ }
442
+ const validationResult = validateTrackTypeMatch(state, clip, targetTrack);
443
+ if (!validationResult.valid) {
444
+ const error = validationResult.errors[0];
445
+ throw new Error(error?.message || "Track type mismatch");
446
+ }
447
+ if (clip.trackId === targetTrackId) {
448
+ return state;
449
+ }
450
+ let newState = removeClip(state, clipId);
451
+ const updatedClip = { ...clip, trackId: targetTrackId };
452
+ newState = addClip(newState, targetTrackId, updatedClip);
453
+ return newState;
454
+ }
455
+
456
+ // src/operations/track-operations.ts
457
+ function addTrack(state, track) {
458
+ return {
459
+ ...state,
460
+ timeline: {
461
+ ...state.timeline,
462
+ tracks: [...state.timeline.tracks, track]
463
+ }
464
+ };
465
+ }
466
+ function removeTrack(state, trackId) {
467
+ return {
468
+ ...state,
469
+ timeline: {
470
+ ...state.timeline,
471
+ tracks: state.timeline.tracks.filter((t) => t.id !== trackId)
472
+ }
473
+ };
474
+ }
475
+ function moveTrack(state, trackId, newIndex) {
476
+ const currentIndex = findTrackIndex(state, trackId);
477
+ if (currentIndex === -1) {
478
+ return state;
479
+ }
480
+ const newTracks = [...state.timeline.tracks];
481
+ const [track] = newTracks.splice(currentIndex, 1);
482
+ if (!track) {
483
+ return state;
484
+ }
485
+ newTracks.splice(newIndex, 0, track);
486
+ return {
487
+ ...state,
488
+ timeline: {
489
+ ...state.timeline,
490
+ tracks: newTracks
491
+ }
492
+ };
493
+ }
494
+ function updateTrack(state, trackId, updates) {
495
+ const trackIndex = findTrackIndex(state, trackId);
496
+ if (trackIndex === -1) {
497
+ return state;
498
+ }
499
+ const newTracks = [...state.timeline.tracks];
500
+ const existingTrack = newTracks[trackIndex];
501
+ if (!existingTrack) {
502
+ return state;
503
+ }
504
+ newTracks[trackIndex] = {
505
+ ...existingTrack,
506
+ ...updates
507
+ };
508
+ return {
509
+ ...state,
510
+ timeline: {
511
+ ...state.timeline,
512
+ tracks: newTracks
513
+ }
514
+ };
515
+ }
516
+ function toggleTrackMute(state, trackId) {
517
+ const trackIndex = findTrackIndex(state, trackId);
518
+ if (trackIndex === -1) {
519
+ return state;
520
+ }
521
+ const track = state.timeline.tracks[trackIndex];
522
+ if (!track) {
523
+ return state;
524
+ }
525
+ return updateTrack(state, trackId, { muted: !track.muted });
526
+ }
527
+ function toggleTrackLock(state, trackId) {
528
+ const trackIndex = findTrackIndex(state, trackId);
529
+ if (trackIndex === -1) {
530
+ return state;
531
+ }
532
+ const track = state.timeline.tracks[trackIndex];
533
+ if (!track) {
534
+ return state;
535
+ }
536
+ return updateTrack(state, trackId, { locked: !track.locked });
537
+ }
538
+
539
+ // src/operations/timeline-operations.ts
540
+ function setTimelineDuration(state, duration) {
541
+ return {
542
+ ...state,
543
+ timeline: {
544
+ ...state.timeline,
545
+ duration
546
+ }
547
+ };
548
+ }
549
+ function setTimelineName(state, name) {
550
+ return {
551
+ ...state,
552
+ timeline: {
553
+ ...state.timeline,
554
+ name
555
+ }
556
+ };
557
+ }
558
+ function updateTimelineMetadata(state, metadata) {
559
+ return {
560
+ ...state,
561
+ timeline: {
562
+ ...state.timeline,
563
+ metadata: {
564
+ ...state.timeline.metadata,
565
+ ...metadata
566
+ }
567
+ }
568
+ };
569
+ }
570
+
571
+ // src/engine/history.ts
572
+ function createHistory(initialState, limit = 50) {
573
+ return {
574
+ past: [],
575
+ present: initialState,
576
+ future: [],
577
+ limit
578
+ };
579
+ }
580
+ function pushHistory(history, newState) {
581
+ const newPast = [...history.past, history.present];
582
+ if (newPast.length > history.limit) {
583
+ newPast.shift();
584
+ }
585
+ return {
586
+ ...history,
587
+ past: newPast,
588
+ present: newState,
589
+ future: []
590
+ // Clear future on new action
591
+ };
592
+ }
593
+ function undo(history) {
594
+ if (history.past.length === 0) {
595
+ return history;
596
+ }
597
+ const newPast = [...history.past];
598
+ const previous = newPast.pop();
599
+ return {
600
+ ...history,
601
+ past: newPast,
602
+ present: previous,
603
+ future: [history.present, ...history.future]
604
+ };
605
+ }
606
+ function redo(history) {
607
+ if (history.future.length === 0) {
608
+ return history;
609
+ }
610
+ const newFuture = [...history.future];
611
+ const next = newFuture.shift();
612
+ return {
613
+ ...history,
614
+ past: [...history.past, history.present],
615
+ present: next,
616
+ future: newFuture
617
+ };
618
+ }
619
+ function canUndo(history) {
620
+ return history.past.length > 0;
621
+ }
622
+ function canRedo(history) {
623
+ return history.future.length > 0;
624
+ }
625
+ function getCurrentState(history) {
626
+ return history.present;
627
+ }
628
+
629
+ // src/engine/dispatcher.ts
630
+ function dispatch(history, operation) {
631
+ const currentState = getCurrentState(history);
632
+ const newState = operation(currentState);
633
+ const validationResult = validateTimeline(newState);
634
+ if (!validationResult.valid) {
635
+ return {
636
+ success: false,
637
+ history,
638
+ // Return unchanged history
639
+ errors: validationResult.errors
640
+ };
641
+ }
642
+ const newHistory = pushHistory(history, newState);
643
+ return {
644
+ success: true,
645
+ history: newHistory
646
+ };
647
+ }
648
+
649
+ // src/engine/timeline-engine.ts
650
+ var TimelineEngine = class {
651
+ history;
652
+ listeners = /* @__PURE__ */ new Set();
653
+ /**
654
+ * Create a new timeline engine
655
+ *
656
+ * @param initialState - Initial timeline state
657
+ * @param historyLimit - Maximum number of undo steps (default: 50)
658
+ */
659
+ constructor(initialState, historyLimit = 50) {
660
+ this.history = createHistory(initialState, historyLimit);
661
+ }
662
+ // ===== SUBSCRIPTION =====
663
+ /**
664
+ * Subscribe to state changes
665
+ *
666
+ * The listener will be called whenever the timeline state changes.
667
+ * This is used by framework adapters (e.g., React) to trigger re-renders.
668
+ *
669
+ * @param listener - Function to call on state changes
670
+ * @returns Unsubscribe function
671
+ *
672
+ * @example
673
+ * ```typescript
674
+ * const unsubscribe = engine.subscribe(() => {
675
+ * console.log('State changed:', engine.getState());
676
+ * });
677
+ *
678
+ * // Later...
679
+ * unsubscribe();
680
+ * ```
681
+ */
682
+ subscribe(listener) {
683
+ this.listeners.add(listener);
684
+ return () => {
685
+ this.listeners.delete(listener);
686
+ };
687
+ }
688
+ /**
689
+ * Notify all subscribers of a state change
690
+ *
691
+ * This is called internally after any operation that modifies state.
692
+ * Framework adapters use this to trigger re-renders.
693
+ */
694
+ notify() {
695
+ this.listeners.forEach((listener) => listener());
696
+ }
697
+ // ===== STATE ACCESS =====
698
+ /**
699
+ * Get the current timeline state
700
+ *
701
+ * @returns Current timeline state
702
+ */
703
+ getState() {
704
+ return getCurrentState(this.history);
705
+ }
706
+ // ===== ASSET OPERATIONS =====
707
+ /**
708
+ * Register an asset
709
+ *
710
+ * @param asset - Asset to register
711
+ * @returns Dispatch result
712
+ */
713
+ registerAsset(asset) {
714
+ const result = dispatch(
715
+ this.history,
716
+ (state) => registerAsset(state, asset)
717
+ );
718
+ if (result.success) {
719
+ this.history = result.history;
720
+ this.notify();
721
+ }
722
+ return result;
723
+ }
724
+ /**
725
+ * Get an asset by ID
726
+ *
727
+ * @param assetId - Asset ID
728
+ * @returns The asset, or undefined if not found
729
+ */
730
+ getAsset(assetId) {
731
+ return getAsset(this.getState(), assetId);
732
+ }
733
+ // ===== CLIP OPERATIONS =====
734
+ /**
735
+ * Add a clip to a track
736
+ *
737
+ * @param trackId - ID of the track to add to
738
+ * @param clip - Clip to add
739
+ * @returns Dispatch result
740
+ */
741
+ addClip(trackId, clip) {
742
+ const result = dispatch(
743
+ this.history,
744
+ (state) => addClip(state, trackId, clip)
745
+ );
746
+ if (result.success) {
747
+ this.history = result.history;
748
+ this.notify();
749
+ }
750
+ return result;
751
+ }
752
+ /**
753
+ * Remove a clip
754
+ *
755
+ * @param clipId - ID of the clip to remove
756
+ * @returns Dispatch result
757
+ */
758
+ removeClip(clipId) {
759
+ const result = dispatch(
760
+ this.history,
761
+ (state) => removeClip(state, clipId)
762
+ );
763
+ if (result.success) {
764
+ this.history = result.history;
765
+ this.notify();
766
+ }
767
+ return result;
768
+ }
769
+ /**
770
+ * Move a clip to a new timeline position
771
+ *
772
+ * @param clipId - ID of the clip to move
773
+ * @param newStart - New timeline start frame
774
+ * @returns Dispatch result
775
+ */
776
+ moveClip(clipId, newStart) {
777
+ const result = dispatch(
778
+ this.history,
779
+ (state) => moveClip(state, clipId, newStart)
780
+ );
781
+ if (result.success) {
782
+ this.history = result.history;
783
+ this.notify();
784
+ }
785
+ return result;
786
+ }
787
+ /**
788
+ * Resize a clip
789
+ *
790
+ * @param clipId - ID of the clip to resize
791
+ * @param newStart - New timeline start frame
792
+ * @param newEnd - New timeline end frame
793
+ * @returns Dispatch result
794
+ */
795
+ resizeClip(clipId, newStart, newEnd) {
796
+ const result = dispatch(
797
+ this.history,
798
+ (state) => resizeClip(state, clipId, newStart, newEnd)
799
+ );
800
+ if (result.success) {
801
+ this.history = result.history;
802
+ this.notify();
803
+ }
804
+ return result;
805
+ }
806
+ /**
807
+ * Trim a clip (change media bounds)
808
+ *
809
+ * @param clipId - ID of the clip to trim
810
+ * @param newMediaIn - New media in frame
811
+ * @param newMediaOut - New media out frame
812
+ * @returns Dispatch result
813
+ */
814
+ trimClip(clipId, newMediaIn, newMediaOut) {
815
+ const result = dispatch(
816
+ this.history,
817
+ (state) => trimClip(state, clipId, newMediaIn, newMediaOut)
818
+ );
819
+ if (result.success) {
820
+ this.history = result.history;
821
+ this.notify();
822
+ }
823
+ return result;
824
+ }
825
+ /**
826
+ * Move a clip to a different track
827
+ *
828
+ * @param clipId - ID of the clip to move
829
+ * @param targetTrackId - ID of the target track
830
+ * @returns Dispatch result
831
+ */
832
+ moveClipToTrack(clipId, targetTrackId) {
833
+ const result = dispatch(
834
+ this.history,
835
+ (state) => moveClipToTrack(state, clipId, targetTrackId)
836
+ );
837
+ if (result.success) {
838
+ this.history = result.history;
839
+ this.notify();
840
+ }
841
+ return result;
842
+ }
843
+ // ===== TRACK OPERATIONS =====
844
+ /**
845
+ * Add a track
846
+ *
847
+ * @param track - Track to add
848
+ * @returns Dispatch result
849
+ */
850
+ addTrack(track) {
851
+ const result = dispatch(
852
+ this.history,
853
+ (state) => addTrack(state, track)
854
+ );
855
+ if (result.success) {
856
+ this.history = result.history;
857
+ this.notify();
858
+ }
859
+ return result;
860
+ }
861
+ /**
862
+ * Remove a track
863
+ *
864
+ * @param trackId - ID of the track to remove
865
+ * @returns Dispatch result
866
+ */
867
+ removeTrack(trackId) {
868
+ const result = dispatch(
869
+ this.history,
870
+ (state) => removeTrack(state, trackId)
871
+ );
872
+ if (result.success) {
873
+ this.history = result.history;
874
+ this.notify();
875
+ }
876
+ return result;
877
+ }
878
+ /**
879
+ * Move a track to a new position
880
+ *
881
+ * @param trackId - ID of the track to move
882
+ * @param newIndex - New index position
883
+ * @returns Dispatch result
884
+ */
885
+ moveTrack(trackId, newIndex) {
886
+ const result = dispatch(
887
+ this.history,
888
+ (state) => moveTrack(state, trackId, newIndex)
889
+ );
890
+ if (result.success) {
891
+ this.history = result.history;
892
+ this.notify();
893
+ }
894
+ return result;
895
+ }
896
+ /**
897
+ * Toggle track mute
898
+ *
899
+ * @param trackId - ID of the track
900
+ * @returns Dispatch result
901
+ */
902
+ toggleTrackMute(trackId) {
903
+ const result = dispatch(
904
+ this.history,
905
+ (state) => toggleTrackMute(state, trackId)
906
+ );
907
+ if (result.success) {
908
+ this.history = result.history;
909
+ this.notify();
910
+ }
911
+ return result;
912
+ }
913
+ /**
914
+ * Toggle track lock
915
+ *
916
+ * @param trackId - ID of the track
917
+ * @returns Dispatch result
918
+ */
919
+ toggleTrackLock(trackId) {
920
+ const result = dispatch(
921
+ this.history,
922
+ (state) => toggleTrackLock(state, trackId)
923
+ );
924
+ if (result.success) {
925
+ this.history = result.history;
926
+ this.notify();
927
+ }
928
+ return result;
929
+ }
930
+ // ===== TIMELINE OPERATIONS =====
931
+ /**
932
+ * Set timeline duration
933
+ *
934
+ * @param duration - New duration in frames
935
+ * @returns Dispatch result
936
+ */
937
+ setTimelineDuration(duration) {
938
+ const result = dispatch(
939
+ this.history,
940
+ (state) => setTimelineDuration(state, duration)
941
+ );
942
+ if (result.success) {
943
+ this.history = result.history;
944
+ this.notify();
945
+ }
946
+ return result;
947
+ }
948
+ /**
949
+ * Set timeline name
950
+ *
951
+ * @param name - New timeline name
952
+ * @returns Dispatch result
953
+ */
954
+ setTimelineName(name) {
955
+ const result = dispatch(
956
+ this.history,
957
+ (state) => setTimelineName(state, name)
958
+ );
959
+ if (result.success) {
960
+ this.history = result.history;
961
+ this.notify();
962
+ }
963
+ return result;
964
+ }
965
+ // ===== HISTORY OPERATIONS =====
966
+ /**
967
+ * Undo the last action
968
+ *
969
+ * @returns true if undo was performed
970
+ */
971
+ undo() {
972
+ if (!this.canUndo()) {
973
+ return false;
974
+ }
975
+ this.history = undo(this.history);
976
+ this.notify();
977
+ return true;
978
+ }
979
+ /**
980
+ * Redo the last undone action
981
+ *
982
+ * @returns true if redo was performed
983
+ */
984
+ redo() {
985
+ if (!this.canRedo()) {
986
+ return false;
987
+ }
988
+ this.history = redo(this.history);
989
+ this.notify();
990
+ return true;
991
+ }
992
+ /**
993
+ * Check if undo is available
994
+ *
995
+ * @returns true if undo is available
996
+ */
997
+ canUndo() {
998
+ return canUndo(this.history);
999
+ }
1000
+ /**
1001
+ * Check if redo is available
1002
+ *
1003
+ * @returns true if redo is available
1004
+ */
1005
+ canRedo() {
1006
+ return canRedo(this.history);
1007
+ }
1008
+ // ===== QUERY OPERATIONS =====
1009
+ /**
1010
+ * Find a clip by ID
1011
+ *
1012
+ * @param clipId - Clip ID
1013
+ * @returns The clip, or undefined if not found
1014
+ */
1015
+ findClipById(clipId) {
1016
+ return findClipById(this.getState(), clipId);
1017
+ }
1018
+ /**
1019
+ * Find a track by ID
1020
+ *
1021
+ * @param trackId - Track ID
1022
+ * @returns The track, or undefined if not found
1023
+ */
1024
+ findTrackById(trackId) {
1025
+ return findTrackById(this.getState(), trackId);
1026
+ }
1027
+ /**
1028
+ * Get all clips on a track
1029
+ *
1030
+ * @param trackId - Track ID
1031
+ * @returns Array of clips on the track
1032
+ */
1033
+ getClipsOnTrack(trackId) {
1034
+ return getClipsOnTrack(this.getState(), trackId);
1035
+ }
1036
+ /**
1037
+ * Get all clips at a specific frame
1038
+ *
1039
+ * @param frame - Frame to check
1040
+ * @returns Array of clips at that frame
1041
+ */
1042
+ getClipsAtFrame(frame2) {
1043
+ return getClipsAtFrame(this.getState(), frame2);
1044
+ }
1045
+ /**
1046
+ * Get all clips in a frame range
1047
+ *
1048
+ * @param start - Start frame
1049
+ * @param end - End frame
1050
+ * @returns Array of clips in the range
1051
+ */
1052
+ getClipsInRange(start, end) {
1053
+ return getClipsInRange(this.getState(), start, end);
1054
+ }
1055
+ /**
1056
+ * Get all clips in the timeline
1057
+ *
1058
+ * @returns Array of all clips
1059
+ */
1060
+ getAllClips() {
1061
+ return getAllClips(this.getState());
1062
+ }
1063
+ /**
1064
+ * Get all tracks in the timeline
1065
+ *
1066
+ * @returns Array of all tracks
1067
+ */
1068
+ getAllTracks() {
1069
+ return getAllTracks(this.getState());
1070
+ }
1071
+ };
1072
+
1073
+ // src/types/timeline.ts
1074
+ function createTimeline(params) {
1075
+ const timeline = {
1076
+ id: params.id,
1077
+ name: params.name,
1078
+ fps: params.fps,
1079
+ duration: params.duration,
1080
+ tracks: params.tracks ?? []
1081
+ };
1082
+ if (params.metadata !== void 0) {
1083
+ timeline.metadata = params.metadata;
1084
+ }
1085
+ return timeline;
1086
+ }
1087
+
1088
+ // src/types/asset.ts
1089
+ function createAsset(params) {
1090
+ const asset = {
1091
+ id: params.id,
1092
+ type: params.type,
1093
+ duration: params.duration,
1094
+ sourceUrl: params.sourceUrl
1095
+ };
1096
+ if (params.metadata !== void 0) {
1097
+ asset.metadata = params.metadata;
1098
+ }
1099
+ return asset;
1100
+ }
1101
+
1102
+ // src/types/state.ts
1103
+ function createTimelineState(params) {
1104
+ const state = {
1105
+ timeline: params.timeline,
1106
+ assets: params.assets ?? /* @__PURE__ */ new Map(),
1107
+ linkGroups: params.linkGroups ?? /* @__PURE__ */ new Map(),
1108
+ groups: params.groups ?? /* @__PURE__ */ new Map(),
1109
+ markers: params.markers ?? {
1110
+ timeline: [],
1111
+ clips: [],
1112
+ regions: []
1113
+ }
1114
+ };
1115
+ if (params.workArea !== void 0) {
1116
+ state.workArea = params.workArea;
1117
+ }
1118
+ return state;
1119
+ }
1120
+
1121
+ // src/types/frame.ts
1122
+ function frame(value) {
1123
+ const rounded = Math.round(value);
1124
+ if (rounded < 0) {
1125
+ throw new Error(`Frame value must be non-negative, got: ${value}`);
1126
+ }
1127
+ return rounded;
1128
+ }
1129
+ function frameRate(value) {
1130
+ if (value <= 0) {
1131
+ throw new Error(`FrameRate must be positive, got: ${value}`);
1132
+ }
1133
+ return value;
1134
+ }
1135
+ function isValidFrame(value) {
1136
+ return Number.isInteger(value) && value >= 0;
1137
+ }
1138
+ function isValidFrameRate(value) {
1139
+ return value > 0;
1140
+ }
1141
+
1142
+ // src/utils/frame.ts
1143
+ function framesToSeconds(frames, fps) {
1144
+ return frames / fps;
1145
+ }
1146
+ function secondsToFrames(seconds, fps) {
1147
+ return frame(seconds * fps);
1148
+ }
1149
+ function framesToTimecode(frames, fps) {
1150
+ const totalFrames = frames;
1151
+ const framesPart = totalFrames % fps;
1152
+ const totalSeconds = Math.floor(totalFrames / fps);
1153
+ const secondsPart = totalSeconds % 60;
1154
+ const totalMinutes = Math.floor(totalSeconds / 60);
1155
+ const minutesPart = totalMinutes % 60;
1156
+ const hoursPart = Math.floor(totalMinutes / 60);
1157
+ return `${pad(hoursPart)}:${pad(minutesPart)}:${pad(secondsPart)}:${pad(framesPart)}`;
1158
+ }
1159
+ function framesToMinutesSeconds(frames, fps) {
1160
+ const totalSeconds = Math.floor(frames / fps);
1161
+ const minutes = Math.floor(totalSeconds / 60);
1162
+ const seconds = totalSeconds % 60;
1163
+ return `${minutes}:${pad(seconds)}`;
1164
+ }
1165
+ function clampFrame(value, min, max) {
1166
+ return frame(Math.max(min, Math.min(max, value)));
1167
+ }
1168
+ function addFrames(a, b) {
1169
+ return frame(a + b);
1170
+ }
1171
+ function subtractFrames(a, b) {
1172
+ return frame(Math.max(0, a - b));
1173
+ }
1174
+ function frameDuration(start, end) {
1175
+ return frame(end - start);
1176
+ }
1177
+ function pad(num, width = 2) {
1178
+ return num.toString().padStart(width, "0");
1179
+ }
1180
+
1181
+ export {
1182
+ createClip,
1183
+ getClipDuration,
1184
+ getClipMediaDuration,
1185
+ clipContainsFrame,
1186
+ clipsOverlap,
1187
+ validResult,
1188
+ invalidResult,
1189
+ invalidResults,
1190
+ combineResults,
1191
+ registerAsset,
1192
+ getAsset,
1193
+ hasAsset,
1194
+ getAllAssets,
1195
+ unregisterAsset,
1196
+ validateClip,
1197
+ validateTrack,
1198
+ validateTimeline,
1199
+ validateNoOverlap,
1200
+ findClipById,
1201
+ findTrackById,
1202
+ getClipsOnTrack,
1203
+ getClipsAtFrame,
1204
+ getClipsInRange,
1205
+ getAllClips,
1206
+ getAllTracks,
1207
+ findTrackIndex,
1208
+ createTrack,
1209
+ sortTrackClips,
1210
+ addClip,
1211
+ removeClip,
1212
+ moveClip,
1213
+ resizeClip,
1214
+ trimClip,
1215
+ updateClip,
1216
+ moveClipToTrack,
1217
+ addTrack,
1218
+ removeTrack,
1219
+ moveTrack,
1220
+ updateTrack,
1221
+ toggleTrackMute,
1222
+ toggleTrackLock,
1223
+ setTimelineDuration,
1224
+ setTimelineName,
1225
+ updateTimelineMetadata,
1226
+ TimelineEngine,
1227
+ createTimeline,
1228
+ createAsset,
1229
+ createTimelineState,
1230
+ frame,
1231
+ frameRate,
1232
+ isValidFrame,
1233
+ isValidFrameRate,
1234
+ framesToSeconds,
1235
+ secondsToFrames,
1236
+ framesToTimecode,
1237
+ framesToMinutesSeconds,
1238
+ clampFrame,
1239
+ addFrames,
1240
+ subtractFrames,
1241
+ frameDuration
1242
+ };