@flycut/timeline-kit 0.0.0-stage → 0.2.0
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/LICENSE +661 -0
- package/README.md +225 -2
- package/dist/chunks/TimelineEditor-N6_Yud4z.js +2046 -0
- package/dist/chunks/TimelineEditor-N6_Yud4z.js.map +1 -0
- package/dist/chunks/index-KK9cAE8q.js +661 -0
- package/dist/chunks/index-KK9cAE8q.js.map +1 -0
- package/dist/chunks/interactionManager-JoPxZ8st.js +775 -0
- package/dist/chunks/interactionManager-JoPxZ8st.js.map +1 -0
- package/dist/chunks/otio-6KO_Tfi1.js +510 -0
- package/dist/chunks/otio-6KO_Tfi1.js.map +1 -0
- package/dist/core.js +1673 -0
- package/dist/core.js.map +1 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/dist/otio.js +313 -0
- package/dist/otio.js.map +1 -0
- package/dist/plugins.js +243 -0
- package/dist/plugins.js.map +1 -0
- package/dist/react.js +259 -0
- package/dist/react.js.map +1 -0
- package/dist/types/adapters/react/index.d.ts +45 -0
- package/dist/types/adapters/vue/index.d.ts +202 -0
- package/dist/types/src/adapters/otio.d.ts +7 -0
- package/dist/types/src/core-entry.d.ts +8 -0
- package/dist/types/src/editor/TimelineEditor.d.ts +96 -0
- package/dist/types/src/editor/index.d.ts +2 -0
- package/dist/types/src/editor/types.d.ts +30 -0
- package/dist/types/src/engine/hitTest.d.ts +23 -0
- package/dist/types/src/engine/index.d.ts +7 -0
- package/dist/types/src/engine/layout.d.ts +47 -0
- package/dist/types/src/engine/magnet.d.ts +46 -0
- package/dist/types/src/engine/placement.d.ts +13 -0
- package/dist/types/src/engine/snap.d.ts +28 -0
- package/dist/types/src/engine/timeScale.d.ts +12 -0
- package/dist/types/src/engine/virtualization.d.ts +11 -0
- package/dist/types/src/frameworks/slotBridge.d.ts +13 -0
- package/dist/types/src/index.d.ts +10 -0
- package/dist/types/src/model/demoDocument.d.ts +3 -0
- package/dist/types/src/model/index.d.ts +4 -0
- package/dist/types/src/model/operations.d.ts +77 -0
- package/dist/types/src/model/structuralSharing.d.ts +11 -0
- package/dist/types/src/model/types.d.ts +55 -0
- package/dist/types/src/otio-core/commands/create.d.ts +6 -0
- package/dist/types/src/otio-core/commands/helpers.d.ts +5 -0
- package/dist/types/src/otio-core/commands/insert.d.ts +2 -0
- package/dist/types/src/otio-core/commands/move.d.ts +2 -0
- package/dist/types/src/otio-core/commands/split.d.ts +2 -0
- package/dist/types/src/otio-core/commands/trim.d.ts +3 -0
- package/dist/types/src/otio-core/flycut-otio-draft.d.ts +239 -0
- package/dist/types/src/otio-core/guards.d.ts +5 -0
- package/dist/types/src/otio-core/index.d.ts +14 -0
- package/dist/types/src/otio-core/media/clipMetadata.d.ts +18 -0
- package/dist/types/src/otio-core/media/reference.d.ts +9 -0
- package/dist/types/src/otio-core/selectors/clip.d.ts +4 -0
- package/dist/types/src/otio-core/selectors/timeline.d.ts +5 -0
- package/dist/types/src/otio-core/selectors/track.d.ts +8 -0
- package/dist/types/src/otio-core/types.d.ts +61 -0
- package/dist/types/src/otio-entry.d.ts +2 -0
- package/dist/types/src/plugins/clipDragPlugin.d.ts +2 -0
- package/dist/types/src/plugins/clipTrimPlugin.d.ts +2 -0
- package/dist/types/src/plugins/externalDropPlugin.d.ts +13 -0
- package/dist/types/src/plugins/index.d.ts +17 -0
- package/dist/types/src/plugins/interactionManager.d.ts +33 -0
- package/dist/types/src/plugins/marqueeSelectPlugin.d.ts +2 -0
- package/dist/types/src/plugins/playheadPlugin.d.ts +2 -0
- package/dist/types/src/plugins/selectionPlugin.d.ts +2 -0
- package/dist/types/src/plugins/shortcutsPlugin.d.ts +7 -0
- package/dist/types/src/plugins/snapProviders.d.ts +9 -0
- package/dist/types/src/plugins/types.d.ts +110 -0
- package/dist/types/src/plugins/wheelPlugin.d.ts +2 -0
- package/dist/types/src/plugins-entry.d.ts +1 -0
- package/dist/types/src/renderer/domRenderer.d.ts +72 -0
- package/dist/types/src/renderer/index.d.ts +6 -0
- package/dist/types/src/renderer/ruler.d.ts +18 -0
- package/dist/types/src/renderer/styles.d.ts +1 -0
- package/dist/types/src/renderer/types.d.ts +65 -0
- package/dist/types/src/renderer/ui/icons.d.ts +7 -0
- package/dist/types/src/renderer/ui/sidebar.d.ts +14 -0
- package/dist/types/src/renderer/ui/toolbar.d.ts +16 -0
- package/dist/types/src/store/builtinCommands.d.ts +56 -0
- package/dist/types/src/store/commandBus.d.ts +18 -0
- package/dist/types/src/store/index.d.ts +4 -0
- package/dist/types/src/store/store.d.ts +53 -0
- package/dist/types/src/store/types.d.ts +59 -0
- package/dist/types/src/utils/trimOffset.d.ts +9 -0
- package/dist/vue.js +278 -0
- package/dist/vue.js.map +1 -0
- package/package.json +111 -4
package/dist/core.js
ADDED
|
@@ -0,0 +1,1673 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* timeline-kit
|
|
3
|
+
* Copyright (c) 2026 timeline-kit authors <x007xyzabc@gmail.com>
|
|
4
|
+
* Dual-licensed: AGPL-3.0-only (see LICENSE) or a commercial license.
|
|
5
|
+
* Commercial licensing: x007xyzabc@gmail.com
|
|
6
|
+
*/
|
|
7
|
+
import { D as DEFAULT_EDIT_POLICIES, r as pxToMs, i as getFramePixel, l as getPixelsPerSecond, p as msToPx, b as clampTrackScale, g as frameToMs } from "./chunks/interactionManager-JoPxZ8st.js";
|
|
8
|
+
import { I, c, a, d, e, f, h, j, k, m, n, o, q, s, t, u } from "./chunks/interactionManager-JoPxZ8st.js";
|
|
9
|
+
import { D, d as d2, f as f2, i, t as t2 } from "./chunks/otio-6KO_Tfi1.js";
|
|
10
|
+
function clonePlainValue(value) {
|
|
11
|
+
if (value === null || typeof value !== "object") {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
return value.map((item) => clonePlainValue(item));
|
|
16
|
+
}
|
|
17
|
+
const record = value;
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const key of Object.keys(record)) {
|
|
20
|
+
result[key] = clonePlainValue(record[key]);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function clamp(value, min, max) {
|
|
25
|
+
return Math.max(min, Math.min(max, value));
|
|
26
|
+
}
|
|
27
|
+
function getMinClipDurationMs(doc, policies) {
|
|
28
|
+
if (typeof policies.minClipDurationMs === "number" && policies.minClipDurationMs > 0) {
|
|
29
|
+
return policies.minClipDurationMs;
|
|
30
|
+
}
|
|
31
|
+
return doc.fps > 0 ? 1e3 / doc.fps : 40;
|
|
32
|
+
}
|
|
33
|
+
function collectExistingClipIds(doc) {
|
|
34
|
+
const ids = /* @__PURE__ */ new Set();
|
|
35
|
+
for (const track of doc.tracks) {
|
|
36
|
+
for (const clip of track.clips) {
|
|
37
|
+
ids.add(clip.id);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return ids;
|
|
41
|
+
}
|
|
42
|
+
function createUniqueClipId(baseId, existingIds) {
|
|
43
|
+
const root = baseId || "clip";
|
|
44
|
+
let index = 2;
|
|
45
|
+
let candidate = `${root}-part-${index}`;
|
|
46
|
+
while (existingIds.has(candidate)) {
|
|
47
|
+
index += 1;
|
|
48
|
+
candidate = `${root}-part-${index}`;
|
|
49
|
+
}
|
|
50
|
+
return candidate;
|
|
51
|
+
}
|
|
52
|
+
function createUniqueTrackId(trackType, existingTracks) {
|
|
53
|
+
const existingIds = new Set(existingTracks.map((track) => track.id));
|
|
54
|
+
const prefix = trackType || "track";
|
|
55
|
+
let index = 1;
|
|
56
|
+
let candidate = `${prefix}-${index}`;
|
|
57
|
+
while (existingIds.has(candidate)) {
|
|
58
|
+
index += 1;
|
|
59
|
+
candidate = `${prefix}-${index}`;
|
|
60
|
+
}
|
|
61
|
+
return candidate;
|
|
62
|
+
}
|
|
63
|
+
function createTrackForClipType(trackType, existingTracks) {
|
|
64
|
+
const normalized = trackType.toLowerCase();
|
|
65
|
+
return {
|
|
66
|
+
id: createUniqueTrackId(trackType, existingTracks),
|
|
67
|
+
name: `${trackType || "track"} Track`,
|
|
68
|
+
type: trackType || "video",
|
|
69
|
+
clips: [],
|
|
70
|
+
locked: false,
|
|
71
|
+
visible: ["video", "image", "text"].includes(normalized) ? true : void 0,
|
|
72
|
+
muted: ["video", "audio"].includes(normalized) ? false : void 0
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function cloneClip(clip) {
|
|
76
|
+
return {
|
|
77
|
+
...clip,
|
|
78
|
+
trim: clip.trim ? { ...clip.trim } : void 0,
|
|
79
|
+
meta: clip.meta !== void 0 ? clonePlainValue(clip.meta) : void 0
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function cloneTrack(track) {
|
|
83
|
+
return {
|
|
84
|
+
...track,
|
|
85
|
+
clips: track.clips.map((clip) => cloneClip(clip)),
|
|
86
|
+
meta: track.meta !== void 0 ? clonePlainValue(track.meta) : void 0
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function cloneDocument(doc) {
|
|
90
|
+
return {
|
|
91
|
+
...doc,
|
|
92
|
+
tracks: doc.tracks.map((track) => cloneTrack(track)),
|
|
93
|
+
meta: doc.meta !== void 0 ? clonePlainValue(doc.meta) : void 0
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function getDocumentDurationMs(doc) {
|
|
97
|
+
let maxEndMs = 0;
|
|
98
|
+
for (const track of doc.tracks) {
|
|
99
|
+
for (const clip of track.clips) {
|
|
100
|
+
if (clip.endMs > maxEndMs) {
|
|
101
|
+
maxEndMs = clip.endMs;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return maxEndMs;
|
|
106
|
+
}
|
|
107
|
+
function findClipLocation(doc, clipId) {
|
|
108
|
+
if (!clipId) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
for (let trackIndex = 0; trackIndex < doc.tracks.length; trackIndex += 1) {
|
|
112
|
+
const track = doc.tracks[trackIndex];
|
|
113
|
+
for (let clipIndex = 0; clipIndex < track.clips.length; clipIndex += 1) {
|
|
114
|
+
const clip = track.clips[clipIndex];
|
|
115
|
+
if (clip.id === clipId) {
|
|
116
|
+
return { trackIndex, clipIndex, track, clip };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
function resolveSelection(doc, clipIds, primaryClipId) {
|
|
123
|
+
const seen = /* @__PURE__ */ new Set();
|
|
124
|
+
const validClipIds = [];
|
|
125
|
+
for (const id of clipIds) {
|
|
126
|
+
if (!id || seen.has(id)) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (findClipLocation(doc, id)) {
|
|
130
|
+
seen.add(id);
|
|
131
|
+
validClipIds.push(id);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (validClipIds.length === 0) {
|
|
135
|
+
return {
|
|
136
|
+
trackIndex: -1,
|
|
137
|
+
clipIndex: -1,
|
|
138
|
+
clipId: null,
|
|
139
|
+
clipIds: []
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const chosenPrimary = primaryClipId && seen.has(primaryClipId) ? primaryClipId : validClipIds[0];
|
|
143
|
+
const location = findClipLocation(doc, chosenPrimary);
|
|
144
|
+
if (!location) {
|
|
145
|
+
return {
|
|
146
|
+
trackIndex: -1,
|
|
147
|
+
clipIndex: -1,
|
|
148
|
+
clipId: null,
|
|
149
|
+
clipIds: []
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
trackIndex: location.trackIndex,
|
|
154
|
+
clipIndex: location.clipIndex,
|
|
155
|
+
clipId: location.clip.id,
|
|
156
|
+
clipIds: validClipIds
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function cleanupTracks(doc, policies = DEFAULT_EDIT_POLICIES, magnetEnabled = false) {
|
|
160
|
+
const nextDoc = cloneDocument(doc);
|
|
161
|
+
let changed = false;
|
|
162
|
+
if (magnetEnabled) {
|
|
163
|
+
for (const track of nextDoc.tracks) {
|
|
164
|
+
if (track.main !== true || track.clips.length === 0) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
track.clips.sort((left, right) => left.startMs - right.startMs);
|
|
168
|
+
let cursorMs = 0;
|
|
169
|
+
for (const clip of track.clips) {
|
|
170
|
+
const durationMs = Math.max(0, clip.endMs - clip.startMs);
|
|
171
|
+
if (clip.startMs !== cursorMs || clip.endMs !== cursorMs + durationMs) {
|
|
172
|
+
clip.startMs = cursorMs;
|
|
173
|
+
clip.endMs = cursorMs + durationMs;
|
|
174
|
+
changed = true;
|
|
175
|
+
}
|
|
176
|
+
cursorMs += durationMs;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (policies.removeEmptyTracks) {
|
|
181
|
+
const nonEmpty = nextDoc.tracks.filter((track) => track.clips.length > 0);
|
|
182
|
+
if (nonEmpty.length !== nextDoc.tracks.length) {
|
|
183
|
+
nextDoc.tracks = nonEmpty;
|
|
184
|
+
changed = true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (policies.orderAudioToBottom) {
|
|
188
|
+
const nonAudio = nextDoc.tracks.filter((track) => track.type !== "audio");
|
|
189
|
+
const audio = nextDoc.tracks.filter((track) => track.type === "audio");
|
|
190
|
+
const reordered = [...nonAudio, ...audio];
|
|
191
|
+
const orderChanged = reordered.some((track, index) => track !== nextDoc.tracks[index]);
|
|
192
|
+
if (orderChanged) {
|
|
193
|
+
nextDoc.tracks = reordered;
|
|
194
|
+
changed = true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
document: changed ? nextDoc : doc,
|
|
199
|
+
changed
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function trimClip(doc, params, policies = DEFAULT_EDIT_POLICIES) {
|
|
203
|
+
const sourceTrack = doc.tracks[params.trackIndex];
|
|
204
|
+
const sourceClip = sourceTrack?.clips[params.clipIndex];
|
|
205
|
+
if (!sourceTrack || !sourceClip || sourceTrack.locked === true || params.offsetMs === 0) {
|
|
206
|
+
return { document: doc, changed: false };
|
|
207
|
+
}
|
|
208
|
+
const nextDoc = cloneDocument(doc);
|
|
209
|
+
const track = nextDoc.tracks[params.trackIndex];
|
|
210
|
+
const clip = track.clips[params.clipIndex];
|
|
211
|
+
const minMs = getMinClipDurationMs(doc, policies);
|
|
212
|
+
const initialStart = clip.startMs;
|
|
213
|
+
const initialEnd = clip.endMs;
|
|
214
|
+
const initialDuration = Math.max(minMs, initialEnd - initialStart);
|
|
215
|
+
let leftBoundary = 0;
|
|
216
|
+
let rightBoundary = Infinity;
|
|
217
|
+
if (!policies.allowOverlap) {
|
|
218
|
+
for (let index = 0; index < track.clips.length; index += 1) {
|
|
219
|
+
if (index === params.clipIndex) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const other = track.clips[index];
|
|
223
|
+
if (other.endMs <= initialStart) {
|
|
224
|
+
leftBoundary = Math.max(leftBoundary, other.endMs);
|
|
225
|
+
}
|
|
226
|
+
if (other.startMs >= initialEnd) {
|
|
227
|
+
rightBoundary = Math.min(rightBoundary, other.startMs);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
const isMediaBounded = clip.type === "video" || clip.type === "audio" || clip.trim !== void 0 || clip.durationMs !== void 0 && clip.durationMs > 0;
|
|
232
|
+
if (isMediaBounded) {
|
|
233
|
+
const origTrim = clip.trim ? { ...clip.trim } : { startMs: 0, endMs: initialDuration };
|
|
234
|
+
const maxSourceDuration = clip.durationMs && clip.durationMs > 0 ? Math.max(clip.durationMs, origTrim.endMs) : Math.max(initialDuration, origTrim.endMs);
|
|
235
|
+
if (params.edge === "start") {
|
|
236
|
+
const minStartByTrim = initialStart - origTrim.startMs;
|
|
237
|
+
const minAllowedStart = Math.max(0, leftBoundary, minStartByTrim);
|
|
238
|
+
const maxAllowedStart = Math.min(rightBoundary - minMs, initialEnd - minMs);
|
|
239
|
+
if (minAllowedStart > maxAllowedStart) {
|
|
240
|
+
return { document: doc, changed: false };
|
|
241
|
+
}
|
|
242
|
+
const nextStart = clamp(initialStart + params.offsetMs, minAllowedStart, maxAllowedStart);
|
|
243
|
+
if (nextStart === initialStart) {
|
|
244
|
+
return { document: doc, changed: false };
|
|
245
|
+
}
|
|
246
|
+
const appliedDelta = nextStart - initialStart;
|
|
247
|
+
clip.startMs = nextStart;
|
|
248
|
+
clip.trim = {
|
|
249
|
+
startMs: Math.max(0, origTrim.startMs + appliedDelta),
|
|
250
|
+
endMs: origTrim.endMs
|
|
251
|
+
};
|
|
252
|
+
} else {
|
|
253
|
+
const maxEndByTrim = initialStart - origTrim.startMs + maxSourceDuration;
|
|
254
|
+
const minAllowedEnd = initialStart + minMs;
|
|
255
|
+
const maxAllowedEnd = Math.min(rightBoundary, maxEndByTrim);
|
|
256
|
+
if (minAllowedEnd > maxAllowedEnd) {
|
|
257
|
+
return { document: doc, changed: false };
|
|
258
|
+
}
|
|
259
|
+
const nextEnd = clamp(initialEnd + params.offsetMs, minAllowedEnd, maxAllowedEnd);
|
|
260
|
+
if (nextEnd === initialEnd) {
|
|
261
|
+
return { document: doc, changed: false };
|
|
262
|
+
}
|
|
263
|
+
clip.endMs = nextEnd;
|
|
264
|
+
clip.trim = {
|
|
265
|
+
startMs: origTrim.startMs,
|
|
266
|
+
endMs: origTrim.startMs + (nextEnd - initialStart)
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
if (params.edge === "start") {
|
|
271
|
+
const minAllowedStart = Math.max(0, leftBoundary);
|
|
272
|
+
const maxAllowedStart = Math.min(rightBoundary - minMs, initialEnd - minMs);
|
|
273
|
+
if (minAllowedStart > maxAllowedStart) {
|
|
274
|
+
return { document: doc, changed: false };
|
|
275
|
+
}
|
|
276
|
+
const nextStart = clamp(initialStart + params.offsetMs, minAllowedStart, maxAllowedStart);
|
|
277
|
+
if (nextStart === initialStart) {
|
|
278
|
+
return { document: doc, changed: false };
|
|
279
|
+
}
|
|
280
|
+
clip.startMs = nextStart;
|
|
281
|
+
} else {
|
|
282
|
+
const minAllowedEnd = initialStart + minMs;
|
|
283
|
+
const maxAllowedEnd = rightBoundary;
|
|
284
|
+
if (minAllowedEnd > maxAllowedEnd) {
|
|
285
|
+
return { document: doc, changed: false };
|
|
286
|
+
}
|
|
287
|
+
const nextEnd = clamp(initialEnd + params.offsetMs, minAllowedEnd, maxAllowedEnd);
|
|
288
|
+
if (nextEnd === initialEnd) {
|
|
289
|
+
return { document: doc, changed: false };
|
|
290
|
+
}
|
|
291
|
+
clip.endMs = nextEnd;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return { document: nextDoc, changed: true };
|
|
295
|
+
}
|
|
296
|
+
function splitClipAtMs(doc, params, policies = DEFAULT_EDIT_POLICIES) {
|
|
297
|
+
const sourceTrack = doc.tracks[params.trackIndex];
|
|
298
|
+
const sourceClip = sourceTrack?.clips[params.clipIndex];
|
|
299
|
+
if (!sourceTrack || !sourceClip || sourceTrack.locked === true) {
|
|
300
|
+
return { document: doc, leftClipId: null, rightClipId: null, changed: false };
|
|
301
|
+
}
|
|
302
|
+
const minMs = getMinClipDurationMs(doc, policies);
|
|
303
|
+
const leftDurationMs = params.splitMs - sourceClip.startMs;
|
|
304
|
+
const rightDurationMs = sourceClip.endMs - params.splitMs;
|
|
305
|
+
if (leftDurationMs < minMs || rightDurationMs < minMs) {
|
|
306
|
+
return { document: doc, leftClipId: null, rightClipId: null, changed: false };
|
|
307
|
+
}
|
|
308
|
+
const nextDoc = cloneDocument(doc);
|
|
309
|
+
const track = nextDoc.tracks[params.trackIndex];
|
|
310
|
+
const originalClip = track.clips[params.clipIndex];
|
|
311
|
+
const existingIds = collectExistingClipIds(nextDoc);
|
|
312
|
+
const rightClipId = createUniqueClipId(originalClip.id, existingIds);
|
|
313
|
+
const leftClip = cloneClip(originalClip);
|
|
314
|
+
leftClip.endMs = params.splitMs;
|
|
315
|
+
const rightClip = cloneClip(originalClip);
|
|
316
|
+
rightClip.id = rightClipId;
|
|
317
|
+
rightClip.startMs = params.splitMs;
|
|
318
|
+
if (originalClip.trim || originalClip.durationMs !== void 0 || originalClip.type === "video" || originalClip.type === "audio") {
|
|
319
|
+
const baseTrimStart = originalClip.trim?.startMs ?? 0;
|
|
320
|
+
const baseTrimEnd = originalClip.trim?.endMs ?? baseTrimStart + (originalClip.endMs - originalClip.startMs);
|
|
321
|
+
leftClip.trim = {
|
|
322
|
+
startMs: baseTrimStart,
|
|
323
|
+
endMs: baseTrimStart + leftDurationMs
|
|
324
|
+
};
|
|
325
|
+
rightClip.trim = {
|
|
326
|
+
startMs: baseTrimStart + leftDurationMs,
|
|
327
|
+
endMs: baseTrimEnd
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
track.clips.splice(params.clipIndex, 1, leftClip, rightClip);
|
|
331
|
+
return {
|
|
332
|
+
document: nextDoc,
|
|
333
|
+
leftClipId: leftClip.id,
|
|
334
|
+
rightClipId: rightClip.id,
|
|
335
|
+
changed: true
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
function deleteClips(doc, clipIds, policies = DEFAULT_EDIT_POLICIES, magnetEnabled = false) {
|
|
339
|
+
if (clipIds.length === 0) {
|
|
340
|
+
return { document: doc, changed: false };
|
|
341
|
+
}
|
|
342
|
+
const targets = new Set(clipIds);
|
|
343
|
+
const nextDoc = cloneDocument(doc);
|
|
344
|
+
let removed = false;
|
|
345
|
+
for (const track of nextDoc.tracks) {
|
|
346
|
+
if (track.locked === true) {
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
const remaining = track.clips.filter((clip) => !targets.has(clip.id));
|
|
350
|
+
if (remaining.length !== track.clips.length) {
|
|
351
|
+
track.clips = remaining;
|
|
352
|
+
removed = true;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (!removed) {
|
|
356
|
+
return { document: doc, changed: false };
|
|
357
|
+
}
|
|
358
|
+
const cleaned = cleanupTracks(nextDoc, policies, magnetEnabled);
|
|
359
|
+
return {
|
|
360
|
+
document: cleaned.document,
|
|
361
|
+
changed: true
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
function hasOverlapInTrack(track, startMs, endMs, excludeClipId) {
|
|
365
|
+
for (const existing of track.clips) {
|
|
366
|
+
if (excludeClipId && existing.id === excludeClipId) {
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
if (startMs < existing.endMs && endMs > existing.startMs) {
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
function moveClip(doc, params, policies = DEFAULT_EDIT_POLICIES, magnetEnabled = false) {
|
|
376
|
+
const sourceTrack = doc.tracks[params.sourceTrackIndex];
|
|
377
|
+
const sourceClip = sourceTrack?.clips[params.sourceClipIndex];
|
|
378
|
+
if (!sourceTrack || !sourceClip || sourceTrack.locked === true) {
|
|
379
|
+
return {
|
|
380
|
+
document: doc,
|
|
381
|
+
selection: resolveSelection(doc, sourceClip ? [sourceClip.id] : []),
|
|
382
|
+
changed: false
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
const durationMs = Math.max(0, params.endMs - params.startMs);
|
|
386
|
+
const nextStartMs = Math.max(0, Math.round(params.startMs));
|
|
387
|
+
const nextEndMs = nextStartMs + Math.round(durationMs);
|
|
388
|
+
if (!params.isNewTrack && params.targetTrackIndex === params.sourceTrackIndex && nextStartMs === sourceClip.startMs && nextEndMs === sourceClip.endMs) {
|
|
389
|
+
return {
|
|
390
|
+
document: doc,
|
|
391
|
+
selection: resolveSelection(doc, [sourceClip.id], sourceClip.id),
|
|
392
|
+
changed: false
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
if (!params.isNewTrack) {
|
|
396
|
+
const targetTrack = doc.tracks[params.targetTrackIndex];
|
|
397
|
+
if (!targetTrack || targetTrack.locked === true) {
|
|
398
|
+
return {
|
|
399
|
+
document: doc,
|
|
400
|
+
selection: resolveSelection(doc, [sourceClip.id]),
|
|
401
|
+
changed: false
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
if (!policies.allowCrossTrackType && targetTrack.type !== sourceClip.type) {
|
|
405
|
+
return {
|
|
406
|
+
document: doc,
|
|
407
|
+
selection: resolveSelection(doc, [sourceClip.id]),
|
|
408
|
+
changed: false
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
const isTargetMagnet = targetTrack.main === true && magnetEnabled;
|
|
412
|
+
if (!policies.allowOverlap && !isTargetMagnet) {
|
|
413
|
+
if (hasOverlapInTrack(targetTrack, nextStartMs, nextEndMs, sourceClip.id)) {
|
|
414
|
+
return {
|
|
415
|
+
document: doc,
|
|
416
|
+
selection: resolveSelection(doc, [sourceClip.id]),
|
|
417
|
+
changed: false
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (policies.canDrop && !policies.canDrop({
|
|
422
|
+
clip: sourceClip,
|
|
423
|
+
sourceTrack,
|
|
424
|
+
targetTrack,
|
|
425
|
+
startMs: nextStartMs,
|
|
426
|
+
endMs: nextEndMs
|
|
427
|
+
})) {
|
|
428
|
+
return {
|
|
429
|
+
document: doc,
|
|
430
|
+
selection: resolveSelection(doc, [sourceClip.id]),
|
|
431
|
+
changed: false
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const nextDoc = cloneDocument(doc);
|
|
436
|
+
const nextSourceTrack = nextDoc.tracks[params.sourceTrackIndex];
|
|
437
|
+
const movedClip = nextSourceTrack.clips[params.sourceClipIndex];
|
|
438
|
+
movedClip.startMs = nextStartMs;
|
|
439
|
+
movedClip.endMs = nextEndMs;
|
|
440
|
+
if (!params.isNewTrack && params.targetTrackIndex === params.sourceTrackIndex) {
|
|
441
|
+
nextSourceTrack.clips.sort((left, right) => left.startMs - right.startMs);
|
|
442
|
+
} else {
|
|
443
|
+
nextSourceTrack.clips.splice(params.sourceClipIndex, 1);
|
|
444
|
+
if (params.isNewTrack) {
|
|
445
|
+
const newTrack = createTrackForClipType(params.clipType ?? movedClip.type, nextDoc.tracks);
|
|
446
|
+
newTrack.clips.push(movedClip);
|
|
447
|
+
const insertIndex = clamp(params.targetTrackIndex, 0, nextDoc.tracks.length);
|
|
448
|
+
nextDoc.tracks.splice(insertIndex, 0, newTrack);
|
|
449
|
+
} else {
|
|
450
|
+
const nextTargetTrack = nextDoc.tracks[params.targetTrackIndex];
|
|
451
|
+
nextTargetTrack.clips.push(movedClip);
|
|
452
|
+
nextTargetTrack.clips.sort((left, right) => left.startMs - right.startMs);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
const cleaned = cleanupTracks(nextDoc, policies, magnetEnabled);
|
|
456
|
+
return {
|
|
457
|
+
document: cleaned.document,
|
|
458
|
+
selection: resolveSelection(cleaned.document, [movedClip.id], movedClip.id),
|
|
459
|
+
changed: true
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function applyMagnetMove(doc, params, policies = DEFAULT_EDIT_POLICIES) {
|
|
463
|
+
const sourceTrack = doc.tracks[params.sourceTrackIndex];
|
|
464
|
+
const sourceClip = sourceTrack?.clips[params.sourceClipIndex];
|
|
465
|
+
const targetTrack = doc.tracks[params.targetTrackIndex];
|
|
466
|
+
if (!sourceTrack || !sourceClip || !targetTrack || sourceTrack.locked === true || targetTrack.locked === true) {
|
|
467
|
+
return {
|
|
468
|
+
document: doc,
|
|
469
|
+
selection: resolveSelection(doc, sourceClip ? [sourceClip.id] : []),
|
|
470
|
+
changed: false
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
const nextDoc = cloneDocument(doc);
|
|
474
|
+
const nextSourceTrack = nextDoc.tracks[params.sourceTrackIndex];
|
|
475
|
+
const [dragClip] = nextSourceTrack.clips.splice(params.sourceClipIndex, 1);
|
|
476
|
+
const nextTargetTrack = nextDoc.tracks[params.targetTrackIndex];
|
|
477
|
+
let dragStartMs = 0;
|
|
478
|
+
for (let index = 0; index < params.insertPosition; index += 1) {
|
|
479
|
+
const prev = params.previewArrangement[index];
|
|
480
|
+
if (prev) {
|
|
481
|
+
dragStartMs = prev.endMs;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const updatedDragClip = cloneClip(dragClip);
|
|
485
|
+
updatedDragClip.startMs = dragStartMs;
|
|
486
|
+
updatedDragClip.endMs = dragStartMs + Math.max(0, params.dragClipDurationMs);
|
|
487
|
+
const rebuiltClips = [
|
|
488
|
+
...params.previewArrangement.slice(0, params.insertPosition).map((clip) => cloneClip(clip)),
|
|
489
|
+
updatedDragClip,
|
|
490
|
+
...params.previewArrangement.slice(params.insertPosition).map((clip) => cloneClip(clip))
|
|
491
|
+
];
|
|
492
|
+
nextTargetTrack.clips = rebuiltClips;
|
|
493
|
+
const cleaned = cleanupTracks(nextDoc, policies, true);
|
|
494
|
+
return {
|
|
495
|
+
document: cleaned.document,
|
|
496
|
+
selection: resolveSelection(cleaned.document, [updatedDragClip.id], updatedDragClip.id),
|
|
497
|
+
changed: true
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function insertClip(doc, params, policies = DEFAULT_EDIT_POLICIES, magnetEnabled = false) {
|
|
501
|
+
const existingIds = collectExistingClipIds(doc);
|
|
502
|
+
const insertedClip = cloneClip(params.clip);
|
|
503
|
+
if (!insertedClip.id || existingIds.has(insertedClip.id)) {
|
|
504
|
+
insertedClip.id = createUniqueClipId(insertedClip.id || "clip", existingIds);
|
|
505
|
+
}
|
|
506
|
+
insertedClip.startMs = Math.max(0, Math.round(insertedClip.startMs));
|
|
507
|
+
insertedClip.endMs = Math.max(insertedClip.startMs + 1, Math.round(insertedClip.endMs));
|
|
508
|
+
if (!params.isNewTrack) {
|
|
509
|
+
const targetTrack = doc.tracks[params.trackIndex];
|
|
510
|
+
if (!targetTrack || targetTrack.locked === true) {
|
|
511
|
+
return {
|
|
512
|
+
document: doc,
|
|
513
|
+
selection: resolveSelection(doc, []),
|
|
514
|
+
changed: false
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
if (!policies.allowCrossTrackType && targetTrack.type !== insertedClip.type) {
|
|
518
|
+
return {
|
|
519
|
+
document: doc,
|
|
520
|
+
selection: resolveSelection(doc, []),
|
|
521
|
+
changed: false
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
const isTargetMagnet = targetTrack.main === true && magnetEnabled;
|
|
525
|
+
if (!policies.allowOverlap && !isTargetMagnet) {
|
|
526
|
+
if (hasOverlapInTrack(targetTrack, insertedClip.startMs, insertedClip.endMs)) {
|
|
527
|
+
return {
|
|
528
|
+
document: doc,
|
|
529
|
+
selection: resolveSelection(doc, []),
|
|
530
|
+
changed: false
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (policies.canDrop && !policies.canDrop({
|
|
535
|
+
clip: insertedClip,
|
|
536
|
+
targetTrack,
|
|
537
|
+
startMs: insertedClip.startMs,
|
|
538
|
+
endMs: insertedClip.endMs
|
|
539
|
+
})) {
|
|
540
|
+
return {
|
|
541
|
+
document: doc,
|
|
542
|
+
selection: resolveSelection(doc, []),
|
|
543
|
+
changed: false
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
const nextDoc = cloneDocument(doc);
|
|
548
|
+
if (params.isNewTrack) {
|
|
549
|
+
const newTrack = createTrackForClipType(insertedClip.type, nextDoc.tracks);
|
|
550
|
+
newTrack.clips.push(insertedClip);
|
|
551
|
+
const insertIndex = clamp(params.trackIndex, 0, nextDoc.tracks.length);
|
|
552
|
+
nextDoc.tracks.splice(insertIndex, 0, newTrack);
|
|
553
|
+
} else {
|
|
554
|
+
const targetTrack = nextDoc.tracks[params.trackIndex];
|
|
555
|
+
targetTrack.clips.push(insertedClip);
|
|
556
|
+
targetTrack.clips.sort((left, right) => left.startMs - right.startMs);
|
|
557
|
+
}
|
|
558
|
+
const cleaned = cleanupTracks(nextDoc, policies, magnetEnabled);
|
|
559
|
+
return {
|
|
560
|
+
document: cleaned.document,
|
|
561
|
+
selection: resolveSelection(cleaned.document, [insertedClip.id], insertedClip.id),
|
|
562
|
+
changed: true
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function toggleTrackFlag(doc, trackIndex, flag) {
|
|
566
|
+
const sourceTrack = doc.tracks[trackIndex];
|
|
567
|
+
if (!sourceTrack) {
|
|
568
|
+
return { document: doc, changed: false };
|
|
569
|
+
}
|
|
570
|
+
const nextDoc = cloneDocument(doc);
|
|
571
|
+
const track = nextDoc.tracks[trackIndex];
|
|
572
|
+
if (flag === "visible") {
|
|
573
|
+
track.visible = !(track.visible ?? true);
|
|
574
|
+
} else if (flag === "locked") {
|
|
575
|
+
track.locked = !(track.locked ?? false);
|
|
576
|
+
} else {
|
|
577
|
+
track.muted = !(track.muted ?? false);
|
|
578
|
+
}
|
|
579
|
+
return { document: nextDoc, changed: true };
|
|
580
|
+
}
|
|
581
|
+
function findTrackByY(tracks, y) {
|
|
582
|
+
for (const track of tracks) {
|
|
583
|
+
if (y >= track.startY && y <= track.endY) {
|
|
584
|
+
return track;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
function hitTestLayout(layout, doc, options) {
|
|
590
|
+
const safeX = Math.max(0, options.x);
|
|
591
|
+
const timeMs = Math.max(0, Math.round(pxToMs(safeX, layout.pixelsPerSecond)));
|
|
592
|
+
const frame = layout.fps > 0 ? Math.max(0, Math.round(timeMs / 1e3 * layout.fps)) : 0;
|
|
593
|
+
const trimHandleWidthPx = options.trimHandleWidthPx ?? 8;
|
|
594
|
+
const minClipWidthPx = options.minClipWidthPx ?? 32;
|
|
595
|
+
if (options.zoneHint === "playhead" || options.zoneHint === "ruler" || options.zoneHint === "toolbar" || options.zoneHint === "sidebar" || options.zoneHint === "outside") {
|
|
596
|
+
return {
|
|
597
|
+
zone: options.zoneHint,
|
|
598
|
+
timeMs,
|
|
599
|
+
frame,
|
|
600
|
+
x: safeX,
|
|
601
|
+
y: options.y,
|
|
602
|
+
trackIndex: options.hintTrackIndex ?? -1,
|
|
603
|
+
clipIndex: -1,
|
|
604
|
+
clipId: null
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
if (options.hintClipId && (options.zoneHint === "trim-start" || options.zoneHint === "trim-end" || options.zoneHint === "clip-body")) {
|
|
608
|
+
for (let trackIndex = 0; trackIndex < doc.tracks.length; trackIndex += 1) {
|
|
609
|
+
const track = doc.tracks[trackIndex];
|
|
610
|
+
const clipIndex = track.clips.findIndex((clip) => clip.id === options.hintClipId);
|
|
611
|
+
if (clipIndex !== -1) {
|
|
612
|
+
return {
|
|
613
|
+
zone: options.zoneHint,
|
|
614
|
+
timeMs,
|
|
615
|
+
frame,
|
|
616
|
+
x: safeX,
|
|
617
|
+
y: options.y,
|
|
618
|
+
trackIndex,
|
|
619
|
+
clipIndex,
|
|
620
|
+
clipId: options.hintClipId
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
const layoutTrack = findTrackByY(layout.tracks, options.y);
|
|
626
|
+
if (!layoutTrack) {
|
|
627
|
+
return {
|
|
628
|
+
zone: "track-empty",
|
|
629
|
+
timeMs,
|
|
630
|
+
frame,
|
|
631
|
+
x: safeX,
|
|
632
|
+
y: options.y,
|
|
633
|
+
trackIndex: -1,
|
|
634
|
+
clipIndex: -1,
|
|
635
|
+
clipId: null
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
for (let index = layoutTrack.clips.length - 1; index >= 0; index -= 1) {
|
|
639
|
+
const layoutClip = layoutTrack.clips[index];
|
|
640
|
+
const clipLeft = layoutClip.leftPx;
|
|
641
|
+
const clipWidth = Math.max(layoutClip.widthPx, minClipWidthPx);
|
|
642
|
+
const clipRight = clipLeft + clipWidth;
|
|
643
|
+
if (options.x >= clipLeft - trimHandleWidthPx && options.x <= clipLeft + trimHandleWidthPx / 2) {
|
|
644
|
+
return {
|
|
645
|
+
zone: "trim-start",
|
|
646
|
+
timeMs,
|
|
647
|
+
frame,
|
|
648
|
+
x: safeX,
|
|
649
|
+
y: options.y,
|
|
650
|
+
trackIndex: layoutClip.trackIndex,
|
|
651
|
+
clipIndex: layoutClip.clipIndex,
|
|
652
|
+
clipId: layoutClip.clip.id
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
if (options.x >= clipRight - trimHandleWidthPx / 2 && options.x <= clipRight + trimHandleWidthPx) {
|
|
656
|
+
return {
|
|
657
|
+
zone: "trim-end",
|
|
658
|
+
timeMs,
|
|
659
|
+
frame,
|
|
660
|
+
x: safeX,
|
|
661
|
+
y: options.y,
|
|
662
|
+
trackIndex: layoutClip.trackIndex,
|
|
663
|
+
clipIndex: layoutClip.clipIndex,
|
|
664
|
+
clipId: layoutClip.clip.id
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
if (options.x >= clipLeft && options.x <= clipRight) {
|
|
668
|
+
return {
|
|
669
|
+
zone: "clip-body",
|
|
670
|
+
timeMs,
|
|
671
|
+
frame,
|
|
672
|
+
x: safeX,
|
|
673
|
+
y: options.y,
|
|
674
|
+
trackIndex: layoutClip.trackIndex,
|
|
675
|
+
clipIndex: layoutClip.clipIndex,
|
|
676
|
+
clipId: layoutClip.clip.id
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return {
|
|
681
|
+
zone: "track-empty",
|
|
682
|
+
timeMs,
|
|
683
|
+
frame,
|
|
684
|
+
x: safeX,
|
|
685
|
+
y: options.y,
|
|
686
|
+
trackIndex: layoutTrack.trackIndex,
|
|
687
|
+
clipIndex: -1,
|
|
688
|
+
clipId: null
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
const DEFAULT_TRACK_HEIGHT = 64;
|
|
692
|
+
const DEFAULT_TRACK_GAP = 4;
|
|
693
|
+
const DEFAULT_TRACK_PADDING_TOP = 20;
|
|
694
|
+
function getTrackHeight(config) {
|
|
695
|
+
return typeof config?.height === "number" && config.height > 0 ? config.height : DEFAULT_TRACK_HEIGHT;
|
|
696
|
+
}
|
|
697
|
+
function computeTimelineLayout(doc, options) {
|
|
698
|
+
const fps = doc.fps > 0 ? doc.fps : 24;
|
|
699
|
+
const framePixel = getFramePixel(options.trackScale, fps);
|
|
700
|
+
const pixelsPerSecond = getPixelsPerSecond(options.trackScale, fps);
|
|
701
|
+
const trackGap = options.trackGap ?? DEFAULT_TRACK_GAP;
|
|
702
|
+
const paddingTop = options.paddingTop ?? DEFAULT_TRACK_PADDING_TOP;
|
|
703
|
+
const trackConfigs = options.trackConfigs ?? {};
|
|
704
|
+
const trackHeights = doc.tracks.map((track) => getTrackHeight(trackConfigs[track.type]));
|
|
705
|
+
const totalTrackHeight = trackHeights.reduce((sum, height) => sum + height, 0);
|
|
706
|
+
const totalGapHeight = Math.max(0, (trackHeights.length - 1) * trackGap);
|
|
707
|
+
const totalRequiredHeight = totalTrackHeight + totalGapHeight;
|
|
708
|
+
const viewportHeight = options.viewportHeight ?? 0;
|
|
709
|
+
const centeredOffset = viewportHeight > 0 && totalRequiredHeight < viewportHeight ? Math.max(paddingTop, (viewportHeight - totalRequiredHeight) / 2) : paddingTop;
|
|
710
|
+
let currentY = centeredOffset;
|
|
711
|
+
const tracks = doc.tracks.map((track, trackIndex) => {
|
|
712
|
+
const height = trackHeights[trackIndex];
|
|
713
|
+
const clips = track.clips.map((clip, clipIndex) => {
|
|
714
|
+
const startMs = Math.max(0, clip.startMs);
|
|
715
|
+
const endMs = Math.max(startMs, clip.endMs);
|
|
716
|
+
const leftPx = msToPx(startMs, pixelsPerSecond);
|
|
717
|
+
const widthPx = msToPx(endMs - startMs, pixelsPerSecond);
|
|
718
|
+
return {
|
|
719
|
+
clip,
|
|
720
|
+
track,
|
|
721
|
+
trackIndex,
|
|
722
|
+
clipIndex,
|
|
723
|
+
type: clip.type || track.type,
|
|
724
|
+
startMs,
|
|
725
|
+
endMs,
|
|
726
|
+
leftPx,
|
|
727
|
+
widthPx
|
|
728
|
+
};
|
|
729
|
+
});
|
|
730
|
+
const startY = currentY;
|
|
731
|
+
const endY = startY + height;
|
|
732
|
+
currentY = endY + trackGap;
|
|
733
|
+
return {
|
|
734
|
+
track,
|
|
735
|
+
trackIndex,
|
|
736
|
+
type: track.type,
|
|
737
|
+
height,
|
|
738
|
+
startY,
|
|
739
|
+
endY,
|
|
740
|
+
clips
|
|
741
|
+
};
|
|
742
|
+
});
|
|
743
|
+
const totalDurationMs = getDocumentDurationMs(doc);
|
|
744
|
+
const contentHeight = tracks.length > 0 ? tracks[tracks.length - 1].endY + centeredOffset : centeredOffset * 2;
|
|
745
|
+
return {
|
|
746
|
+
fps,
|
|
747
|
+
trackScale: options.trackScale,
|
|
748
|
+
framePixel,
|
|
749
|
+
pixelsPerSecond,
|
|
750
|
+
totalDurationMs,
|
|
751
|
+
totalDurationSec: totalDurationMs / 1e3,
|
|
752
|
+
contentTop: centeredOffset,
|
|
753
|
+
contentBottom: centeredOffset,
|
|
754
|
+
contentHeight,
|
|
755
|
+
tracks
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
const DEFAULT_OVERSCAN_PX = 240;
|
|
759
|
+
function computeVisibleRange(scrollLeft, viewportWidth, pixelsPerSecond, paddingLeft = 0, overscanPx = DEFAULT_OVERSCAN_PX) {
|
|
760
|
+
const safeWidth = Math.max(0, viewportWidth);
|
|
761
|
+
const startPx = Math.max(0, scrollLeft - paddingLeft - overscanPx);
|
|
762
|
+
const endPx = Math.max(startPx, scrollLeft - paddingLeft + safeWidth + overscanPx);
|
|
763
|
+
return {
|
|
764
|
+
startPx,
|
|
765
|
+
endPx,
|
|
766
|
+
startMs: pxToMs(startPx, pixelsPerSecond),
|
|
767
|
+
endMs: pxToMs(endPx, pixelsPerSecond)
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
function isClipInVisibleRange(clip, range) {
|
|
771
|
+
return clip.endMs >= range.startMs && clip.startMs <= range.endMs;
|
|
772
|
+
}
|
|
773
|
+
function filterVisibleClips(clips, range, alwaysIncludeIds) {
|
|
774
|
+
return clips.filter((clipLayout) => {
|
|
775
|
+
if (alwaysIncludeIds?.has(clipLayout.clip.id)) {
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
return isClipInVisibleRange(clipLayout, range);
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
function deepEqual(a2, b) {
|
|
782
|
+
if (Object.is(a2, b)) {
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
if (typeof a2 !== "object" || typeof b !== "object" || a2 === null || b === null) {
|
|
786
|
+
return false;
|
|
787
|
+
}
|
|
788
|
+
if (Array.isArray(a2) !== Array.isArray(b)) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
if (Array.isArray(a2)) {
|
|
792
|
+
const other = b;
|
|
793
|
+
if (a2.length !== other.length) {
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
796
|
+
for (let i2 = 0; i2 < a2.length; i2 += 1) {
|
|
797
|
+
if (!deepEqual(a2[i2], other[i2])) {
|
|
798
|
+
return false;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
return true;
|
|
802
|
+
}
|
|
803
|
+
const ra = a2;
|
|
804
|
+
const rb = b;
|
|
805
|
+
const keysA = Object.keys(ra).filter((key) => ra[key] !== void 0);
|
|
806
|
+
const keysB = Object.keys(rb).filter((key) => rb[key] !== void 0);
|
|
807
|
+
if (keysA.length !== keysB.length) {
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
for (const key of keysA) {
|
|
811
|
+
if (!deepEqual(ra[key], rb[key])) {
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
return true;
|
|
816
|
+
}
|
|
817
|
+
function fieldsEqual(a2, b, skip) {
|
|
818
|
+
const ra = a2;
|
|
819
|
+
const rb = b;
|
|
820
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(ra), ...Object.keys(rb)]);
|
|
821
|
+
for (const key of keys) {
|
|
822
|
+
if (key === skip) {
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
if (!deepEqual(ra[key], rb[key])) {
|
|
826
|
+
return false;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return true;
|
|
830
|
+
}
|
|
831
|
+
function shareStructure(prev, next) {
|
|
832
|
+
if (!prev || prev === next) {
|
|
833
|
+
return next;
|
|
834
|
+
}
|
|
835
|
+
const prevClips = /* @__PURE__ */ new Map();
|
|
836
|
+
const prevTracks = /* @__PURE__ */ new Map();
|
|
837
|
+
for (const track of prev.tracks) {
|
|
838
|
+
prevTracks.set(track.id, track);
|
|
839
|
+
for (const clip of track.clips) {
|
|
840
|
+
prevClips.set(clip.id, clip);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
let allTracksReused = next.tracks.length === prev.tracks.length;
|
|
844
|
+
const tracks = next.tracks.map((track, trackIndex) => {
|
|
845
|
+
let clipsReused = true;
|
|
846
|
+
const clips = track.clips.map((clip) => {
|
|
847
|
+
const old = prevClips.get(clip.id);
|
|
848
|
+
if (old && fieldsEqual(old, clip, "")) {
|
|
849
|
+
return old;
|
|
850
|
+
}
|
|
851
|
+
clipsReused = false;
|
|
852
|
+
return clip;
|
|
853
|
+
});
|
|
854
|
+
const oldTrack = prevTracks.get(track.id);
|
|
855
|
+
if (oldTrack && clipsReused && oldTrack.clips.length === clips.length && oldTrack.clips.every((clip, index) => clip === clips[index]) && fieldsEqual(oldTrack, track, "clips")) {
|
|
856
|
+
if (prev.tracks[trackIndex] !== oldTrack) {
|
|
857
|
+
allTracksReused = false;
|
|
858
|
+
}
|
|
859
|
+
return oldTrack;
|
|
860
|
+
}
|
|
861
|
+
allTracksReused = false;
|
|
862
|
+
return clipsReused && track.clips.every((clip, index) => clip === clips[index]) ? track : { ...track, clips };
|
|
863
|
+
});
|
|
864
|
+
if (allTracksReused && fieldsEqual(prev, next, "tracks")) {
|
|
865
|
+
return prev;
|
|
866
|
+
}
|
|
867
|
+
return { ...next, tracks };
|
|
868
|
+
}
|
|
869
|
+
const DEFAULT_RENDER_HINTS = {
|
|
870
|
+
zoomActive: false,
|
|
871
|
+
settleVersion: 0
|
|
872
|
+
};
|
|
873
|
+
const ALL_DATA_CHANNELS = [
|
|
874
|
+
"tracks",
|
|
875
|
+
"sidebar",
|
|
876
|
+
"ruler",
|
|
877
|
+
"toolbar",
|
|
878
|
+
"selection",
|
|
879
|
+
"playhead"
|
|
880
|
+
];
|
|
881
|
+
function selectionsEqual(a2, b) {
|
|
882
|
+
if (a2.clipId !== b.clipId || a2.trackIndex !== b.trackIndex || a2.clipIndex !== b.clipIndex) {
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
885
|
+
if (a2.clipIds.length !== b.clipIds.length) {
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
for (let index = 0; index < a2.clipIds.length; index += 1) {
|
|
889
|
+
if (a2.clipIds[index] !== b.clipIds[index]) {
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
return true;
|
|
894
|
+
}
|
|
895
|
+
class TimelineStore {
|
|
896
|
+
state;
|
|
897
|
+
historyMode;
|
|
898
|
+
maxHistory;
|
|
899
|
+
historyPast;
|
|
900
|
+
historyFuture;
|
|
901
|
+
externalCanUndo;
|
|
902
|
+
externalCanRedo;
|
|
903
|
+
listeners;
|
|
904
|
+
constructor(options) {
|
|
905
|
+
const initialDoc = cloneDocument(options.document);
|
|
906
|
+
this.historyMode = options.historyMode ?? "internal";
|
|
907
|
+
this.maxHistory = options.maxHistory ?? 50;
|
|
908
|
+
this.historyPast = [];
|
|
909
|
+
this.historyFuture = [];
|
|
910
|
+
this.externalCanUndo = false;
|
|
911
|
+
this.externalCanRedo = false;
|
|
912
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
913
|
+
this.state = {
|
|
914
|
+
document: initialDoc,
|
|
915
|
+
currentFrame: Math.max(0, Math.round(options.currentFrame ?? 0)),
|
|
916
|
+
trackScale: clampTrackScale(options.trackScale ?? 50),
|
|
917
|
+
isMagnetEnabled: options.isMagnetEnabled ?? false,
|
|
918
|
+
selection: {
|
|
919
|
+
trackIndex: -1,
|
|
920
|
+
clipIndex: -1,
|
|
921
|
+
clipId: null,
|
|
922
|
+
clipIds: []
|
|
923
|
+
},
|
|
924
|
+
canUndo: false,
|
|
925
|
+
canRedo: false,
|
|
926
|
+
renderHints: {
|
|
927
|
+
...DEFAULT_RENDER_HINTS,
|
|
928
|
+
...options.renderHints ?? {}
|
|
929
|
+
},
|
|
930
|
+
theme: options.theme ?? "auto"
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
getState() {
|
|
934
|
+
return this.state;
|
|
935
|
+
}
|
|
936
|
+
getSnapshot() {
|
|
937
|
+
return {
|
|
938
|
+
...this.state,
|
|
939
|
+
document: cloneDocument(this.state.document),
|
|
940
|
+
selection: {
|
|
941
|
+
...this.state.selection,
|
|
942
|
+
clipIds: [...this.state.selection.clipIds]
|
|
943
|
+
},
|
|
944
|
+
renderHints: { ...this.state.renderHints }
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
getHistoryMode() {
|
|
948
|
+
return this.historyMode;
|
|
949
|
+
}
|
|
950
|
+
setHistoryMode(mode) {
|
|
951
|
+
if (this.historyMode === mode) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
this.historyMode = mode;
|
|
955
|
+
if (mode === "external") {
|
|
956
|
+
this.historyPast = [];
|
|
957
|
+
this.historyFuture = [];
|
|
958
|
+
}
|
|
959
|
+
this.commitState(
|
|
960
|
+
{
|
|
961
|
+
...this.state,
|
|
962
|
+
canUndo: this.computeCanUndo(),
|
|
963
|
+
canRedo: this.computeCanRedo()
|
|
964
|
+
},
|
|
965
|
+
["toolbar"]
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
setExternalHistoryState(flags) {
|
|
969
|
+
if (this.historyMode !== "external") {
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
const nextUndo = flags.canUndo ?? this.externalCanUndo;
|
|
973
|
+
const nextRedo = flags.canRedo ?? this.externalCanRedo;
|
|
974
|
+
if (nextUndo === this.externalCanUndo && nextRedo === this.externalCanRedo) {
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
this.externalCanUndo = nextUndo;
|
|
978
|
+
this.externalCanRedo = nextRedo;
|
|
979
|
+
this.commitState(
|
|
980
|
+
{
|
|
981
|
+
...this.state,
|
|
982
|
+
canUndo: this.externalCanUndo,
|
|
983
|
+
canRedo: this.externalCanRedo
|
|
984
|
+
},
|
|
985
|
+
["toolbar"]
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
setDocument(doc, options) {
|
|
989
|
+
const shouldPushHistory = options?.pushHistory === true && this.historyMode === "internal";
|
|
990
|
+
if (shouldPushHistory) {
|
|
991
|
+
this.pushHistorySnapshot(this.state.document);
|
|
992
|
+
}
|
|
993
|
+
const nextDoc = shareStructure(this.state.document, cloneDocument(doc));
|
|
994
|
+
const targetClipIds = options?.selectionClipIds ?? this.state.selection.clipIds;
|
|
995
|
+
const targetPrimary = options?.primaryClipId !== void 0 ? options.primaryClipId : this.state.selection.clipId;
|
|
996
|
+
const nextSelection = resolveSelection(nextDoc, targetClipIds, targetPrimary);
|
|
997
|
+
this.commitState(
|
|
998
|
+
{
|
|
999
|
+
...this.state,
|
|
1000
|
+
document: nextDoc,
|
|
1001
|
+
selection: nextSelection,
|
|
1002
|
+
canUndo: this.computeCanUndo(),
|
|
1003
|
+
canRedo: this.computeCanRedo()
|
|
1004
|
+
},
|
|
1005
|
+
options?.dirty ?? ALL_DATA_CHANNELS
|
|
1006
|
+
);
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
setCurrentFrame(frame) {
|
|
1010
|
+
const nextFrame = Math.max(0, Math.round(frame));
|
|
1011
|
+
if (!Number.isFinite(nextFrame) || nextFrame === this.state.currentFrame) {
|
|
1012
|
+
return false;
|
|
1013
|
+
}
|
|
1014
|
+
this.commitState(
|
|
1015
|
+
{
|
|
1016
|
+
...this.state,
|
|
1017
|
+
currentFrame: nextFrame
|
|
1018
|
+
},
|
|
1019
|
+
["playhead"]
|
|
1020
|
+
);
|
|
1021
|
+
return true;
|
|
1022
|
+
}
|
|
1023
|
+
setTrackScale(scale, renderHints) {
|
|
1024
|
+
const nextScale = clampTrackScale(scale);
|
|
1025
|
+
const nextHints = renderHints ? { ...this.state.renderHints, ...renderHints } : this.state.renderHints;
|
|
1026
|
+
const scaleChanged = nextScale !== this.state.trackScale;
|
|
1027
|
+
const hintsChanged = nextHints.zoomActive !== this.state.renderHints.zoomActive || nextHints.settleVersion !== this.state.renderHints.settleVersion;
|
|
1028
|
+
if (!scaleChanged && !hintsChanged) {
|
|
1029
|
+
return false;
|
|
1030
|
+
}
|
|
1031
|
+
this.commitState(
|
|
1032
|
+
{
|
|
1033
|
+
...this.state,
|
|
1034
|
+
trackScale: nextScale,
|
|
1035
|
+
renderHints: nextHints
|
|
1036
|
+
},
|
|
1037
|
+
["ruler", "tracks", "playhead", "toolbar"]
|
|
1038
|
+
);
|
|
1039
|
+
return true;
|
|
1040
|
+
}
|
|
1041
|
+
setRenderHints(hints) {
|
|
1042
|
+
const nextHints = {
|
|
1043
|
+
...this.state.renderHints,
|
|
1044
|
+
...hints
|
|
1045
|
+
};
|
|
1046
|
+
if (nextHints.zoomActive === this.state.renderHints.zoomActive && nextHints.settleVersion === this.state.renderHints.settleVersion) {
|
|
1047
|
+
return false;
|
|
1048
|
+
}
|
|
1049
|
+
this.commitState(
|
|
1050
|
+
{
|
|
1051
|
+
...this.state,
|
|
1052
|
+
renderHints: nextHints
|
|
1053
|
+
},
|
|
1054
|
+
["tracks"]
|
|
1055
|
+
);
|
|
1056
|
+
return true;
|
|
1057
|
+
}
|
|
1058
|
+
setTheme(theme) {
|
|
1059
|
+
if (theme === this.state.theme) {
|
|
1060
|
+
return false;
|
|
1061
|
+
}
|
|
1062
|
+
this.commitState(
|
|
1063
|
+
{
|
|
1064
|
+
...this.state,
|
|
1065
|
+
theme
|
|
1066
|
+
},
|
|
1067
|
+
["toolbar", "sidebar", "ruler", "tracks"]
|
|
1068
|
+
);
|
|
1069
|
+
return true;
|
|
1070
|
+
}
|
|
1071
|
+
setMagnetEnabled(enabled) {
|
|
1072
|
+
if (enabled === this.state.isMagnetEnabled) {
|
|
1073
|
+
return false;
|
|
1074
|
+
}
|
|
1075
|
+
this.commitState(
|
|
1076
|
+
{
|
|
1077
|
+
...this.state,
|
|
1078
|
+
isMagnetEnabled: enabled
|
|
1079
|
+
},
|
|
1080
|
+
["toolbar"]
|
|
1081
|
+
);
|
|
1082
|
+
return true;
|
|
1083
|
+
}
|
|
1084
|
+
toggleMagnetEnabled() {
|
|
1085
|
+
return this.setMagnetEnabled(!this.state.isMagnetEnabled);
|
|
1086
|
+
}
|
|
1087
|
+
setSelection(clipIds, primaryClipId) {
|
|
1088
|
+
const nextSelection = resolveSelection(
|
|
1089
|
+
this.state.document,
|
|
1090
|
+
clipIds,
|
|
1091
|
+
primaryClipId ?? clipIds[0] ?? null
|
|
1092
|
+
);
|
|
1093
|
+
if (selectionsEqual(this.state.selection, nextSelection)) {
|
|
1094
|
+
return false;
|
|
1095
|
+
}
|
|
1096
|
+
this.commitState(
|
|
1097
|
+
{
|
|
1098
|
+
...this.state,
|
|
1099
|
+
selection: nextSelection
|
|
1100
|
+
},
|
|
1101
|
+
["selection", "toolbar"]
|
|
1102
|
+
);
|
|
1103
|
+
return true;
|
|
1104
|
+
}
|
|
1105
|
+
clearSelection() {
|
|
1106
|
+
if (this.state.selection.clipId === null && this.state.selection.clipIds.length === 0) {
|
|
1107
|
+
return false;
|
|
1108
|
+
}
|
|
1109
|
+
this.commitState(
|
|
1110
|
+
{
|
|
1111
|
+
...this.state,
|
|
1112
|
+
selection: {
|
|
1113
|
+
trackIndex: -1,
|
|
1114
|
+
clipIndex: -1,
|
|
1115
|
+
clipId: null,
|
|
1116
|
+
clipIds: []
|
|
1117
|
+
}
|
|
1118
|
+
},
|
|
1119
|
+
["selection", "toolbar"]
|
|
1120
|
+
);
|
|
1121
|
+
return true;
|
|
1122
|
+
}
|
|
1123
|
+
undo() {
|
|
1124
|
+
if (this.historyMode !== "internal") {
|
|
1125
|
+
return false;
|
|
1126
|
+
}
|
|
1127
|
+
const previous = this.historyPast.pop();
|
|
1128
|
+
if (!previous) {
|
|
1129
|
+
return false;
|
|
1130
|
+
}
|
|
1131
|
+
this.historyFuture.push(this.state.document);
|
|
1132
|
+
const nextSelection = resolveSelection(
|
|
1133
|
+
previous,
|
|
1134
|
+
this.state.selection.clipIds,
|
|
1135
|
+
this.state.selection.clipId
|
|
1136
|
+
);
|
|
1137
|
+
this.commitState(
|
|
1138
|
+
{
|
|
1139
|
+
...this.state,
|
|
1140
|
+
document: previous,
|
|
1141
|
+
selection: nextSelection,
|
|
1142
|
+
canUndo: this.computeCanUndo(),
|
|
1143
|
+
canRedo: this.computeCanRedo()
|
|
1144
|
+
},
|
|
1145
|
+
ALL_DATA_CHANNELS
|
|
1146
|
+
);
|
|
1147
|
+
return true;
|
|
1148
|
+
}
|
|
1149
|
+
redo() {
|
|
1150
|
+
if (this.historyMode !== "internal") {
|
|
1151
|
+
return false;
|
|
1152
|
+
}
|
|
1153
|
+
const next = this.historyFuture.pop();
|
|
1154
|
+
if (!next) {
|
|
1155
|
+
return false;
|
|
1156
|
+
}
|
|
1157
|
+
this.historyPast.push(this.state.document);
|
|
1158
|
+
const nextSelection = resolveSelection(
|
|
1159
|
+
next,
|
|
1160
|
+
this.state.selection.clipIds,
|
|
1161
|
+
this.state.selection.clipId
|
|
1162
|
+
);
|
|
1163
|
+
this.commitState(
|
|
1164
|
+
{
|
|
1165
|
+
...this.state,
|
|
1166
|
+
document: next,
|
|
1167
|
+
selection: nextSelection,
|
|
1168
|
+
canUndo: this.computeCanUndo(),
|
|
1169
|
+
canRedo: this.computeCanRedo()
|
|
1170
|
+
},
|
|
1171
|
+
ALL_DATA_CHANNELS
|
|
1172
|
+
);
|
|
1173
|
+
return true;
|
|
1174
|
+
}
|
|
1175
|
+
subscribe(listener) {
|
|
1176
|
+
this.listeners.add(listener);
|
|
1177
|
+
return () => {
|
|
1178
|
+
this.listeners.delete(listener);
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
subscribeSelector(selector, listener, isEqual = Object.is) {
|
|
1182
|
+
let currentSelected = selector(this.state);
|
|
1183
|
+
return this.subscribe(({ state }) => {
|
|
1184
|
+
const nextSelected = selector(state);
|
|
1185
|
+
if (!isEqual(currentSelected, nextSelected)) {
|
|
1186
|
+
const previous = currentSelected;
|
|
1187
|
+
currentSelected = nextSelected;
|
|
1188
|
+
listener(nextSelected, previous);
|
|
1189
|
+
}
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
computeCanUndo() {
|
|
1193
|
+
return this.historyMode === "internal" ? this.historyPast.length > 0 : this.externalCanUndo;
|
|
1194
|
+
}
|
|
1195
|
+
computeCanRedo() {
|
|
1196
|
+
return this.historyMode === "internal" ? this.historyFuture.length > 0 : this.externalCanRedo;
|
|
1197
|
+
}
|
|
1198
|
+
pushHistorySnapshot(doc) {
|
|
1199
|
+
this.historyPast.push(doc);
|
|
1200
|
+
if (this.historyPast.length > this.maxHistory) {
|
|
1201
|
+
this.historyPast.shift();
|
|
1202
|
+
}
|
|
1203
|
+
this.historyFuture = [];
|
|
1204
|
+
}
|
|
1205
|
+
commitState(nextState, dirtyList) {
|
|
1206
|
+
const prevState = this.state;
|
|
1207
|
+
this.state = nextState;
|
|
1208
|
+
const dirty = new Set(dirtyList);
|
|
1209
|
+
const change = {
|
|
1210
|
+
state: nextState,
|
|
1211
|
+
prevState,
|
|
1212
|
+
dirty
|
|
1213
|
+
};
|
|
1214
|
+
this.listeners.forEach((listener) => {
|
|
1215
|
+
listener(change);
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
class CommandBus {
|
|
1220
|
+
context;
|
|
1221
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- heterogeneous payload types per command
|
|
1222
|
+
commands;
|
|
1223
|
+
aliases;
|
|
1224
|
+
beforeHooks;
|
|
1225
|
+
afterHooks;
|
|
1226
|
+
constructor(context) {
|
|
1227
|
+
this.context = context;
|
|
1228
|
+
this.commands = /* @__PURE__ */ new Map();
|
|
1229
|
+
this.aliases = /* @__PURE__ */ new Map();
|
|
1230
|
+
this.beforeHooks = /* @__PURE__ */ new Set();
|
|
1231
|
+
this.afterHooks = /* @__PURE__ */ new Set();
|
|
1232
|
+
}
|
|
1233
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- public default kept for API compatibility
|
|
1234
|
+
register(type, spec) {
|
|
1235
|
+
this.commands.set(type, spec);
|
|
1236
|
+
return () => {
|
|
1237
|
+
if (this.commands.get(type) === spec) {
|
|
1238
|
+
this.commands.delete(type);
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
unregister(type) {
|
|
1243
|
+
this.commands.delete(type);
|
|
1244
|
+
}
|
|
1245
|
+
registerAlias(alias, targetType) {
|
|
1246
|
+
this.aliases.set(alias, targetType);
|
|
1247
|
+
return () => {
|
|
1248
|
+
if (this.aliases.get(alias) === targetType) {
|
|
1249
|
+
this.aliases.delete(alias);
|
|
1250
|
+
}
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
resolveType(type) {
|
|
1254
|
+
return this.aliases.get(type) ?? type;
|
|
1255
|
+
}
|
|
1256
|
+
has(type) {
|
|
1257
|
+
return this.commands.has(this.resolveType(type));
|
|
1258
|
+
}
|
|
1259
|
+
addBeforeHook(hook) {
|
|
1260
|
+
this.beforeHooks.add(hook);
|
|
1261
|
+
return () => {
|
|
1262
|
+
this.beforeHooks.delete(hook);
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
addAfterHook(hook) {
|
|
1266
|
+
this.afterHooks.add(hook);
|
|
1267
|
+
return () => {
|
|
1268
|
+
this.afterHooks.delete(hook);
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
canExecute(type, payload) {
|
|
1272
|
+
const resolved = this.resolveType(type);
|
|
1273
|
+
const spec = this.commands.get(resolved);
|
|
1274
|
+
if (!spec) {
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
return spec.canExecute ? spec.canExecute(this.context, payload) : true;
|
|
1278
|
+
}
|
|
1279
|
+
execute(type, payload) {
|
|
1280
|
+
let currentCommand = {
|
|
1281
|
+
type: this.resolveType(type),
|
|
1282
|
+
payload
|
|
1283
|
+
};
|
|
1284
|
+
for (const hook of this.beforeHooks) {
|
|
1285
|
+
const outcome = hook(currentCommand, this.context);
|
|
1286
|
+
if (outcome === false) {
|
|
1287
|
+
this.afterHooks.forEach((after) => after(currentCommand, false, this.context));
|
|
1288
|
+
return false;
|
|
1289
|
+
}
|
|
1290
|
+
if (outcome && typeof outcome === "object" && typeof outcome.type === "string") {
|
|
1291
|
+
currentCommand = {
|
|
1292
|
+
type: this.resolveType(outcome.type),
|
|
1293
|
+
payload: outcome.payload
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const spec = this.commands.get(currentCommand.type);
|
|
1298
|
+
if (!spec) {
|
|
1299
|
+
this.afterHooks.forEach((after) => after(currentCommand, false, this.context));
|
|
1300
|
+
return false;
|
|
1301
|
+
}
|
|
1302
|
+
if (spec.canExecute && !spec.canExecute(this.context, currentCommand.payload)) {
|
|
1303
|
+
this.afterHooks.forEach((after) => after(currentCommand, false, this.context));
|
|
1304
|
+
return false;
|
|
1305
|
+
}
|
|
1306
|
+
const result = spec.execute(this.context, currentCommand.payload);
|
|
1307
|
+
const executed = result !== false;
|
|
1308
|
+
this.afterHooks.forEach((after) => after(currentCommand, executed, this.context));
|
|
1309
|
+
return executed;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
function registerBuiltinCommands(bus) {
|
|
1313
|
+
bus.register("clip:move", {
|
|
1314
|
+
execute(ctx, payload) {
|
|
1315
|
+
const state = ctx.store.getState();
|
|
1316
|
+
const result = moveClip(
|
|
1317
|
+
state.document,
|
|
1318
|
+
payload,
|
|
1319
|
+
ctx.getPolicies(),
|
|
1320
|
+
state.isMagnetEnabled
|
|
1321
|
+
);
|
|
1322
|
+
if (!result.changed) {
|
|
1323
|
+
return false;
|
|
1324
|
+
}
|
|
1325
|
+
return ctx.store.setDocument(result.document, {
|
|
1326
|
+
pushHistory: true,
|
|
1327
|
+
selectionClipIds: result.selection.clipIds,
|
|
1328
|
+
primaryClipId: result.selection.clipId
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
});
|
|
1332
|
+
bus.register("clip:magnet-move", {
|
|
1333
|
+
execute(ctx, payload) {
|
|
1334
|
+
const state = ctx.store.getState();
|
|
1335
|
+
const result = applyMagnetMove(state.document, payload, ctx.getPolicies());
|
|
1336
|
+
if (!result.changed) {
|
|
1337
|
+
return false;
|
|
1338
|
+
}
|
|
1339
|
+
return ctx.store.setDocument(result.document, {
|
|
1340
|
+
pushHistory: true,
|
|
1341
|
+
selectionClipIds: result.selection.clipIds,
|
|
1342
|
+
primaryClipId: result.selection.clipId
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
});
|
|
1346
|
+
bus.register("clip:trim", {
|
|
1347
|
+
execute(ctx, payload) {
|
|
1348
|
+
const state = ctx.store.getState();
|
|
1349
|
+
const result = trimClip(state.document, payload, ctx.getPolicies());
|
|
1350
|
+
if (!result.changed) {
|
|
1351
|
+
return false;
|
|
1352
|
+
}
|
|
1353
|
+
return ctx.store.setDocument(result.document, {
|
|
1354
|
+
pushHistory: true
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
});
|
|
1358
|
+
bus.register("clip:split", {
|
|
1359
|
+
canExecute(ctx, payload) {
|
|
1360
|
+
const state = ctx.store.getState();
|
|
1361
|
+
const trackIndex = payload?.trackIndex ?? state.selection.trackIndex;
|
|
1362
|
+
const clipIndex = payload?.clipIndex ?? state.selection.clipIndex;
|
|
1363
|
+
const track = state.document.tracks[trackIndex];
|
|
1364
|
+
const clip = track?.clips[clipIndex];
|
|
1365
|
+
if (!track || !clip || track.locked === true) {
|
|
1366
|
+
return false;
|
|
1367
|
+
}
|
|
1368
|
+
const splitMs = payload?.splitMs ?? frameToMs(state.currentFrame, state.document.fps);
|
|
1369
|
+
const minMs = ctx.getPolicies().minClipDurationMs ?? (state.document.fps > 0 ? 1e3 / state.document.fps : 40);
|
|
1370
|
+
return splitMs - clip.startMs >= minMs && clip.endMs - splitMs >= minMs;
|
|
1371
|
+
},
|
|
1372
|
+
execute(ctx, payload) {
|
|
1373
|
+
const state = ctx.store.getState();
|
|
1374
|
+
const trackIndex = payload?.trackIndex ?? state.selection.trackIndex;
|
|
1375
|
+
const clipIndex = payload?.clipIndex ?? state.selection.clipIndex;
|
|
1376
|
+
const splitMs = payload?.splitMs ?? frameToMs(state.currentFrame, state.document.fps);
|
|
1377
|
+
const result = splitClipAtMs(
|
|
1378
|
+
state.document,
|
|
1379
|
+
{ trackIndex, clipIndex, splitMs },
|
|
1380
|
+
ctx.getPolicies()
|
|
1381
|
+
);
|
|
1382
|
+
if (!result.changed) {
|
|
1383
|
+
return false;
|
|
1384
|
+
}
|
|
1385
|
+
return ctx.store.setDocument(result.document, {
|
|
1386
|
+
pushHistory: true,
|
|
1387
|
+
selectionClipIds: result.leftClipId ? [result.leftClipId] : [],
|
|
1388
|
+
primaryClipId: result.leftClipId
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
bus.register("clip:delete", {
|
|
1393
|
+
canExecute(ctx, payload) {
|
|
1394
|
+
const state = ctx.store.getState();
|
|
1395
|
+
const clipIds = payload?.clipIds ?? state.selection.clipIds;
|
|
1396
|
+
return clipIds.length > 0;
|
|
1397
|
+
},
|
|
1398
|
+
execute(ctx, payload) {
|
|
1399
|
+
const state = ctx.store.getState();
|
|
1400
|
+
const clipIds = payload?.clipIds ?? state.selection.clipIds;
|
|
1401
|
+
const result = deleteClips(
|
|
1402
|
+
state.document,
|
|
1403
|
+
clipIds,
|
|
1404
|
+
ctx.getPolicies(),
|
|
1405
|
+
state.isMagnetEnabled
|
|
1406
|
+
);
|
|
1407
|
+
if (!result.changed) {
|
|
1408
|
+
return false;
|
|
1409
|
+
}
|
|
1410
|
+
return ctx.store.setDocument(result.document, {
|
|
1411
|
+
pushHistory: true,
|
|
1412
|
+
selectionClipIds: [],
|
|
1413
|
+
primaryClipId: null
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
bus.register("clip:insert", {
|
|
1418
|
+
execute(ctx, payload) {
|
|
1419
|
+
const state = ctx.store.getState();
|
|
1420
|
+
const result = insertClip(
|
|
1421
|
+
state.document,
|
|
1422
|
+
payload,
|
|
1423
|
+
ctx.getPolicies(),
|
|
1424
|
+
state.isMagnetEnabled
|
|
1425
|
+
);
|
|
1426
|
+
if (!result.changed) {
|
|
1427
|
+
return false;
|
|
1428
|
+
}
|
|
1429
|
+
return ctx.store.setDocument(result.document, {
|
|
1430
|
+
pushHistory: true,
|
|
1431
|
+
selectionClipIds: result.selection.clipIds,
|
|
1432
|
+
primaryClipId: result.selection.clipId
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
bus.register("track:toggle-flag", {
|
|
1437
|
+
execute(ctx, payload) {
|
|
1438
|
+
const state = ctx.store.getState();
|
|
1439
|
+
const result = toggleTrackFlag(state.document, payload.trackIndex, payload.flag);
|
|
1440
|
+
if (!result.changed) {
|
|
1441
|
+
return false;
|
|
1442
|
+
}
|
|
1443
|
+
return ctx.store.setDocument(result.document, {
|
|
1444
|
+
pushHistory: true
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
});
|
|
1448
|
+
bus.register("selection:set", {
|
|
1449
|
+
execute(ctx, payload) {
|
|
1450
|
+
const clipIds = payload?.clipIds ?? (payload?.clipId ? [payload.clipId] : []);
|
|
1451
|
+
return ctx.store.setSelection(clipIds, payload?.clipId ?? clipIds[0] ?? null);
|
|
1452
|
+
}
|
|
1453
|
+
});
|
|
1454
|
+
bus.register("selection:clear", {
|
|
1455
|
+
execute(ctx) {
|
|
1456
|
+
return ctx.store.clearSelection();
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
bus.register("playhead:set", {
|
|
1460
|
+
execute(ctx, payload) {
|
|
1461
|
+
const rawFrame = typeof payload === "number" ? payload : payload.frame;
|
|
1462
|
+
const clampToDuration = typeof payload === "object" && payload !== null ? payload.clampToDuration === true : false;
|
|
1463
|
+
const state = ctx.store.getState();
|
|
1464
|
+
let nextFrame = Math.max(0, Math.round(rawFrame));
|
|
1465
|
+
if (clampToDuration) {
|
|
1466
|
+
const durationMs = getDocumentDurationMs(state.document);
|
|
1467
|
+
const maxFrame = state.document.fps > 0 ? Math.round(durationMs / 1e3 * state.document.fps) : 0;
|
|
1468
|
+
nextFrame = Math.min(nextFrame, Math.max(0, maxFrame));
|
|
1469
|
+
}
|
|
1470
|
+
return ctx.store.setCurrentFrame(nextFrame);
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
bus.register("scale:set", {
|
|
1474
|
+
execute(ctx, payload) {
|
|
1475
|
+
if (typeof payload === "number") {
|
|
1476
|
+
return ctx.store.setTrackScale(payload);
|
|
1477
|
+
}
|
|
1478
|
+
return ctx.store.setTrackScale(payload.scale, payload.renderHints);
|
|
1479
|
+
}
|
|
1480
|
+
});
|
|
1481
|
+
bus.register("scale:zoom-in", {
|
|
1482
|
+
execute(ctx, payload) {
|
|
1483
|
+
const step = payload?.step ?? 10;
|
|
1484
|
+
const current = ctx.store.getState().trackScale;
|
|
1485
|
+
return ctx.store.setTrackScale(current + step);
|
|
1486
|
+
}
|
|
1487
|
+
});
|
|
1488
|
+
bus.register("scale:zoom-out", {
|
|
1489
|
+
execute(ctx, payload) {
|
|
1490
|
+
const step = payload?.step ?? 10;
|
|
1491
|
+
const current = ctx.store.getState().trackScale;
|
|
1492
|
+
return ctx.store.setTrackScale(current - step);
|
|
1493
|
+
}
|
|
1494
|
+
});
|
|
1495
|
+
bus.register("scale:fit", {
|
|
1496
|
+
execute(ctx) {
|
|
1497
|
+
const fitted = ctx.fitToView?.();
|
|
1498
|
+
if (typeof fitted === "number" && Number.isFinite(fitted)) {
|
|
1499
|
+
return ctx.store.setTrackScale(fitted);
|
|
1500
|
+
}
|
|
1501
|
+
return false;
|
|
1502
|
+
}
|
|
1503
|
+
});
|
|
1504
|
+
bus.register("magnet:toggle", {
|
|
1505
|
+
execute(ctx) {
|
|
1506
|
+
const state = ctx.store.getState();
|
|
1507
|
+
const nextMagnet = !state.isMagnetEnabled;
|
|
1508
|
+
ctx.store.setMagnetEnabled(nextMagnet);
|
|
1509
|
+
if (nextMagnet) {
|
|
1510
|
+
const cleaned = cleanupTracks(state.document, ctx.getPolicies(), true);
|
|
1511
|
+
if (cleaned.changed) {
|
|
1512
|
+
ctx.store.setDocument(cleaned.document, { pushHistory: false });
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
return true;
|
|
1516
|
+
}
|
|
1517
|
+
});
|
|
1518
|
+
bus.register("magnet:set", {
|
|
1519
|
+
execute(ctx, payload) {
|
|
1520
|
+
const enabled = typeof payload === "boolean" ? payload : payload.enabled;
|
|
1521
|
+
const changed = ctx.store.setMagnetEnabled(enabled);
|
|
1522
|
+
if (enabled) {
|
|
1523
|
+
const state = ctx.store.getState();
|
|
1524
|
+
const cleaned = cleanupTracks(state.document, ctx.getPolicies(), true);
|
|
1525
|
+
if (cleaned.changed) {
|
|
1526
|
+
ctx.store.setDocument(cleaned.document, { pushHistory: false });
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
return changed;
|
|
1530
|
+
}
|
|
1531
|
+
});
|
|
1532
|
+
bus.register("history:undo", {
|
|
1533
|
+
canExecute(ctx) {
|
|
1534
|
+
return ctx.store.getState().canUndo;
|
|
1535
|
+
},
|
|
1536
|
+
execute(ctx) {
|
|
1537
|
+
return ctx.store.undo();
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
bus.register("history:redo", {
|
|
1541
|
+
canExecute(ctx) {
|
|
1542
|
+
return ctx.store.getState().canRedo;
|
|
1543
|
+
},
|
|
1544
|
+
execute(ctx) {
|
|
1545
|
+
return ctx.store.redo();
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
1548
|
+
bus.registerAlias("undo", "history:undo");
|
|
1549
|
+
bus.registerAlias("redo", "history:redo");
|
|
1550
|
+
bus.registerAlias("split", "clip:split");
|
|
1551
|
+
bus.registerAlias("delete", "clip:delete");
|
|
1552
|
+
bus.registerAlias("magnet", "magnet:toggle");
|
|
1553
|
+
bus.registerAlias("fit", "scale:fit");
|
|
1554
|
+
bus.registerAlias("zoom-in", "scale:zoom-in");
|
|
1555
|
+
bus.registerAlias("zoom-out", "scale:zoom-out");
|
|
1556
|
+
}
|
|
1557
|
+
function createSlotBridge(getKey) {
|
|
1558
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1559
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1560
|
+
let snapshot = [];
|
|
1561
|
+
const emit = () => {
|
|
1562
|
+
snapshot = Array.from(entries.values());
|
|
1563
|
+
listeners.forEach((listener) => {
|
|
1564
|
+
listener();
|
|
1565
|
+
});
|
|
1566
|
+
};
|
|
1567
|
+
const slot = {
|
|
1568
|
+
mount(host, ctx) {
|
|
1569
|
+
const key = getKey(ctx);
|
|
1570
|
+
entries.set(key, { key, host, context: ctx });
|
|
1571
|
+
emit();
|
|
1572
|
+
return {
|
|
1573
|
+
update(nextCtx) {
|
|
1574
|
+
const nextKey = getKey(nextCtx);
|
|
1575
|
+
if (nextKey !== key) {
|
|
1576
|
+
const current = entries.get(key);
|
|
1577
|
+
if (current && current.host === host) {
|
|
1578
|
+
entries.delete(key);
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
entries.set(nextKey, { key: nextKey, host, context: nextCtx });
|
|
1582
|
+
emit();
|
|
1583
|
+
},
|
|
1584
|
+
destroy() {
|
|
1585
|
+
const current = entries.get(key);
|
|
1586
|
+
if (current && current.host === host) {
|
|
1587
|
+
entries.delete(key);
|
|
1588
|
+
emit();
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
};
|
|
1592
|
+
}
|
|
1593
|
+
};
|
|
1594
|
+
return {
|
|
1595
|
+
slot,
|
|
1596
|
+
getEntries() {
|
|
1597
|
+
return snapshot;
|
|
1598
|
+
},
|
|
1599
|
+
subscribe(listener) {
|
|
1600
|
+
listeners.add(listener);
|
|
1601
|
+
return () => {
|
|
1602
|
+
listeners.delete(listener);
|
|
1603
|
+
};
|
|
1604
|
+
},
|
|
1605
|
+
clear() {
|
|
1606
|
+
if (entries.size === 0) {
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
entries.clear();
|
|
1610
|
+
emit();
|
|
1611
|
+
}
|
|
1612
|
+
};
|
|
1613
|
+
}
|
|
1614
|
+
export {
|
|
1615
|
+
CommandBus,
|
|
1616
|
+
DEFAULT_EDIT_POLICIES,
|
|
1617
|
+
DEFAULT_OVERSCAN_PX,
|
|
1618
|
+
DEFAULT_TRACK_GAP,
|
|
1619
|
+
DEFAULT_TRACK_HEIGHT,
|
|
1620
|
+
DEFAULT_TRACK_PADDING_TOP,
|
|
1621
|
+
D as DEMO_FPS,
|
|
1622
|
+
I as InteractionManager,
|
|
1623
|
+
TimelineStore,
|
|
1624
|
+
applyMagnetMove,
|
|
1625
|
+
c as calculateMagnetArrangement,
|
|
1626
|
+
a as checkTrackOverlap,
|
|
1627
|
+
clampTrackScale,
|
|
1628
|
+
cleanupTracks,
|
|
1629
|
+
cloneClip,
|
|
1630
|
+
cloneDocument,
|
|
1631
|
+
cloneTrack,
|
|
1632
|
+
d as collectSnapPoints,
|
|
1633
|
+
computeTimelineLayout,
|
|
1634
|
+
computeVisibleRange,
|
|
1635
|
+
createSlotBridge,
|
|
1636
|
+
deleteClips,
|
|
1637
|
+
d2 as demoDocument,
|
|
1638
|
+
e as detectMagnetState,
|
|
1639
|
+
filterVisibleClips,
|
|
1640
|
+
f as findBestSnap,
|
|
1641
|
+
findClipLocation,
|
|
1642
|
+
frameToMs,
|
|
1643
|
+
h as frameToSeconds,
|
|
1644
|
+
f2 as fromOtioTimeline,
|
|
1645
|
+
getDocumentDurationMs,
|
|
1646
|
+
getFramePixel,
|
|
1647
|
+
j as getGridSize,
|
|
1648
|
+
k as getMagnetPreviewTransforms,
|
|
1649
|
+
getPixelsPerSecond,
|
|
1650
|
+
getTrackHeight,
|
|
1651
|
+
hitTestLayout,
|
|
1652
|
+
insertClip,
|
|
1653
|
+
isClipInVisibleRange,
|
|
1654
|
+
m as isMagnetTrack,
|
|
1655
|
+
i as isOtioTimeline,
|
|
1656
|
+
n as matchesKeyChord,
|
|
1657
|
+
moveClip,
|
|
1658
|
+
o as msToFrame,
|
|
1659
|
+
msToPx,
|
|
1660
|
+
q as pxToFrame,
|
|
1661
|
+
pxToMs,
|
|
1662
|
+
registerBuiltinCommands,
|
|
1663
|
+
resolveSelection,
|
|
1664
|
+
s as resolveTargetTrack,
|
|
1665
|
+
t as scaleToPixelsPerSecond,
|
|
1666
|
+
u as secondsToFrame,
|
|
1667
|
+
shareStructure,
|
|
1668
|
+
splitClipAtMs,
|
|
1669
|
+
t2 as toOtioTimeline,
|
|
1670
|
+
toggleTrackFlag,
|
|
1671
|
+
trimClip
|
|
1672
|
+
};
|
|
1673
|
+
//# sourceMappingURL=core.js.map
|