@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.
- package/CHANGELOG.md +36 -0
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/dist/chunk-27XCNVPR.js +5969 -0
- package/dist/chunk-6PDBJDHM.js +2263 -0
- package/dist/chunk-BWPS6NQT.js +7465 -0
- package/dist/chunk-FBOYSUYV.js +1280 -0
- package/dist/chunk-FR632TZX.js +1870 -0
- package/dist/chunk-HW4Z7YLJ.js +1242 -0
- package/dist/chunk-HWW62IFH.js +5424 -0
- package/dist/chunk-I2GZXRH4.js +4790 -0
- package/dist/chunk-JQZE3OK4.js +1255 -0
- package/dist/chunk-KF7JNK2F.js +1864 -0
- package/dist/chunk-KR3P2DYK.js +5655 -0
- package/dist/chunk-MO5DSFSW.js +2214 -0
- package/dist/chunk-MQAW33RJ.js +5530 -0
- package/dist/chunk-N4WUWZZX.js +2833 -0
- package/dist/chunk-NRJV7I4C.js +1331 -0
- package/dist/chunk-NXG52532.js +2230 -0
- package/dist/chunk-PVXF67CN.js +1278 -0
- package/dist/chunk-QSB6DHIF.js +5429 -0
- package/dist/chunk-QYWJT7HR.js +5837 -0
- package/dist/chunk-SWBRCMW7.js +7466 -0
- package/dist/chunk-TAT3ULSV.js +2214 -0
- package/dist/chunk-TTDP5JUM.js +2228 -0
- package/dist/chunk-UAGP4VPG.js +1739 -0
- package/dist/chunk-WIG6SY7A.js +1183 -0
- package/dist/chunk-YJ2K5N2R.js +6187 -0
- package/dist/index-3Lr_vKBd.d.cts +2810 -0
- package/dist/index-3Lr_vKBd.d.ts +2810 -0
- package/dist/index-7IPJn1yM.d.cts +1146 -0
- package/dist/index-7IPJn1yM.d.ts +1146 -0
- package/dist/index-B0xOv0V0.d.cts +3259 -0
- package/dist/index-B0xOv0V0.d.ts +3259 -0
- package/dist/index-B2m3zwg7.d.cts +1381 -0
- package/dist/index-B2m3zwg7.d.ts +1381 -0
- package/dist/index-B3sUrU_X.d.cts +1249 -0
- package/dist/index-B3sUrU_X.d.ts +1249 -0
- package/dist/index-B6wla7ZJ.d.cts +2751 -0
- package/dist/index-B6wla7ZJ.d.ts +2751 -0
- package/dist/index-BIv8RWWT.d.cts +1574 -0
- package/dist/index-BIv8RWWT.d.ts +1574 -0
- package/dist/index-BJv6hDHL.d.cts +3255 -0
- package/dist/index-BJv6hDHL.d.ts +3255 -0
- package/dist/index-BUCimS2e.d.cts +1393 -0
- package/dist/index-BUCimS2e.d.ts +1393 -0
- package/dist/index-Bw_nvNcG.d.cts +1275 -0
- package/dist/index-Bw_nvNcG.d.ts +1275 -0
- package/dist/index-ByG0gOtd.d.cts +1167 -0
- package/dist/index-ByG0gOtd.d.ts +1167 -0
- package/dist/index-CDGd2XXv.d.cts +2492 -0
- package/dist/index-CDGd2XXv.d.ts +2492 -0
- package/dist/index-CznAVeJ6.d.cts +1145 -0
- package/dist/index-CznAVeJ6.d.ts +1145 -0
- package/dist/index-DQD9IMh7.d.cts +2534 -0
- package/dist/index-DQD9IMh7.d.ts +2534 -0
- package/dist/index-Dl3qtJEI.d.cts +2178 -0
- package/dist/index-Dl3qtJEI.d.ts +2178 -0
- package/dist/index-DnE2A-Nz.d.cts +2603 -0
- package/dist/index-DnE2A-Nz.d.ts +2603 -0
- package/dist/index-DrOA6QmW.d.cts +2492 -0
- package/dist/index-DrOA6QmW.d.ts +2492 -0
- package/dist/index-Vpa3rPEM.d.cts +1402 -0
- package/dist/index-Vpa3rPEM.d.ts +1402 -0
- package/dist/index-jP6BomSd.d.cts +2640 -0
- package/dist/index-jP6BomSd.d.ts +2640 -0
- package/dist/index-wiGRwVyY.d.cts +3259 -0
- package/dist/index-wiGRwVyY.d.ts +3259 -0
- package/dist/index.cjs +7386 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +263 -0
- package/dist/internal.cjs +7721 -0
- package/dist/internal.d.cts +704 -0
- package/dist/internal.d.ts +704 -0
- package/dist/internal.js +405 -0
- package/package.json +58 -0
|
@@ -0,0 +1,2230 @@
|
|
|
1
|
+
// src/types/timeline.ts
|
|
2
|
+
var DEFAULT_SEQUENCE_SETTINGS = {
|
|
3
|
+
pixelAspectRatio: 1,
|
|
4
|
+
fieldOrder: "progressive",
|
|
5
|
+
colorSpace: "sRGB",
|
|
6
|
+
audioSampleRate: 48e3,
|
|
7
|
+
audioChannelCount: 2
|
|
8
|
+
};
|
|
9
|
+
function createTimeline(params) {
|
|
10
|
+
return {
|
|
11
|
+
id: params.id,
|
|
12
|
+
name: params.name,
|
|
13
|
+
fps: params.fps,
|
|
14
|
+
duration: params.duration,
|
|
15
|
+
startTimecode: params.startTimecode ?? "00:00:00:00",
|
|
16
|
+
tracks: params.tracks ?? [],
|
|
17
|
+
sequenceSettings: { ...DEFAULT_SEQUENCE_SETTINGS, ...params.sequenceSettings },
|
|
18
|
+
version: 0
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/types/track.ts
|
|
23
|
+
var toTrackId = (s) => s;
|
|
24
|
+
function createTrack(params) {
|
|
25
|
+
return {
|
|
26
|
+
id: params.id,
|
|
27
|
+
name: params.name,
|
|
28
|
+
type: params.type,
|
|
29
|
+
clips: params.clips ?? [],
|
|
30
|
+
locked: params.locked ?? false,
|
|
31
|
+
muted: params.muted ?? false,
|
|
32
|
+
solo: params.solo ?? false,
|
|
33
|
+
height: params.height ?? 56
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function sortTrackClips(track) {
|
|
37
|
+
return {
|
|
38
|
+
...track,
|
|
39
|
+
clips: [...track.clips].sort((a, b) => a.timelineStart - b.timelineStart)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/types/clip.ts
|
|
44
|
+
var toClipId = (s) => s;
|
|
45
|
+
function createClip(params) {
|
|
46
|
+
return {
|
|
47
|
+
id: params.id,
|
|
48
|
+
assetId: params.assetId,
|
|
49
|
+
trackId: params.trackId,
|
|
50
|
+
timelineStart: params.timelineStart,
|
|
51
|
+
timelineEnd: params.timelineEnd,
|
|
52
|
+
mediaIn: params.mediaIn,
|
|
53
|
+
mediaOut: params.mediaOut,
|
|
54
|
+
speed: params.speed ?? 1,
|
|
55
|
+
enabled: params.enabled ?? true,
|
|
56
|
+
reversed: params.reversed ?? false,
|
|
57
|
+
name: params.name ?? null,
|
|
58
|
+
color: params.color ?? null,
|
|
59
|
+
metadata: params.metadata ?? {}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function getClipDuration(clip) {
|
|
63
|
+
return clip.timelineEnd - clip.timelineStart;
|
|
64
|
+
}
|
|
65
|
+
function getClipMediaDuration(clip) {
|
|
66
|
+
return clip.mediaOut - clip.mediaIn;
|
|
67
|
+
}
|
|
68
|
+
function clipContainsFrame(clip, f) {
|
|
69
|
+
return f >= clip.timelineStart && f < clip.timelineEnd;
|
|
70
|
+
}
|
|
71
|
+
function clipsOverlap(a, b) {
|
|
72
|
+
return a.timelineStart < b.timelineEnd && b.timelineStart < a.timelineEnd;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/types/asset.ts
|
|
76
|
+
var toAssetId = (s) => s;
|
|
77
|
+
function createAsset(params) {
|
|
78
|
+
return {
|
|
79
|
+
id: params.id,
|
|
80
|
+
name: params.name,
|
|
81
|
+
mediaType: params.mediaType,
|
|
82
|
+
filePath: params.filePath,
|
|
83
|
+
intrinsicDuration: params.intrinsicDuration,
|
|
84
|
+
nativeFps: params.nativeFps,
|
|
85
|
+
sourceTimecodeOffset: params.sourceTimecodeOffset,
|
|
86
|
+
status: params.status ?? "online"
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/types/state.ts
|
|
91
|
+
var CURRENT_SCHEMA_VERSION = 1;
|
|
92
|
+
function createTimelineState(params) {
|
|
93
|
+
const registry = params.assetRegistry ?? (params.assets ? params.assets : /* @__PURE__ */ new Map());
|
|
94
|
+
return {
|
|
95
|
+
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
96
|
+
timeline: params.timeline,
|
|
97
|
+
assetRegistry: registry
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/types/frame.ts
|
|
102
|
+
var toFrame = (n) => n;
|
|
103
|
+
function frame(value) {
|
|
104
|
+
const rounded = Math.round(value);
|
|
105
|
+
if (rounded < 0) {
|
|
106
|
+
throw new Error(`TimelineFrame must be non-negative, got: ${value}`);
|
|
107
|
+
}
|
|
108
|
+
return rounded;
|
|
109
|
+
}
|
|
110
|
+
var FrameRates = {
|
|
111
|
+
CINEMA: 24,
|
|
112
|
+
PAL: 25,
|
|
113
|
+
NTSC_DF: 29.97,
|
|
114
|
+
NTSC: 30,
|
|
115
|
+
PAL_HFR: 50,
|
|
116
|
+
NTSC_HFR: 59.94,
|
|
117
|
+
HFR: 60
|
|
118
|
+
};
|
|
119
|
+
function frameRate(value) {
|
|
120
|
+
const valid = [23.976, 24, 25, 29.97, 30, 50, 59.94, 60];
|
|
121
|
+
if (!valid.includes(value)) {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`FrameRate must be one of ${valid.join(", ")}, got: ${value}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
var toTimecode = (s) => s;
|
|
129
|
+
function isValidFrame(value) {
|
|
130
|
+
return Number.isInteger(value) && value >= 0;
|
|
131
|
+
}
|
|
132
|
+
function isDropFrame(fps) {
|
|
133
|
+
return fps === 29.97 || fps === 59.94;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/utils/frame.ts
|
|
137
|
+
function framesToSeconds(frames, fps) {
|
|
138
|
+
return frames / fps;
|
|
139
|
+
}
|
|
140
|
+
function secondsToFrames(seconds, fps) {
|
|
141
|
+
return toFrame(seconds * fps);
|
|
142
|
+
}
|
|
143
|
+
function framesToTimecode(frames, fps) {
|
|
144
|
+
const totalFrames = frames;
|
|
145
|
+
const framesPart = totalFrames % fps;
|
|
146
|
+
const totalSeconds = Math.floor(totalFrames / fps);
|
|
147
|
+
const secondsPart = totalSeconds % 60;
|
|
148
|
+
const totalMinutes = Math.floor(totalSeconds / 60);
|
|
149
|
+
const minutesPart = totalMinutes % 60;
|
|
150
|
+
const hoursPart = Math.floor(totalMinutes / 60);
|
|
151
|
+
return `${pad(hoursPart)}:${pad(minutesPart)}:${pad(secondsPart)}:${pad(framesPart)}`;
|
|
152
|
+
}
|
|
153
|
+
function framesToMinutesSeconds(frames, fps) {
|
|
154
|
+
const totalSeconds = Math.floor(frames / fps);
|
|
155
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
156
|
+
const seconds = totalSeconds % 60;
|
|
157
|
+
return `${minutes}:${pad(seconds)}`;
|
|
158
|
+
}
|
|
159
|
+
function clampFrame(value, min, max) {
|
|
160
|
+
return toFrame(Math.max(min, Math.min(max, value)));
|
|
161
|
+
}
|
|
162
|
+
function addFrames(a, b) {
|
|
163
|
+
return toFrame(a + b);
|
|
164
|
+
}
|
|
165
|
+
function subtractFrames(a, b) {
|
|
166
|
+
return toFrame(Math.max(0, a - b));
|
|
167
|
+
}
|
|
168
|
+
function frameDuration(start, end) {
|
|
169
|
+
return toFrame(end - start);
|
|
170
|
+
}
|
|
171
|
+
function pad(num, width = 2) {
|
|
172
|
+
return num.toString().padStart(width, "0");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/engine/history.ts
|
|
176
|
+
function createHistory(initialState, limit = 50) {
|
|
177
|
+
return {
|
|
178
|
+
past: [],
|
|
179
|
+
present: initialState,
|
|
180
|
+
future: [],
|
|
181
|
+
limit
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
function pushHistory(history, newState) {
|
|
185
|
+
const newPast = [...history.past, history.present];
|
|
186
|
+
if (newPast.length > history.limit) {
|
|
187
|
+
newPast.shift();
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
...history,
|
|
191
|
+
past: newPast,
|
|
192
|
+
present: newState,
|
|
193
|
+
future: []
|
|
194
|
+
// Clear future on new action
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function undo(history) {
|
|
198
|
+
if (history.past.length === 0) {
|
|
199
|
+
return history;
|
|
200
|
+
}
|
|
201
|
+
const newPast = [...history.past];
|
|
202
|
+
const previous = newPast.pop();
|
|
203
|
+
return {
|
|
204
|
+
...history,
|
|
205
|
+
past: newPast,
|
|
206
|
+
present: previous,
|
|
207
|
+
future: [history.present, ...history.future]
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function redo(history) {
|
|
211
|
+
if (history.future.length === 0) {
|
|
212
|
+
return history;
|
|
213
|
+
}
|
|
214
|
+
const newFuture = [...history.future];
|
|
215
|
+
const next = newFuture.shift();
|
|
216
|
+
return {
|
|
217
|
+
...history,
|
|
218
|
+
past: [...history.past, history.present],
|
|
219
|
+
present: next,
|
|
220
|
+
future: newFuture
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function canUndo(history) {
|
|
224
|
+
return history.past.length > 0;
|
|
225
|
+
}
|
|
226
|
+
function canRedo(history) {
|
|
227
|
+
return history.future.length > 0;
|
|
228
|
+
}
|
|
229
|
+
function getCurrentState(history) {
|
|
230
|
+
return history.present;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/systems/queries.ts
|
|
234
|
+
function findClipById(state, clipId) {
|
|
235
|
+
for (const track of state.timeline.tracks) {
|
|
236
|
+
const clip = track.clips.find((c) => c.id === clipId);
|
|
237
|
+
if (clip) {
|
|
238
|
+
return clip;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return void 0;
|
|
242
|
+
}
|
|
243
|
+
function findTrackById(state, trackId) {
|
|
244
|
+
return state.timeline.tracks.find((t) => t.id === trackId);
|
|
245
|
+
}
|
|
246
|
+
function getClipsOnTrack(state, trackId) {
|
|
247
|
+
const track = findTrackById(state, trackId);
|
|
248
|
+
return track ? Array.from(track.clips) : [];
|
|
249
|
+
}
|
|
250
|
+
function getClipsAtFrame(state, frame2) {
|
|
251
|
+
const clips = [];
|
|
252
|
+
for (const track of state.timeline.tracks) {
|
|
253
|
+
for (const clip of track.clips) {
|
|
254
|
+
if (clipContainsFrame(clip, frame2)) {
|
|
255
|
+
clips.push(clip);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return clips;
|
|
260
|
+
}
|
|
261
|
+
function getClipsInRange(state, start, end) {
|
|
262
|
+
const clips = [];
|
|
263
|
+
for (const track of state.timeline.tracks) {
|
|
264
|
+
for (const clip of track.clips) {
|
|
265
|
+
if (clip.timelineStart < end && clip.timelineEnd > start) {
|
|
266
|
+
clips.push(clip);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return clips;
|
|
271
|
+
}
|
|
272
|
+
function getAllClips(state) {
|
|
273
|
+
const clips = [];
|
|
274
|
+
for (const track of state.timeline.tracks) {
|
|
275
|
+
clips.push(...track.clips);
|
|
276
|
+
}
|
|
277
|
+
return clips;
|
|
278
|
+
}
|
|
279
|
+
function getAllTracks(state) {
|
|
280
|
+
return state.timeline.tracks;
|
|
281
|
+
}
|
|
282
|
+
function findTrackIndex(state, trackId) {
|
|
283
|
+
return state.timeline.tracks.findIndex((t) => t.id === trackId);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/types/validation.ts
|
|
287
|
+
function validResult() {
|
|
288
|
+
return {
|
|
289
|
+
valid: true,
|
|
290
|
+
errors: []
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function invalidResult(code, message, context) {
|
|
294
|
+
const error = { code, message };
|
|
295
|
+
if (context !== void 0) {
|
|
296
|
+
error.context = context;
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
valid: false,
|
|
300
|
+
errors: [error]
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function invalidResults(errors) {
|
|
304
|
+
return {
|
|
305
|
+
valid: false,
|
|
306
|
+
errors
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
function combineResults(...results) {
|
|
310
|
+
const allErrors = [];
|
|
311
|
+
for (const result of results) {
|
|
312
|
+
if (!result.valid) {
|
|
313
|
+
allErrors.push(...result.errors);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (allErrors.length > 0) {
|
|
317
|
+
return invalidResults(allErrors);
|
|
318
|
+
}
|
|
319
|
+
return validResult();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/systems/asset-registry.ts
|
|
323
|
+
function registerAsset(state, asset) {
|
|
324
|
+
const next = new Map(state.assetRegistry);
|
|
325
|
+
next.set(asset.id, asset);
|
|
326
|
+
return { ...state, assetRegistry: next };
|
|
327
|
+
}
|
|
328
|
+
function getAsset(state, assetId) {
|
|
329
|
+
return state.assetRegistry.get(toAssetId(assetId));
|
|
330
|
+
}
|
|
331
|
+
function hasAsset(state, assetId) {
|
|
332
|
+
return state.assetRegistry.has(toAssetId(assetId));
|
|
333
|
+
}
|
|
334
|
+
function getAllAssets(state) {
|
|
335
|
+
return Array.from(state.assetRegistry.values());
|
|
336
|
+
}
|
|
337
|
+
function unregisterAsset(state, assetId) {
|
|
338
|
+
const next = new Map(state.assetRegistry);
|
|
339
|
+
next.delete(toAssetId(assetId));
|
|
340
|
+
return { ...state, assetRegistry: next };
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/systems/validation.ts
|
|
344
|
+
function validateClip(state, clip) {
|
|
345
|
+
const errors = [];
|
|
346
|
+
const asset = getAsset(state, clip.assetId);
|
|
347
|
+
if (!asset) {
|
|
348
|
+
errors.push(invalidResult(
|
|
349
|
+
"ASSET_NOT_FOUND",
|
|
350
|
+
`Asset '${clip.assetId}' not found in registry`,
|
|
351
|
+
{ clipId: clip.id, assetId: clip.assetId }
|
|
352
|
+
));
|
|
353
|
+
return combineResults(...errors);
|
|
354
|
+
}
|
|
355
|
+
if (clip.timelineEnd <= clip.timelineStart) {
|
|
356
|
+
errors.push(invalidResult(
|
|
357
|
+
"INVALID_TIMELINE_BOUNDS",
|
|
358
|
+
`Clip timeline end (${clip.timelineEnd}) must be greater than start (${clip.timelineStart})`,
|
|
359
|
+
{ clipId: clip.id, timelineStart: clip.timelineStart, timelineEnd: clip.timelineEnd }
|
|
360
|
+
));
|
|
361
|
+
}
|
|
362
|
+
if (clip.mediaIn < 0) {
|
|
363
|
+
errors.push(invalidResult(
|
|
364
|
+
"INVALID_MEDIA_IN",
|
|
365
|
+
`Clip media in (${clip.mediaIn}) must be >= 0`,
|
|
366
|
+
{ clipId: clip.id, mediaIn: clip.mediaIn }
|
|
367
|
+
));
|
|
368
|
+
}
|
|
369
|
+
if (clip.mediaOut <= clip.mediaIn) {
|
|
370
|
+
errors.push(invalidResult(
|
|
371
|
+
"INVALID_MEDIA_BOUNDS",
|
|
372
|
+
`Clip media out (${clip.mediaOut}) must be greater than media in (${clip.mediaIn})`,
|
|
373
|
+
{ clipId: clip.id, mediaIn: clip.mediaIn, mediaOut: clip.mediaOut }
|
|
374
|
+
));
|
|
375
|
+
}
|
|
376
|
+
if (clip.mediaOut > asset.intrinsicDuration) {
|
|
377
|
+
errors.push(invalidResult(
|
|
378
|
+
"MEDIA_EXCEEDS_ASSET",
|
|
379
|
+
`Clip media out (${clip.mediaOut}) exceeds asset duration (${asset.intrinsicDuration})`,
|
|
380
|
+
{ clipId: clip.id, mediaOut: clip.mediaOut, assetDuration: asset.intrinsicDuration }
|
|
381
|
+
));
|
|
382
|
+
}
|
|
383
|
+
const timelineDuration = getClipDuration(clip);
|
|
384
|
+
const mediaDuration = getClipMediaDuration(clip);
|
|
385
|
+
if (timelineDuration !== mediaDuration) {
|
|
386
|
+
errors.push(invalidResult(
|
|
387
|
+
"DURATION_MISMATCH",
|
|
388
|
+
`Clip timeline duration (${timelineDuration}) must match media duration (${mediaDuration}) in Phase 1`,
|
|
389
|
+
{ clipId: clip.id, timelineDuration, mediaDuration }
|
|
390
|
+
));
|
|
391
|
+
}
|
|
392
|
+
return combineResults(...errors);
|
|
393
|
+
}
|
|
394
|
+
function validateTrack(state, track) {
|
|
395
|
+
const errors = [];
|
|
396
|
+
for (const clip of track.clips) {
|
|
397
|
+
const clipResult = validateClip(state, clip);
|
|
398
|
+
if (!clipResult.valid) {
|
|
399
|
+
errors.push(clipResult);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
for (let i = 0; i < track.clips.length; i++) {
|
|
403
|
+
for (let j = i + 1; j < track.clips.length; j++) {
|
|
404
|
+
const clip1 = track.clips[i];
|
|
405
|
+
const clip2 = track.clips[j];
|
|
406
|
+
if (!clip1 || !clip2) {
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (clipsOverlap(clip1, clip2)) {
|
|
410
|
+
errors.push(invalidResult(
|
|
411
|
+
"CLIPS_OVERLAP",
|
|
412
|
+
`Clips '${clip1.id}' and '${clip2.id}' overlap on track '${track.id}'`,
|
|
413
|
+
{
|
|
414
|
+
trackId: track.id,
|
|
415
|
+
clip1Id: clip1.id,
|
|
416
|
+
clip2Id: clip2.id,
|
|
417
|
+
clip1Start: clip1.timelineStart,
|
|
418
|
+
clip1End: clip1.timelineEnd,
|
|
419
|
+
clip2Start: clip2.timelineStart,
|
|
420
|
+
clip2End: clip2.timelineEnd
|
|
421
|
+
}
|
|
422
|
+
));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return combineResults(...errors);
|
|
427
|
+
}
|
|
428
|
+
function validateTimeline(state) {
|
|
429
|
+
const errors = [];
|
|
430
|
+
if (state.timeline.fps <= 0) {
|
|
431
|
+
errors.push(invalidResult(
|
|
432
|
+
"INVALID_FPS",
|
|
433
|
+
`Timeline FPS must be positive, got ${state.timeline.fps}`,
|
|
434
|
+
{ fps: state.timeline.fps }
|
|
435
|
+
));
|
|
436
|
+
}
|
|
437
|
+
if (state.timeline.duration <= 0) {
|
|
438
|
+
errors.push(invalidResult(
|
|
439
|
+
"INVALID_DURATION",
|
|
440
|
+
`Timeline duration must be positive, got ${state.timeline.duration}`,
|
|
441
|
+
{ duration: state.timeline.duration }
|
|
442
|
+
));
|
|
443
|
+
}
|
|
444
|
+
for (const track of state.timeline.tracks) {
|
|
445
|
+
const trackResult = validateTrack(state, track);
|
|
446
|
+
if (!trackResult.valid) {
|
|
447
|
+
errors.push(trackResult);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return combineResults(...errors);
|
|
451
|
+
}
|
|
452
|
+
function validateNoOverlap(track, clip) {
|
|
453
|
+
for (const existingClip of track.clips) {
|
|
454
|
+
if (existingClip.id === clip.id) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (clipsOverlap(existingClip, clip)) {
|
|
458
|
+
return invalidResult(
|
|
459
|
+
"CLIPS_OVERLAP",
|
|
460
|
+
`Clip '${clip.id}' would overlap with existing clip '${existingClip.id}' on track '${track.id}'`,
|
|
461
|
+
{
|
|
462
|
+
trackId: track.id,
|
|
463
|
+
newClipId: clip.id,
|
|
464
|
+
existingClipId: existingClip.id,
|
|
465
|
+
newClipStart: clip.timelineStart,
|
|
466
|
+
newClipEnd: clip.timelineEnd,
|
|
467
|
+
existingClipStart: existingClip.timelineStart,
|
|
468
|
+
existingClipEnd: existingClip.timelineEnd
|
|
469
|
+
}
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return validResult();
|
|
474
|
+
}
|
|
475
|
+
function validateTrackTypeMatch(state, clip, targetTrack) {
|
|
476
|
+
const asset = getAsset(state, clip.assetId);
|
|
477
|
+
if (!asset) {
|
|
478
|
+
return invalidResult(
|
|
479
|
+
"ASSET_NOT_FOUND",
|
|
480
|
+
`Asset '${clip.assetId}' not found in registry`,
|
|
481
|
+
{ clipId: clip.id, assetId: clip.assetId }
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
if (asset.mediaType === "video" && targetTrack.type !== "video") {
|
|
485
|
+
return invalidResult(
|
|
486
|
+
"TRACK_TYPE_MISMATCH",
|
|
487
|
+
`Cannot place video clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
|
|
488
|
+
{
|
|
489
|
+
clipId: clip.id,
|
|
490
|
+
assetType: asset.mediaType,
|
|
491
|
+
trackType: targetTrack.type,
|
|
492
|
+
trackId: targetTrack.id
|
|
493
|
+
}
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
if (asset.mediaType === "audio" && targetTrack.type !== "audio") {
|
|
497
|
+
return invalidResult(
|
|
498
|
+
"TRACK_TYPE_MISMATCH",
|
|
499
|
+
`Cannot place audio clip '${clip.id}' on ${targetTrack.type} track '${targetTrack.id}'`,
|
|
500
|
+
{
|
|
501
|
+
clipId: clip.id,
|
|
502
|
+
assetType: asset.mediaType,
|
|
503
|
+
trackType: targetTrack.type,
|
|
504
|
+
trackId: targetTrack.id
|
|
505
|
+
}
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
return validResult();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// src/operations/clip-operations.ts
|
|
512
|
+
function addClip(state, trackId, clip) {
|
|
513
|
+
const trackIndex = state.timeline.tracks.findIndex((t) => t.id === trackId);
|
|
514
|
+
if (trackIndex === -1) {
|
|
515
|
+
return state;
|
|
516
|
+
}
|
|
517
|
+
const track = state.timeline.tracks[trackIndex];
|
|
518
|
+
if (!track) {
|
|
519
|
+
return state;
|
|
520
|
+
}
|
|
521
|
+
const newTrack = sortTrackClips({
|
|
522
|
+
...track,
|
|
523
|
+
clips: [...track.clips, clip]
|
|
524
|
+
});
|
|
525
|
+
const newTracks = [...state.timeline.tracks];
|
|
526
|
+
newTracks[trackIndex] = newTrack;
|
|
527
|
+
return {
|
|
528
|
+
...state,
|
|
529
|
+
timeline: {
|
|
530
|
+
...state.timeline,
|
|
531
|
+
tracks: newTracks
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function removeClip(state, clipId) {
|
|
536
|
+
const newTracks = state.timeline.tracks.map((track) => ({
|
|
537
|
+
...track,
|
|
538
|
+
clips: track.clips.filter((c) => c.id !== clipId)
|
|
539
|
+
}));
|
|
540
|
+
return {
|
|
541
|
+
...state,
|
|
542
|
+
timeline: {
|
|
543
|
+
...state.timeline,
|
|
544
|
+
tracks: newTracks
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
function moveClip(state, clipId, newStart) {
|
|
549
|
+
const clip = findClipById(state, clipId);
|
|
550
|
+
if (!clip) {
|
|
551
|
+
return state;
|
|
552
|
+
}
|
|
553
|
+
const duration = clip.timelineEnd - clip.timelineStart;
|
|
554
|
+
const newEnd = newStart + duration;
|
|
555
|
+
return updateClip(state, clipId, {
|
|
556
|
+
timelineStart: newStart,
|
|
557
|
+
timelineEnd: newEnd
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
function resizeClip(state, clipId, newStart, newEnd) {
|
|
561
|
+
const clip = findClipById(state, clipId);
|
|
562
|
+
if (!clip) {
|
|
563
|
+
return state;
|
|
564
|
+
}
|
|
565
|
+
const startDelta = newStart - clip.timelineStart;
|
|
566
|
+
const endDelta = newEnd - clip.timelineEnd;
|
|
567
|
+
let newMediaIn = clip.mediaIn;
|
|
568
|
+
let newMediaOut = clip.mediaOut;
|
|
569
|
+
if (startDelta !== 0) {
|
|
570
|
+
newMediaIn = clip.mediaIn + startDelta;
|
|
571
|
+
}
|
|
572
|
+
if (endDelta !== 0) {
|
|
573
|
+
newMediaOut = clip.mediaOut + endDelta;
|
|
574
|
+
}
|
|
575
|
+
return updateClip(state, clipId, {
|
|
576
|
+
timelineStart: newStart,
|
|
577
|
+
timelineEnd: newEnd,
|
|
578
|
+
mediaIn: newMediaIn,
|
|
579
|
+
mediaOut: newMediaOut
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
function trimClip(state, clipId, newMediaIn, newMediaOut) {
|
|
583
|
+
return updateClip(state, clipId, {
|
|
584
|
+
mediaIn: newMediaIn,
|
|
585
|
+
mediaOut: newMediaOut
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
function updateClip(state, clipId, updates) {
|
|
589
|
+
const newTracks = state.timeline.tracks.map((track) => {
|
|
590
|
+
const clipIndex = track.clips.findIndex((c) => c.id === clipId);
|
|
591
|
+
if (clipIndex === -1) {
|
|
592
|
+
return track;
|
|
593
|
+
}
|
|
594
|
+
const newClips = [...track.clips];
|
|
595
|
+
const existingClip = newClips[clipIndex];
|
|
596
|
+
if (!existingClip) {
|
|
597
|
+
return track;
|
|
598
|
+
}
|
|
599
|
+
newClips[clipIndex] = {
|
|
600
|
+
...existingClip,
|
|
601
|
+
...updates
|
|
602
|
+
};
|
|
603
|
+
return sortTrackClips({
|
|
604
|
+
...track,
|
|
605
|
+
clips: newClips
|
|
606
|
+
});
|
|
607
|
+
});
|
|
608
|
+
return {
|
|
609
|
+
...state,
|
|
610
|
+
timeline: {
|
|
611
|
+
...state.timeline,
|
|
612
|
+
tracks: newTracks
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
function moveClipToTrack(state, clipId, targetTrackId) {
|
|
617
|
+
const clip = findClipById(state, clipId);
|
|
618
|
+
if (!clip) {
|
|
619
|
+
return state;
|
|
620
|
+
}
|
|
621
|
+
const targetTrack = findTrackById(state, targetTrackId);
|
|
622
|
+
if (!targetTrack) {
|
|
623
|
+
return state;
|
|
624
|
+
}
|
|
625
|
+
const validationResult = validateTrackTypeMatch(state, clip, targetTrack);
|
|
626
|
+
if (!validationResult.valid) {
|
|
627
|
+
return state;
|
|
628
|
+
}
|
|
629
|
+
if (clip.trackId === targetTrackId) {
|
|
630
|
+
return state;
|
|
631
|
+
}
|
|
632
|
+
let newState = removeClip(state, clipId);
|
|
633
|
+
const updatedClip = { ...clip, trackId: targetTrackId };
|
|
634
|
+
newState = addClip(newState, targetTrackId, updatedClip);
|
|
635
|
+
return newState;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// src/operations/track-operations.ts
|
|
639
|
+
function addTrack(state, track) {
|
|
640
|
+
return {
|
|
641
|
+
...state,
|
|
642
|
+
timeline: {
|
|
643
|
+
...state.timeline,
|
|
644
|
+
tracks: [...state.timeline.tracks, track]
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
function removeTrack(state, trackId) {
|
|
649
|
+
return {
|
|
650
|
+
...state,
|
|
651
|
+
timeline: {
|
|
652
|
+
...state.timeline,
|
|
653
|
+
tracks: state.timeline.tracks.filter((t) => t.id !== trackId)
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
function moveTrack(state, trackId, newIndex) {
|
|
658
|
+
const currentIndex = findTrackIndex(state, trackId);
|
|
659
|
+
if (currentIndex === -1) {
|
|
660
|
+
return state;
|
|
661
|
+
}
|
|
662
|
+
const newTracks = [...state.timeline.tracks];
|
|
663
|
+
const [track] = newTracks.splice(currentIndex, 1);
|
|
664
|
+
if (!track) {
|
|
665
|
+
return state;
|
|
666
|
+
}
|
|
667
|
+
newTracks.splice(newIndex, 0, track);
|
|
668
|
+
return {
|
|
669
|
+
...state,
|
|
670
|
+
timeline: {
|
|
671
|
+
...state.timeline,
|
|
672
|
+
tracks: newTracks
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
}
|
|
676
|
+
function updateTrack(state, trackId, updates) {
|
|
677
|
+
const trackIndex = findTrackIndex(state, trackId);
|
|
678
|
+
if (trackIndex === -1) {
|
|
679
|
+
return state;
|
|
680
|
+
}
|
|
681
|
+
const newTracks = [...state.timeline.tracks];
|
|
682
|
+
const existingTrack = newTracks[trackIndex];
|
|
683
|
+
if (!existingTrack) {
|
|
684
|
+
return state;
|
|
685
|
+
}
|
|
686
|
+
newTracks[trackIndex] = {
|
|
687
|
+
...existingTrack,
|
|
688
|
+
...updates
|
|
689
|
+
};
|
|
690
|
+
return {
|
|
691
|
+
...state,
|
|
692
|
+
timeline: {
|
|
693
|
+
...state.timeline,
|
|
694
|
+
tracks: newTracks
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
function toggleTrackMute(state, trackId) {
|
|
699
|
+
const trackIndex = findTrackIndex(state, trackId);
|
|
700
|
+
if (trackIndex === -1) {
|
|
701
|
+
return state;
|
|
702
|
+
}
|
|
703
|
+
const track = state.timeline.tracks[trackIndex];
|
|
704
|
+
if (!track) {
|
|
705
|
+
return state;
|
|
706
|
+
}
|
|
707
|
+
return updateTrack(state, trackId, { muted: !track.muted });
|
|
708
|
+
}
|
|
709
|
+
function toggleTrackLock(state, trackId) {
|
|
710
|
+
const trackIndex = findTrackIndex(state, trackId);
|
|
711
|
+
if (trackIndex === -1) {
|
|
712
|
+
return state;
|
|
713
|
+
}
|
|
714
|
+
const track = state.timeline.tracks[trackIndex];
|
|
715
|
+
if (!track) {
|
|
716
|
+
return state;
|
|
717
|
+
}
|
|
718
|
+
return updateTrack(state, trackId, { locked: !track.locked });
|
|
719
|
+
}
|
|
720
|
+
function toggleTrackSolo(state, trackId) {
|
|
721
|
+
const trackIndex = findTrackIndex(state, trackId);
|
|
722
|
+
if (trackIndex === -1) {
|
|
723
|
+
return state;
|
|
724
|
+
}
|
|
725
|
+
const track = state.timeline.tracks[trackIndex];
|
|
726
|
+
if (!track) {
|
|
727
|
+
return state;
|
|
728
|
+
}
|
|
729
|
+
return updateTrack(state, trackId, { solo: !track.solo });
|
|
730
|
+
}
|
|
731
|
+
function setTrackHeight(state, trackId, height) {
|
|
732
|
+
return updateTrack(state, trackId, { height: Math.max(40, Math.min(200, height)) });
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// src/operations/timeline-operations.ts
|
|
736
|
+
function setTimelineDuration(state, duration) {
|
|
737
|
+
return {
|
|
738
|
+
...state,
|
|
739
|
+
timeline: {
|
|
740
|
+
...state.timeline,
|
|
741
|
+
duration
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
function setTimelineName(state, name) {
|
|
746
|
+
return {
|
|
747
|
+
...state,
|
|
748
|
+
timeline: {
|
|
749
|
+
...state.timeline,
|
|
750
|
+
name
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
// src/engine/transactions.ts
|
|
756
|
+
function beginTransaction(state) {
|
|
757
|
+
return {
|
|
758
|
+
initialState: state,
|
|
759
|
+
currentState: state,
|
|
760
|
+
operations: [],
|
|
761
|
+
finalized: false
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
function applyOperation(tx, operation) {
|
|
765
|
+
if (tx.finalized) {
|
|
766
|
+
throw new Error("Cannot apply operation to finalized transaction");
|
|
767
|
+
}
|
|
768
|
+
const newState = operation(tx.currentState);
|
|
769
|
+
return {
|
|
770
|
+
...tx,
|
|
771
|
+
currentState: newState,
|
|
772
|
+
operations: [...tx.operations, operation]
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
function commitTransaction(tx) {
|
|
776
|
+
if (tx.finalized) {
|
|
777
|
+
throw new Error("Transaction already finalized");
|
|
778
|
+
}
|
|
779
|
+
tx.finalized = true;
|
|
780
|
+
return tx.currentState;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// src/operations/ripple.ts
|
|
784
|
+
function rippleDelete(state, clipId) {
|
|
785
|
+
const clip = findClipById(state, clipId);
|
|
786
|
+
if (!clip) {
|
|
787
|
+
throw new Error(`Clip not found: ${clipId}`);
|
|
788
|
+
}
|
|
789
|
+
const track = findTrackById(state, clip.trackId);
|
|
790
|
+
if (!track) {
|
|
791
|
+
throw new Error(`Track not found: ${clip.trackId}`);
|
|
792
|
+
}
|
|
793
|
+
const clipDuration = getClipDuration(clip);
|
|
794
|
+
const deleteEnd = clip.timelineEnd;
|
|
795
|
+
const clipsToShift = track.clips.filter(
|
|
796
|
+
(c) => c.id !== clipId && c.timelineStart >= deleteEnd
|
|
797
|
+
);
|
|
798
|
+
let tx = beginTransaction(state);
|
|
799
|
+
tx = applyOperation(tx, (s) => removeClip(s, clipId));
|
|
800
|
+
for (const clipToShift of clipsToShift) {
|
|
801
|
+
const newStart = clipToShift.timelineStart - clipDuration;
|
|
802
|
+
tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
|
|
803
|
+
}
|
|
804
|
+
return commitTransaction(tx);
|
|
805
|
+
}
|
|
806
|
+
function rippleTrim(state, clipId, newEnd) {
|
|
807
|
+
const clip = findClipById(state, clipId);
|
|
808
|
+
if (!clip) {
|
|
809
|
+
throw new Error(`Clip not found: ${clipId}`);
|
|
810
|
+
}
|
|
811
|
+
const track = findTrackById(state, clip.trackId);
|
|
812
|
+
if (!track) {
|
|
813
|
+
throw new Error(`Track not found: ${clip.trackId}`);
|
|
814
|
+
}
|
|
815
|
+
if (newEnd <= clip.timelineStart) {
|
|
816
|
+
throw new Error("New end must be after clip start");
|
|
817
|
+
}
|
|
818
|
+
const delta = newEnd - clip.timelineEnd;
|
|
819
|
+
const clipsToShift = track.clips.filter(
|
|
820
|
+
(c) => c.id !== clipId && c.timelineStart >= clip.timelineEnd
|
|
821
|
+
);
|
|
822
|
+
let tx = beginTransaction(state);
|
|
823
|
+
tx = applyOperation(tx, (s) => resizeClip(s, clipId, clip.timelineStart, newEnd));
|
|
824
|
+
for (const clipToShift of clipsToShift) {
|
|
825
|
+
const newStart = clipToShift.timelineStart + delta;
|
|
826
|
+
tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
|
|
827
|
+
}
|
|
828
|
+
return commitTransaction(tx);
|
|
829
|
+
}
|
|
830
|
+
function insertEdit(state, trackId, clip, atFrame) {
|
|
831
|
+
const track = findTrackById(state, trackId);
|
|
832
|
+
if (!track) {
|
|
833
|
+
throw new Error(`Track not found: ${trackId}`);
|
|
834
|
+
}
|
|
835
|
+
const clipDuration = getClipDuration(clip);
|
|
836
|
+
const clipsToShift = track.clips.filter((c) => c.timelineStart >= atFrame);
|
|
837
|
+
const adjustedClip = {
|
|
838
|
+
...clip,
|
|
839
|
+
timelineStart: atFrame,
|
|
840
|
+
timelineEnd: atFrame + clipDuration
|
|
841
|
+
};
|
|
842
|
+
let tx = beginTransaction(state);
|
|
843
|
+
for (const clipToShift of clipsToShift) {
|
|
844
|
+
const newStart = clipToShift.timelineStart + clipDuration;
|
|
845
|
+
tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, newStart));
|
|
846
|
+
}
|
|
847
|
+
tx = applyOperation(tx, (s) => addClip(s, trackId, adjustedClip));
|
|
848
|
+
return commitTransaction(tx);
|
|
849
|
+
}
|
|
850
|
+
function rippleMove(state, clipId, newStart) {
|
|
851
|
+
const clip = findClipById(state, clipId);
|
|
852
|
+
if (!clip) {
|
|
853
|
+
throw new Error(`Clip not found: ${clipId}`);
|
|
854
|
+
}
|
|
855
|
+
const track = findTrackById(state, clip.trackId);
|
|
856
|
+
if (!track) {
|
|
857
|
+
throw new Error(`Track not found: ${clip.trackId}`);
|
|
858
|
+
}
|
|
859
|
+
const clipDuration = getClipDuration(clip);
|
|
860
|
+
const newEnd = newStart + clipDuration;
|
|
861
|
+
if (newStart < 0) {
|
|
862
|
+
throw new Error("Cannot move clip before timeline start (frame 0)");
|
|
863
|
+
}
|
|
864
|
+
if (newEnd > state.timeline.duration) {
|
|
865
|
+
throw new Error(`Cannot move clip beyond timeline duration (${state.timeline.duration} frames)`);
|
|
866
|
+
}
|
|
867
|
+
const originalStart = clip.timelineStart;
|
|
868
|
+
const originalEnd = clip.timelineEnd;
|
|
869
|
+
if (newStart === originalStart) {
|
|
870
|
+
return state;
|
|
871
|
+
}
|
|
872
|
+
let tx = beginTransaction(state);
|
|
873
|
+
if (newStart > originalStart) {
|
|
874
|
+
const afterSource = track.clips.filter((c) => c.id !== clipId && c.timelineStart >= originalEnd).sort((a, b) => a.timelineStart - b.timelineStart);
|
|
875
|
+
for (const other of afterSource) {
|
|
876
|
+
const s = other.timelineStart - clipDuration;
|
|
877
|
+
tx = applyOperation(tx, (st) => moveClip(st, other.id, s));
|
|
878
|
+
}
|
|
879
|
+
const anyClipBetween = afterSource.some((c) => c.timelineStart < newStart);
|
|
880
|
+
const collapsedDest = anyClipBetween ? newStart - clipDuration : newStart;
|
|
881
|
+
const currentTrack = tx.currentState.timeline.tracks.find((t) => t.id === track.id);
|
|
882
|
+
const atDest = currentTrack.clips.filter((c) => c.id !== clipId && c.timelineStart >= collapsedDest).sort((a, b) => b.timelineStart - a.timelineStart);
|
|
883
|
+
for (const other of atDest) {
|
|
884
|
+
const s = other.timelineStart + clipDuration;
|
|
885
|
+
tx = applyOperation(tx, (st) => moveClip(st, other.id, s));
|
|
886
|
+
}
|
|
887
|
+
tx = applyOperation(tx, (st) => moveClip(st, clipId, collapsedDest));
|
|
888
|
+
} else {
|
|
889
|
+
const afterSource = track.clips.filter((c) => c.id !== clipId && c.timelineStart >= originalEnd).sort((a, b) => a.timelineStart - b.timelineStart);
|
|
890
|
+
for (const other of afterSource) {
|
|
891
|
+
const s = other.timelineStart - clipDuration;
|
|
892
|
+
tx = applyOperation(tx, (st) => moveClip(st, other.id, s));
|
|
893
|
+
}
|
|
894
|
+
tx = applyOperation(tx, (st) => moveClip(st, clipId, newStart));
|
|
895
|
+
}
|
|
896
|
+
return commitTransaction(tx);
|
|
897
|
+
}
|
|
898
|
+
function insertMove(state, clipId, newStart) {
|
|
899
|
+
const clip = findClipById(state, clipId);
|
|
900
|
+
if (!clip) {
|
|
901
|
+
throw new Error(`Clip not found: ${clipId}`);
|
|
902
|
+
}
|
|
903
|
+
const track = findTrackById(state, clip.trackId);
|
|
904
|
+
if (!track) {
|
|
905
|
+
throw new Error(`Track not found: ${clip.trackId}`);
|
|
906
|
+
}
|
|
907
|
+
const clipDuration = getClipDuration(clip);
|
|
908
|
+
const newEnd = newStart + clipDuration;
|
|
909
|
+
if (newStart < 0) {
|
|
910
|
+
throw new Error("Cannot move clip before timeline start (frame 0)");
|
|
911
|
+
}
|
|
912
|
+
if (newEnd > state.timeline.duration) {
|
|
913
|
+
throw new Error(`Cannot move clip beyond timeline duration (${state.timeline.duration} frames)`);
|
|
914
|
+
}
|
|
915
|
+
if (newStart === clip.timelineStart) {
|
|
916
|
+
return state;
|
|
917
|
+
}
|
|
918
|
+
let tx = beginTransaction(state);
|
|
919
|
+
const clipsToShift = track.clips.filter(
|
|
920
|
+
(c) => c.id !== clipId && c.timelineStart >= newStart
|
|
921
|
+
);
|
|
922
|
+
for (const clipToShift of clipsToShift) {
|
|
923
|
+
const shiftedStart = clipToShift.timelineStart + clipDuration;
|
|
924
|
+
tx = applyOperation(tx, (s) => moveClip(s, clipToShift.id, shiftedStart));
|
|
925
|
+
}
|
|
926
|
+
tx = applyOperation(tx, (s) => moveClip(s, clipId, newStart));
|
|
927
|
+
return commitTransaction(tx);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// src/engine/timeline-engine.ts
|
|
931
|
+
function legacyDispatch(history, operation) {
|
|
932
|
+
const currentState = getCurrentState(history);
|
|
933
|
+
let newState;
|
|
934
|
+
try {
|
|
935
|
+
newState = operation(currentState);
|
|
936
|
+
} catch (err) {
|
|
937
|
+
return { accepted: false, errors: [{ code: "OPERATION_ERROR", message: String(err) }] };
|
|
938
|
+
}
|
|
939
|
+
const newHistory = pushHistory(history, newState);
|
|
940
|
+
return { accepted: true, history: newHistory };
|
|
941
|
+
}
|
|
942
|
+
var TimelineEngine = class {
|
|
943
|
+
history;
|
|
944
|
+
listeners = /* @__PURE__ */ new Set();
|
|
945
|
+
/**
|
|
946
|
+
* Create a new timeline engine
|
|
947
|
+
*
|
|
948
|
+
* @param initialState - Initial timeline state
|
|
949
|
+
* @param historyLimit - Maximum number of undo steps (default: 50)
|
|
950
|
+
*/
|
|
951
|
+
constructor(initialState, historyLimit = 50) {
|
|
952
|
+
this.history = createHistory(initialState, historyLimit);
|
|
953
|
+
}
|
|
954
|
+
// ===== SUBSCRIPTION =====
|
|
955
|
+
/**
|
|
956
|
+
* Subscribe to state changes
|
|
957
|
+
*
|
|
958
|
+
* The listener will be called whenever the timeline state changes,
|
|
959
|
+
* with the new state passed as an argument.
|
|
960
|
+
* This is used by framework adapters (e.g., React) to trigger re-renders.
|
|
961
|
+
*
|
|
962
|
+
* @param listener - Function to call on state changes, receives new state
|
|
963
|
+
* @returns Unsubscribe function
|
|
964
|
+
*
|
|
965
|
+
* @example
|
|
966
|
+
* ```typescript
|
|
967
|
+
* const unsubscribe = engine.subscribe((state) => {
|
|
968
|
+
* console.log('State changed:', state);
|
|
969
|
+
* });
|
|
970
|
+
*
|
|
971
|
+
* // Later...
|
|
972
|
+
* unsubscribe();
|
|
973
|
+
* ```
|
|
974
|
+
*/
|
|
975
|
+
subscribe(listener) {
|
|
976
|
+
this.listeners.add(listener);
|
|
977
|
+
return () => {
|
|
978
|
+
this.listeners.delete(listener);
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Notify all subscribers of a state change
|
|
983
|
+
*
|
|
984
|
+
* This is called internally after any operation that modifies state.
|
|
985
|
+
* Framework adapters use this to trigger re-renders.
|
|
986
|
+
*/
|
|
987
|
+
notify() {
|
|
988
|
+
const state = this.getState();
|
|
989
|
+
this.listeners.forEach((listener) => listener(state));
|
|
990
|
+
}
|
|
991
|
+
// ===== STATE ACCESS =====
|
|
992
|
+
/**
|
|
993
|
+
* Get the current timeline state
|
|
994
|
+
*
|
|
995
|
+
* @returns Current timeline state
|
|
996
|
+
*/
|
|
997
|
+
getState() {
|
|
998
|
+
return getCurrentState(this.history);
|
|
999
|
+
}
|
|
1000
|
+
// ===== ASSET OPERATIONS =====
|
|
1001
|
+
/**
|
|
1002
|
+
* Register an asset
|
|
1003
|
+
*
|
|
1004
|
+
* @param asset - Asset to register
|
|
1005
|
+
* @returns Dispatch result
|
|
1006
|
+
*/
|
|
1007
|
+
registerAsset(asset) {
|
|
1008
|
+
const result = legacyDispatch(
|
|
1009
|
+
this.history,
|
|
1010
|
+
(state) => registerAsset(state, asset)
|
|
1011
|
+
);
|
|
1012
|
+
if (result.accepted && result.history) {
|
|
1013
|
+
this.history = result.history;
|
|
1014
|
+
this.notify();
|
|
1015
|
+
}
|
|
1016
|
+
return result;
|
|
1017
|
+
}
|
|
1018
|
+
/**
|
|
1019
|
+
* Get an asset by ID
|
|
1020
|
+
*
|
|
1021
|
+
* @param assetId - Asset ID
|
|
1022
|
+
* @returns The asset, or undefined if not found
|
|
1023
|
+
*/
|
|
1024
|
+
getAsset(assetId) {
|
|
1025
|
+
return getAsset(this.getState(), assetId);
|
|
1026
|
+
}
|
|
1027
|
+
// ===== CLIP OPERATIONS =====
|
|
1028
|
+
/**
|
|
1029
|
+
* Add a clip to a track
|
|
1030
|
+
*
|
|
1031
|
+
* @param trackId - ID of the track to add to
|
|
1032
|
+
* @param clip - Clip to add
|
|
1033
|
+
* @returns Dispatch result
|
|
1034
|
+
*/
|
|
1035
|
+
addClip(trackId, clip) {
|
|
1036
|
+
const result = legacyDispatch(
|
|
1037
|
+
this.history,
|
|
1038
|
+
(state) => addClip(state, trackId, clip)
|
|
1039
|
+
);
|
|
1040
|
+
if (result.accepted && result.history) {
|
|
1041
|
+
this.history = result.history;
|
|
1042
|
+
this.notify();
|
|
1043
|
+
}
|
|
1044
|
+
return result;
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Remove a clip
|
|
1048
|
+
*
|
|
1049
|
+
* @param clipId - ID of the clip to remove
|
|
1050
|
+
* @returns Dispatch result
|
|
1051
|
+
*/
|
|
1052
|
+
removeClip(clipId) {
|
|
1053
|
+
const result = legacyDispatch(
|
|
1054
|
+
this.history,
|
|
1055
|
+
(state) => removeClip(state, clipId)
|
|
1056
|
+
);
|
|
1057
|
+
if (result.accepted && result.history) {
|
|
1058
|
+
this.history = result.history;
|
|
1059
|
+
this.notify();
|
|
1060
|
+
}
|
|
1061
|
+
return result;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Move a clip to a new timeline position
|
|
1065
|
+
*
|
|
1066
|
+
* @param clipId - ID of the clip to move
|
|
1067
|
+
* @param newStart - New timeline start frame
|
|
1068
|
+
* @returns Dispatch result
|
|
1069
|
+
*/
|
|
1070
|
+
moveClip(clipId, newStart) {
|
|
1071
|
+
const result = legacyDispatch(
|
|
1072
|
+
this.history,
|
|
1073
|
+
(state) => moveClip(state, clipId, newStart)
|
|
1074
|
+
);
|
|
1075
|
+
if (result.accepted && result.history) {
|
|
1076
|
+
this.history = result.history;
|
|
1077
|
+
this.notify();
|
|
1078
|
+
}
|
|
1079
|
+
return result;
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* Resize a clip
|
|
1083
|
+
*
|
|
1084
|
+
* @param clipId - ID of the clip to resize
|
|
1085
|
+
* @param newStart - New timeline start frame
|
|
1086
|
+
* @param newEnd - New timeline end frame
|
|
1087
|
+
* @returns Dispatch result
|
|
1088
|
+
*/
|
|
1089
|
+
resizeClip(clipId, newStart, newEnd) {
|
|
1090
|
+
const result = legacyDispatch(
|
|
1091
|
+
this.history,
|
|
1092
|
+
(state) => resizeClip(state, clipId, newStart, newEnd)
|
|
1093
|
+
);
|
|
1094
|
+
if (result.accepted && result.history) {
|
|
1095
|
+
this.history = result.history;
|
|
1096
|
+
this.notify();
|
|
1097
|
+
}
|
|
1098
|
+
return result;
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Trim a clip (change media bounds)
|
|
1102
|
+
*
|
|
1103
|
+
* @param clipId - ID of the clip to trim
|
|
1104
|
+
* @param newMediaIn - New media in frame
|
|
1105
|
+
* @param newMediaOut - New media out frame
|
|
1106
|
+
* @returns Dispatch result
|
|
1107
|
+
*/
|
|
1108
|
+
trimClip(clipId, newMediaIn, newMediaOut) {
|
|
1109
|
+
const result = legacyDispatch(
|
|
1110
|
+
this.history,
|
|
1111
|
+
(state) => trimClip(state, clipId, newMediaIn, newMediaOut)
|
|
1112
|
+
);
|
|
1113
|
+
if (result.accepted && result.history) {
|
|
1114
|
+
this.history = result.history;
|
|
1115
|
+
this.notify();
|
|
1116
|
+
}
|
|
1117
|
+
return result;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Move a clip to a different track
|
|
1121
|
+
*
|
|
1122
|
+
* @param clipId - ID of the clip to move
|
|
1123
|
+
* @param targetTrackId - ID of the target track
|
|
1124
|
+
* @returns Dispatch result
|
|
1125
|
+
*/
|
|
1126
|
+
moveClipToTrack(clipId, targetTrackId) {
|
|
1127
|
+
const result = legacyDispatch(
|
|
1128
|
+
this.history,
|
|
1129
|
+
(state) => moveClipToTrack(state, clipId, targetTrackId)
|
|
1130
|
+
);
|
|
1131
|
+
if (result.accepted && result.history) {
|
|
1132
|
+
this.history = result.history;
|
|
1133
|
+
this.notify();
|
|
1134
|
+
}
|
|
1135
|
+
return result;
|
|
1136
|
+
}
|
|
1137
|
+
// ===== TRACK OPERATIONS =====
|
|
1138
|
+
/**
|
|
1139
|
+
* Add a track
|
|
1140
|
+
*
|
|
1141
|
+
* @param track - Track to add
|
|
1142
|
+
* @returns Dispatch result
|
|
1143
|
+
*/
|
|
1144
|
+
addTrack(track) {
|
|
1145
|
+
const result = legacyDispatch(
|
|
1146
|
+
this.history,
|
|
1147
|
+
(state) => addTrack(state, track)
|
|
1148
|
+
);
|
|
1149
|
+
if (result.accepted && result.history) {
|
|
1150
|
+
this.history = result.history;
|
|
1151
|
+
this.notify();
|
|
1152
|
+
}
|
|
1153
|
+
return result;
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Remove a track
|
|
1157
|
+
*
|
|
1158
|
+
* @param trackId - ID of the track to remove
|
|
1159
|
+
* @returns Dispatch result
|
|
1160
|
+
*/
|
|
1161
|
+
removeTrack(trackId) {
|
|
1162
|
+
const result = legacyDispatch(
|
|
1163
|
+
this.history,
|
|
1164
|
+
(state) => removeTrack(state, trackId)
|
|
1165
|
+
);
|
|
1166
|
+
if (result.accepted && result.history) {
|
|
1167
|
+
this.history = result.history;
|
|
1168
|
+
this.notify();
|
|
1169
|
+
}
|
|
1170
|
+
return result;
|
|
1171
|
+
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Move a track to a new position
|
|
1174
|
+
*
|
|
1175
|
+
* @param trackId - ID of the track to move
|
|
1176
|
+
* @param newIndex - New index position
|
|
1177
|
+
* @returns Dispatch result
|
|
1178
|
+
*/
|
|
1179
|
+
moveTrack(trackId, newIndex) {
|
|
1180
|
+
const result = legacyDispatch(
|
|
1181
|
+
this.history,
|
|
1182
|
+
(state) => moveTrack(state, trackId, newIndex)
|
|
1183
|
+
);
|
|
1184
|
+
if (result.accepted && result.history) {
|
|
1185
|
+
this.history = result.history;
|
|
1186
|
+
this.notify();
|
|
1187
|
+
}
|
|
1188
|
+
return result;
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Toggle track mute
|
|
1192
|
+
*
|
|
1193
|
+
* @param trackId - ID of the track
|
|
1194
|
+
* @returns Dispatch result
|
|
1195
|
+
*/
|
|
1196
|
+
toggleTrackMute(trackId) {
|
|
1197
|
+
const result = legacyDispatch(
|
|
1198
|
+
this.history,
|
|
1199
|
+
(state) => toggleTrackMute(state, trackId)
|
|
1200
|
+
);
|
|
1201
|
+
if (result.accepted && result.history) {
|
|
1202
|
+
this.history = result.history;
|
|
1203
|
+
this.notify();
|
|
1204
|
+
}
|
|
1205
|
+
return result;
|
|
1206
|
+
}
|
|
1207
|
+
/**
|
|
1208
|
+
* Toggle track lock
|
|
1209
|
+
*
|
|
1210
|
+
* @param trackId - ID of the track
|
|
1211
|
+
* @returns Dispatch result
|
|
1212
|
+
*/
|
|
1213
|
+
toggleTrackLock(trackId) {
|
|
1214
|
+
const result = legacyDispatch(
|
|
1215
|
+
this.history,
|
|
1216
|
+
(state) => toggleTrackLock(state, trackId)
|
|
1217
|
+
);
|
|
1218
|
+
if (result.accepted && result.history) {
|
|
1219
|
+
this.history = result.history;
|
|
1220
|
+
this.notify();
|
|
1221
|
+
}
|
|
1222
|
+
return result;
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* Toggle track solo
|
|
1226
|
+
*
|
|
1227
|
+
* @param trackId - ID of the track
|
|
1228
|
+
* @returns Dispatch result
|
|
1229
|
+
*/
|
|
1230
|
+
toggleTrackSolo(trackId) {
|
|
1231
|
+
const result = legacyDispatch(
|
|
1232
|
+
this.history,
|
|
1233
|
+
(state) => toggleTrackSolo(state, trackId)
|
|
1234
|
+
);
|
|
1235
|
+
if (result.accepted && result.history) {
|
|
1236
|
+
this.history = result.history;
|
|
1237
|
+
this.notify();
|
|
1238
|
+
}
|
|
1239
|
+
return result;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Set track height
|
|
1243
|
+
*
|
|
1244
|
+
* @param trackId - ID of the track
|
|
1245
|
+
* @param height - New height in pixels
|
|
1246
|
+
* @returns Dispatch result
|
|
1247
|
+
*/
|
|
1248
|
+
setTrackHeight(trackId, height) {
|
|
1249
|
+
const result = legacyDispatch(
|
|
1250
|
+
this.history,
|
|
1251
|
+
(state) => setTrackHeight(state, trackId, height)
|
|
1252
|
+
);
|
|
1253
|
+
if (result.accepted && result.history) {
|
|
1254
|
+
this.history = result.history;
|
|
1255
|
+
this.notify();
|
|
1256
|
+
}
|
|
1257
|
+
return result;
|
|
1258
|
+
}
|
|
1259
|
+
// ===== TIMELINE OPERATIONS =====
|
|
1260
|
+
/**
|
|
1261
|
+
* Set timeline duration
|
|
1262
|
+
*
|
|
1263
|
+
* @param duration - New duration in frames
|
|
1264
|
+
* @returns Dispatch result
|
|
1265
|
+
*/
|
|
1266
|
+
setTimelineDuration(duration) {
|
|
1267
|
+
const result = legacyDispatch(
|
|
1268
|
+
this.history,
|
|
1269
|
+
(state) => setTimelineDuration(state, duration)
|
|
1270
|
+
);
|
|
1271
|
+
if (result.accepted && result.history) {
|
|
1272
|
+
this.history = result.history;
|
|
1273
|
+
this.notify();
|
|
1274
|
+
}
|
|
1275
|
+
return result;
|
|
1276
|
+
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Set timeline name
|
|
1279
|
+
*
|
|
1280
|
+
* @param name - New timeline name
|
|
1281
|
+
* @returns Dispatch result
|
|
1282
|
+
*/
|
|
1283
|
+
setTimelineName(name) {
|
|
1284
|
+
const result = legacyDispatch(
|
|
1285
|
+
this.history,
|
|
1286
|
+
(state) => setTimelineName(state, name)
|
|
1287
|
+
);
|
|
1288
|
+
if (result.accepted && result.history) {
|
|
1289
|
+
this.history = result.history;
|
|
1290
|
+
this.notify();
|
|
1291
|
+
}
|
|
1292
|
+
return result;
|
|
1293
|
+
}
|
|
1294
|
+
// ===== HISTORY OPERATIONS =====
|
|
1295
|
+
/**
|
|
1296
|
+
* Undo the last action
|
|
1297
|
+
*
|
|
1298
|
+
* @returns true if undo was performed
|
|
1299
|
+
*/
|
|
1300
|
+
undo() {
|
|
1301
|
+
if (!this.canUndo()) {
|
|
1302
|
+
return false;
|
|
1303
|
+
}
|
|
1304
|
+
this.history = undo(this.history);
|
|
1305
|
+
this.notify();
|
|
1306
|
+
return true;
|
|
1307
|
+
}
|
|
1308
|
+
/**
|
|
1309
|
+
* Redo the last undone action
|
|
1310
|
+
*
|
|
1311
|
+
* @returns true if redo was performed
|
|
1312
|
+
*/
|
|
1313
|
+
redo() {
|
|
1314
|
+
if (!this.canRedo()) {
|
|
1315
|
+
return false;
|
|
1316
|
+
}
|
|
1317
|
+
this.history = redo(this.history);
|
|
1318
|
+
this.notify();
|
|
1319
|
+
return true;
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Check if undo is available
|
|
1323
|
+
*
|
|
1324
|
+
* @returns true if undo is available
|
|
1325
|
+
*/
|
|
1326
|
+
canUndo() {
|
|
1327
|
+
return canUndo(this.history);
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Check if redo is available
|
|
1331
|
+
*
|
|
1332
|
+
* @returns true if redo is available
|
|
1333
|
+
*/
|
|
1334
|
+
canRedo() {
|
|
1335
|
+
return canRedo(this.history);
|
|
1336
|
+
}
|
|
1337
|
+
// ===== QUERY OPERATIONS =====
|
|
1338
|
+
/**
|
|
1339
|
+
* Find a clip by ID
|
|
1340
|
+
*
|
|
1341
|
+
* @param clipId - Clip ID
|
|
1342
|
+
* @returns The clip, or undefined if not found
|
|
1343
|
+
*/
|
|
1344
|
+
findClipById(clipId) {
|
|
1345
|
+
return findClipById(this.getState(), clipId);
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Find a track by ID
|
|
1349
|
+
*
|
|
1350
|
+
* @param trackId - Track ID
|
|
1351
|
+
* @returns The track, or undefined if not found
|
|
1352
|
+
*/
|
|
1353
|
+
findTrackById(trackId) {
|
|
1354
|
+
return findTrackById(this.getState(), trackId);
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* Get all clips on a track
|
|
1358
|
+
*
|
|
1359
|
+
* @param trackId - Track ID
|
|
1360
|
+
* @returns Array of clips on the track
|
|
1361
|
+
*/
|
|
1362
|
+
getClipsOnTrack(trackId) {
|
|
1363
|
+
return getClipsOnTrack(this.getState(), trackId);
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
* Get all clips at a specific frame
|
|
1367
|
+
*
|
|
1368
|
+
* @param frame - Frame to check
|
|
1369
|
+
* @returns Array of clips at that frame
|
|
1370
|
+
*/
|
|
1371
|
+
getClipsAtFrame(f) {
|
|
1372
|
+
return getClipsAtFrame(this.getState(), f);
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* Get all clips in a frame range
|
|
1376
|
+
*
|
|
1377
|
+
* @param start - Start frame
|
|
1378
|
+
* @param end - End frame
|
|
1379
|
+
* @returns Array of clips in the range
|
|
1380
|
+
*/
|
|
1381
|
+
getClipsInRange(start, end) {
|
|
1382
|
+
return getClipsInRange(this.getState(), start, end);
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* Get all clips in the timeline
|
|
1386
|
+
*
|
|
1387
|
+
* @returns Array of all clips
|
|
1388
|
+
*/
|
|
1389
|
+
getAllClips() {
|
|
1390
|
+
return getAllClips(this.getState());
|
|
1391
|
+
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Get all tracks in the timeline
|
|
1394
|
+
*
|
|
1395
|
+
* @returns Array of all tracks
|
|
1396
|
+
*/
|
|
1397
|
+
getAllTracks() {
|
|
1398
|
+
return getAllTracks(this.getState());
|
|
1399
|
+
}
|
|
1400
|
+
// ===== RIPPLE OPERATIONS =====
|
|
1401
|
+
/**
|
|
1402
|
+
* Ripple delete - delete clip and shift subsequent clips left
|
|
1403
|
+
*
|
|
1404
|
+
* @param clipId - ID of the clip to delete
|
|
1405
|
+
* @returns Dispatch result
|
|
1406
|
+
*/
|
|
1407
|
+
rippleDelete(clipId) {
|
|
1408
|
+
const result = legacyDispatch(
|
|
1409
|
+
this.history,
|
|
1410
|
+
(state) => rippleDelete(state, clipId)
|
|
1411
|
+
);
|
|
1412
|
+
if (result.accepted && result.history) {
|
|
1413
|
+
this.history = result.history;
|
|
1414
|
+
this.notify();
|
|
1415
|
+
}
|
|
1416
|
+
return result;
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Ripple trim - trim clip end and shift subsequent clips
|
|
1420
|
+
*
|
|
1421
|
+
* @param clipId - ID of the clip to trim
|
|
1422
|
+
* @param newEnd - New end frame for the clip
|
|
1423
|
+
* @returns Dispatch result
|
|
1424
|
+
*/
|
|
1425
|
+
rippleTrim(clipId, newEnd) {
|
|
1426
|
+
const result = legacyDispatch(
|
|
1427
|
+
this.history,
|
|
1428
|
+
(state) => rippleTrim(state, clipId, newEnd)
|
|
1429
|
+
);
|
|
1430
|
+
if (result.accepted && result.history) {
|
|
1431
|
+
this.history = result.history;
|
|
1432
|
+
this.notify();
|
|
1433
|
+
}
|
|
1434
|
+
return result;
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* Insert edit - insert clip and shift subsequent clips right
|
|
1438
|
+
*
|
|
1439
|
+
* @param trackId - ID of the track to insert into
|
|
1440
|
+
* @param clip - Clip to insert
|
|
1441
|
+
* @param atFrame - Frame to insert at
|
|
1442
|
+
* @returns Dispatch result
|
|
1443
|
+
*/
|
|
1444
|
+
insertEdit(trackId, clip, atFrame) {
|
|
1445
|
+
const result = legacyDispatch(
|
|
1446
|
+
this.history,
|
|
1447
|
+
(state) => insertEdit(state, trackId, clip, atFrame)
|
|
1448
|
+
);
|
|
1449
|
+
if (result.accepted && result.history) {
|
|
1450
|
+
this.history = result.history;
|
|
1451
|
+
this.notify();
|
|
1452
|
+
}
|
|
1453
|
+
return result;
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Ripple move - move clip and shift surrounding clips to accommodate
|
|
1457
|
+
*
|
|
1458
|
+
* This moves a clip to a new position while maintaining timeline continuity:
|
|
1459
|
+
* - Closes the gap at the source position
|
|
1460
|
+
* - Makes space at the destination position
|
|
1461
|
+
* - All operations are atomic (single undo entry)
|
|
1462
|
+
*
|
|
1463
|
+
* @param clipId - ID of the clip to move
|
|
1464
|
+
* @param newStart - New start frame for the clip
|
|
1465
|
+
* @returns Dispatch result
|
|
1466
|
+
*/
|
|
1467
|
+
rippleMove(clipId, newStart) {
|
|
1468
|
+
const result = legacyDispatch(
|
|
1469
|
+
this.history,
|
|
1470
|
+
(state) => rippleMove(state, clipId, newStart)
|
|
1471
|
+
);
|
|
1472
|
+
if (result.accepted && result.history) {
|
|
1473
|
+
this.history = result.history;
|
|
1474
|
+
this.notify();
|
|
1475
|
+
} else if (!result.accepted && result.errors?.[0]?.code === "OPERATION_ERROR") {
|
|
1476
|
+
throw new Error(result.errors[0].message);
|
|
1477
|
+
}
|
|
1478
|
+
return result;
|
|
1479
|
+
}
|
|
1480
|
+
/**
|
|
1481
|
+
* Insert move - move clip and shift destination clips right
|
|
1482
|
+
*
|
|
1483
|
+
* This moves a clip to a new position without closing the gap at source:
|
|
1484
|
+
* - Leaves gap at the source position
|
|
1485
|
+
* - Pushes all clips at destination right to make space
|
|
1486
|
+
* - All operations are atomic (single undo entry)
|
|
1487
|
+
*
|
|
1488
|
+
* @param clipId - ID of the clip to move
|
|
1489
|
+
* @param newStart - New start frame for the clip
|
|
1490
|
+
* @returns Dispatch result
|
|
1491
|
+
*/
|
|
1492
|
+
insertMove(clipId, newStart) {
|
|
1493
|
+
const result = legacyDispatch(
|
|
1494
|
+
this.history,
|
|
1495
|
+
(state) => insertMove(state, clipId, newStart)
|
|
1496
|
+
);
|
|
1497
|
+
if (result.accepted && result.history) {
|
|
1498
|
+
this.history = result.history;
|
|
1499
|
+
this.notify();
|
|
1500
|
+
} else if (!result.accepted && result.errors?.[0]?.code === "OPERATION_ERROR") {
|
|
1501
|
+
throw new Error(result.errors[0].message);
|
|
1502
|
+
}
|
|
1503
|
+
return result;
|
|
1504
|
+
}
|
|
1505
|
+
// Phase 2: Marker and WorkArea operations are gated to Phase 2.
|
|
1506
|
+
// They are intentionally omitted here to keep Phase 0 clean.
|
|
1507
|
+
};
|
|
1508
|
+
|
|
1509
|
+
// src/validation/invariants.ts
|
|
1510
|
+
function checkInvariants(state) {
|
|
1511
|
+
const violations = [];
|
|
1512
|
+
if (state.schemaVersion !== CURRENT_SCHEMA_VERSION) {
|
|
1513
|
+
violations.push({
|
|
1514
|
+
type: "SCHEMA_VERSION_MISMATCH",
|
|
1515
|
+
entityId: "timeline",
|
|
1516
|
+
message: `Expected schema v${CURRENT_SCHEMA_VERSION}, got v${state.schemaVersion}`
|
|
1517
|
+
});
|
|
1518
|
+
return violations;
|
|
1519
|
+
}
|
|
1520
|
+
for (const track of state.timeline.tracks) {
|
|
1521
|
+
checkTrack(state, track, violations);
|
|
1522
|
+
}
|
|
1523
|
+
return violations;
|
|
1524
|
+
}
|
|
1525
|
+
function checkTrack(state, track, violations) {
|
|
1526
|
+
const clips = track.clips;
|
|
1527
|
+
for (let i = 1; i < clips.length; i++) {
|
|
1528
|
+
const prev = clips[i - 1];
|
|
1529
|
+
const curr = clips[i];
|
|
1530
|
+
if (prev.timelineStart > curr.timelineStart) {
|
|
1531
|
+
violations.push({
|
|
1532
|
+
type: "TRACK_NOT_SORTED",
|
|
1533
|
+
entityId: track.id,
|
|
1534
|
+
message: `Track '${track.id}': clip '${curr.id}' (start=${curr.timelineStart}) appears after clip '${prev.id}' (start=${prev.timelineStart}) \u2014 not sorted.`
|
|
1535
|
+
});
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
for (let i = 0; i < clips.length; i++) {
|
|
1539
|
+
for (let j = i + 1; j < clips.length; j++) {
|
|
1540
|
+
const a = clips[i];
|
|
1541
|
+
const b = clips[j];
|
|
1542
|
+
if (clipsOverlap(a, b)) {
|
|
1543
|
+
violations.push({
|
|
1544
|
+
type: "OVERLAP",
|
|
1545
|
+
entityId: a.id,
|
|
1546
|
+
message: `Track '${track.id}': clips '${a.id}' [${a.timelineStart}-${a.timelineEnd}) and '${b.id}' [${b.timelineStart}-${b.timelineEnd}) overlap.`
|
|
1547
|
+
});
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
for (const clip of clips) {
|
|
1552
|
+
checkClip(state, track, clip, violations);
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
function checkClip(state, track, clip, violations) {
|
|
1556
|
+
const asset = state.assetRegistry.get(clip.assetId);
|
|
1557
|
+
if (!asset) {
|
|
1558
|
+
violations.push({
|
|
1559
|
+
type: "ASSET_MISSING",
|
|
1560
|
+
entityId: clip.id,
|
|
1561
|
+
message: `Clip '${clip.id}' references asset '${clip.assetId}' which is not in the registry.`
|
|
1562
|
+
});
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
if (asset.mediaType !== track.type) {
|
|
1566
|
+
violations.push({
|
|
1567
|
+
type: "TRACK_TYPE_MISMATCH",
|
|
1568
|
+
entityId: clip.id,
|
|
1569
|
+
message: `Clip '${clip.id}' has asset mediaType '${asset.mediaType}' but is on a '${track.type}' track '${track.id}'.`
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
if (clip.mediaIn < 0) {
|
|
1573
|
+
violations.push({
|
|
1574
|
+
type: "MEDIA_BOUNDS_INVALID",
|
|
1575
|
+
entityId: clip.id,
|
|
1576
|
+
message: `Clip '${clip.id}': mediaIn (${clip.mediaIn}) must be >= 0.`
|
|
1577
|
+
});
|
|
1578
|
+
}
|
|
1579
|
+
if (clip.mediaOut > asset.intrinsicDuration) {
|
|
1580
|
+
violations.push({
|
|
1581
|
+
type: "MEDIA_BOUNDS_INVALID",
|
|
1582
|
+
entityId: clip.id,
|
|
1583
|
+
message: `Clip '${clip.id}': mediaOut (${clip.mediaOut}) exceeds asset intrinsicDuration (${asset.intrinsicDuration}).`
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
const mediaDuration = clip.mediaOut - clip.mediaIn;
|
|
1587
|
+
const timelineDuration = clip.timelineEnd - clip.timelineStart;
|
|
1588
|
+
const expectedMediaDuration = timelineDuration / clip.speed;
|
|
1589
|
+
if (Math.abs(mediaDuration - expectedMediaDuration) > 0.5) {
|
|
1590
|
+
violations.push({
|
|
1591
|
+
type: "DURATION_MISMATCH",
|
|
1592
|
+
entityId: clip.id,
|
|
1593
|
+
message: `Clip '${clip.id}': mediaDuration (${mediaDuration}) \u2260 timelineDuration/speed (${expectedMediaDuration.toFixed(2)}).`
|
|
1594
|
+
});
|
|
1595
|
+
}
|
|
1596
|
+
if (clip.timelineEnd > state.timeline.duration) {
|
|
1597
|
+
violations.push({
|
|
1598
|
+
type: "CLIP_BEYOND_TIMELINE",
|
|
1599
|
+
entityId: clip.id,
|
|
1600
|
+
message: `Clip '${clip.id}': timelineEnd (${clip.timelineEnd}) exceeds timeline duration (${state.timeline.duration}).`
|
|
1601
|
+
});
|
|
1602
|
+
}
|
|
1603
|
+
if (clip.speed <= 0) {
|
|
1604
|
+
violations.push({
|
|
1605
|
+
type: "SPEED_INVALID",
|
|
1606
|
+
entityId: clip.id,
|
|
1607
|
+
message: `Clip '${clip.id}': speed (${clip.speed}) must be > 0.`
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// src/engine/apply.ts
|
|
1613
|
+
function applyOperation2(state, op) {
|
|
1614
|
+
switch (op.type) {
|
|
1615
|
+
// — Clip operations ——————————————————————————————————————————————————
|
|
1616
|
+
case "INSERT_CLIP": {
|
|
1617
|
+
return updateTrack2(
|
|
1618
|
+
state,
|
|
1619
|
+
op.trackId,
|
|
1620
|
+
(track) => sortTrackClips({ ...track, clips: [...track.clips, op.clip] })
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
case "DELETE_CLIP": {
|
|
1624
|
+
return updateTrackOfClip(state, op.clipId, (track) => ({
|
|
1625
|
+
...track,
|
|
1626
|
+
clips: track.clips.filter((c) => c.id !== op.clipId)
|
|
1627
|
+
}));
|
|
1628
|
+
}
|
|
1629
|
+
case "MOVE_CLIP": {
|
|
1630
|
+
const targetTrackId = op.targetTrackId;
|
|
1631
|
+
let foundClip;
|
|
1632
|
+
for (const track of state.timeline.tracks) {
|
|
1633
|
+
const c = track.clips.find((c2) => c2.id === op.clipId);
|
|
1634
|
+
if (c) {
|
|
1635
|
+
foundClip = c;
|
|
1636
|
+
break;
|
|
1637
|
+
}
|
|
1638
|
+
}
|
|
1639
|
+
if (!foundClip) return state;
|
|
1640
|
+
const delta = op.newTimelineStart - foundClip.timelineStart;
|
|
1641
|
+
const movedClip = {
|
|
1642
|
+
...foundClip,
|
|
1643
|
+
trackId: targetTrackId ?? foundClip.trackId,
|
|
1644
|
+
timelineStart: op.newTimelineStart,
|
|
1645
|
+
timelineEnd: foundClip.timelineEnd + delta
|
|
1646
|
+
};
|
|
1647
|
+
const effectiveTargetTrackId = targetTrackId ?? foundClip.trackId;
|
|
1648
|
+
const isCrossTrack = effectiveTargetTrackId !== foundClip.trackId;
|
|
1649
|
+
if (!isCrossTrack) {
|
|
1650
|
+
return updateClip2(state, op.clipId, () => movedClip);
|
|
1651
|
+
}
|
|
1652
|
+
const newTracks = state.timeline.tracks.map((track) => {
|
|
1653
|
+
if (track.id === foundClip.trackId) {
|
|
1654
|
+
return { ...track, clips: track.clips.filter((c) => c.id !== op.clipId) };
|
|
1655
|
+
}
|
|
1656
|
+
if (track.id === effectiveTargetTrackId) {
|
|
1657
|
+
return sortTrackClips({ ...track, clips: [...track.clips, movedClip] });
|
|
1658
|
+
}
|
|
1659
|
+
return track;
|
|
1660
|
+
});
|
|
1661
|
+
return { ...state, timeline: { ...state.timeline, tracks: newTracks } };
|
|
1662
|
+
}
|
|
1663
|
+
case "RESIZE_CLIP": {
|
|
1664
|
+
return updateClip2(state, op.clipId, (clip) => {
|
|
1665
|
+
if (op.edge === "start") {
|
|
1666
|
+
const delta = op.newFrame - clip.timelineStart;
|
|
1667
|
+
return {
|
|
1668
|
+
...clip,
|
|
1669
|
+
timelineStart: op.newFrame,
|
|
1670
|
+
mediaIn: clip.mediaIn + delta
|
|
1671
|
+
};
|
|
1672
|
+
} else {
|
|
1673
|
+
const delta = op.newFrame - clip.timelineEnd;
|
|
1674
|
+
return {
|
|
1675
|
+
...clip,
|
|
1676
|
+
timelineEnd: op.newFrame,
|
|
1677
|
+
mediaOut: clip.mediaOut + delta
|
|
1678
|
+
};
|
|
1679
|
+
}
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
case "SLICE_CLIP": {
|
|
1683
|
+
return state;
|
|
1684
|
+
}
|
|
1685
|
+
case "SET_MEDIA_BOUNDS": {
|
|
1686
|
+
return updateClip2(state, op.clipId, (clip) => ({
|
|
1687
|
+
...clip,
|
|
1688
|
+
mediaIn: op.mediaIn,
|
|
1689
|
+
mediaOut: op.mediaOut
|
|
1690
|
+
}));
|
|
1691
|
+
}
|
|
1692
|
+
case "SET_CLIP_ENABLED": {
|
|
1693
|
+
return updateClip2(state, op.clipId, (clip) => ({ ...clip, enabled: op.enabled }));
|
|
1694
|
+
}
|
|
1695
|
+
case "SET_CLIP_REVERSED": {
|
|
1696
|
+
return updateClip2(state, op.clipId, (clip) => ({ ...clip, reversed: op.reversed }));
|
|
1697
|
+
}
|
|
1698
|
+
case "SET_CLIP_SPEED": {
|
|
1699
|
+
return updateClip2(state, op.clipId, (clip) => ({ ...clip, speed: op.speed }));
|
|
1700
|
+
}
|
|
1701
|
+
case "SET_CLIP_COLOR": {
|
|
1702
|
+
return updateClip2(state, op.clipId, (clip) => ({ ...clip, color: op.color }));
|
|
1703
|
+
}
|
|
1704
|
+
case "SET_CLIP_NAME": {
|
|
1705
|
+
return updateClip2(state, op.clipId, (clip) => ({ ...clip, name: op.name }));
|
|
1706
|
+
}
|
|
1707
|
+
// — Track operations ——————————————————————————————————————————————————
|
|
1708
|
+
case "ADD_TRACK": {
|
|
1709
|
+
return {
|
|
1710
|
+
...state,
|
|
1711
|
+
timeline: {
|
|
1712
|
+
...state.timeline,
|
|
1713
|
+
tracks: [...state.timeline.tracks, op.track]
|
|
1714
|
+
}
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
case "DELETE_TRACK": {
|
|
1718
|
+
return {
|
|
1719
|
+
...state,
|
|
1720
|
+
timeline: {
|
|
1721
|
+
...state.timeline,
|
|
1722
|
+
tracks: state.timeline.tracks.filter((t) => t.id !== op.trackId)
|
|
1723
|
+
}
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
case "REORDER_TRACK": {
|
|
1727
|
+
const tracks = [...state.timeline.tracks];
|
|
1728
|
+
const idx = tracks.findIndex((t) => t.id === op.trackId);
|
|
1729
|
+
if (idx === -1) return state;
|
|
1730
|
+
const [track] = tracks.splice(idx, 1);
|
|
1731
|
+
if (!track) return state;
|
|
1732
|
+
tracks.splice(op.newIndex, 0, track);
|
|
1733
|
+
return { ...state, timeline: { ...state.timeline, tracks } };
|
|
1734
|
+
}
|
|
1735
|
+
case "SET_TRACK_HEIGHT": {
|
|
1736
|
+
return updateTrack2(state, op.trackId, (t) => ({ ...t, height: op.height }));
|
|
1737
|
+
}
|
|
1738
|
+
case "SET_TRACK_NAME": {
|
|
1739
|
+
return updateTrack2(state, op.trackId, (t) => ({ ...t, name: op.name }));
|
|
1740
|
+
}
|
|
1741
|
+
// — Asset operations ——————————————————————————————————————————————————
|
|
1742
|
+
case "REGISTER_ASSET": {
|
|
1743
|
+
const next = new Map(state.assetRegistry);
|
|
1744
|
+
next.set(op.asset.id, op.asset);
|
|
1745
|
+
return { ...state, assetRegistry: next };
|
|
1746
|
+
}
|
|
1747
|
+
case "UNREGISTER_ASSET": {
|
|
1748
|
+
const next = new Map(state.assetRegistry);
|
|
1749
|
+
next.delete(op.assetId);
|
|
1750
|
+
return { ...state, assetRegistry: next };
|
|
1751
|
+
}
|
|
1752
|
+
case "SET_ASSET_STATUS": {
|
|
1753
|
+
const asset = state.assetRegistry.get(op.assetId);
|
|
1754
|
+
if (!asset) return state;
|
|
1755
|
+
const next = new Map(state.assetRegistry);
|
|
1756
|
+
next.set(op.assetId, { ...asset, status: op.status });
|
|
1757
|
+
return { ...state, assetRegistry: next };
|
|
1758
|
+
}
|
|
1759
|
+
// — Timeline operations ———————————————————————————————————————————————
|
|
1760
|
+
case "RENAME_TIMELINE": {
|
|
1761
|
+
return { ...state, timeline: { ...state.timeline, name: op.name } };
|
|
1762
|
+
}
|
|
1763
|
+
case "SET_TIMELINE_DURATION": {
|
|
1764
|
+
return { ...state, timeline: { ...state.timeline, duration: op.duration } };
|
|
1765
|
+
}
|
|
1766
|
+
case "SET_TIMELINE_START_TC": {
|
|
1767
|
+
return { ...state, timeline: { ...state.timeline, startTimecode: op.startTimecode } };
|
|
1768
|
+
}
|
|
1769
|
+
case "SET_SEQUENCE_SETTINGS": {
|
|
1770
|
+
return {
|
|
1771
|
+
...state,
|
|
1772
|
+
timeline: {
|
|
1773
|
+
...state.timeline,
|
|
1774
|
+
sequenceSettings: { ...state.timeline.sequenceSettings, ...op.settings }
|
|
1775
|
+
}
|
|
1776
|
+
};
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
function updateTrack2(state, trackId, fn) {
|
|
1781
|
+
return {
|
|
1782
|
+
...state,
|
|
1783
|
+
timeline: {
|
|
1784
|
+
...state.timeline,
|
|
1785
|
+
tracks: state.timeline.tracks.map((t) => t.id === trackId ? fn(t) : t)
|
|
1786
|
+
}
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
function updateTrackOfClip(state, clipId, fn) {
|
|
1790
|
+
return {
|
|
1791
|
+
...state,
|
|
1792
|
+
timeline: {
|
|
1793
|
+
...state.timeline,
|
|
1794
|
+
tracks: state.timeline.tracks.map(
|
|
1795
|
+
(t) => t.clips.some((c) => c.id === clipId) ? fn(t) : t
|
|
1796
|
+
)
|
|
1797
|
+
}
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
function updateClip2(state, clipId, fn) {
|
|
1801
|
+
return {
|
|
1802
|
+
...state,
|
|
1803
|
+
timeline: {
|
|
1804
|
+
...state.timeline,
|
|
1805
|
+
tracks: state.timeline.tracks.map((track) => {
|
|
1806
|
+
if (!track.clips.some((c) => c.id === clipId)) return track;
|
|
1807
|
+
const updatedTrack = {
|
|
1808
|
+
...track,
|
|
1809
|
+
clips: track.clips.map((c) => c.id === clipId ? fn(c) : c)
|
|
1810
|
+
};
|
|
1811
|
+
return sortTrackClips(updatedTrack);
|
|
1812
|
+
})
|
|
1813
|
+
}
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
// src/validation/validators.ts
|
|
1818
|
+
function validateOperation(state, op) {
|
|
1819
|
+
switch (op.type) {
|
|
1820
|
+
case "MOVE_CLIP":
|
|
1821
|
+
return validateMoveClip(state, op);
|
|
1822
|
+
case "RESIZE_CLIP":
|
|
1823
|
+
return validateResizeClip(state, op);
|
|
1824
|
+
case "SLICE_CLIP":
|
|
1825
|
+
return validateSliceClip(state, op);
|
|
1826
|
+
case "DELETE_CLIP":
|
|
1827
|
+
return validateDeleteClip(state, op);
|
|
1828
|
+
case "INSERT_CLIP":
|
|
1829
|
+
return validateInsertClip(state, op);
|
|
1830
|
+
case "SET_MEDIA_BOUNDS":
|
|
1831
|
+
return validateSetMediaBounds(state, op);
|
|
1832
|
+
case "SET_CLIP_SPEED":
|
|
1833
|
+
return validateSetClipSpeed(state, op);
|
|
1834
|
+
case "ADD_TRACK":
|
|
1835
|
+
return validateAddTrack(state, op);
|
|
1836
|
+
case "DELETE_TRACK":
|
|
1837
|
+
return validateDeleteTrack(state, op);
|
|
1838
|
+
case "UNREGISTER_ASSET":
|
|
1839
|
+
return validateUnregisterAsset(state, op);
|
|
1840
|
+
// All other ops are always valid at the primitive level
|
|
1841
|
+
default:
|
|
1842
|
+
return null;
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
function validateMoveClip(state, op) {
|
|
1846
|
+
const clip = findClip(state, op.clipId);
|
|
1847
|
+
if (!clip) return { reason: "ASSET_MISSING", message: `Clip '${op.clipId}' not found.` };
|
|
1848
|
+
const targetTrackId = op.targetTrackId ?? clip.trackId;
|
|
1849
|
+
const track = state.timeline.tracks.find((t) => t.id === targetTrackId);
|
|
1850
|
+
if (!track) return { reason: "OUT_OF_BOUNDS", message: `Track '${targetTrackId}' not found.` };
|
|
1851
|
+
if (track.locked) return { reason: "LOCKED_TRACK", message: `Track '${targetTrackId}' is locked.` };
|
|
1852
|
+
const duration = clip.timelineEnd - clip.timelineStart;
|
|
1853
|
+
const newEnd = op.newTimelineStart + duration;
|
|
1854
|
+
if (op.newTimelineStart < 0 || newEnd > state.timeline.duration) {
|
|
1855
|
+
return { reason: "OUT_OF_BOUNDS", message: `MOVE_CLIP would place clip '${op.clipId}' outside timeline bounds.` };
|
|
1856
|
+
}
|
|
1857
|
+
for (const existing of track.clips) {
|
|
1858
|
+
if (existing.id === op.clipId) continue;
|
|
1859
|
+
const overlaps = op.newTimelineStart < existing.timelineEnd && newEnd > existing.timelineStart;
|
|
1860
|
+
if (overlaps) {
|
|
1861
|
+
return { reason: "OVERLAP", message: `Clip '${op.clipId}' would overlap '${existing.id}' on track '${targetTrackId}'.` };
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
return null;
|
|
1865
|
+
}
|
|
1866
|
+
function validateResizeClip(state, op) {
|
|
1867
|
+
const clip = findClip(state, op.clipId);
|
|
1868
|
+
if (!clip) return { reason: "ASSET_MISSING", message: `Clip '${op.clipId}' not found.` };
|
|
1869
|
+
if (op.edge === "start" && op.newFrame >= clip.timelineEnd) {
|
|
1870
|
+
return { reason: "OUT_OF_BOUNDS", message: `RESIZE_CLIP start edge must be < timelineEnd.` };
|
|
1871
|
+
}
|
|
1872
|
+
if (op.edge === "end" && op.newFrame <= clip.timelineStart) {
|
|
1873
|
+
return { reason: "OUT_OF_BOUNDS", message: `RESIZE_CLIP end edge must be > timelineStart.` };
|
|
1874
|
+
}
|
|
1875
|
+
return null;
|
|
1876
|
+
}
|
|
1877
|
+
function validateSliceClip(state, op) {
|
|
1878
|
+
const clip = findClip(state, op.clipId);
|
|
1879
|
+
if (!clip) return { reason: "ASSET_MISSING", message: `Clip '${op.clipId}' not found.` };
|
|
1880
|
+
if (op.atFrame <= clip.timelineStart || op.atFrame >= clip.timelineEnd) {
|
|
1881
|
+
return { reason: "OUT_OF_BOUNDS", message: `SLICE_CLIP atFrame must be strictly inside the clip bounds.` };
|
|
1882
|
+
}
|
|
1883
|
+
return null;
|
|
1884
|
+
}
|
|
1885
|
+
function validateDeleteClip(state, op) {
|
|
1886
|
+
const clip = findClip(state, op.clipId);
|
|
1887
|
+
if (!clip) return { reason: "ASSET_MISSING", message: `Clip '${op.clipId}' not found.` };
|
|
1888
|
+
const track = state.timeline.tracks.find((t) => t.id === clip.trackId);
|
|
1889
|
+
if (track?.locked) return { reason: "LOCKED_TRACK", message: `Track '${clip.trackId}' is locked.` };
|
|
1890
|
+
return null;
|
|
1891
|
+
}
|
|
1892
|
+
function validateInsertClip(state, op) {
|
|
1893
|
+
const track = state.timeline.tracks.find((t) => t.id === op.trackId);
|
|
1894
|
+
if (!track) return { reason: "OUT_OF_BOUNDS", message: `Track '${op.trackId}' not found.` };
|
|
1895
|
+
if (track.locked) return { reason: "LOCKED_TRACK", message: `Track '${op.trackId}' is locked.` };
|
|
1896
|
+
const asset = state.assetRegistry.get(op.clip.assetId);
|
|
1897
|
+
if (!asset) return { reason: "ASSET_MISSING", message: `Asset '${op.clip.assetId}' not in registry.` };
|
|
1898
|
+
if (asset.mediaType !== track.type) return { reason: "TYPE_MISMATCH", message: `Asset mediaType '${asset.mediaType}' \u2260 track type '${track.type}'.` };
|
|
1899
|
+
for (const existing of track.clips) {
|
|
1900
|
+
const overlaps = op.clip.timelineStart < existing.timelineEnd && op.clip.timelineEnd > existing.timelineStart;
|
|
1901
|
+
if (overlaps) {
|
|
1902
|
+
return { reason: "OVERLAP", message: `INSERT_CLIP would overlap '${existing.id}'.` };
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
return null;
|
|
1906
|
+
}
|
|
1907
|
+
function validateSetMediaBounds(state, op) {
|
|
1908
|
+
const clip = findClip(state, op.clipId);
|
|
1909
|
+
if (!clip) return { reason: "ASSET_MISSING", message: `Clip '${op.clipId}' not found.` };
|
|
1910
|
+
const asset = state.assetRegistry.get(clip.assetId);
|
|
1911
|
+
if (!asset) return { reason: "ASSET_MISSING", message: `Asset '${clip.assetId}' not found.` };
|
|
1912
|
+
if (op.mediaIn < 0) return { reason: "MEDIA_BOUNDS_INVALID", message: `mediaIn must be >= 0.` };
|
|
1913
|
+
if (op.mediaOut > asset.intrinsicDuration) {
|
|
1914
|
+
return { reason: "MEDIA_BOUNDS_INVALID", message: `mediaOut (${op.mediaOut}) exceeds asset intrinsicDuration (${asset.intrinsicDuration}).` };
|
|
1915
|
+
}
|
|
1916
|
+
return null;
|
|
1917
|
+
}
|
|
1918
|
+
function validateSetClipSpeed(_state, op) {
|
|
1919
|
+
if (op.speed <= 0) return { reason: "SPEED_INVALID", message: `speed must be > 0, got ${op.speed}.` };
|
|
1920
|
+
return null;
|
|
1921
|
+
}
|
|
1922
|
+
function validateAddTrack(state, op) {
|
|
1923
|
+
if (state.timeline.tracks.some((t) => t.id === op.track.id)) {
|
|
1924
|
+
return { reason: "OVERLAP", message: `Track '${op.track.id}' already exists.` };
|
|
1925
|
+
}
|
|
1926
|
+
return null;
|
|
1927
|
+
}
|
|
1928
|
+
function validateDeleteTrack(state, op) {
|
|
1929
|
+
const track = state.timeline.tracks.find((t) => t.id === op.trackId);
|
|
1930
|
+
if (!track) return { reason: "OUT_OF_BOUNDS", message: `Track '${op.trackId}' not found.` };
|
|
1931
|
+
if (track.clips.length > 0) return { reason: "TRACK_NOT_EMPTY", message: `Cannot delete track '${op.trackId}': it has ${track.clips.length} clips. Delete all clips first.` };
|
|
1932
|
+
return null;
|
|
1933
|
+
}
|
|
1934
|
+
function validateUnregisterAsset(state, op) {
|
|
1935
|
+
for (const track of state.timeline.tracks) {
|
|
1936
|
+
for (const clip of track.clips) {
|
|
1937
|
+
if (clip.assetId === op.assetId) {
|
|
1938
|
+
return { reason: "ASSET_IN_USE", message: `Asset '${op.assetId}' is referenced by clip '${clip.id}'.` };
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
return null;
|
|
1943
|
+
}
|
|
1944
|
+
function findClip(state, clipId) {
|
|
1945
|
+
for (const track of state.timeline.tracks) {
|
|
1946
|
+
const clip = track.clips.find((c) => c.id === clipId);
|
|
1947
|
+
if (clip) return clip;
|
|
1948
|
+
}
|
|
1949
|
+
return void 0;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
// src/engine/dispatcher.ts
|
|
1953
|
+
function dispatch(state, transaction) {
|
|
1954
|
+
for (const op of transaction.operations) {
|
|
1955
|
+
const rejection = validateOperation(state, op);
|
|
1956
|
+
if (rejection) {
|
|
1957
|
+
return {
|
|
1958
|
+
accepted: false,
|
|
1959
|
+
reason: rejection.reason,
|
|
1960
|
+
message: rejection.message
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
let proposedState = state;
|
|
1965
|
+
for (const op of transaction.operations) {
|
|
1966
|
+
proposedState = applyOperation2(proposedState, op);
|
|
1967
|
+
}
|
|
1968
|
+
const violations = checkInvariants(proposedState);
|
|
1969
|
+
if (violations.length > 0) {
|
|
1970
|
+
return {
|
|
1971
|
+
accepted: false,
|
|
1972
|
+
reason: "INVARIANT_VIOLATED",
|
|
1973
|
+
message: violations.map((v) => v.message).join("; ")
|
|
1974
|
+
};
|
|
1975
|
+
}
|
|
1976
|
+
const nextState = {
|
|
1977
|
+
...proposedState,
|
|
1978
|
+
timeline: {
|
|
1979
|
+
...proposedState.timeline,
|
|
1980
|
+
version: state.timeline.version + 1
|
|
1981
|
+
}
|
|
1982
|
+
};
|
|
1983
|
+
return { accepted: true, nextState };
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
// src/snap-index.ts
|
|
1987
|
+
var PRIORITIES = {
|
|
1988
|
+
Marker: 100,
|
|
1989
|
+
InPoint: 90,
|
|
1990
|
+
OutPoint: 90,
|
|
1991
|
+
ClipStart: 80,
|
|
1992
|
+
ClipEnd: 80,
|
|
1993
|
+
Playhead: 70,
|
|
1994
|
+
BeatGrid: 50
|
|
1995
|
+
};
|
|
1996
|
+
function buildSnapIndex(state, playheadFrame, enabled = true) {
|
|
1997
|
+
const points = [];
|
|
1998
|
+
for (const track of state.timeline.tracks) {
|
|
1999
|
+
for (const clip of track.clips) {
|
|
2000
|
+
points.push({
|
|
2001
|
+
frame: clip.timelineStart,
|
|
2002
|
+
type: "ClipStart",
|
|
2003
|
+
priority: PRIORITIES.ClipStart,
|
|
2004
|
+
trackId: track.id,
|
|
2005
|
+
sourceId: clip.id
|
|
2006
|
+
});
|
|
2007
|
+
points.push({
|
|
2008
|
+
frame: clip.timelineEnd,
|
|
2009
|
+
type: "ClipEnd",
|
|
2010
|
+
priority: PRIORITIES.ClipEnd,
|
|
2011
|
+
trackId: track.id,
|
|
2012
|
+
sourceId: clip.id
|
|
2013
|
+
});
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
points.push({
|
|
2017
|
+
frame: playheadFrame,
|
|
2018
|
+
type: "Playhead",
|
|
2019
|
+
priority: PRIORITIES.Playhead,
|
|
2020
|
+
trackId: null,
|
|
2021
|
+
sourceId: "__playhead__"
|
|
2022
|
+
});
|
|
2023
|
+
points.sort((a, b) => a.frame - b.frame);
|
|
2024
|
+
return { points, builtAt: Date.now(), enabled };
|
|
2025
|
+
}
|
|
2026
|
+
function nearest(index, frame2, radiusFrames, exclude, allowedTypes) {
|
|
2027
|
+
if (!index.enabled) return null;
|
|
2028
|
+
const excludeSet = exclude ? new Set(exclude) : null;
|
|
2029
|
+
let best = null;
|
|
2030
|
+
let bestDist = Infinity;
|
|
2031
|
+
for (const point of index.points) {
|
|
2032
|
+
if (excludeSet && excludeSet.has(point.sourceId)) continue;
|
|
2033
|
+
if (allowedTypes && !allowedTypes.includes(point.type)) continue;
|
|
2034
|
+
const dist = Math.abs(point.frame - frame2);
|
|
2035
|
+
if (dist > radiusFrames) continue;
|
|
2036
|
+
if (dist < bestDist || dist === bestDist && best !== null && point.priority > best.priority) {
|
|
2037
|
+
best = point;
|
|
2038
|
+
bestDist = dist;
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
return best;
|
|
2042
|
+
}
|
|
2043
|
+
function toggleSnap(index, enabled) {
|
|
2044
|
+
return { ...index, enabled };
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
// src/tools/types.ts
|
|
2048
|
+
function toToolId(s) {
|
|
2049
|
+
return s;
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
// src/tools/registry.ts
|
|
2053
|
+
function createRegistry(tools, defaultId) {
|
|
2054
|
+
const map = /* @__PURE__ */ new Map();
|
|
2055
|
+
for (const tool of tools) {
|
|
2056
|
+
map.set(tool.id, tool);
|
|
2057
|
+
}
|
|
2058
|
+
if (!map.has(defaultId)) {
|
|
2059
|
+
throw new Error(
|
|
2060
|
+
`createRegistry: defaultId "${defaultId}" not found in tools. Available: [${[...map.keys()].join(", ")}]`
|
|
2061
|
+
);
|
|
2062
|
+
}
|
|
2063
|
+
return { tools: map, activeToolId: defaultId };
|
|
2064
|
+
}
|
|
2065
|
+
function activateTool(registry, id) {
|
|
2066
|
+
getActiveTool(registry).onCancel();
|
|
2067
|
+
if (!registry.tools.has(id)) {
|
|
2068
|
+
throw new Error(
|
|
2069
|
+
`activateTool: unknown toolId "${id}". Registered: [${[...registry.tools.keys()].join(", ")}]`
|
|
2070
|
+
);
|
|
2071
|
+
}
|
|
2072
|
+
return { ...registry, activeToolId: id };
|
|
2073
|
+
}
|
|
2074
|
+
function getActiveTool(registry) {
|
|
2075
|
+
const tool = registry.tools.get(registry.activeToolId);
|
|
2076
|
+
if (!tool) {
|
|
2077
|
+
throw new Error(
|
|
2078
|
+
`getActiveTool: activeToolId "${registry.activeToolId}" is not registered. Registry is corrupt.`
|
|
2079
|
+
);
|
|
2080
|
+
}
|
|
2081
|
+
return tool;
|
|
2082
|
+
}
|
|
2083
|
+
function registerTool(registry, tool) {
|
|
2084
|
+
const next = new Map(registry.tools);
|
|
2085
|
+
next.set(tool.id, tool);
|
|
2086
|
+
return { ...registry, tools: next };
|
|
2087
|
+
}
|
|
2088
|
+
var NoOpTool = {
|
|
2089
|
+
id: toToolId("noop"),
|
|
2090
|
+
shortcutKey: "",
|
|
2091
|
+
getCursor(_ctx) {
|
|
2092
|
+
return "default";
|
|
2093
|
+
},
|
|
2094
|
+
getSnapCandidateTypes() {
|
|
2095
|
+
return [];
|
|
2096
|
+
},
|
|
2097
|
+
onPointerDown(_evt, _ctx) {
|
|
2098
|
+
},
|
|
2099
|
+
onPointerMove(_evt, _ctx) {
|
|
2100
|
+
return null;
|
|
2101
|
+
},
|
|
2102
|
+
onPointerUp(_evt, _ctx) {
|
|
2103
|
+
return null;
|
|
2104
|
+
},
|
|
2105
|
+
onKeyDown(_evt, _ctx) {
|
|
2106
|
+
return null;
|
|
2107
|
+
},
|
|
2108
|
+
onKeyUp(_evt, _ctx) {
|
|
2109
|
+
},
|
|
2110
|
+
onCancel() {
|
|
2111
|
+
}
|
|
2112
|
+
};
|
|
2113
|
+
|
|
2114
|
+
// src/tools/provisional.ts
|
|
2115
|
+
function createProvisionalManager() {
|
|
2116
|
+
return { current: null };
|
|
2117
|
+
}
|
|
2118
|
+
function setProvisional(_manager, state) {
|
|
2119
|
+
return { current: state };
|
|
2120
|
+
}
|
|
2121
|
+
function clearProvisional(_manager) {
|
|
2122
|
+
return { current: null };
|
|
2123
|
+
}
|
|
2124
|
+
function resolveClip(clipId, state, manager) {
|
|
2125
|
+
if (manager.current !== null) {
|
|
2126
|
+
const ghost = manager.current.clips.find((c) => c.id === clipId);
|
|
2127
|
+
if (ghost) return ghost;
|
|
2128
|
+
}
|
|
2129
|
+
for (const track of state.timeline.tracks) {
|
|
2130
|
+
const clip = track.clips.find((c) => c.id === clipId);
|
|
2131
|
+
if (clip) return clip;
|
|
2132
|
+
}
|
|
2133
|
+
return void 0;
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
export {
|
|
2137
|
+
createTimeline,
|
|
2138
|
+
toTrackId,
|
|
2139
|
+
createTrack,
|
|
2140
|
+
sortTrackClips,
|
|
2141
|
+
toClipId,
|
|
2142
|
+
createClip,
|
|
2143
|
+
getClipDuration,
|
|
2144
|
+
getClipMediaDuration,
|
|
2145
|
+
clipContainsFrame,
|
|
2146
|
+
clipsOverlap,
|
|
2147
|
+
toAssetId,
|
|
2148
|
+
createAsset,
|
|
2149
|
+
CURRENT_SCHEMA_VERSION,
|
|
2150
|
+
createTimelineState,
|
|
2151
|
+
toFrame,
|
|
2152
|
+
frame,
|
|
2153
|
+
FrameRates,
|
|
2154
|
+
frameRate,
|
|
2155
|
+
toTimecode,
|
|
2156
|
+
isValidFrame,
|
|
2157
|
+
isDropFrame,
|
|
2158
|
+
framesToSeconds,
|
|
2159
|
+
secondsToFrames,
|
|
2160
|
+
framesToTimecode,
|
|
2161
|
+
framesToMinutesSeconds,
|
|
2162
|
+
clampFrame,
|
|
2163
|
+
addFrames,
|
|
2164
|
+
subtractFrames,
|
|
2165
|
+
frameDuration,
|
|
2166
|
+
createHistory,
|
|
2167
|
+
pushHistory,
|
|
2168
|
+
undo,
|
|
2169
|
+
redo,
|
|
2170
|
+
canUndo,
|
|
2171
|
+
canRedo,
|
|
2172
|
+
getCurrentState,
|
|
2173
|
+
findClipById,
|
|
2174
|
+
findTrackById,
|
|
2175
|
+
getClipsOnTrack,
|
|
2176
|
+
getClipsAtFrame,
|
|
2177
|
+
getClipsInRange,
|
|
2178
|
+
getAllClips,
|
|
2179
|
+
getAllTracks,
|
|
2180
|
+
findTrackIndex,
|
|
2181
|
+
validResult,
|
|
2182
|
+
invalidResult,
|
|
2183
|
+
invalidResults,
|
|
2184
|
+
combineResults,
|
|
2185
|
+
registerAsset,
|
|
2186
|
+
getAsset,
|
|
2187
|
+
hasAsset,
|
|
2188
|
+
getAllAssets,
|
|
2189
|
+
unregisterAsset,
|
|
2190
|
+
validateClip,
|
|
2191
|
+
validateTrack,
|
|
2192
|
+
validateTimeline,
|
|
2193
|
+
validateNoOverlap,
|
|
2194
|
+
addClip,
|
|
2195
|
+
removeClip,
|
|
2196
|
+
moveClip,
|
|
2197
|
+
resizeClip,
|
|
2198
|
+
trimClip,
|
|
2199
|
+
updateClip,
|
|
2200
|
+
moveClipToTrack,
|
|
2201
|
+
addTrack,
|
|
2202
|
+
removeTrack,
|
|
2203
|
+
moveTrack,
|
|
2204
|
+
updateTrack,
|
|
2205
|
+
toggleTrackMute,
|
|
2206
|
+
toggleTrackLock,
|
|
2207
|
+
setTimelineDuration,
|
|
2208
|
+
setTimelineName,
|
|
2209
|
+
rippleDelete,
|
|
2210
|
+
rippleTrim,
|
|
2211
|
+
insertEdit,
|
|
2212
|
+
rippleMove,
|
|
2213
|
+
insertMove,
|
|
2214
|
+
TimelineEngine,
|
|
2215
|
+
checkInvariants,
|
|
2216
|
+
dispatch,
|
|
2217
|
+
buildSnapIndex,
|
|
2218
|
+
nearest,
|
|
2219
|
+
toggleSnap,
|
|
2220
|
+
toToolId,
|
|
2221
|
+
createRegistry,
|
|
2222
|
+
activateTool,
|
|
2223
|
+
getActiveTool,
|
|
2224
|
+
registerTool,
|
|
2225
|
+
NoOpTool,
|
|
2226
|
+
createProvisionalManager,
|
|
2227
|
+
setProvisional,
|
|
2228
|
+
clearProvisional,
|
|
2229
|
+
resolveClip
|
|
2230
|
+
};
|