@toneflix/studio 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +80 -2
- package/dist/edits.d.ts +47 -0
- package/dist/edits.js +140 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.js +24 -0
- package/dist/grid.d.ts +33 -0
- package/dist/grid.js +72 -0
- package/dist/history.d.ts +37 -0
- package/dist/history.js +75 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/model.d.ts +43 -0
- package/dist/model.js +80 -0
- package/dist/overlap.d.ts +17 -0
- package/dist/overlap.js +37 -0
- package/dist/peaks-cache.d.ts +23 -0
- package/dist/peaks-cache.js +51 -0
- package/dist/peaks-worker.d.ts +22 -0
- package/dist/peaks-worker.js +68 -0
- package/dist/peaks.d.ts +31 -0
- package/dist/peaks.js +101 -0
- package/dist/types.d.ts +47 -0
- package/dist/types.js +1 -0
- package/dist/view/clip-view.d.ts +26 -0
- package/dist/view/clip-view.js +99 -0
- package/dist/view/styles.d.ts +4 -0
- package/dist/view/styles.js +81 -0
- package/dist/view/timeline.d.ts +148 -0
- package/dist/view/timeline.js +717 -0
- package/dist/waveform.d.ts +15 -0
- package/dist/waveform.js +61 -0
- package/dist/waveforms.d.ts +20 -0
- package/dist/waveforms.js +38 -0
- package/package.json +65 -4
|
@@ -0,0 +1,717 @@
|
|
|
1
|
+
import { addTrack, duplicateClips, moveClips, removeClips, setFades, splitClips, timelineEnd, trimEnd, trimStart, } from '../edits.js';
|
|
2
|
+
import { Emitter } from '../emitter.js';
|
|
3
|
+
import { Grid, nearestSnap } from '../grid.js';
|
|
4
|
+
import { History } from '../history.js';
|
|
5
|
+
import { TimelineModel } from '../model.js';
|
|
6
|
+
import { stackOrder } from '../overlap.js';
|
|
7
|
+
import { clipEnd } from '../types.js';
|
|
8
|
+
import { ClipView } from './clip-view.js';
|
|
9
|
+
import { injectStyles } from './styles.js';
|
|
10
|
+
const PALETTE = ['#4f9cf9', '#f97362', '#5fd08a', '#c58af9', '#f9c74f', '#4fd1d9', '#f98fc4', '#a3b18a'];
|
|
11
|
+
const LABELS = [0.1, 0.25, 0.5, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600];
|
|
12
|
+
const isTyping = (target) => target instanceof HTMLElement && (target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName));
|
|
13
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
14
|
+
const formatTime = (seconds, step) => {
|
|
15
|
+
const minutes = Math.floor(seconds / 60);
|
|
16
|
+
const rest = seconds - minutes * 60;
|
|
17
|
+
const digits = step < 1 ? 1 : 0;
|
|
18
|
+
return `${minutes}:${rest.toFixed(digits).padStart(digits ? 4 : 2, '0')}`;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A multitrack timeline: lanes of clips that drag freely by the body, overlap,
|
|
22
|
+
* move between lanes, trim, fade, split and snap, with one undo history.
|
|
23
|
+
*/
|
|
24
|
+
export class StudioTimeline extends Emitter {
|
|
25
|
+
model;
|
|
26
|
+
grid;
|
|
27
|
+
root;
|
|
28
|
+
doc;
|
|
29
|
+
scroller;
|
|
30
|
+
content;
|
|
31
|
+
corner;
|
|
32
|
+
ruler;
|
|
33
|
+
rows;
|
|
34
|
+
overlay;
|
|
35
|
+
playhead;
|
|
36
|
+
rangeEl;
|
|
37
|
+
snapEl;
|
|
38
|
+
trackRows = new Map();
|
|
39
|
+
clipViews = new Map();
|
|
40
|
+
selected = new Set();
|
|
41
|
+
currentRange = null;
|
|
42
|
+
gesture = null;
|
|
43
|
+
frame = 0;
|
|
44
|
+
resize;
|
|
45
|
+
unsubscribe = [];
|
|
46
|
+
options;
|
|
47
|
+
pps;
|
|
48
|
+
playheadTime = 0;
|
|
49
|
+
constructor(container, options = {}) {
|
|
50
|
+
super();
|
|
51
|
+
this.doc = container.ownerDocument;
|
|
52
|
+
this.model = options.model ?? new TimelineModel(undefined, options.history);
|
|
53
|
+
this.grid = new Grid(options.grid);
|
|
54
|
+
this.options = {
|
|
55
|
+
showGrid: options.showGrid ?? true,
|
|
56
|
+
pxPerSecond: options.pxPerSecond ?? 40,
|
|
57
|
+
minPxPerSecond: options.minPxPerSecond ?? 2,
|
|
58
|
+
maxPxPerSecond: options.maxPxPerSecond ?? 2000,
|
|
59
|
+
trackHeight: options.trackHeight ?? 96,
|
|
60
|
+
headerWidth: options.headerWidth ?? 180,
|
|
61
|
+
readonly: options.readonly ?? false,
|
|
62
|
+
snapToClips: options.snapToClips ?? true,
|
|
63
|
+
follow: options.follow ?? true,
|
|
64
|
+
keyboard: options.keyboard ?? true,
|
|
65
|
+
palette: options.palette ?? PALETTE,
|
|
66
|
+
renderHeader: options.renderHeader,
|
|
67
|
+
createTrack: options.createTrack,
|
|
68
|
+
};
|
|
69
|
+
this.pps = clamp(this.options.pxPerSecond, this.options.minPxPerSecond, this.options.maxPxPerSecond);
|
|
70
|
+
injectStyles(this.doc);
|
|
71
|
+
const el = (tag, className, parent) => {
|
|
72
|
+
const node = this.doc.createElement(tag);
|
|
73
|
+
node.className = className;
|
|
74
|
+
parent?.append(node);
|
|
75
|
+
return node;
|
|
76
|
+
};
|
|
77
|
+
this.root = el('div', 'tfs');
|
|
78
|
+
this.root.tabIndex = 0;
|
|
79
|
+
this.root.setAttribute('role', 'application');
|
|
80
|
+
this.root.setAttribute('aria-label', 'Timeline');
|
|
81
|
+
this.scroller = el('div', 'tfs-scroll', this.root);
|
|
82
|
+
this.content = el('div', 'tfs-content', this.scroller);
|
|
83
|
+
const rulerRow = el('div', 'tfs-ruler-row', this.content);
|
|
84
|
+
this.corner = el('div', 'tfs-corner', rulerRow);
|
|
85
|
+
this.ruler = el('div', 'tfs-ruler', rulerRow);
|
|
86
|
+
this.rows = el('div', 'tfs-rows', this.content);
|
|
87
|
+
this.overlay = el('div', 'tfs-overlay', this.content);
|
|
88
|
+
this.rangeEl = el('div', 'tfs-range', this.overlay);
|
|
89
|
+
this.snapEl = el('div', 'tfs-snap', this.overlay);
|
|
90
|
+
this.playhead = el('div', 'tfs-playhead', this.overlay);
|
|
91
|
+
this.snapEl.style.display = 'none';
|
|
92
|
+
this.ruler.style.touchAction = 'none';
|
|
93
|
+
container.append(this.root);
|
|
94
|
+
this.listen();
|
|
95
|
+
this.render();
|
|
96
|
+
}
|
|
97
|
+
// ---- state ----
|
|
98
|
+
get state() {
|
|
99
|
+
return this.model.state;
|
|
100
|
+
}
|
|
101
|
+
get history() {
|
|
102
|
+
return this.model.history;
|
|
103
|
+
}
|
|
104
|
+
get pxPerSecond() {
|
|
105
|
+
return this.pps;
|
|
106
|
+
}
|
|
107
|
+
get time() {
|
|
108
|
+
return this.playheadTime;
|
|
109
|
+
}
|
|
110
|
+
get selection() {
|
|
111
|
+
return [...this.selected];
|
|
112
|
+
}
|
|
113
|
+
get range() {
|
|
114
|
+
return this.currentRange;
|
|
115
|
+
}
|
|
116
|
+
/** Replace the tracks and clips without an undo step. */
|
|
117
|
+
load(state) {
|
|
118
|
+
this.model.load(state);
|
|
119
|
+
}
|
|
120
|
+
/** Tell the timeline a source's length, and its peaks once known. */
|
|
121
|
+
setSource(source, peaks) {
|
|
122
|
+
this.model.setSource(source, peaks);
|
|
123
|
+
}
|
|
124
|
+
setGrid(options) {
|
|
125
|
+
this.grid.set(options);
|
|
126
|
+
if (options.show !== undefined)
|
|
127
|
+
this.options.showGrid = options.show;
|
|
128
|
+
this.schedule();
|
|
129
|
+
}
|
|
130
|
+
setOptions(options) {
|
|
131
|
+
Object.assign(this.options, options);
|
|
132
|
+
for (const view of this.clipViews.values())
|
|
133
|
+
view.invalidate();
|
|
134
|
+
this.schedule();
|
|
135
|
+
}
|
|
136
|
+
select(ids) {
|
|
137
|
+
this.selected = new Set([...ids].filter((id) => this.state.clips.some((clip) => clip.id === id)));
|
|
138
|
+
this.emit('select', { clips: this.selection });
|
|
139
|
+
this.schedule();
|
|
140
|
+
}
|
|
141
|
+
setRange(range) {
|
|
142
|
+
this.currentRange = range && range.end - range.start > 1e-3 ? range : null;
|
|
143
|
+
this.emit('range', { range: this.currentRange });
|
|
144
|
+
this.schedule();
|
|
145
|
+
}
|
|
146
|
+
/** Move the playhead. While following, the view scrolls to keep it in sight. */
|
|
147
|
+
setTime(time) {
|
|
148
|
+
this.playheadTime = Math.max(0, time);
|
|
149
|
+
this.placePlayhead();
|
|
150
|
+
if (this.options.follow && !this.gesture) {
|
|
151
|
+
const x = this.playheadTime * this.pps;
|
|
152
|
+
const left = this.scroller.scrollLeft;
|
|
153
|
+
const width = this.scroller.clientWidth - this.options.headerWidth;
|
|
154
|
+
if (x < left || x > left + width - 24)
|
|
155
|
+
this.scroller.scrollLeft = Math.max(0, x - width * 0.1);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Zoom to a number of pixels per second, keeping `anchor` (a time) still on screen. */
|
|
159
|
+
setZoom(pxPerSecond, anchor = this.playheadTime) {
|
|
160
|
+
const next = clamp(pxPerSecond, this.options.minPxPerSecond, this.options.maxPxPerSecond);
|
|
161
|
+
if (next === this.pps)
|
|
162
|
+
return;
|
|
163
|
+
const screenX = anchor * this.pps - this.scroller.scrollLeft;
|
|
164
|
+
this.pps = next;
|
|
165
|
+
this.render();
|
|
166
|
+
this.scroller.scrollLeft = Math.max(0, anchor * next - screenX);
|
|
167
|
+
this.emit('zoom', { pxPerSecond: next });
|
|
168
|
+
}
|
|
169
|
+
/** Zoom so every clip fits on screen. */
|
|
170
|
+
fit() {
|
|
171
|
+
const end = timelineEnd(this.state);
|
|
172
|
+
const width = this.scroller.clientWidth - this.options.headerWidth - 24;
|
|
173
|
+
if (end > 0 && width > 0)
|
|
174
|
+
this.setZoom(width / end, 0);
|
|
175
|
+
this.scroller.scrollLeft = 0;
|
|
176
|
+
}
|
|
177
|
+
/** The timeline time under a point on screen. */
|
|
178
|
+
timeAt(clientX) {
|
|
179
|
+
return Math.max(0, (clientX - this.ruler.getBoundingClientRect().left) / this.pps);
|
|
180
|
+
}
|
|
181
|
+
// ---- edits ----
|
|
182
|
+
undo() {
|
|
183
|
+
return this.history.undo();
|
|
184
|
+
}
|
|
185
|
+
redo() {
|
|
186
|
+
return this.history.redo();
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Split the selected clips at a time, or every clip under it when none are
|
|
190
|
+
* selected. Only the left pieces stay selected, so the right ones drag alone.
|
|
191
|
+
*/
|
|
192
|
+
split(time = this.playheadTime) {
|
|
193
|
+
const ids = this.selected.size
|
|
194
|
+
? this.selected
|
|
195
|
+
: new Set(this.state.clips.filter((clip) => clip.start < time && clipEnd(clip) > time).map((clip) => clip.id));
|
|
196
|
+
this.model.apply('Split', (state) => {
|
|
197
|
+
const result = splitClips(state, ids, time);
|
|
198
|
+
return result.created.length ? result.state : state;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
deleteSelected() {
|
|
202
|
+
if (!this.selected.size)
|
|
203
|
+
return;
|
|
204
|
+
const ids = new Set(this.selected);
|
|
205
|
+
this.model.apply(ids.size > 1 ? 'Delete clips' : 'Delete clip', (state) => removeClips(state, ids));
|
|
206
|
+
this.select([]);
|
|
207
|
+
}
|
|
208
|
+
duplicateSelected() {
|
|
209
|
+
if (!this.selected.size)
|
|
210
|
+
return;
|
|
211
|
+
let created = [];
|
|
212
|
+
this.model.apply('Duplicate', (state) => {
|
|
213
|
+
const result = duplicateClips(state, this.selected);
|
|
214
|
+
created = result.created;
|
|
215
|
+
return result.state;
|
|
216
|
+
});
|
|
217
|
+
this.select(created);
|
|
218
|
+
}
|
|
219
|
+
/** Move the selected clips by a time and a number of lanes. */
|
|
220
|
+
nudge(seconds, lanes = 0) {
|
|
221
|
+
if (!this.selected.size)
|
|
222
|
+
return;
|
|
223
|
+
this.model.apply('Move', (state) => moveClips(state, this.selected, seconds, lanes));
|
|
224
|
+
}
|
|
225
|
+
destroy() {
|
|
226
|
+
cancelAnimationFrame(this.frame);
|
|
227
|
+
this.resize?.disconnect();
|
|
228
|
+
for (const off of this.unsubscribe)
|
|
229
|
+
off();
|
|
230
|
+
for (const row of this.trackRows.values())
|
|
231
|
+
row.cleanup?.();
|
|
232
|
+
this.root.remove();
|
|
233
|
+
this.removeAllListeners();
|
|
234
|
+
}
|
|
235
|
+
// ---- rendering ----
|
|
236
|
+
schedule() {
|
|
237
|
+
if (this.frame)
|
|
238
|
+
return;
|
|
239
|
+
this.frame = requestAnimationFrame(() => {
|
|
240
|
+
this.frame = 0;
|
|
241
|
+
this.render();
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
render() {
|
|
245
|
+
const { headerWidth } = this.options;
|
|
246
|
+
const state = this.state;
|
|
247
|
+
const viewport = Math.max(0, this.scroller.clientWidth - headerWidth);
|
|
248
|
+
const width = Math.max(viewport, (timelineEnd(state) + 30) * this.pps, (this.playheadTime + 5) * this.pps);
|
|
249
|
+
this.root.classList.toggle('tfs--grid', this.options.showGrid);
|
|
250
|
+
this.root.classList.toggle('tfs--readonly', this.options.readonly);
|
|
251
|
+
this.root.style.setProperty('--tfs-beat-px', `${this.grid.beat * this.pps}px`);
|
|
252
|
+
this.root.style.setProperty('--tfs-bar-px', `${this.grid.bar * this.pps}px`);
|
|
253
|
+
this.content.style.width = `${headerWidth + width}px`;
|
|
254
|
+
this.corner.style.width = `${headerWidth}px`;
|
|
255
|
+
this.ruler.style.width = `${width}px`;
|
|
256
|
+
this.overlay.style.left = `${headerWidth}px`;
|
|
257
|
+
this.overlay.style.width = `${width}px`;
|
|
258
|
+
this.renderTracks(state, width);
|
|
259
|
+
this.renderClips(state);
|
|
260
|
+
this.renderRuler();
|
|
261
|
+
this.renderRange();
|
|
262
|
+
this.placePlayhead();
|
|
263
|
+
this.paint();
|
|
264
|
+
}
|
|
265
|
+
renderTracks(state, width) {
|
|
266
|
+
const { trackHeight, headerWidth } = this.options;
|
|
267
|
+
const seen = new Set();
|
|
268
|
+
state.tracks.forEach((track, index) => {
|
|
269
|
+
seen.add(track.id);
|
|
270
|
+
let row = this.trackRows.get(track.id);
|
|
271
|
+
if (!row) {
|
|
272
|
+
const rowEl = this.doc.createElement('div');
|
|
273
|
+
const header = this.doc.createElement('div');
|
|
274
|
+
const lane = this.doc.createElement('div');
|
|
275
|
+
rowEl.className = 'tfs-row';
|
|
276
|
+
header.className = 'tfs-header';
|
|
277
|
+
lane.className = 'tfs-lane';
|
|
278
|
+
lane.dataset.track = track.id;
|
|
279
|
+
rowEl.append(header, lane);
|
|
280
|
+
row = { row: rowEl, header, lane };
|
|
281
|
+
this.trackRows.set(track.id, row);
|
|
282
|
+
this.fillHeader(track, row);
|
|
283
|
+
}
|
|
284
|
+
else if (row.header.dataset.name !== track.name || row.header.dataset.color !== this.trackColor(track, index)) {
|
|
285
|
+
row.cleanup?.();
|
|
286
|
+
this.fillHeader(track, row);
|
|
287
|
+
}
|
|
288
|
+
row.row.style.height = `${trackHeight}px`;
|
|
289
|
+
row.row.style.boxSizing = 'border-box';
|
|
290
|
+
row.row.style.setProperty('--clip', this.trackColor(track, index));
|
|
291
|
+
row.header.style.width = `${headerWidth}px`;
|
|
292
|
+
row.header.style.display = headerWidth ? '' : 'none';
|
|
293
|
+
row.lane.style.width = `${width}px`;
|
|
294
|
+
row.row.classList.toggle('tfs-row--new', this.gesture?.kind === 'move' && this.gesture.track?.id === track.id);
|
|
295
|
+
if (this.rows.children[index] !== row.row)
|
|
296
|
+
this.rows.insertBefore(row.row, this.rows.children[index] ?? null);
|
|
297
|
+
});
|
|
298
|
+
for (const [id, row] of this.trackRows) {
|
|
299
|
+
if (seen.has(id))
|
|
300
|
+
continue;
|
|
301
|
+
row.cleanup?.();
|
|
302
|
+
row.row.remove();
|
|
303
|
+
this.trackRows.delete(id);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
fillHeader(track, row) {
|
|
307
|
+
const index = this.state.tracks.indexOf(track);
|
|
308
|
+
row.header.replaceChildren();
|
|
309
|
+
row.header.dataset.name = track.name;
|
|
310
|
+
row.header.dataset.color = this.trackColor(track, index);
|
|
311
|
+
if (this.options.renderHeader) {
|
|
312
|
+
row.cleanup = this.options.renderHeader(track, row.header) || undefined;
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const cell = this.doc.createElement('div');
|
|
316
|
+
const swatch = this.doc.createElement('i');
|
|
317
|
+
const name = this.doc.createElement('span');
|
|
318
|
+
cell.className = 'tfs-header-default';
|
|
319
|
+
name.textContent = track.name;
|
|
320
|
+
cell.append(swatch, name);
|
|
321
|
+
row.header.append(cell);
|
|
322
|
+
row.cleanup = undefined;
|
|
323
|
+
}
|
|
324
|
+
trackColor(track, index) {
|
|
325
|
+
return track.color ?? this.options.palette[Math.max(0, index) % this.options.palette.length];
|
|
326
|
+
}
|
|
327
|
+
renderClips(state) {
|
|
328
|
+
const seen = new Set();
|
|
329
|
+
const colors = new Map(state.tracks.map((track, index) => [track.id, this.trackColor(track, index)]));
|
|
330
|
+
for (const track of state.tracks) {
|
|
331
|
+
const lane = this.trackRows.get(track.id).lane;
|
|
332
|
+
const stack = stackOrder(state.clips.filter((clip) => clip.track === track.id));
|
|
333
|
+
stack.forEach((clip, z) => {
|
|
334
|
+
seen.add(clip.id);
|
|
335
|
+
let view = this.clipViews.get(clip.id);
|
|
336
|
+
if (!view) {
|
|
337
|
+
view = new ClipView(clip, this.doc);
|
|
338
|
+
this.clipViews.set(clip.id, view);
|
|
339
|
+
}
|
|
340
|
+
if (view.el.parentElement !== lane)
|
|
341
|
+
lane.append(view.el);
|
|
342
|
+
view.layout(clip, this.pps, z + 1, this.selected.has(clip.id), clip.color ?? colors.get(track.id));
|
|
343
|
+
view.el.classList.toggle('tfs-clip--dragging', this.gesture?.kind === 'move' && this.gesture.started && this.gesture.ids.has(clip.id));
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
for (const [id, view] of this.clipViews) {
|
|
347
|
+
if (seen.has(id))
|
|
348
|
+
continue;
|
|
349
|
+
view.el.remove();
|
|
350
|
+
this.clipViews.delete(id);
|
|
351
|
+
}
|
|
352
|
+
for (const id of this.selected)
|
|
353
|
+
if (!seen.has(id))
|
|
354
|
+
this.selected.delete(id);
|
|
355
|
+
}
|
|
356
|
+
/** Draw waveforms for what is on screen. */
|
|
357
|
+
paint() {
|
|
358
|
+
const left = this.scroller.scrollLeft;
|
|
359
|
+
const right = left + this.scroller.clientWidth;
|
|
360
|
+
const ratio = this.doc.defaultView?.devicePixelRatio || 1;
|
|
361
|
+
for (const view of this.clipViews.values()) {
|
|
362
|
+
const x = view.clip.start * this.pps;
|
|
363
|
+
const peaks = this.model.peaks.get(view.clip.source);
|
|
364
|
+
if (x + view.clip.duration * this.pps < left - this.scroller.clientWidth || x > right + this.scroller.clientWidth)
|
|
365
|
+
continue;
|
|
366
|
+
view.paint(left - x, right - x, this.pps, peaks, ratio);
|
|
367
|
+
}
|
|
368
|
+
this.renderRuler();
|
|
369
|
+
}
|
|
370
|
+
renderRuler() {
|
|
371
|
+
const left = this.scroller.scrollLeft;
|
|
372
|
+
const span = this.scroller.clientWidth;
|
|
373
|
+
const from = Math.max(0, (left - span) / this.pps);
|
|
374
|
+
const to = (left + span * 2) / this.pps;
|
|
375
|
+
const labels = [];
|
|
376
|
+
const add = (time, text) => {
|
|
377
|
+
const label = this.doc.createElement('span');
|
|
378
|
+
label.className = 'tfs-ruler-label';
|
|
379
|
+
label.style.left = `${time * this.pps}px`;
|
|
380
|
+
label.textContent = text;
|
|
381
|
+
labels.push(label);
|
|
382
|
+
};
|
|
383
|
+
if (this.options.showGrid) {
|
|
384
|
+
let every = 1;
|
|
385
|
+
while (this.grid.bar * every * this.pps < 48)
|
|
386
|
+
every *= 2;
|
|
387
|
+
const step = this.grid.bar * every;
|
|
388
|
+
for (let bar = Math.floor(from / step) * every; bar * this.grid.bar < to; bar += every)
|
|
389
|
+
add(bar * this.grid.bar, String(bar + 1));
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
const step = LABELS.find((s) => s * this.pps >= 70) ?? 600;
|
|
393
|
+
for (let time = Math.floor(from / step) * step; time < to; time += step)
|
|
394
|
+
add(time, formatTime(time, step));
|
|
395
|
+
}
|
|
396
|
+
this.ruler.replaceChildren(...labels);
|
|
397
|
+
}
|
|
398
|
+
renderRange() {
|
|
399
|
+
const range = this.currentRange;
|
|
400
|
+
const style = this.rangeEl.style;
|
|
401
|
+
if (!range) {
|
|
402
|
+
style.display = 'none';
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const lanes = range.tracks.map((id) => this.state.tracks.findIndex((track) => track.id === id)).filter((i) => i >= 0);
|
|
406
|
+
const ruler = this.ruler.offsetHeight;
|
|
407
|
+
const first = lanes.length ? Math.min(...lanes) : 0;
|
|
408
|
+
const last = lanes.length ? Math.max(...lanes) : this.state.tracks.length - 1;
|
|
409
|
+
style.display = '';
|
|
410
|
+
style.left = `${range.start * this.pps}px`;
|
|
411
|
+
style.width = `${(range.end - range.start) * this.pps}px`;
|
|
412
|
+
style.top = `${ruler + first * this.options.trackHeight}px`;
|
|
413
|
+
style.height = `${(last - first + 1) * this.options.trackHeight}px`;
|
|
414
|
+
}
|
|
415
|
+
placePlayhead() {
|
|
416
|
+
this.playhead.style.transform = `translateX(${this.playheadTime * this.pps}px)`;
|
|
417
|
+
}
|
|
418
|
+
// ---- input ----
|
|
419
|
+
listen() {
|
|
420
|
+
const on = (target, type, fn, opts) => {
|
|
421
|
+
target.addEventListener(type, fn, opts);
|
|
422
|
+
this.unsubscribe.push(() => target.removeEventListener(type, fn, opts));
|
|
423
|
+
};
|
|
424
|
+
this.unsubscribe.push(this.model.on('change', (event) => {
|
|
425
|
+
this.schedule();
|
|
426
|
+
this.emit('change', event);
|
|
427
|
+
}), this.model.on('source', ({ id }) => {
|
|
428
|
+
for (const view of this.clipViews.values())
|
|
429
|
+
if (view.clip.source === id)
|
|
430
|
+
view.invalidate();
|
|
431
|
+
this.schedule();
|
|
432
|
+
}));
|
|
433
|
+
on(this.scroller, 'scroll', () => this.paint(), { passive: true });
|
|
434
|
+
on(this.content, 'pointerdown', (e) => this.onPointerDown(e));
|
|
435
|
+
on(this.content, 'pointermove', (e) => this.onPointerMove(e));
|
|
436
|
+
on(this.content, 'pointerup', (e) => this.onPointerUp(e));
|
|
437
|
+
on(this.content, 'pointercancel', () => this.cancelGesture());
|
|
438
|
+
on(this.root, 'keydown', (e) => this.onKey(e));
|
|
439
|
+
on(this.scroller, 'wheel', (e) => this.onWheel(e), { passive: false });
|
|
440
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
441
|
+
this.resize = new ResizeObserver(() => this.schedule());
|
|
442
|
+
this.resize.observe(this.root);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
laneAt(clientY) {
|
|
446
|
+
return Math.floor((clientY - this.rows.getBoundingClientRect().top) / this.options.trackHeight);
|
|
447
|
+
}
|
|
448
|
+
onPointerDown(e) {
|
|
449
|
+
if (e.button !== 0 || this.gesture)
|
|
450
|
+
return;
|
|
451
|
+
const target = e.target;
|
|
452
|
+
if (target.closest('.tfs-header, .tfs-corner'))
|
|
453
|
+
return;
|
|
454
|
+
const clipEl = target.closest('.tfs-clip');
|
|
455
|
+
const clip = clipEl ? this.state.clips.find((c) => c.id === clipEl.dataset.clip) : undefined;
|
|
456
|
+
const part = target.dataset.part;
|
|
457
|
+
const touch = e.pointerType === 'touch';
|
|
458
|
+
e.preventDefault();
|
|
459
|
+
this.root.focus({ preventScroll: true });
|
|
460
|
+
if (clip && this.options.readonly) {
|
|
461
|
+
this.gesture = { kind: 'click-clip', id: e.pointerId, x: e.clientX, y: e.clientY, clip };
|
|
462
|
+
}
|
|
463
|
+
else if (clip && part && part !== 'body') {
|
|
464
|
+
this.gesture = { kind: part, id: e.pointerId, x: e.clientX, started: false, clip };
|
|
465
|
+
this.select([clip.id]);
|
|
466
|
+
}
|
|
467
|
+
else if (clip) {
|
|
468
|
+
const toggle = e.ctrlKey || e.metaKey;
|
|
469
|
+
if (toggle || e.shiftKey) {
|
|
470
|
+
const next = new Set(this.selected);
|
|
471
|
+
if (toggle && next.has(clip.id))
|
|
472
|
+
next.delete(clip.id);
|
|
473
|
+
else
|
|
474
|
+
next.add(clip.id);
|
|
475
|
+
this.select(next);
|
|
476
|
+
}
|
|
477
|
+
else if (!this.selected.has(clip.id)) {
|
|
478
|
+
this.select([clip.id]);
|
|
479
|
+
}
|
|
480
|
+
if (!this.selected.has(clip.id))
|
|
481
|
+
return;
|
|
482
|
+
this.gesture = {
|
|
483
|
+
kind: 'move',
|
|
484
|
+
id: e.pointerId,
|
|
485
|
+
x: e.clientX,
|
|
486
|
+
y: e.clientY,
|
|
487
|
+
started: false,
|
|
488
|
+
clip,
|
|
489
|
+
ids: new Set(this.selected),
|
|
490
|
+
lane: this.laneAt(e.clientY),
|
|
491
|
+
base: this.state,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
else if (target.closest('.tfs-lane, .tfs-ruler')) {
|
|
495
|
+
this.gesture = {
|
|
496
|
+
kind: 'range',
|
|
497
|
+
id: e.pointerId,
|
|
498
|
+
x: e.clientX,
|
|
499
|
+
y: e.clientY,
|
|
500
|
+
started: false,
|
|
501
|
+
time: this.timeAt(e.clientX),
|
|
502
|
+
lane: this.laneAt(e.clientY),
|
|
503
|
+
additive: e.shiftKey,
|
|
504
|
+
ruler: !!target.closest('.tfs-ruler'),
|
|
505
|
+
touch: touch && !target.closest('.tfs-ruler'),
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (!touch || this.gesture.kind !== 'range' || !this.gesture.touch)
|
|
512
|
+
this.content.setPointerCapture?.(e.pointerId);
|
|
513
|
+
}
|
|
514
|
+
onPointerMove(e) {
|
|
515
|
+
const g = this.gesture;
|
|
516
|
+
if (!g || g.id !== e.pointerId || g.kind === 'click-clip')
|
|
517
|
+
return;
|
|
518
|
+
if (!g.started) {
|
|
519
|
+
if (Math.abs(e.clientX - g.x) < 3 && Math.abs(e.clientY - ('y' in g ? g.y : e.clientY)) < 3)
|
|
520
|
+
return;
|
|
521
|
+
if (g.kind === 'range' && g.touch)
|
|
522
|
+
return;
|
|
523
|
+
g.started = true;
|
|
524
|
+
}
|
|
525
|
+
this.autoScroll(e.clientX);
|
|
526
|
+
const time = this.timeAt(e.clientX);
|
|
527
|
+
const free = e.altKey;
|
|
528
|
+
switch (g.kind) {
|
|
529
|
+
case 'move':
|
|
530
|
+
this.dragMove(g, e, free);
|
|
531
|
+
break;
|
|
532
|
+
case 'trim-start': {
|
|
533
|
+
const snapped = this.snapTime(time, new Set([g.clip.id]), free);
|
|
534
|
+
this.model.draft('Trim', (state) => trimStart(state, this.model.sources, g.clip.id, snapped));
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
case 'trim-end': {
|
|
538
|
+
const snapped = this.snapTime(time, new Set([g.clip.id]), free);
|
|
539
|
+
this.model.draft('Trim', (state) => trimEnd(state, this.model.sources, g.clip.id, snapped));
|
|
540
|
+
break;
|
|
541
|
+
}
|
|
542
|
+
case 'fade-in':
|
|
543
|
+
this.model.draft('Fade in', (state) => setFades(state, g.clip.id, { fadeIn: time - g.clip.start }));
|
|
544
|
+
break;
|
|
545
|
+
case 'fade-out':
|
|
546
|
+
this.model.draft('Fade out', (state) => setFades(state, g.clip.id, { fadeOut: clipEnd(g.clip) - time }));
|
|
547
|
+
break;
|
|
548
|
+
case 'range': {
|
|
549
|
+
const snap = (t) => (free || !this.options.showGrid ? t : this.grid.round(t));
|
|
550
|
+
const start = snap(Math.min(g.time, time));
|
|
551
|
+
const end = snap(Math.max(g.time, time));
|
|
552
|
+
const lanes = g.ruler ? [0, this.state.tracks.length - 1] : [g.lane, this.laneAt(e.clientY)].sort((a, b) => a - b);
|
|
553
|
+
const tracks = this.state.tracks.slice(Math.max(0, lanes[0]), Math.max(0, lanes[1]) + 1).map((track) => track.id);
|
|
554
|
+
this.currentRange = { start, end, tracks };
|
|
555
|
+
this.renderRange();
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
dragMove(g, e, free) {
|
|
561
|
+
const base = g.base;
|
|
562
|
+
const moving = base.clips.filter((clip) => g.ids.has(clip.id));
|
|
563
|
+
const index = new Map(base.tracks.map((track, i) => [track.id, i]));
|
|
564
|
+
const lowest = Math.max(...moving.map((clip) => index.get(clip.track) ?? 0));
|
|
565
|
+
let lanes = this.laneAt(e.clientY) - g.lane;
|
|
566
|
+
let state = base;
|
|
567
|
+
if (this.options.createTrack && lowest + lanes >= base.tracks.length) {
|
|
568
|
+
g.track ??= this.options.createTrack();
|
|
569
|
+
state = addTrack(base, g.track);
|
|
570
|
+
lanes = base.tracks.length - lowest;
|
|
571
|
+
}
|
|
572
|
+
const raw = (e.clientX - g.x) / this.pps;
|
|
573
|
+
const start = g.clip.start + raw;
|
|
574
|
+
const shift = raw + this.snapShift([start, start + g.clip.duration], g.ids, free);
|
|
575
|
+
this.model.draft('Move', () => moveClips(state, g.ids, shift, lanes));
|
|
576
|
+
}
|
|
577
|
+
onPointerUp(e) {
|
|
578
|
+
const g = this.gesture;
|
|
579
|
+
if (!g || g.id !== e.pointerId)
|
|
580
|
+
return;
|
|
581
|
+
this.gesture = null;
|
|
582
|
+
this.snapEl.style.display = 'none';
|
|
583
|
+
if (g.kind === 'click-clip') {
|
|
584
|
+
this.select([g.clip.id]);
|
|
585
|
+
}
|
|
586
|
+
else if (g.kind === 'range') {
|
|
587
|
+
this.endRange(g);
|
|
588
|
+
}
|
|
589
|
+
else if (g.started) {
|
|
590
|
+
const labels = { move: 'Move', 'trim-start': 'Trim', 'trim-end': 'Trim', 'fade-in': 'Fade in', 'fade-out': 'Fade out' };
|
|
591
|
+
this.model.commit(g.kind === 'move' && g.ids.size > 1 ? 'Move clips' : labels[g.kind]);
|
|
592
|
+
}
|
|
593
|
+
else if (g.kind === 'move' && !e.shiftKey && !e.ctrlKey && !e.metaKey) {
|
|
594
|
+
this.select([g.clip.id]);
|
|
595
|
+
}
|
|
596
|
+
this.schedule();
|
|
597
|
+
}
|
|
598
|
+
endRange(g) {
|
|
599
|
+
const range = this.currentRange;
|
|
600
|
+
if (!g.started) {
|
|
601
|
+
this.select([]);
|
|
602
|
+
this.setRange(null);
|
|
603
|
+
this.seek(g.time);
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (g.additive && range) {
|
|
607
|
+
const hit = this.state.clips.filter((clip) => range.tracks.includes(clip.track) && clip.start < range.end && clipEnd(clip) > range.start);
|
|
608
|
+
this.select([...this.selected, ...hit.map((clip) => clip.id)]);
|
|
609
|
+
this.setRange(null);
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
this.select([]);
|
|
613
|
+
this.setRange(range);
|
|
614
|
+
}
|
|
615
|
+
cancelGesture() {
|
|
616
|
+
const g = this.gesture;
|
|
617
|
+
this.gesture = null;
|
|
618
|
+
this.snapEl.style.display = 'none';
|
|
619
|
+
if (g?.kind === 'range' && g.touch && !g.started)
|
|
620
|
+
return;
|
|
621
|
+
if (this.model.drafting)
|
|
622
|
+
this.model.cancel();
|
|
623
|
+
this.schedule();
|
|
624
|
+
}
|
|
625
|
+
seek(time) {
|
|
626
|
+
this.setTime(time);
|
|
627
|
+
this.emit('seek', { time });
|
|
628
|
+
}
|
|
629
|
+
/** Grid snap when it is on, otherwise a pull towards nearby clip edges and the playhead. */
|
|
630
|
+
snapShift(edges, exclude, free) {
|
|
631
|
+
this.snapEl.style.display = 'none';
|
|
632
|
+
if (free)
|
|
633
|
+
return 0;
|
|
634
|
+
if (this.options.showGrid && this.grid.snap !== 'off') {
|
|
635
|
+
const shift = this.grid.round(edges[0]) - edges[0];
|
|
636
|
+
this.showSnap(edges[0] + shift);
|
|
637
|
+
return shift;
|
|
638
|
+
}
|
|
639
|
+
if (!this.options.snapToClips)
|
|
640
|
+
return 0;
|
|
641
|
+
const targets = [0, this.playheadTime];
|
|
642
|
+
for (const clip of this.state.clips) {
|
|
643
|
+
if (!exclude.has(clip.id))
|
|
644
|
+
targets.push(clip.start, clipEnd(clip));
|
|
645
|
+
}
|
|
646
|
+
const shift = nearestSnap(edges, targets, 8 / this.pps);
|
|
647
|
+
if (shift)
|
|
648
|
+
this.showSnap(edges.find((edge) => targets.some((t) => Math.abs(t - edge - shift) < 1e-9)) + shift);
|
|
649
|
+
return shift;
|
|
650
|
+
}
|
|
651
|
+
snapTime(time, exclude, free) {
|
|
652
|
+
return Math.max(0, time + this.snapShift([time], exclude, free));
|
|
653
|
+
}
|
|
654
|
+
showSnap(time) {
|
|
655
|
+
this.snapEl.style.display = '';
|
|
656
|
+
this.snapEl.style.transform = `translateX(${time * this.pps}px)`;
|
|
657
|
+
}
|
|
658
|
+
autoScroll(clientX) {
|
|
659
|
+
const rect = this.scroller.getBoundingClientRect();
|
|
660
|
+
const edge = 32;
|
|
661
|
+
if (clientX > rect.right - edge)
|
|
662
|
+
this.scroller.scrollLeft += 16;
|
|
663
|
+
else if (clientX < rect.left + this.options.headerWidth + edge)
|
|
664
|
+
this.scroller.scrollLeft -= 16;
|
|
665
|
+
}
|
|
666
|
+
onWheel(e) {
|
|
667
|
+
if (!e.ctrlKey && !e.metaKey)
|
|
668
|
+
return;
|
|
669
|
+
e.preventDefault();
|
|
670
|
+
this.setZoom(this.pps * Math.exp(-e.deltaY * 0.002), this.timeAt(e.clientX));
|
|
671
|
+
}
|
|
672
|
+
onKey(e) {
|
|
673
|
+
if (!this.options.keyboard || isTyping(e.target))
|
|
674
|
+
return;
|
|
675
|
+
const mod = e.ctrlKey || e.metaKey;
|
|
676
|
+
const key = e.key.toLowerCase();
|
|
677
|
+
const step = this.options.showGrid && this.grid.step ? this.grid.step : 0.1;
|
|
678
|
+
const edit = !this.options.readonly;
|
|
679
|
+
let handled = true;
|
|
680
|
+
if (mod && key === 'z' && edit) {
|
|
681
|
+
if (e.shiftKey)
|
|
682
|
+
this.redo();
|
|
683
|
+
else
|
|
684
|
+
this.undo();
|
|
685
|
+
}
|
|
686
|
+
else if (mod && key === 'y' && edit)
|
|
687
|
+
this.redo();
|
|
688
|
+
else if (mod && key === 'a')
|
|
689
|
+
this.select(this.state.clips.map((clip) => clip.id));
|
|
690
|
+
else if (mod && key === 'd' && edit)
|
|
691
|
+
this.duplicateSelected();
|
|
692
|
+
else if ((key === 'delete' || key === 'backspace') && edit)
|
|
693
|
+
this.deleteSelected();
|
|
694
|
+
else if (key === 's' && !mod && edit)
|
|
695
|
+
this.split();
|
|
696
|
+
else if (key === 'escape') {
|
|
697
|
+
if (this.gesture)
|
|
698
|
+
this.cancelGesture();
|
|
699
|
+
else {
|
|
700
|
+
this.select([]);
|
|
701
|
+
this.setRange(null);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
else if (key === 'arrowleft' && edit && this.selected.size)
|
|
705
|
+
this.nudge(-step);
|
|
706
|
+
else if (key === 'arrowright' && edit && this.selected.size)
|
|
707
|
+
this.nudge(step);
|
|
708
|
+
else if (key === 'arrowup' && edit && this.selected.size)
|
|
709
|
+
this.nudge(0, -1);
|
|
710
|
+
else if (key === 'arrowdown' && edit && this.selected.size)
|
|
711
|
+
this.nudge(0, 1);
|
|
712
|
+
else
|
|
713
|
+
handled = false;
|
|
714
|
+
if (handled)
|
|
715
|
+
e.preventDefault();
|
|
716
|
+
}
|
|
717
|
+
}
|