@link-foundation/ui-designer 0.0.0-stage → 0.0.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 +24 -0
- package/README.md +180 -2
- package/package.json +41 -4
- package/src/actions.js +195 -0
- package/src/canvas-view.js +171 -0
- package/src/canvas.js +317 -0
- package/src/catalog.js +81 -0
- package/src/commands.js +229 -0
- package/src/designer.css +316 -0
- package/src/designer.js +266 -0
- package/src/designer.lino +104 -0
- package/src/dom.js +22 -0
- package/src/drop.js +82 -0
- package/src/editing.js +124 -0
- package/src/geometry.js +54 -0
- package/src/index.js +28 -0
- package/src/layout-text.js +3 -0
- package/src/panels.js +243 -0
- package/src/selection.js +68 -0
- package/src/snap.js +43 -0
- package/src/source-panel.js +111 -0
- package/src/sync.js +284 -0
- package/src/timeline/commands.js +208 -0
- package/src/timeline/layout-text.js +3 -0
- package/src/timeline/model.js +128 -0
- package/src/timeline/preview.js +105 -0
- package/src/timeline/properties.js +86 -0
- package/src/timeline/scale.js +16 -0
- package/src/timeline/timeline.js +757 -0
- package/src/timeline/timeline.lino +23 -0
|
@@ -0,0 +1,757 @@
|
|
|
1
|
+
/** Docked timeline. Gestures preview freely and commit source once on release. */
|
|
2
|
+
import { parseOrThrow } from '@link-foundation/ui-core';
|
|
3
|
+
import { createStore } from '@link-foundation/ui-store';
|
|
4
|
+
import { render } from '@link-foundation/ui-dom';
|
|
5
|
+
import { CONTROL_KIND_SPECS } from '@link-foundation/ui-components/controls';
|
|
6
|
+
import { designerVocabulary } from '../catalog.js';
|
|
7
|
+
import { element, editable } from '../dom.js';
|
|
8
|
+
import { readTimeline } from './model.js';
|
|
9
|
+
import { timelineEdit } from './commands.js';
|
|
10
|
+
import { timeToX, xToTime, snapTime } from './scale.js';
|
|
11
|
+
import { TIMELINE_LAYOUT } from './layout-text.js';
|
|
12
|
+
import { createPreview } from './preview.js';
|
|
13
|
+
import { timelineProperties } from './properties.js';
|
|
14
|
+
import { updateProperties } from '../panels.js';
|
|
15
|
+
|
|
16
|
+
export function mountTimeline(panel, context) {
|
|
17
|
+
return panel ? new TimelinePanel(panel, context) : null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class TimelinePanel {
|
|
21
|
+
constructor(panel, context) {
|
|
22
|
+
this.panel = panel;
|
|
23
|
+
this.context = context;
|
|
24
|
+
this.document = this.context.document;
|
|
25
|
+
this.window = this.context.window;
|
|
26
|
+
this.session = this.context.session;
|
|
27
|
+
this.controller = this.context.controller;
|
|
28
|
+
this.report = this.context.report;
|
|
29
|
+
this.panel.classList.add('designer-timeline');
|
|
30
|
+
this.store = createStore({
|
|
31
|
+
document: parseOrThrow(TIMELINE_LAYOUT, {
|
|
32
|
+
vocabulary: designerVocabulary(),
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
35
|
+
this.shell = render(this.panel, this.store, {
|
|
36
|
+
kinds: CONTROL_KIND_SPECS,
|
|
37
|
+
setTitle: false,
|
|
38
|
+
});
|
|
39
|
+
this.content = this.shell.nodeFor('timeline-content');
|
|
40
|
+
this.viewport = element(this.document, 'div', {
|
|
41
|
+
class: 'timeline-viewport',
|
|
42
|
+
tabindex: 0,
|
|
43
|
+
role: 'region',
|
|
44
|
+
'aria-label': 'Timeline tracks',
|
|
45
|
+
});
|
|
46
|
+
this.rows = element(this.document, 'div', { class: 'timeline-rows' });
|
|
47
|
+
this.viewport.append(this.rows);
|
|
48
|
+
this.content.append(this.viewport);
|
|
49
|
+
this.preview = createPreview(this.context);
|
|
50
|
+
this.collapsed = new Set();
|
|
51
|
+
this.model = undefined;
|
|
52
|
+
this.active = undefined;
|
|
53
|
+
this.selected = [];
|
|
54
|
+
this.time = 0;
|
|
55
|
+
this.zoom = 0.4;
|
|
56
|
+
this.frame = null;
|
|
57
|
+
this.loop = false;
|
|
58
|
+
this.running = false;
|
|
59
|
+
this.origin = 0;
|
|
60
|
+
this.gesture = null;
|
|
61
|
+
this.toolbar = {
|
|
62
|
+
play: this.play,
|
|
63
|
+
pause: this.pause,
|
|
64
|
+
stop: () => {
|
|
65
|
+
this.pause();
|
|
66
|
+
this.seek(0);
|
|
67
|
+
},
|
|
68
|
+
loop: () => {
|
|
69
|
+
this.loop = !this.loop;
|
|
70
|
+
this.shell
|
|
71
|
+
.nodeFor('timeline-loop')
|
|
72
|
+
.querySelector('button')
|
|
73
|
+
?.setAttribute('aria-pressed', String(this.loop));
|
|
74
|
+
},
|
|
75
|
+
add: this.add,
|
|
76
|
+
delete: this.remove,
|
|
77
|
+
'zoom-in': () => this.zoomBy(1.25),
|
|
78
|
+
'zoom-out': () => this.zoomBy(0.8),
|
|
79
|
+
};
|
|
80
|
+
for (const [name, fn] of Object.entries(this.toolbar)) {
|
|
81
|
+
this.on(this.shell.nodeFor(`timeline-${name}`), 'click', fn);
|
|
82
|
+
}
|
|
83
|
+
this.on(this.viewport, 'wheel', (event) => {
|
|
84
|
+
if (event.ctrlKey || event.metaKey) {
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
this.zoomBy(event.deltaY < 0 ? 1.25 : 0.8);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
this.on(this.panel, 'keydown', (event) => {
|
|
90
|
+
if (editable(event.target)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'z') {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
event.stopPropagation();
|
|
97
|
+
if (event.key === 'Escape') {
|
|
98
|
+
this.gesture = null;
|
|
99
|
+
this.update();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const handlers = {
|
|
103
|
+
Home: () => this.seek(0),
|
|
104
|
+
End: () => this.seek(this.duration(this.current())),
|
|
105
|
+
' ': () => (this.running ? this.pause() : this.play()),
|
|
106
|
+
k: this.add,
|
|
107
|
+
K: this.add,
|
|
108
|
+
Delete: this.remove,
|
|
109
|
+
Backspace: this.remove,
|
|
110
|
+
ArrowLeft: () =>
|
|
111
|
+
this.selected.length && event.target.getAttribute('role') !== 'slider'
|
|
112
|
+
? this.moveSelection(
|
|
113
|
+
(-(event.shiftKey ? 10 : 1) * 1000) / this.fps()
|
|
114
|
+
)
|
|
115
|
+
: this.seek(this.time - 1000 / this.fps()),
|
|
116
|
+
ArrowRight: () =>
|
|
117
|
+
this.selected.length && event.target.getAttribute('role') !== 'slider'
|
|
118
|
+
? this.moveSelection(
|
|
119
|
+
((event.shiftKey ? 10 : 1) * 1000) / this.fps()
|
|
120
|
+
)
|
|
121
|
+
: this.seek(this.time + 1000 / this.fps()),
|
|
122
|
+
};
|
|
123
|
+
if (handlers[event.key]) {
|
|
124
|
+
event.preventDefault();
|
|
125
|
+
handlers[event.key]();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
this.on(this.context.canvas, 'focusin', () => {
|
|
129
|
+
if (this.selected.length) {
|
|
130
|
+
this.clearSelection();
|
|
131
|
+
this.context.refresh();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
this.unsubscribe = this.session.subscribe(() => {
|
|
135
|
+
this.pause();
|
|
136
|
+
this.preview.cancel();
|
|
137
|
+
this.update();
|
|
138
|
+
});
|
|
139
|
+
this.update();
|
|
140
|
+
}
|
|
141
|
+
on = (node, type, callback) =>
|
|
142
|
+
node.addEventListener(type, callback, {
|
|
143
|
+
signal: this.rows?.contains(node)
|
|
144
|
+
? this.drawController.signal
|
|
145
|
+
: this.controller.signal,
|
|
146
|
+
});
|
|
147
|
+
fps = () => this.context.options.timelineFps ?? 60;
|
|
148
|
+
current = () =>
|
|
149
|
+
[...this.model.animations, ...this.model.recordings].find(
|
|
150
|
+
(a) => a.id === this.active
|
|
151
|
+
) ??
|
|
152
|
+
this.model.animations[0] ??
|
|
153
|
+
this.model.recordings[0];
|
|
154
|
+
isRecording = (item) => !!item?.events;
|
|
155
|
+
duration = (item) =>
|
|
156
|
+
this.isRecording(item)
|
|
157
|
+
? (item.events.at(-1)?.timeMs ?? 0)
|
|
158
|
+
: (item?.durationMs ?? 0);
|
|
159
|
+
maximum = () =>
|
|
160
|
+
Math.max(
|
|
161
|
+
1000,
|
|
162
|
+
...this.model.animations.map((a) => a.durationMs),
|
|
163
|
+
...this.model.recordings.map((r) => this.duration(r))
|
|
164
|
+
);
|
|
165
|
+
state = () => ({ zoom: this.zoom, scroll: this.viewport.scrollLeft });
|
|
166
|
+
timeAt = (event, lane) =>
|
|
167
|
+
Math.max(
|
|
168
|
+
0,
|
|
169
|
+
xToTime(event.clientX - lane.getBoundingClientRect().left, {
|
|
170
|
+
zoom: this.zoom,
|
|
171
|
+
scroll: 0,
|
|
172
|
+
})
|
|
173
|
+
);
|
|
174
|
+
identity = (key) => `${key.id}/${key.track ?? 'event'}/${key.index}`;
|
|
175
|
+
same = (a, b) => this.identity(a) === this.identity(b);
|
|
176
|
+
hasSelection = () => this.selected.length > 0;
|
|
177
|
+
props = () => {
|
|
178
|
+
if (this.selected.length) {
|
|
179
|
+
this.propertySelection = true;
|
|
180
|
+
timelineProperties(
|
|
181
|
+
this.context.shell.nodeFor('properties'),
|
|
182
|
+
this.selected,
|
|
183
|
+
this.current(),
|
|
184
|
+
this.commit,
|
|
185
|
+
this.context.command
|
|
186
|
+
);
|
|
187
|
+
} else if (this.propertySelection) {
|
|
188
|
+
this.propertySelection = false;
|
|
189
|
+
updateProperties(this.context.shell.nodeFor('properties'), this.context);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
pause = () => {
|
|
193
|
+
this.running = false;
|
|
194
|
+
if (this.frame !== null) {
|
|
195
|
+
this.window.cancelAnimationFrame(this.frame);
|
|
196
|
+
}
|
|
197
|
+
this.frame = null;
|
|
198
|
+
this.store.setProp('timeline-play', 'label', ['Play']);
|
|
199
|
+
};
|
|
200
|
+
seek = (ms) => {
|
|
201
|
+
const item = this.current();
|
|
202
|
+
if (!item) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
this.time = Math.max(0, Math.min(this.duration(item), ms));
|
|
206
|
+
try {
|
|
207
|
+
this.preview.seek(item, this.time, this.isRecording(item));
|
|
208
|
+
} catch (error) {
|
|
209
|
+
this.report(error.message);
|
|
210
|
+
}
|
|
211
|
+
this.rows.querySelectorAll('.timeline-playhead').forEach((node) => {
|
|
212
|
+
node.style.left = `${timeToX(this.time, { zoom: this.zoom })}px`;
|
|
213
|
+
});
|
|
214
|
+
const ruler = this.rows.querySelector('[role=slider]');
|
|
215
|
+
ruler?.setAttribute('aria-valuenow', String(Math.round(this.time)));
|
|
216
|
+
ruler?.setAttribute('aria-valuemax', String(this.duration(item)));
|
|
217
|
+
ruler?.setAttribute(
|
|
218
|
+
'aria-valuetext',
|
|
219
|
+
`${Math.round(this.time)} milliseconds`
|
|
220
|
+
);
|
|
221
|
+
};
|
|
222
|
+
play = () => {
|
|
223
|
+
if (this.running || !this.current()) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (this.time >= this.duration(this.current())) {
|
|
227
|
+
this.time = 0;
|
|
228
|
+
}
|
|
229
|
+
this.running = true;
|
|
230
|
+
this.origin = this.window.performance.now() - this.time;
|
|
231
|
+
this.store.setProp('timeline-play', 'label', ['Playing']);
|
|
232
|
+
const tick = (stamp) => {
|
|
233
|
+
const end = this.duration(this.current());
|
|
234
|
+
const elapsed = stamp - this.origin;
|
|
235
|
+
if (elapsed >= end) {
|
|
236
|
+
if (this.loop && end > 0) {
|
|
237
|
+
this.origin = stamp;
|
|
238
|
+
this.seek(0);
|
|
239
|
+
} else {
|
|
240
|
+
this.seek(end);
|
|
241
|
+
this.pause();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
this.seek(elapsed);
|
|
246
|
+
}
|
|
247
|
+
this.frame = this.window.requestAnimationFrame(tick);
|
|
248
|
+
};
|
|
249
|
+
this.frame = this.window.requestAnimationFrame(tick);
|
|
250
|
+
};
|
|
251
|
+
activate = (item) => {
|
|
252
|
+
if (this.active !== item.id) {
|
|
253
|
+
this.pause();
|
|
254
|
+
this.preview.cancel();
|
|
255
|
+
this.active = item.id;
|
|
256
|
+
this.time = 0;
|
|
257
|
+
this.selected = [];
|
|
258
|
+
this.updateSelection();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
commit = (changes) => {
|
|
262
|
+
this.pause();
|
|
263
|
+
try {
|
|
264
|
+
const approved = changes.map((change) => {
|
|
265
|
+
if (change.type === 'duration') {
|
|
266
|
+
const item = this.model.animations.find((a) => a.id === change.id);
|
|
267
|
+
if (
|
|
268
|
+
Array.isArray(item.duration) &&
|
|
269
|
+
!this.window.confirm(
|
|
270
|
+
'Replace the duration token with a literal millisecond value?'
|
|
271
|
+
)
|
|
272
|
+
) {
|
|
273
|
+
throw new Error('Duration unchanged');
|
|
274
|
+
}
|
|
275
|
+
return { ...change, replaceToken: true };
|
|
276
|
+
}
|
|
277
|
+
return change;
|
|
278
|
+
});
|
|
279
|
+
const text = approved.reduce(timelineEdit, this.session.getText());
|
|
280
|
+
this.retainSelection(text, approved);
|
|
281
|
+
this.preview.cancel();
|
|
282
|
+
const result = this.context.editSource(text);
|
|
283
|
+
if (result.error) {
|
|
284
|
+
throw new Error(result.error.message);
|
|
285
|
+
}
|
|
286
|
+
this.context.options.trace?.('timeline-commit', {
|
|
287
|
+
changes: approved,
|
|
288
|
+
tx: this.context.store.log().at(-1)?.tx,
|
|
289
|
+
});
|
|
290
|
+
this.report('Timeline changes saved');
|
|
291
|
+
this.update();
|
|
292
|
+
this.seek(this.time);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
this.report(error.message);
|
|
295
|
+
this.update();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
retainSelection = (text, changes) => {
|
|
299
|
+
const next = readTimeline(text, this.context.options.resolveToken);
|
|
300
|
+
this.selected = this.selected.flatMap((key) => {
|
|
301
|
+
const edit = changes.find((c) => this.same(c, key));
|
|
302
|
+
if (edit?.type.endsWith('-delete')) {
|
|
303
|
+
return [];
|
|
304
|
+
}
|
|
305
|
+
if (key.recording) {
|
|
306
|
+
return this.retainEventSelection(next, key, edit);
|
|
307
|
+
}
|
|
308
|
+
const before = this.model.animations.find((a) => a.id === key.id)?.tracks[
|
|
309
|
+
key.track
|
|
310
|
+
]?.keys[key.index];
|
|
311
|
+
const after =
|
|
312
|
+
next.animations.find((a) => a.id === key.id)?.tracks[key.track]?.keys ??
|
|
313
|
+
[];
|
|
314
|
+
const offset =
|
|
315
|
+
edit?.type === 'key-move'
|
|
316
|
+
? Math.max(0, Math.min(1, edit.offset))
|
|
317
|
+
: before?.offset;
|
|
318
|
+
const index = after.findIndex((k) => Math.abs(k.offset - offset) < 1e-9);
|
|
319
|
+
return index < 0 ? [] : [{ ...key, index }];
|
|
320
|
+
});
|
|
321
|
+
};
|
|
322
|
+
retainEventSelection = (next, key, edit) => {
|
|
323
|
+
const before = this.model.recordings.find((r) => r.id === key.id)?.events[
|
|
324
|
+
key.index
|
|
325
|
+
];
|
|
326
|
+
const after = next.recordings.find((r) => r.id === key.id)?.events ?? [];
|
|
327
|
+
const stamp =
|
|
328
|
+
edit?.type === 'event-move'
|
|
329
|
+
? Math.max(0, Math.round(edit.ms))
|
|
330
|
+
: before?.timeMs;
|
|
331
|
+
const index = after.findIndex(
|
|
332
|
+
(e) =>
|
|
333
|
+
e.timeMs === stamp &&
|
|
334
|
+
(edit?.type === 'event-action' ||
|
|
335
|
+
JSON.stringify(e.action) === JSON.stringify(before?.action))
|
|
336
|
+
);
|
|
337
|
+
return index < 0 ? [] : [{ ...key, index }];
|
|
338
|
+
};
|
|
339
|
+
add = () => {
|
|
340
|
+
const item = this.current();
|
|
341
|
+
if (!item) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if (this.isRecording(item)) {
|
|
345
|
+
this.commit([{ id: item.id, type: 'event-insert', ms: this.time }]);
|
|
346
|
+
} else {
|
|
347
|
+
const track = this.selected[0]?.track ?? 0;
|
|
348
|
+
const lane = item.tracks[track];
|
|
349
|
+
if (lane && item.durationMs > 0) {
|
|
350
|
+
this.commit([
|
|
351
|
+
{
|
|
352
|
+
id: item.id,
|
|
353
|
+
track,
|
|
354
|
+
type: 'key-insert',
|
|
355
|
+
offset: this.time / item.durationMs,
|
|
356
|
+
value:
|
|
357
|
+
lane.keys.findLast((k) => k.offset <= this.time / item.durationMs)
|
|
358
|
+
?.value ?? '1',
|
|
359
|
+
},
|
|
360
|
+
]);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
remove = () => {
|
|
365
|
+
// Reverse indices keep the remaining selection addresses valid.
|
|
366
|
+
this.commit(
|
|
367
|
+
[...this.selected]
|
|
368
|
+
.sort((a, b) => b.index - a.index)
|
|
369
|
+
.map((key) => ({
|
|
370
|
+
...key,
|
|
371
|
+
type: key.recording ? 'event-delete' : 'key-delete',
|
|
372
|
+
}))
|
|
373
|
+
);
|
|
374
|
+
this.selected = [];
|
|
375
|
+
this.update();
|
|
376
|
+
};
|
|
377
|
+
clearSelection = () => {
|
|
378
|
+
this.selected = [];
|
|
379
|
+
this.pause();
|
|
380
|
+
this.preview.cancel();
|
|
381
|
+
};
|
|
382
|
+
select = (key, event) => {
|
|
383
|
+
const item = [...this.model.animations, ...this.model.recordings].find(
|
|
384
|
+
(a) => a.id === key.id
|
|
385
|
+
);
|
|
386
|
+
this.activate(item);
|
|
387
|
+
this.selected = event.shiftKey
|
|
388
|
+
? this.selected.some((k) => this.same(k, key))
|
|
389
|
+
? this.selected.filter((k) => !this.same(k, key))
|
|
390
|
+
: [...this.selected, key]
|
|
391
|
+
: this.selected.some((k) => this.same(k, key))
|
|
392
|
+
? this.selected
|
|
393
|
+
: [key];
|
|
394
|
+
this.updateSelection();
|
|
395
|
+
};
|
|
396
|
+
updateSelection = () => {
|
|
397
|
+
this.rows
|
|
398
|
+
.querySelectorAll('[data-marker]')
|
|
399
|
+
.forEach((node) =>
|
|
400
|
+
node.setAttribute(
|
|
401
|
+
'aria-pressed',
|
|
402
|
+
String(
|
|
403
|
+
this.selected.some((k) => this.identity(k) === node.dataset.marker)
|
|
404
|
+
)
|
|
405
|
+
)
|
|
406
|
+
);
|
|
407
|
+
this.props();
|
|
408
|
+
};
|
|
409
|
+
snapped = (ms, item) => {
|
|
410
|
+
const candidates = [
|
|
411
|
+
this.time,
|
|
412
|
+
...(this.isRecording(item)
|
|
413
|
+
? item.events.map((e) => e.timeMs)
|
|
414
|
+
: item.tracks.flatMap((t) =>
|
|
415
|
+
t.keys.map((k) => k.offset * item.durationMs)
|
|
416
|
+
)),
|
|
417
|
+
];
|
|
418
|
+
return snapTime(ms, {
|
|
419
|
+
fps: this.context.options.timelineFps ?? 60,
|
|
420
|
+
candidates,
|
|
421
|
+
threshold: 6 / this.zoom,
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
marker = (lane, item, key, ms, label) => {
|
|
425
|
+
const button = element(
|
|
426
|
+
this.document,
|
|
427
|
+
'button',
|
|
428
|
+
{
|
|
429
|
+
type: 'button',
|
|
430
|
+
class: 'timeline-marker',
|
|
431
|
+
'data-marker': this.identity(key),
|
|
432
|
+
'aria-label': label,
|
|
433
|
+
'aria-pressed': this.selected.some((k) => this.same(k, key)),
|
|
434
|
+
},
|
|
435
|
+
key.recording ? '●' : '◆'
|
|
436
|
+
);
|
|
437
|
+
button.style.left = `${timeToX(ms, { zoom: this.zoom })}px`;
|
|
438
|
+
if (ms === 0) {
|
|
439
|
+
button.style.transform = 'none';
|
|
440
|
+
}
|
|
441
|
+
lane.append(button);
|
|
442
|
+
this.on(button, 'click', (event) => {
|
|
443
|
+
if (event.detail === 0) {
|
|
444
|
+
this.select(key, event);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
this.on(button, 'pointerdown', (event) => {
|
|
448
|
+
if (event.button !== 0) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
event.preventDefault();
|
|
452
|
+
this.select(key, event);
|
|
453
|
+
button.focus();
|
|
454
|
+
this.pause();
|
|
455
|
+
this.gesture = { key, item, lane, start: event.clientX, ms, button };
|
|
456
|
+
button.setPointerCapture(event.pointerId);
|
|
457
|
+
});
|
|
458
|
+
this.on(button, 'pointermove', (event) => {
|
|
459
|
+
if (!this.gesture || this.gesture.button !== button) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const next = this.snapped(
|
|
463
|
+
ms + (event.clientX - this.gesture.start) / this.zoom,
|
|
464
|
+
item
|
|
465
|
+
);
|
|
466
|
+
this.gesture.next = Math.max(
|
|
467
|
+
0,
|
|
468
|
+
key.recording ? next : Math.min(item.durationMs, next)
|
|
469
|
+
);
|
|
470
|
+
button.style.left = `${this.gesture.next * this.zoom}px`;
|
|
471
|
+
});
|
|
472
|
+
this.on(button, 'pointerup', () => {
|
|
473
|
+
if (!this.gesture || this.gesture.button !== button) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
const g = this.gesture;
|
|
477
|
+
this.gesture = null;
|
|
478
|
+
if (g.next !== undefined && Math.abs(g.next - ms) > 0.01) {
|
|
479
|
+
this.moveSelection(g.next - ms);
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
this.on(button, 'pointercancel', () => {
|
|
483
|
+
this.gesture = null;
|
|
484
|
+
this.update();
|
|
485
|
+
});
|
|
486
|
+
this.on(button, 'lostpointercapture', () => {
|
|
487
|
+
if (this.gesture?.button === button) {
|
|
488
|
+
this.gesture = null;
|
|
489
|
+
this.update();
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
};
|
|
493
|
+
moveSelection = (delta) => {
|
|
494
|
+
const item = this.current();
|
|
495
|
+
// Moving right applies the rightmost key first so group moves do not collide.
|
|
496
|
+
const changes = [...this.selected]
|
|
497
|
+
.sort((a, b) => (delta > 0 ? b.index - a.index : a.index - b.index))
|
|
498
|
+
.map((key) =>
|
|
499
|
+
key.recording
|
|
500
|
+
? {
|
|
501
|
+
...key,
|
|
502
|
+
type: 'event-move',
|
|
503
|
+
ms: item.events[key.index].timeMs + delta,
|
|
504
|
+
}
|
|
505
|
+
: {
|
|
506
|
+
...key,
|
|
507
|
+
type: 'key-move',
|
|
508
|
+
offset:
|
|
509
|
+
item.tracks[key.track].keys[key.index].offset +
|
|
510
|
+
delta / item.durationMs,
|
|
511
|
+
}
|
|
512
|
+
);
|
|
513
|
+
this.commit(changes);
|
|
514
|
+
};
|
|
515
|
+
laneRow = (label) => {
|
|
516
|
+
const row = element(this.document, 'div', { class: 'timeline-row' });
|
|
517
|
+
row.append(
|
|
518
|
+
element(this.document, 'span', { class: 'timeline-label' }, label)
|
|
519
|
+
);
|
|
520
|
+
const lane = element(this.document, 'div', { class: 'timeline-lane' });
|
|
521
|
+
row.append(lane);
|
|
522
|
+
this.rows.append(row);
|
|
523
|
+
return lane;
|
|
524
|
+
};
|
|
525
|
+
zoomBy = (factor) => {
|
|
526
|
+
const center = xToTime(this.viewport.clientWidth / 2, this.state());
|
|
527
|
+
this.zoom = Math.max(0.05, Math.min(4, this.zoom * factor));
|
|
528
|
+
this.update();
|
|
529
|
+
this.viewport.scrollLeft = Math.max(
|
|
530
|
+
0,
|
|
531
|
+
timeToX(center, { zoom: this.zoom }) - this.viewport.clientWidth / 2
|
|
532
|
+
);
|
|
533
|
+
};
|
|
534
|
+
animationRow = (item) => {
|
|
535
|
+
const lane = this.laneRow('');
|
|
536
|
+
const toggle = element(
|
|
537
|
+
this.document,
|
|
538
|
+
'button',
|
|
539
|
+
{ type: 'button', 'aria-expanded': !this.collapsed.has(item.id) },
|
|
540
|
+
`${item.id} · ${Math.round(item.durationMs)} ms`
|
|
541
|
+
);
|
|
542
|
+
lane.previousSibling.replaceWith(toggle);
|
|
543
|
+
toggle.className = 'timeline-label';
|
|
544
|
+
this.on(toggle, 'click', () => {
|
|
545
|
+
this.activate(item);
|
|
546
|
+
this.collapsed.has(item.id)
|
|
547
|
+
? this.collapsed.delete(item.id)
|
|
548
|
+
: this.collapsed.add(item.id);
|
|
549
|
+
this.update();
|
|
550
|
+
});
|
|
551
|
+
const end = element(
|
|
552
|
+
this.document,
|
|
553
|
+
'button',
|
|
554
|
+
{
|
|
555
|
+
type: 'button',
|
|
556
|
+
class: 'timeline-end',
|
|
557
|
+
'aria-label': `Duration of ${item.id}`,
|
|
558
|
+
'data-duration': item.id,
|
|
559
|
+
},
|
|
560
|
+
'┃'
|
|
561
|
+
);
|
|
562
|
+
end.style.left = `${item.durationMs * this.zoom}px`;
|
|
563
|
+
lane.append(end);
|
|
564
|
+
let start;
|
|
565
|
+
this.on(end, 'pointerdown', (event) => {
|
|
566
|
+
this.activate(item);
|
|
567
|
+
start = event.clientX;
|
|
568
|
+
end.setPointerCapture(event.pointerId);
|
|
569
|
+
});
|
|
570
|
+
this.on(end, 'pointermove', (event) => {
|
|
571
|
+
if (start !== undefined) {
|
|
572
|
+
end.style.left = `${Math.max(1, item.durationMs + (event.clientX - start) / this.zoom) * this.zoom}px`;
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
this.on(end, 'pointerup', (event) => {
|
|
576
|
+
if (start === undefined) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const ms = Math.max(
|
|
580
|
+
1,
|
|
581
|
+
this.snapped(
|
|
582
|
+
item.durationMs + (event.clientX - start) / this.zoom,
|
|
583
|
+
item
|
|
584
|
+
)
|
|
585
|
+
);
|
|
586
|
+
start = undefined;
|
|
587
|
+
this.commit([{ id: item.id, type: 'duration', ms }]);
|
|
588
|
+
});
|
|
589
|
+
this.on(end, 'pointercancel', () => {
|
|
590
|
+
start = undefined;
|
|
591
|
+
this.update();
|
|
592
|
+
});
|
|
593
|
+
this.on(end, 'keydown', (event) => {
|
|
594
|
+
if (['ArrowLeft', 'ArrowRight'].includes(event.key)) {
|
|
595
|
+
event.preventDefault();
|
|
596
|
+
event.stopPropagation();
|
|
597
|
+
this.commit([
|
|
598
|
+
{
|
|
599
|
+
id: item.id,
|
|
600
|
+
type: 'duration',
|
|
601
|
+
ms: Math.max(
|
|
602
|
+
1,
|
|
603
|
+
item.durationMs +
|
|
604
|
+
((event.key === 'ArrowLeft' ? -1 : 1) *
|
|
605
|
+
(event.shiftKey ? 10 : 1) *
|
|
606
|
+
1000) /
|
|
607
|
+
this.fps()
|
|
608
|
+
),
|
|
609
|
+
},
|
|
610
|
+
]);
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
if (!this.collapsed.has(item.id)) {
|
|
614
|
+
item.tracks.forEach((track, ti) => {
|
|
615
|
+
const lane = this.laneRow(track.property);
|
|
616
|
+
track.keys.forEach((key, index) =>
|
|
617
|
+
this.marker(
|
|
618
|
+
lane,
|
|
619
|
+
item,
|
|
620
|
+
{ id: item.id, track: ti, index },
|
|
621
|
+
key.offset * item.durationMs,
|
|
622
|
+
`${track.property} key at ${Math.round(key.offset * 100)}%, value ${key.value}`
|
|
623
|
+
)
|
|
624
|
+
);
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
selectionExists = (key) => {
|
|
629
|
+
const item = [...this.model.animations, ...this.model.recordings].find(
|
|
630
|
+
(item) => item.id === key.id
|
|
631
|
+
);
|
|
632
|
+
return key.recording
|
|
633
|
+
? !!item?.events?.[key.index]
|
|
634
|
+
: !!item?.tracks?.[key.track]?.keys[key.index];
|
|
635
|
+
};
|
|
636
|
+
update = () => {
|
|
637
|
+
const focused = this.document.activeElement;
|
|
638
|
+
const rulerFocused =
|
|
639
|
+
focused?.getAttribute('role') === 'slider' && this.rows.contains(focused);
|
|
640
|
+
this.drawController?.abort();
|
|
641
|
+
this.drawController = new this.window.AbortController();
|
|
642
|
+
this.model = readTimeline(
|
|
643
|
+
this.context.store,
|
|
644
|
+
this.context.options.resolveToken
|
|
645
|
+
);
|
|
646
|
+
if (!this.current()) {
|
|
647
|
+
this.selected = [];
|
|
648
|
+
this.props();
|
|
649
|
+
this.rows.replaceChildren(
|
|
650
|
+
element(
|
|
651
|
+
this.document,
|
|
652
|
+
'p',
|
|
653
|
+
{},
|
|
654
|
+
'No animations or recordings in this document'
|
|
655
|
+
)
|
|
656
|
+
);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
this.activate(this.current());
|
|
660
|
+
this.selected = this.selected.filter(this.selectionExists);
|
|
661
|
+
this.rows.replaceChildren();
|
|
662
|
+
this.rows.style.width = `${Math.max(600, this.maximum() * this.zoom + 230)}px`;
|
|
663
|
+
const ruler = this.laneRow('Time (ms)');
|
|
664
|
+
ruler.classList.add('timeline-ruler');
|
|
665
|
+
ruler.setAttribute('role', 'slider');
|
|
666
|
+
ruler.setAttribute('tabindex', '0');
|
|
667
|
+
ruler.setAttribute('aria-label', 'Playhead time');
|
|
668
|
+
ruler.setAttribute('aria-valuemin', '0');
|
|
669
|
+
for (
|
|
670
|
+
let ms = 0;
|
|
671
|
+
ms <= this.maximum();
|
|
672
|
+
ms += Math.max(
|
|
673
|
+
100,
|
|
674
|
+
Math.ceil(80 / this.zoom / 100) * 100,
|
|
675
|
+
Math.ceil(this.maximum() / 200 / 100) * 100
|
|
676
|
+
)
|
|
677
|
+
) {
|
|
678
|
+
const tick = element(this.document, 'span', {}, String(ms));
|
|
679
|
+
tick.style.left = `${ms * this.zoom}px`;
|
|
680
|
+
ruler.append(tick);
|
|
681
|
+
}
|
|
682
|
+
const scrub = (event) => {
|
|
683
|
+
this.pause();
|
|
684
|
+
this.seek(this.timeAt(event, ruler));
|
|
685
|
+
};
|
|
686
|
+
this.on(ruler, 'pointerdown', (event) => {
|
|
687
|
+
ruler.setPointerCapture(event.pointerId);
|
|
688
|
+
scrub(event);
|
|
689
|
+
});
|
|
690
|
+
this.on(ruler, 'pointermove', (event) => {
|
|
691
|
+
if (ruler.hasPointerCapture(event.pointerId)) {
|
|
692
|
+
scrub(event);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
for (const item of this.model.animations) {
|
|
696
|
+
this.animationRow(item);
|
|
697
|
+
}
|
|
698
|
+
for (const item of this.model.recordings) {
|
|
699
|
+
const lane = this.laneRow(item.id);
|
|
700
|
+
const choose = element(
|
|
701
|
+
this.document,
|
|
702
|
+
'button',
|
|
703
|
+
{ type: 'button' },
|
|
704
|
+
item.id
|
|
705
|
+
);
|
|
706
|
+
lane.previousSibling.replaceWith(choose);
|
|
707
|
+
choose.className = 'timeline-label';
|
|
708
|
+
this.on(choose, 'click', () => {
|
|
709
|
+
this.activate(item);
|
|
710
|
+
this.seek(0);
|
|
711
|
+
});
|
|
712
|
+
item.events.forEach((event, index) =>
|
|
713
|
+
this.marker(
|
|
714
|
+
lane,
|
|
715
|
+
item,
|
|
716
|
+
{ id: item.id, index, recording: true },
|
|
717
|
+
event.timeMs,
|
|
718
|
+
`${item.id} event at ${event.timeMs} ms: ${event.action.join(' ')}`
|
|
719
|
+
)
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
this.rows.querySelectorAll('.timeline-lane').forEach((lane) => {
|
|
723
|
+
const head = element(this.document, 'div', {
|
|
724
|
+
class: 'timeline-playhead',
|
|
725
|
+
'aria-hidden': true,
|
|
726
|
+
});
|
|
727
|
+
head.style.left = `${this.time * this.zoom}px`;
|
|
728
|
+
lane.append(head);
|
|
729
|
+
});
|
|
730
|
+
ruler.setAttribute('aria-valuenow', String(Math.round(this.time)));
|
|
731
|
+
ruler.setAttribute('aria-valuemax', String(this.duration(this.current())));
|
|
732
|
+
this.props();
|
|
733
|
+
this.restoreFocus(focused, rulerFocused);
|
|
734
|
+
};
|
|
735
|
+
restoreFocus = (focused, rulerFocused) => {
|
|
736
|
+
const marker = focused?.dataset?.marker;
|
|
737
|
+
const duration = focused?.dataset?.duration;
|
|
738
|
+
if (marker) {
|
|
739
|
+
[...this.rows.querySelectorAll('[data-marker]')]
|
|
740
|
+
.find((node) => node.dataset.marker === marker)
|
|
741
|
+
?.focus();
|
|
742
|
+
} else if (duration) {
|
|
743
|
+
[...this.rows.querySelectorAll('[data-duration]')]
|
|
744
|
+
.find((node) => node.dataset.duration === duration)
|
|
745
|
+
?.focus();
|
|
746
|
+
} else if (rulerFocused) {
|
|
747
|
+
this.rows.querySelector('[role=slider]')?.focus();
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
destroy = () => {
|
|
751
|
+
this.drawController?.abort();
|
|
752
|
+
this.pause();
|
|
753
|
+
this.preview.cancel();
|
|
754
|
+
this.unsubscribe();
|
|
755
|
+
this.shell.destroy();
|
|
756
|
+
};
|
|
757
|
+
}
|