@vitavision/workbench 0.0.0-bootstrap.0 → 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-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +89 -0
- package/dist/index.d.ts +671 -0
- package/dist/index.js +1469 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -3
- package/src/components/AppShell.tsx +141 -0
- package/src/components/FileDrop.tsx +223 -0
- package/src/components/PlaybackBar.tsx +303 -0
- package/src/components/SplitPane.tsx +306 -0
- package/src/components/Toaster.tsx +149 -0
- package/src/components/TreeView.tsx +244 -0
- package/src/components/dropFiles.ts +131 -0
- package/src/components/playhead.ts +158 -0
- package/src/components/splitSize.ts +160 -0
- package/src/components/toastStore.ts +128 -0
- package/src/components/treeModel.ts +188 -0
- package/src/components/usePlayback.ts +105 -0
- package/src/index.ts +67 -0
- package/src/styles.css +7 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1469 @@
|
|
|
1
|
+
import { Button, Select, Slider, byDensity, cn, focusRing, toneColor, useDensity } from "@vitavision/ui";
|
|
2
|
+
import { useCallback, useEffect, useEffectEvent, useId, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
+
import { ChevronFirst, ChevronLast, ChevronRight, CircleCheck, CircleX, FolderOpen, Info, Pause, Play, Repeat, SkipBack, SkipForward, StepBack, StepForward, TriangleAlert, X } from "lucide-react";
|
|
5
|
+
//#region src/components/splitSize.ts
|
|
6
|
+
/**
|
|
7
|
+
* A size in pixels.
|
|
8
|
+
*
|
|
9
|
+
* @param size - Pixels, or a percentage of `length`.
|
|
10
|
+
* @param length - The split's length along its axis, in pixels.
|
|
11
|
+
* @returns The size in pixels (never negative).
|
|
12
|
+
*/
|
|
13
|
+
function resolveSize(size, length) {
|
|
14
|
+
const px = typeof size === "number" ? size : Number.parseFloat(size) / 100 * length;
|
|
15
|
+
return Number.isFinite(px) ? Math.max(0, px) : 0;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The limits in pixels for a split of `length`. `max` never exceeds `length`, and `min`
|
|
19
|
+
* never exceeds `max`.
|
|
20
|
+
*
|
|
21
|
+
* @param length - The split's length along its axis, in pixels.
|
|
22
|
+
* @param minSize - The smallest open size. Defaults to 0.
|
|
23
|
+
* @param maxSize - The largest size. Defaults to the whole length.
|
|
24
|
+
* @param collapsible - Whether the pane may close below `minSize`.
|
|
25
|
+
* @returns The resolved limits.
|
|
26
|
+
*/
|
|
27
|
+
function resolveLimits(length, minSize = 0, maxSize = "100%", collapsible = false) {
|
|
28
|
+
const max = Math.min(resolveSize(maxSize, length), Math.max(0, length));
|
|
29
|
+
return {
|
|
30
|
+
min: Math.min(resolveSize(minSize, length), max),
|
|
31
|
+
max,
|
|
32
|
+
collapsible
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Hold a proposed size to the limits. A collapsible pane dragged below half its minimum
|
|
37
|
+
* snaps shut (0); between half the minimum and the minimum it stays at the minimum.
|
|
38
|
+
*
|
|
39
|
+
* @param size - The proposed size, in pixels.
|
|
40
|
+
* @param limits - The resolved limits.
|
|
41
|
+
* @returns The size to apply, in pixels, rounded to whole pixels.
|
|
42
|
+
*/
|
|
43
|
+
function clampSize(size, limits) {
|
|
44
|
+
if (limits.collapsible && size < limits.min / 2) return 0;
|
|
45
|
+
return Math.round(Math.min(limits.max, Math.max(limits.min, size)));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The divider's keyboard (the WAI-ARIA window-splitter pattern).
|
|
49
|
+
*
|
|
50
|
+
* The arrow keys along the split's axis move the divider the way they point — so the sized
|
|
51
|
+
* pane grows or shrinks depending on which side it is on — by `step`, or four steps with
|
|
52
|
+
* Shift. Home gives the sized pane its minimum, End its maximum, and Enter collapses or
|
|
53
|
+
* restores a collapsible pane.
|
|
54
|
+
*
|
|
55
|
+
* @param key - `KeyboardEvent.key`.
|
|
56
|
+
* @param shift - Whether Shift is held.
|
|
57
|
+
* @param size - The sized pane's current size, in pixels.
|
|
58
|
+
* @param options - The layout, the step in pixels and the resolved limits.
|
|
59
|
+
* @returns The action, or `null` when the key is not the divider's.
|
|
60
|
+
*/
|
|
61
|
+
function splitKeyAction(key, shift, size, options) {
|
|
62
|
+
const { orientation, sizedPane, step, limits } = options;
|
|
63
|
+
const [back, forward] = orientation === "horizontal" ? ["ArrowLeft", "ArrowRight"] : ["ArrowUp", "ArrowDown"];
|
|
64
|
+
const grow = sizedPane === "start" ? 1 : -1;
|
|
65
|
+
const delta = shift ? step * 4 : step;
|
|
66
|
+
switch (key) {
|
|
67
|
+
case forward: return {
|
|
68
|
+
type: "resize",
|
|
69
|
+
size: clampOpen(size + grow * delta, limits)
|
|
70
|
+
};
|
|
71
|
+
case back: return {
|
|
72
|
+
type: "resize",
|
|
73
|
+
size: clampOpen(size - grow * delta, limits)
|
|
74
|
+
};
|
|
75
|
+
case "Home": return {
|
|
76
|
+
type: "resize",
|
|
77
|
+
size: Math.round(limits.min)
|
|
78
|
+
};
|
|
79
|
+
case "End": return {
|
|
80
|
+
type: "resize",
|
|
81
|
+
size: Math.round(limits.max)
|
|
82
|
+
};
|
|
83
|
+
case "Enter": return limits.collapsible ? { type: "toggle-collapse" } : null;
|
|
84
|
+
default: return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** A key step never snaps a pane shut (that is Enter's job): clamp to the open range. */
|
|
88
|
+
function clampOpen(size, limits) {
|
|
89
|
+
return Math.round(Math.min(limits.max, Math.max(limits.min, size)));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The size stored under `key`, if any.
|
|
93
|
+
*
|
|
94
|
+
* @param key - The `localStorage` key.
|
|
95
|
+
* @returns The stored size in pixels, or `null` when nothing valid is stored or storage
|
|
96
|
+
* cannot be read (private browsing, the server).
|
|
97
|
+
*/
|
|
98
|
+
function readStoredSize(key) {
|
|
99
|
+
try {
|
|
100
|
+
const raw = window.localStorage.getItem(key);
|
|
101
|
+
if (raw === null) return null;
|
|
102
|
+
const value = Number(raw);
|
|
103
|
+
return Number.isFinite(value) && value >= 0 ? value : null;
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Remember a size under `key`. Failures (storage full, disabled, the server) are ignored: a
|
|
110
|
+
* size that is not remembered is not worth an error.
|
|
111
|
+
*
|
|
112
|
+
* @param key - The `localStorage` key.
|
|
113
|
+
* @param size - The size in pixels.
|
|
114
|
+
*/
|
|
115
|
+
function writeStoredSize(key, size) {
|
|
116
|
+
try {
|
|
117
|
+
window.localStorage.setItem(key, String(Math.round(size)));
|
|
118
|
+
} catch {}
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/components/SplitPane.tsx
|
|
122
|
+
/**
|
|
123
|
+
* Two panes with a draggable, keyboard-operable divider between them.
|
|
124
|
+
*
|
|
125
|
+
* The divider is a `role="separator"` focusable control carrying `aria-valuenow` (the sized
|
|
126
|
+
* pane's size in pixels), `aria-valuemin`/`aria-valuemax` and `aria-controls` (the sized
|
|
127
|
+
* pane). Arrow keys along the axis move it by `step` (Shift: four steps), Home/End give the
|
|
128
|
+
* sized pane its minimum/maximum, Enter collapses or restores a `collapsible` pane. Dragging
|
|
129
|
+
* uses pointer capture, so the drag survives the pointer leaving the divider.
|
|
130
|
+
*
|
|
131
|
+
* Fills its container (`h-full w-full`), so it nests. State is exposed as `data-orientation`
|
|
132
|
+
* on the root, `data-dragging` on the root and the divider while a drag is in progress, and
|
|
133
|
+
* `data-collapsed` on the root and the sized pane while it is closed.
|
|
134
|
+
*/
|
|
135
|
+
function SplitPane({ children, orientation = "horizontal", sizedPane = "start", size: controlledSize, defaultSize = "50%", onSizeChange, minSize = 0, maxSize = "100%", collapsible = false, step = 16, storageKey, "aria-label": ariaLabel = "Resize panes", className }) {
|
|
136
|
+
const paneId = useId();
|
|
137
|
+
const horizontal = orientation === "horizontal";
|
|
138
|
+
const [length, setLength] = useState(null);
|
|
139
|
+
const [internalSize, setInternalSize] = useState(null);
|
|
140
|
+
const [dragging, setDragging] = useState(false);
|
|
141
|
+
const rootRef = useRef(null);
|
|
142
|
+
const dragRef = useRef(null);
|
|
143
|
+
const lastOpenRef = useRef(null);
|
|
144
|
+
const limits = length === null ? null : resolveLimits(length, minSize, maxSize, collapsible);
|
|
145
|
+
const size = controlledSize ?? internalSize ?? (length === null || limits === null ? null : clampSize(resolveSize(defaultSize, length), limits));
|
|
146
|
+
const collapsed = size === 0;
|
|
147
|
+
const controlled = controlledSize !== void 0;
|
|
148
|
+
const attach = useCallback((element) => {
|
|
149
|
+
rootRef.current = element;
|
|
150
|
+
if (!element) return;
|
|
151
|
+
const measure = () => {
|
|
152
|
+
const rect = element.getBoundingClientRect();
|
|
153
|
+
setLength(horizontal ? rect.width : rect.height);
|
|
154
|
+
};
|
|
155
|
+
measure();
|
|
156
|
+
if (storageKey !== void 0 && !controlled) {
|
|
157
|
+
const stored = readStoredSize(storageKey);
|
|
158
|
+
if (stored !== null) setInternalSize(stored);
|
|
159
|
+
}
|
|
160
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
161
|
+
const observer = new ResizeObserver(measure);
|
|
162
|
+
observer.observe(element);
|
|
163
|
+
return () => observer.disconnect();
|
|
164
|
+
}, [
|
|
165
|
+
horizontal,
|
|
166
|
+
storageKey,
|
|
167
|
+
controlled
|
|
168
|
+
]);
|
|
169
|
+
/** The live length and limits, measured now (a drag or a key press must not use stale ones). */
|
|
170
|
+
const measureNow = () => {
|
|
171
|
+
const rect = rootRef.current?.getBoundingClientRect();
|
|
172
|
+
const now = rect ? horizontal ? rect.width : rect.height : length ?? 0;
|
|
173
|
+
return {
|
|
174
|
+
length: now,
|
|
175
|
+
limits: resolveLimits(now, minSize, maxSize, collapsible)
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
/** Take a new size: state, the caller, and (on `commit`) storage. `changed` defaults to a comparison with the rendered size. */
|
|
179
|
+
const apply = (next, commit, changed = next !== size) => {
|
|
180
|
+
if (next > 0) lastOpenRef.current = next;
|
|
181
|
+
if (!controlled) setInternalSize(next);
|
|
182
|
+
if (changed) onSizeChange?.(next);
|
|
183
|
+
if (commit && storageKey !== void 0 && !controlled) writeStoredSize(storageKey, next);
|
|
184
|
+
};
|
|
185
|
+
const coordinate = (event) => horizontal ? event.clientX : event.clientY;
|
|
186
|
+
const onPointerDown = (event) => {
|
|
187
|
+
if (event.button !== 0) return;
|
|
188
|
+
event.preventDefault();
|
|
189
|
+
const { length: now } = measureNow();
|
|
190
|
+
const current = size ?? 0;
|
|
191
|
+
dragRef.current = {
|
|
192
|
+
origin: coordinate(event),
|
|
193
|
+
start: current,
|
|
194
|
+
length: now,
|
|
195
|
+
reached: current
|
|
196
|
+
};
|
|
197
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
198
|
+
event.currentTarget.focus();
|
|
199
|
+
setDragging(true);
|
|
200
|
+
};
|
|
201
|
+
const onPointerMove = (event) => {
|
|
202
|
+
const drag = dragRef.current;
|
|
203
|
+
if (drag === null) return;
|
|
204
|
+
const delta = (coordinate(event) - drag.origin) * (sizedPane === "start" ? 1 : -1);
|
|
205
|
+
const next = clampSize(drag.start + delta, resolveLimits(drag.length, minSize, maxSize, collapsible));
|
|
206
|
+
if (next === drag.reached) return;
|
|
207
|
+
drag.reached = next;
|
|
208
|
+
apply(next, false, true);
|
|
209
|
+
};
|
|
210
|
+
const endDrag = (event) => {
|
|
211
|
+
const drag = dragRef.current;
|
|
212
|
+
if (drag === null) return;
|
|
213
|
+
dragRef.current = null;
|
|
214
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
|
|
215
|
+
setDragging(false);
|
|
216
|
+
if (drag.reached > 0) lastOpenRef.current = drag.reached;
|
|
217
|
+
if (storageKey !== void 0 && !controlled) writeStoredSize(storageKey, drag.reached);
|
|
218
|
+
};
|
|
219
|
+
const onKeyDown = (event) => {
|
|
220
|
+
const { limits: now } = measureNow();
|
|
221
|
+
const current = size ?? 0;
|
|
222
|
+
const action = splitKeyAction(event.key, event.shiftKey, current, {
|
|
223
|
+
orientation,
|
|
224
|
+
sizedPane,
|
|
225
|
+
step,
|
|
226
|
+
limits: now
|
|
227
|
+
});
|
|
228
|
+
if (action === null) return;
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
if (action.type === "resize") apply(action.size, true);
|
|
231
|
+
else if (current > 0) {
|
|
232
|
+
lastOpenRef.current = current;
|
|
233
|
+
apply(0, true);
|
|
234
|
+
} else apply(clampSize(lastOpenRef.current ?? now.min, {
|
|
235
|
+
...now,
|
|
236
|
+
collapsible: false
|
|
237
|
+
}), true);
|
|
238
|
+
};
|
|
239
|
+
const basis = size !== null ? `${size}px` : typeof defaultSize === "number" ? `${defaultSize}px` : defaultSize;
|
|
240
|
+
const cssSize = (value) => typeof value === "number" ? `${value}px` : value;
|
|
241
|
+
const sizedStyle = {
|
|
242
|
+
flex: `0 0 ${basis}`,
|
|
243
|
+
...collapsed ? {} : horizontal ? {
|
|
244
|
+
minWidth: cssSize(minSize),
|
|
245
|
+
maxWidth: cssSize(maxSize)
|
|
246
|
+
} : {
|
|
247
|
+
minHeight: cssSize(minSize),
|
|
248
|
+
maxHeight: cssSize(maxSize)
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
const [first, second] = children;
|
|
252
|
+
const paneClasses = "relative min-h-0 min-w-0 overflow-hidden";
|
|
253
|
+
const sized = /* @__PURE__ */ jsx("div", {
|
|
254
|
+
id: paneId,
|
|
255
|
+
style: sizedStyle,
|
|
256
|
+
className: paneClasses,
|
|
257
|
+
"data-collapsed": collapsed ? "" : void 0,
|
|
258
|
+
inert: collapsed,
|
|
259
|
+
children: sizedPane === "start" ? first : second
|
|
260
|
+
});
|
|
261
|
+
const rest = /* @__PURE__ */ jsx("div", {
|
|
262
|
+
className: cn(paneClasses, "flex-1 basis-0"),
|
|
263
|
+
children: sizedPane === "start" ? second : first
|
|
264
|
+
});
|
|
265
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
266
|
+
ref: attach,
|
|
267
|
+
"data-orientation": orientation,
|
|
268
|
+
"data-dragging": dragging ? "" : void 0,
|
|
269
|
+
"data-collapsed": collapsed ? "" : void 0,
|
|
270
|
+
className: cn("flex h-full min-h-0 w-full min-w-0", horizontal ? "flex-row" : "flex-col", dragging && (horizontal ? "cursor-col-resize select-none" : "cursor-row-resize select-none"), className),
|
|
271
|
+
children: [
|
|
272
|
+
sizedPane === "start" ? sized : rest,
|
|
273
|
+
/* @__PURE__ */ jsx("div", {
|
|
274
|
+
role: "separator",
|
|
275
|
+
tabIndex: 0,
|
|
276
|
+
"aria-label": ariaLabel,
|
|
277
|
+
"aria-orientation": horizontal ? "vertical" : "horizontal",
|
|
278
|
+
"aria-controls": paneId,
|
|
279
|
+
"aria-valuenow": size === null ? void 0 : Math.round(size),
|
|
280
|
+
"aria-valuemin": limits === null ? void 0 : collapsible ? 0 : Math.round(limits.min),
|
|
281
|
+
"aria-valuemax": limits === null ? void 0 : Math.round(limits.max),
|
|
282
|
+
"data-dragging": dragging ? "" : void 0,
|
|
283
|
+
onPointerDown,
|
|
284
|
+
onPointerMove,
|
|
285
|
+
onPointerUp: endDrag,
|
|
286
|
+
onPointerCancel: endDrag,
|
|
287
|
+
onKeyDown,
|
|
288
|
+
className: cn("relative z-10 shrink-0 touch-none bg-line transition-colors", "before:absolute before:content-['']", horizontal ? "w-px cursor-col-resize before:inset-y-0 before:-right-1 before:-left-1" : "h-px cursor-row-resize before:inset-x-0 before:-top-1 before:-bottom-1", "hover:bg-signal data-dragging:bg-signal", focusRing)
|
|
289
|
+
}),
|
|
290
|
+
sizedPane === "start" ? rest : sized
|
|
291
|
+
]
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/components/AppShell.tsx
|
|
296
|
+
const LEFT_DEFAULTS = {
|
|
297
|
+
defaultSize: 280,
|
|
298
|
+
minSize: 180,
|
|
299
|
+
maxSize: "50%"
|
|
300
|
+
};
|
|
301
|
+
const RIGHT_DEFAULTS = {
|
|
302
|
+
defaultSize: 320,
|
|
303
|
+
minSize: 220,
|
|
304
|
+
maxSize: "50%"
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* The full-viewport frame of a studio app: `header` across the top, `left` | `main` |
|
|
308
|
+
* `right` in the middle with resizable side panels, `bottom` across the foot. Every slot but
|
|
309
|
+
* `main` is optional, and an absent slot takes no space.
|
|
310
|
+
*
|
|
311
|
+
* Landmarks: the header is a `<header>`, the main surface a `<main>`, the side panels
|
|
312
|
+
* `<aside>`s named by `leftLabel` and `rightLabel`. Each side panel is divided from the main
|
|
313
|
+
* surface by a `SplitPane` divider (drag, or focus it and use the arrow keys). Only the
|
|
314
|
+
* insides of the panels scroll; the shell itself is fixed to the viewport (`h-dvh`).
|
|
315
|
+
*/
|
|
316
|
+
function AppShell({ header, left, main, right, bottom, leftLabel = "Navigator", rightLabel = "Inspector", leftSize, rightSize, storageKey, className }) {
|
|
317
|
+
const leftSizing = {
|
|
318
|
+
...LEFT_DEFAULTS,
|
|
319
|
+
...leftSize
|
|
320
|
+
};
|
|
321
|
+
const rightSizing = {
|
|
322
|
+
...RIGHT_DEFAULTS,
|
|
323
|
+
...rightSize
|
|
324
|
+
};
|
|
325
|
+
const hasLeft = left !== void 0 && left !== null && left !== false;
|
|
326
|
+
const hasRight = right !== void 0 && right !== null && right !== false;
|
|
327
|
+
const mainSurface = /* @__PURE__ */ jsx("main", {
|
|
328
|
+
className: "relative h-full min-h-0 min-w-0 overflow-hidden",
|
|
329
|
+
children: main
|
|
330
|
+
});
|
|
331
|
+
const withRight = hasRight ? /* @__PURE__ */ jsxs(SplitPane, {
|
|
332
|
+
sizedPane: "end",
|
|
333
|
+
...rightSizing,
|
|
334
|
+
storageKey: storageKey === void 0 ? void 0 : `${storageKey}:right`,
|
|
335
|
+
"aria-label": `Resize ${rightLabel.toLowerCase()}`,
|
|
336
|
+
children: [mainSurface, /* @__PURE__ */ jsx("aside", {
|
|
337
|
+
"aria-label": rightLabel,
|
|
338
|
+
className: "h-full overflow-auto bg-surface",
|
|
339
|
+
children: right
|
|
340
|
+
})]
|
|
341
|
+
}) : mainSurface;
|
|
342
|
+
const middle = hasLeft ? /* @__PURE__ */ jsxs(SplitPane, {
|
|
343
|
+
sizedPane: "start",
|
|
344
|
+
...leftSizing,
|
|
345
|
+
storageKey: storageKey === void 0 ? void 0 : `${storageKey}:left`,
|
|
346
|
+
"aria-label": `Resize ${leftLabel.toLowerCase()}`,
|
|
347
|
+
children: [/* @__PURE__ */ jsx("aside", {
|
|
348
|
+
"aria-label": leftLabel,
|
|
349
|
+
className: "h-full overflow-auto bg-surface",
|
|
350
|
+
children: left
|
|
351
|
+
}), withRight]
|
|
352
|
+
}) : withRight;
|
|
353
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
354
|
+
className: cn("grid h-dvh w-full grid-rows-[auto_minmax(0,1fr)_auto] overflow-hidden bg-ground text-fg", className),
|
|
355
|
+
children: [
|
|
356
|
+
header ? /* @__PURE__ */ jsx("header", {
|
|
357
|
+
className: "min-w-0 border-b border-line bg-surface",
|
|
358
|
+
children: header
|
|
359
|
+
}) : /* @__PURE__ */ jsx("div", { "aria-hidden": true }),
|
|
360
|
+
/* @__PURE__ */ jsx("div", {
|
|
361
|
+
className: "min-h-0 min-w-0",
|
|
362
|
+
children: middle
|
|
363
|
+
}),
|
|
364
|
+
bottom ? /* @__PURE__ */ jsx("div", {
|
|
365
|
+
className: "min-w-0 border-t border-line bg-surface",
|
|
366
|
+
children: bottom
|
|
367
|
+
}) : /* @__PURE__ */ jsx("div", { "aria-hidden": true })
|
|
368
|
+
]
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/components/treeModel.ts
|
|
373
|
+
/**
|
|
374
|
+
* The rows a reader can see: every root, and the children of every expanded parent, in
|
|
375
|
+
* document order.
|
|
376
|
+
*
|
|
377
|
+
* @param nodes - The roots.
|
|
378
|
+
* @param expanded - The ids of the expanded parents.
|
|
379
|
+
* @returns The visible rows, in order.
|
|
380
|
+
*/
|
|
381
|
+
function visibleRows(nodes, expanded) {
|
|
382
|
+
const rows = [];
|
|
383
|
+
const walk = (siblings, level, parentId) => {
|
|
384
|
+
siblings.forEach((node, index) => {
|
|
385
|
+
const isParent = node.children !== void 0;
|
|
386
|
+
const isExpanded = isParent && expanded.has(node.id);
|
|
387
|
+
rows.push({
|
|
388
|
+
node,
|
|
389
|
+
level,
|
|
390
|
+
parentId,
|
|
391
|
+
posInSet: index + 1,
|
|
392
|
+
setSize: siblings.length,
|
|
393
|
+
isParent,
|
|
394
|
+
isExpanded
|
|
395
|
+
});
|
|
396
|
+
if (isExpanded && node.children) walk(node.children, level + 1, node.id);
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
walk(nodes, 1, null);
|
|
400
|
+
return rows;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Every parent's id — the expanded set for "everything open".
|
|
404
|
+
*
|
|
405
|
+
* @param nodes - The roots.
|
|
406
|
+
* @returns The ids of every node with a children array, at any depth.
|
|
407
|
+
*/
|
|
408
|
+
function parentIds(nodes) {
|
|
409
|
+
const ids = [];
|
|
410
|
+
const walk = (siblings) => {
|
|
411
|
+
for (const node of siblings) {
|
|
412
|
+
if (node.children === void 0) continue;
|
|
413
|
+
ids.push(node.id);
|
|
414
|
+
walk(node.children);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
walk(nodes);
|
|
418
|
+
return ids;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* The ancestors of a node, root first — what must be expanded for it to be visible.
|
|
422
|
+
*
|
|
423
|
+
* @param nodes - The roots.
|
|
424
|
+
* @param id - The node.
|
|
425
|
+
* @returns The ancestor ids, root first; empty for a root or an id not in the tree.
|
|
426
|
+
*/
|
|
427
|
+
function ancestorIds(nodes, id) {
|
|
428
|
+
const walk = (siblings, path) => {
|
|
429
|
+
for (const node of siblings) {
|
|
430
|
+
if (node.id === id) return path;
|
|
431
|
+
if (node.children) {
|
|
432
|
+
const found = walk(node.children, [...path, node.id]);
|
|
433
|
+
if (found) return found;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return null;
|
|
437
|
+
};
|
|
438
|
+
return walk(nodes, []) ?? [];
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* The tree's keyboard (the WAI-ARIA tree pattern), as a pure function.
|
|
442
|
+
*
|
|
443
|
+
* ↓/↑ move to the next/previous visible row; → expands a closed parent, or moves into an
|
|
444
|
+
* open one's first child; ← collapses an open parent, or moves to the parent; Home/End go to
|
|
445
|
+
* the first/last visible row; Enter and Space select; `*` expands every sibling of the
|
|
446
|
+
* focused row; a printable character moves to the next row whose label starts with it
|
|
447
|
+
* (type-ahead, wrapping).
|
|
448
|
+
*
|
|
449
|
+
* @param rows - The visible rows, from `visibleRows`.
|
|
450
|
+
* @param focusedId - The row with focus.
|
|
451
|
+
* @param key - `KeyboardEvent.key`.
|
|
452
|
+
* @returns The action, or `null` when the key does nothing here.
|
|
453
|
+
*/
|
|
454
|
+
function treeKeyAction(rows, focusedId, key) {
|
|
455
|
+
const index = rows.findIndex((row) => row.node.id === focusedId);
|
|
456
|
+
const row = rows[index];
|
|
457
|
+
if (row === void 0) return null;
|
|
458
|
+
const focus = (target) => target === void 0 || target === row ? null : {
|
|
459
|
+
type: "focus",
|
|
460
|
+
id: target.node.id
|
|
461
|
+
};
|
|
462
|
+
switch (key) {
|
|
463
|
+
case "ArrowDown": return focus(rows[index + 1]);
|
|
464
|
+
case "ArrowUp": return focus(rows[index - 1]);
|
|
465
|
+
case "Home": return focus(rows[0]);
|
|
466
|
+
case "End": return focus(rows[rows.length - 1]);
|
|
467
|
+
case "ArrowRight":
|
|
468
|
+
if (!row.isParent) return null;
|
|
469
|
+
if (!row.isExpanded) return {
|
|
470
|
+
type: "expand",
|
|
471
|
+
id: row.node.id
|
|
472
|
+
};
|
|
473
|
+
return rows[index + 1]?.parentId === row.node.id ? focus(rows[index + 1]) : null;
|
|
474
|
+
case "ArrowLeft":
|
|
475
|
+
if (row.isExpanded) return {
|
|
476
|
+
type: "collapse",
|
|
477
|
+
id: row.node.id
|
|
478
|
+
};
|
|
479
|
+
return row.parentId === null ? null : focus(rows.find((candidate) => candidate.node.id === row.parentId));
|
|
480
|
+
case "Enter":
|
|
481
|
+
case " ": return row.node.disabled ? null : {
|
|
482
|
+
type: "select",
|
|
483
|
+
id: row.node.id
|
|
484
|
+
};
|
|
485
|
+
case "*": {
|
|
486
|
+
const ids = rows.filter((candidate) => candidate.parentId === row.parentId && candidate.isParent && !candidate.isExpanded).map((candidate) => candidate.node.id);
|
|
487
|
+
return ids.length === 0 ? null : {
|
|
488
|
+
type: "expand-siblings",
|
|
489
|
+
ids
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
default: return typeAhead(rows, index, key);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/** The next row after `from` (wrapping) whose label starts with `key`, for a single printable character. */
|
|
496
|
+
function typeAhead(rows, from, key) {
|
|
497
|
+
if (key.length !== 1 || key.trim() === "") return null;
|
|
498
|
+
const needle = key.toLocaleLowerCase();
|
|
499
|
+
for (let offset = 1; offset <= rows.length; offset += 1) {
|
|
500
|
+
const candidate = rows[(from + offset) % rows.length];
|
|
501
|
+
if (candidate?.node.label.toLocaleLowerCase().startsWith(needle)) return offset === rows.length ? null : {
|
|
502
|
+
type: "focus",
|
|
503
|
+
id: candidate.node.id
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/components/TreeView.tsx
|
|
510
|
+
/**
|
|
511
|
+
* A data-driven tree (the WAI-ARIA tree pattern) with single selection.
|
|
512
|
+
*
|
|
513
|
+
* Keyboard: the tree is one Tab stop (roving `tabindex`). ↓/↑ move between visible rows, →
|
|
514
|
+
* expands a parent or enters it, ← collapses a parent or goes up to its parent, Home/End jump
|
|
515
|
+
* to the ends, Enter/Space select, `*` expands the focused row's siblings, and a letter jumps
|
|
516
|
+
* to the next row starting with it. Clicking a row selects it; clicking its chevron toggles it.
|
|
517
|
+
*
|
|
518
|
+
* Selection is controlled (`selectedId`/`onSelect`). When `selectedId` changes to a node
|
|
519
|
+
* inside a collapsed parent, its ancestors are expanded (through `onExpandedChange` when
|
|
520
|
+
* `expanded` is controlled) and the row is scrolled into view — so a pick in a viewport can
|
|
521
|
+
* drive the tree.
|
|
522
|
+
*
|
|
523
|
+
* Each row is a `treeitem` with `aria-level`, `aria-posinset`, `aria-setsize`,
|
|
524
|
+
* `aria-selected` and, for parents, `aria-expanded`; also `data-selected` and, for parents,
|
|
525
|
+
* `data-state` (`open`/`closed`). Row height follows the density in force.
|
|
526
|
+
*/
|
|
527
|
+
function TreeView({ nodes, selectedId = null, onSelect, expanded: controlledExpanded, defaultExpanded, defaultExpandAll = false, onExpandedChange, "aria-label": ariaLabel, className }) {
|
|
528
|
+
const density = useDensity();
|
|
529
|
+
const [internalExpanded, setInternalExpanded] = useState(() => defaultExpandAll ? parentIds(nodes) : defaultExpanded ?? []);
|
|
530
|
+
const expandedIds = controlledExpanded ?? internalExpanded;
|
|
531
|
+
const expandedSet = new Set(expandedIds);
|
|
532
|
+
const rows = visibleRows(nodes, expandedSet);
|
|
533
|
+
const [focusedId, setFocusedId] = useState(null);
|
|
534
|
+
const rowElementsRef = useRef(/* @__PURE__ */ new Map());
|
|
535
|
+
const setExpanded = (next) => {
|
|
536
|
+
if (controlledExpanded === void 0) setInternalExpanded(next);
|
|
537
|
+
onExpandedChange?.([...next]);
|
|
538
|
+
};
|
|
539
|
+
const toggle = (id, open) => {
|
|
540
|
+
if (open === expandedSet.has(id)) return;
|
|
541
|
+
setExpanded(open ? [...expandedIds, id] : expandedIds.filter((other) => other !== id));
|
|
542
|
+
};
|
|
543
|
+
const latestRef = useRef({
|
|
544
|
+
nodes,
|
|
545
|
+
expandedIds,
|
|
546
|
+
setExpanded
|
|
547
|
+
});
|
|
548
|
+
useEffect(() => {
|
|
549
|
+
latestRef.current = {
|
|
550
|
+
nodes,
|
|
551
|
+
expandedIds,
|
|
552
|
+
setExpanded
|
|
553
|
+
};
|
|
554
|
+
});
|
|
555
|
+
useEffect(() => {
|
|
556
|
+
if (selectedId === null) return;
|
|
557
|
+
const { nodes: tree, expandedIds: open, setExpanded: update } = latestRef.current;
|
|
558
|
+
const missing = ancestorIds(tree, selectedId).filter((id) => !open.includes(id));
|
|
559
|
+
if (missing.length > 0) update([...open, ...missing]);
|
|
560
|
+
}, [selectedId]);
|
|
561
|
+
useEffect(() => {
|
|
562
|
+
if (selectedId === null) return;
|
|
563
|
+
const element = rowElementsRef.current.get(selectedId);
|
|
564
|
+
if (typeof element?.scrollIntoView === "function") element.scrollIntoView({ block: "nearest" });
|
|
565
|
+
}, [selectedId, rows.length]);
|
|
566
|
+
const isVisible = (id) => id !== null && rows.some((row) => row.node.id === id);
|
|
567
|
+
const tabStop = isVisible(focusedId) ? focusedId : isVisible(selectedId) ? selectedId : rows[0]?.node.id ?? null;
|
|
568
|
+
const focusRow = (id) => {
|
|
569
|
+
setFocusedId(id);
|
|
570
|
+
rowElementsRef.current.get(id)?.focus();
|
|
571
|
+
};
|
|
572
|
+
const select = (row) => {
|
|
573
|
+
if (!row.node.disabled) onSelect?.(row.node.id, row.node);
|
|
574
|
+
};
|
|
575
|
+
const onKeyDown = (event) => {
|
|
576
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
577
|
+
const id = event.target.dataset["treeId"];
|
|
578
|
+
if (id === void 0) return;
|
|
579
|
+
const action = treeKeyAction(rows, id, event.key);
|
|
580
|
+
if (action === null) return;
|
|
581
|
+
event.preventDefault();
|
|
582
|
+
switch (action.type) {
|
|
583
|
+
case "focus":
|
|
584
|
+
focusRow(action.id);
|
|
585
|
+
break;
|
|
586
|
+
case "expand":
|
|
587
|
+
toggle(action.id, true);
|
|
588
|
+
break;
|
|
589
|
+
case "collapse":
|
|
590
|
+
toggle(action.id, false);
|
|
591
|
+
break;
|
|
592
|
+
case "select": {
|
|
593
|
+
const row = rows.find((candidate) => candidate.node.id === action.id);
|
|
594
|
+
if (row) select(row);
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
case "expand-siblings": setExpanded([...expandedIds, ...action.ids]);
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
const onChevron = (event, row) => {
|
|
601
|
+
event.stopPropagation();
|
|
602
|
+
toggle(row.node.id, !row.isExpanded);
|
|
603
|
+
focusRow(row.node.id);
|
|
604
|
+
};
|
|
605
|
+
return /* @__PURE__ */ jsx("div", {
|
|
606
|
+
role: "tree",
|
|
607
|
+
"aria-label": ariaLabel,
|
|
608
|
+
onKeyDown,
|
|
609
|
+
className: cn("flex flex-col py-1 select-none", className),
|
|
610
|
+
children: rows.map((row) => {
|
|
611
|
+
const { node } = row;
|
|
612
|
+
const selected = node.id === selectedId;
|
|
613
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
614
|
+
ref: (element) => {
|
|
615
|
+
if (element) rowElementsRef.current.set(node.id, element);
|
|
616
|
+
else rowElementsRef.current.delete(node.id);
|
|
617
|
+
},
|
|
618
|
+
role: "treeitem",
|
|
619
|
+
"aria-level": row.level,
|
|
620
|
+
"aria-posinset": row.posInSet,
|
|
621
|
+
"aria-setsize": row.setSize,
|
|
622
|
+
"aria-selected": selected,
|
|
623
|
+
"aria-expanded": row.isParent ? row.isExpanded : void 0,
|
|
624
|
+
"aria-disabled": node.disabled || void 0,
|
|
625
|
+
tabIndex: node.id === tabStop ? 0 : -1,
|
|
626
|
+
"data-tree-id": node.id,
|
|
627
|
+
"data-selected": selected ? "" : void 0,
|
|
628
|
+
"data-state": row.isParent ? row.isExpanded ? "open" : "closed" : void 0,
|
|
629
|
+
onFocus: (event) => {
|
|
630
|
+
if (event.target === event.currentTarget) setFocusedId(node.id);
|
|
631
|
+
},
|
|
632
|
+
onClick: () => {
|
|
633
|
+
focusRow(node.id);
|
|
634
|
+
select(row);
|
|
635
|
+
},
|
|
636
|
+
style: { paddingLeft: `calc(${row.level - 1} * 0.875rem + 0.25rem)` },
|
|
637
|
+
className: cn("mx-1 flex shrink-0 cursor-default items-center gap-1.5 rounded-control pr-2 text-fg transition-colors", "outline-none focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-signal", byDensity(density, "h-7 text-sm", "h-6 text-xs"), selected ? "bg-signal/12" : "hover:bg-raised", node.disabled && "text-fg-subtle"),
|
|
638
|
+
children: [
|
|
639
|
+
row.isParent ? /* @__PURE__ */ jsx("span", {
|
|
640
|
+
"aria-hidden": true,
|
|
641
|
+
onClick: (event) => onChevron(event, row),
|
|
642
|
+
className: "grid size-4 shrink-0 place-items-center rounded-sm text-fg-subtle hover:text-fg",
|
|
643
|
+
children: /* @__PURE__ */ jsx(ChevronRight, { className: cn("size-3.5 transition-transform", row.isExpanded && "rotate-90") })
|
|
644
|
+
}) : /* @__PURE__ */ jsx("span", {
|
|
645
|
+
"aria-hidden": true,
|
|
646
|
+
className: "size-4 shrink-0"
|
|
647
|
+
}),
|
|
648
|
+
node.icon !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
649
|
+
"aria-hidden": true,
|
|
650
|
+
className: cn("grid shrink-0 place-items-center [&>svg]:size-3.5", selected ? "text-signal" : "text-fg-muted"),
|
|
651
|
+
children: node.icon
|
|
652
|
+
}),
|
|
653
|
+
/* @__PURE__ */ jsx("span", {
|
|
654
|
+
className: "min-w-0 flex-1 truncate",
|
|
655
|
+
children: node.label
|
|
656
|
+
}),
|
|
657
|
+
node.meta !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
658
|
+
className: cn("shrink-0 font-mono text-[11px]", selected ? "text-fg" : "text-fg-subtle"),
|
|
659
|
+
children: node.meta
|
|
660
|
+
})
|
|
661
|
+
]
|
|
662
|
+
}, node.id);
|
|
663
|
+
})
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
//#endregion
|
|
667
|
+
//#region src/components/playhead.ts
|
|
668
|
+
/**
|
|
669
|
+
* A playhead over `count` samples spaced `dt` seconds apart, starting at sample 0.
|
|
670
|
+
*
|
|
671
|
+
* Create one per app (not per render — keep it in a `useState` initialiser or module
|
|
672
|
+
* scope), hand it to `PlaybackBar` and to the render loop, and replace its timeline with
|
|
673
|
+
* `setTimeline` when a new scenario loads.
|
|
674
|
+
*
|
|
675
|
+
* @param count - The number of samples (a non-negative integer).
|
|
676
|
+
* @param dt - Seconds between samples (positive).
|
|
677
|
+
* @returns The store.
|
|
678
|
+
*/
|
|
679
|
+
function createPlayhead(count, dt) {
|
|
680
|
+
let index = 0;
|
|
681
|
+
let timeline = checkTimeline(count, dt);
|
|
682
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
683
|
+
const notify = () => {
|
|
684
|
+
for (const listener of [...listeners]) listener();
|
|
685
|
+
};
|
|
686
|
+
return {
|
|
687
|
+
get: () => index,
|
|
688
|
+
set: (k) => {
|
|
689
|
+
const next = clampIndex(k, timeline.count);
|
|
690
|
+
if (next === index) return;
|
|
691
|
+
index = next;
|
|
692
|
+
notify();
|
|
693
|
+
},
|
|
694
|
+
subscribe: (listener) => {
|
|
695
|
+
listeners.add(listener);
|
|
696
|
+
return () => {
|
|
697
|
+
listeners.delete(listener);
|
|
698
|
+
};
|
|
699
|
+
},
|
|
700
|
+
get count() {
|
|
701
|
+
return timeline.count;
|
|
702
|
+
},
|
|
703
|
+
get dt() {
|
|
704
|
+
return timeline.dt;
|
|
705
|
+
},
|
|
706
|
+
setTimeline: (nextCount, nextDt) => {
|
|
707
|
+
const next = checkTimeline(nextCount, nextDt);
|
|
708
|
+
const nextIndex = clampIndex(index, next.count);
|
|
709
|
+
if (next.count === timeline.count && next.dt === timeline.dt && nextIndex === index) return;
|
|
710
|
+
timeline = next;
|
|
711
|
+
index = nextIndex;
|
|
712
|
+
notify();
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
function checkTimeline(count, dt) {
|
|
717
|
+
if (!Number.isInteger(count) || count < 0) throw new RangeError(`playhead: count must be a non-negative integer, got ${count}`);
|
|
718
|
+
if (!(dt > 0) || !Number.isFinite(dt)) throw new RangeError(`playhead: dt must be a positive number of seconds, got ${dt}`);
|
|
719
|
+
return {
|
|
720
|
+
count,
|
|
721
|
+
dt
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* A sample index made legal: rounded, and clamped to `0 … count − 1`.
|
|
726
|
+
*
|
|
727
|
+
* @param k - Any number (`NaN` reads as 0).
|
|
728
|
+
* @param count - The number of samples.
|
|
729
|
+
* @returns The index; 0 when `count` is 0.
|
|
730
|
+
*/
|
|
731
|
+
function clampIndex(k, count) {
|
|
732
|
+
if (count <= 0 || Number.isNaN(k)) return 0;
|
|
733
|
+
return Math.min(count - 1, Math.max(0, Math.round(k)));
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* One step of real-time playback, in samples.
|
|
737
|
+
*
|
|
738
|
+
* Each sample is shown for `dt`: a looping timeline has period `count · dt` and wraps from
|
|
739
|
+
* the last sample to the first; a non-looping one stops on the last sample and reports
|
|
740
|
+
* `ended`. The position is fractional so that playback slower than one sample per frame
|
|
741
|
+
* still advances.
|
|
742
|
+
*
|
|
743
|
+
* @param position - The current position in samples (fractional).
|
|
744
|
+
* @param delta - How many samples to advance (elapsed seconds × speed ÷ dt); not negative.
|
|
745
|
+
* @param count - The number of samples.
|
|
746
|
+
* @param loop - Whether to wrap at the end.
|
|
747
|
+
* @returns The new position and whether playback ended.
|
|
748
|
+
*/
|
|
749
|
+
function advance(position, delta, count, loop) {
|
|
750
|
+
if (count <= 1) return {
|
|
751
|
+
position: 0,
|
|
752
|
+
ended: !loop
|
|
753
|
+
};
|
|
754
|
+
const next = position + Math.max(0, delta);
|
|
755
|
+
if (loop) return {
|
|
756
|
+
position: next % count,
|
|
757
|
+
ended: false
|
|
758
|
+
};
|
|
759
|
+
const last = count - 1;
|
|
760
|
+
return next >= last ? {
|
|
761
|
+
position: last,
|
|
762
|
+
ended: true
|
|
763
|
+
} : {
|
|
764
|
+
position: next,
|
|
765
|
+
ended: false
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* The default time readout: seconds with millisecond resolution, `"1.234 s"`.
|
|
770
|
+
*
|
|
771
|
+
* @param seconds - The time.
|
|
772
|
+
* @returns The formatted time.
|
|
773
|
+
*/
|
|
774
|
+
function formatSeconds(seconds) {
|
|
775
|
+
return `${seconds.toFixed(3)} s`;
|
|
776
|
+
}
|
|
777
|
+
//#endregion
|
|
778
|
+
//#region src/components/usePlayback.ts
|
|
779
|
+
/**
|
|
780
|
+
* The playhead's current sample index, re-rendering the calling component when it changes.
|
|
781
|
+
*
|
|
782
|
+
* Use it only where the index is *displayed* (the playback bar, a readout). A render loop
|
|
783
|
+
* reads `playhead.get()` in its own frame callback instead, and re-renders nothing.
|
|
784
|
+
*
|
|
785
|
+
* @param playhead - The store.
|
|
786
|
+
* @returns The current sample index.
|
|
787
|
+
*/
|
|
788
|
+
function usePlayhead(playhead) {
|
|
789
|
+
return useSyncExternalStore(playhead.subscribe, playhead.get, playhead.get);
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* The playhead's timeline — `count` and `dt` — re-rendering when `setTimeline` changes it.
|
|
793
|
+
*
|
|
794
|
+
* @param playhead - The store.
|
|
795
|
+
* @returns The number of samples and the seconds between them.
|
|
796
|
+
*/
|
|
797
|
+
function usePlayheadTimeline(playhead) {
|
|
798
|
+
return {
|
|
799
|
+
count: useSyncExternalStore(playhead.subscribe, () => playhead.count, () => playhead.count),
|
|
800
|
+
dt: useSyncExternalStore(playhead.subscribe, () => playhead.dt, () => playhead.dt)
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Advance a playhead in real time × `speed` while `playing`, from `requestAnimationFrame`.
|
|
805
|
+
*
|
|
806
|
+
* Position is accumulated fractionally, so slow playback of a finely sampled timeline still
|
|
807
|
+
* moves; the store only ever holds the integer index. A seek from elsewhere (the scrubber, a
|
|
808
|
+
* marker) is picked up on the next frame. Starting a non-looping timeline that sits on its
|
|
809
|
+
* last sample rewinds it to 0 first. `speed`, `loop` and `onEnd` may change while playing
|
|
810
|
+
* without restarting the clock.
|
|
811
|
+
*
|
|
812
|
+
* Renders nothing and re-renders nothing: call it once, in the component that owns the
|
|
813
|
+
* `playing` state.
|
|
814
|
+
*
|
|
815
|
+
* @param options - The playhead and the transport state.
|
|
816
|
+
*/
|
|
817
|
+
function usePlaybackClock({ playhead, playing, speed = 1, loop = false, onEnd, maxFrameMs = 100 }) {
|
|
818
|
+
const step = useEffectEvent((position, elapsedMs) => {
|
|
819
|
+
return advance(position, Math.min(elapsedMs, maxFrameMs) / 1e3 * (speed / playhead.dt), playhead.count, loop);
|
|
820
|
+
});
|
|
821
|
+
const end = useEffectEvent(() => onEnd?.());
|
|
822
|
+
const rewindIfAtEnd = useEffectEvent(() => {
|
|
823
|
+
if (!loop && playhead.count > 0 && playhead.get() >= playhead.count - 1) playhead.set(0);
|
|
824
|
+
});
|
|
825
|
+
useEffect(() => {
|
|
826
|
+
if (!playing) return;
|
|
827
|
+
rewindIfAtEnd();
|
|
828
|
+
let position = playhead.get();
|
|
829
|
+
let last = null;
|
|
830
|
+
let frame = requestAnimationFrame(function tick(now) {
|
|
831
|
+
if (Math.floor(position) !== playhead.get()) position = playhead.get();
|
|
832
|
+
const result = last === null ? {
|
|
833
|
+
position,
|
|
834
|
+
ended: false
|
|
835
|
+
} : step(position, now - last);
|
|
836
|
+
last = now;
|
|
837
|
+
position = result.position;
|
|
838
|
+
playhead.set(Math.floor(position));
|
|
839
|
+
if (result.ended) {
|
|
840
|
+
end();
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
frame = requestAnimationFrame(tick);
|
|
844
|
+
});
|
|
845
|
+
return () => cancelAnimationFrame(frame);
|
|
846
|
+
}, [playing, playhead]);
|
|
847
|
+
}
|
|
848
|
+
//#endregion
|
|
849
|
+
//#region src/components/PlaybackBar.tsx
|
|
850
|
+
/** The speeds offered by default. */
|
|
851
|
+
const DEFAULT_SPEEDS = [
|
|
852
|
+
.25,
|
|
853
|
+
.5,
|
|
854
|
+
1,
|
|
855
|
+
2,
|
|
856
|
+
4
|
|
857
|
+
];
|
|
858
|
+
/**
|
|
859
|
+
* The transport for a sampled timeline: jump to start, previous marker, step back,
|
|
860
|
+
* play/pause, step forward, next marker, jump to end; a scrubber over the samples with the
|
|
861
|
+
* markers as ticks; the time `t = k · dt`; the speed; loop.
|
|
862
|
+
*
|
|
863
|
+
* `playing`, `speed` and `loop` are controlled props. The position is the `playhead` store,
|
|
864
|
+
* which the bar subscribes to itself — so during playback only the bar re-renders. Drive the
|
|
865
|
+
* store with `usePlaybackClock`, and read `playhead.get()` in a render loop.
|
|
866
|
+
*
|
|
867
|
+
* A `role="toolbar"`. Keyboard, while focus is inside it: the scrubber is a slider (←/→ one
|
|
868
|
+
* sample, PageUp/PageDown ten, Home/End); on the buttons, ←/→ step one sample; Space
|
|
869
|
+
* toggles playback anywhere in the bar except on a button, where it presses the button. The
|
|
870
|
+
* root carries `data-playing` while playing and `data-empty` when the timeline has no samples.
|
|
871
|
+
*/
|
|
872
|
+
function PlaybackBar({ playhead, playing, onPlayingChange, speed = 1, onSpeedChange, speeds = DEFAULT_SPEEDS, loop = false, onLoopChange, markers = [], onMarkerSelect, formatTime = formatSeconds, "aria-label": ariaLabel = "Playback", className }) {
|
|
873
|
+
const density = useDensity();
|
|
874
|
+
const index = usePlayhead(playhead);
|
|
875
|
+
const { count, dt } = usePlayheadTimeline(playhead);
|
|
876
|
+
const last = Math.max(0, count - 1);
|
|
877
|
+
const empty = count === 0;
|
|
878
|
+
const sorted = [...markers].filter((m) => m.index >= 0 && m.index <= last).sort((a, b) => a.index - b.index);
|
|
879
|
+
const previousMarker = sorted.filter((m) => m.index < index).at(-1);
|
|
880
|
+
const nextMarker = sorted.find((m) => m.index > index);
|
|
881
|
+
const seek = (k) => playhead.set(k);
|
|
882
|
+
const onKeyDown = (event) => {
|
|
883
|
+
const target = event.target;
|
|
884
|
+
const onButton = target.tagName === "BUTTON";
|
|
885
|
+
if (event.key === " " && !onButton && target.getAttribute("role") !== "combobox") {
|
|
886
|
+
event.preventDefault();
|
|
887
|
+
if (!empty) onPlayingChange(!playing);
|
|
888
|
+
} else if (onButton && (event.key === "ArrowLeft" || event.key === "ArrowRight")) {
|
|
889
|
+
event.preventDefault();
|
|
890
|
+
seek(index + (event.key === "ArrowRight" ? 1 : -1));
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
894
|
+
role: "toolbar",
|
|
895
|
+
"aria-label": ariaLabel,
|
|
896
|
+
"data-playing": playing ? "" : void 0,
|
|
897
|
+
"data-empty": empty ? "" : void 0,
|
|
898
|
+
onKeyDown,
|
|
899
|
+
className: cn("flex w-full min-w-0 items-center bg-surface", byDensity(density, "gap-3 px-3 py-2", "gap-2 px-2 py-1"), className),
|
|
900
|
+
children: [
|
|
901
|
+
/* @__PURE__ */ jsxs("div", {
|
|
902
|
+
className: "flex shrink-0 items-center gap-0.5",
|
|
903
|
+
children: [
|
|
904
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
905
|
+
label: "Jump to start",
|
|
906
|
+
onClick: () => seek(0),
|
|
907
|
+
disabled: empty || index === 0,
|
|
908
|
+
children: /* @__PURE__ */ jsx(ChevronFirst, {
|
|
909
|
+
className: "size-4",
|
|
910
|
+
"aria-hidden": true
|
|
911
|
+
})
|
|
912
|
+
}),
|
|
913
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
914
|
+
label: "Previous marker",
|
|
915
|
+
onClick: () => previousMarker && seek(previousMarker.index),
|
|
916
|
+
disabled: previousMarker === void 0,
|
|
917
|
+
children: /* @__PURE__ */ jsx(SkipBack, {
|
|
918
|
+
className: "size-4",
|
|
919
|
+
"aria-hidden": true
|
|
920
|
+
})
|
|
921
|
+
}),
|
|
922
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
923
|
+
label: "Step back",
|
|
924
|
+
onClick: () => seek(index - 1),
|
|
925
|
+
disabled: empty || index === 0,
|
|
926
|
+
children: /* @__PURE__ */ jsx(StepBack, {
|
|
927
|
+
className: "size-4",
|
|
928
|
+
"aria-hidden": true
|
|
929
|
+
})
|
|
930
|
+
}),
|
|
931
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
932
|
+
label: playing ? "Pause" : "Play",
|
|
933
|
+
onClick: () => onPlayingChange(!playing),
|
|
934
|
+
disabled: empty,
|
|
935
|
+
className: "text-fg",
|
|
936
|
+
children: playing ? /* @__PURE__ */ jsx(Pause, {
|
|
937
|
+
className: "size-4",
|
|
938
|
+
"aria-hidden": true
|
|
939
|
+
}) : /* @__PURE__ */ jsx(Play, {
|
|
940
|
+
className: "size-4",
|
|
941
|
+
"aria-hidden": true
|
|
942
|
+
})
|
|
943
|
+
}),
|
|
944
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
945
|
+
label: "Step forward",
|
|
946
|
+
onClick: () => seek(index + 1),
|
|
947
|
+
disabled: empty || index >= last,
|
|
948
|
+
children: /* @__PURE__ */ jsx(StepForward, {
|
|
949
|
+
className: "size-4",
|
|
950
|
+
"aria-hidden": true
|
|
951
|
+
})
|
|
952
|
+
}),
|
|
953
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
954
|
+
label: "Next marker",
|
|
955
|
+
onClick: () => nextMarker && seek(nextMarker.index),
|
|
956
|
+
disabled: nextMarker === void 0,
|
|
957
|
+
children: /* @__PURE__ */ jsx(SkipForward, {
|
|
958
|
+
className: "size-4",
|
|
959
|
+
"aria-hidden": true
|
|
960
|
+
})
|
|
961
|
+
}),
|
|
962
|
+
/* @__PURE__ */ jsx(BarButton, {
|
|
963
|
+
label: "Jump to end",
|
|
964
|
+
onClick: () => seek(last),
|
|
965
|
+
disabled: empty || index >= last,
|
|
966
|
+
children: /* @__PURE__ */ jsx(ChevronLast, {
|
|
967
|
+
className: "size-4",
|
|
968
|
+
"aria-hidden": true
|
|
969
|
+
})
|
|
970
|
+
})
|
|
971
|
+
]
|
|
972
|
+
}),
|
|
973
|
+
/* @__PURE__ */ jsxs("div", {
|
|
974
|
+
className: "relative flex min-w-24 flex-1 flex-col justify-center",
|
|
975
|
+
children: [sorted.length > 0 && /* @__PURE__ */ jsx("div", {
|
|
976
|
+
className: "relative h-2.5",
|
|
977
|
+
children: sorted.map((marker) => /* @__PURE__ */ jsx(MarkerTick, {
|
|
978
|
+
marker,
|
|
979
|
+
fraction: last === 0 ? 0 : marker.index / last,
|
|
980
|
+
time: formatTime(marker.index * dt),
|
|
981
|
+
active: marker.index === index,
|
|
982
|
+
onClick: () => {
|
|
983
|
+
seek(marker.index);
|
|
984
|
+
onMarkerSelect?.(marker);
|
|
985
|
+
}
|
|
986
|
+
}, `${marker.index}:${marker.label ?? ""}`))
|
|
987
|
+
}), /* @__PURE__ */ jsx(Slider, {
|
|
988
|
+
value: index,
|
|
989
|
+
onValueChange: seek,
|
|
990
|
+
min: 0,
|
|
991
|
+
max: last,
|
|
992
|
+
step: 1,
|
|
993
|
+
disabled: empty,
|
|
994
|
+
"aria-label": "Playback position"
|
|
995
|
+
})]
|
|
996
|
+
}),
|
|
997
|
+
/* @__PURE__ */ jsxs("span", {
|
|
998
|
+
className: "shrink-0 font-mono text-xs whitespace-nowrap text-fg tabular-nums",
|
|
999
|
+
children: [
|
|
1000
|
+
/* @__PURE__ */ jsx("span", {
|
|
1001
|
+
className: "text-fg-subtle",
|
|
1002
|
+
children: "t = "
|
|
1003
|
+
}),
|
|
1004
|
+
formatTime(index * dt),
|
|
1005
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1006
|
+
className: "ml-2 text-fg-subtle",
|
|
1007
|
+
children: [
|
|
1008
|
+
empty ? "–" : index,
|
|
1009
|
+
"/",
|
|
1010
|
+
last
|
|
1011
|
+
]
|
|
1012
|
+
})
|
|
1013
|
+
]
|
|
1014
|
+
}),
|
|
1015
|
+
onSpeedChange && /* @__PURE__ */ jsx(Select, {
|
|
1016
|
+
value: String(speed),
|
|
1017
|
+
onValueChange: (value) => value !== "" && onSpeedChange(Number(value)),
|
|
1018
|
+
options: speeds.map((s) => ({
|
|
1019
|
+
value: String(s),
|
|
1020
|
+
label: `${s}×`
|
|
1021
|
+
})),
|
|
1022
|
+
"aria-label": "Playback speed",
|
|
1023
|
+
className: cn("w-20 shrink-0 font-mono", byDensity(density, "h-8", "h-7"))
|
|
1024
|
+
}),
|
|
1025
|
+
onLoopChange && /* @__PURE__ */ jsx(BarButton, {
|
|
1026
|
+
label: "Loop",
|
|
1027
|
+
pressed: loop,
|
|
1028
|
+
onClick: () => onLoopChange(!loop),
|
|
1029
|
+
children: /* @__PURE__ */ jsx(Repeat, {
|
|
1030
|
+
className: "size-4",
|
|
1031
|
+
"aria-hidden": true
|
|
1032
|
+
})
|
|
1033
|
+
})
|
|
1034
|
+
]
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
function MarkerTick({ marker, fraction, time, active, onClick }) {
|
|
1038
|
+
const name = marker.label ? `${marker.label} (${time})` : `Marker at ${time}`;
|
|
1039
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1040
|
+
type: "button",
|
|
1041
|
+
tabIndex: -1,
|
|
1042
|
+
title: name,
|
|
1043
|
+
"aria-label": name,
|
|
1044
|
+
"data-active": active ? "" : void 0,
|
|
1045
|
+
onClick,
|
|
1046
|
+
style: { left: `calc(7px + ${fraction} * (100% - 14px))` },
|
|
1047
|
+
className: "group absolute top-0 flex h-full w-2.5 -translate-x-1/2 cursor-pointer justify-center",
|
|
1048
|
+
children: /* @__PURE__ */ jsx("span", {
|
|
1049
|
+
"aria-hidden": true,
|
|
1050
|
+
className: "h-full w-0.5 rounded-full opacity-70 transition-opacity group-hover:opacity-100 group-data-active:w-1 group-data-active:opacity-100",
|
|
1051
|
+
style: { backgroundColor: toneColor(marker.tone) }
|
|
1052
|
+
})
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
/** An icon button in the style of `@vitavision/stage2d`'s `StageButton`, sized by density. */
|
|
1056
|
+
function BarButton({ label, pressed, className, children, ...props }) {
|
|
1057
|
+
const density = useDensity();
|
|
1058
|
+
return /* @__PURE__ */ jsx("button", {
|
|
1059
|
+
...props,
|
|
1060
|
+
type: "button",
|
|
1061
|
+
title: label,
|
|
1062
|
+
"aria-label": label,
|
|
1063
|
+
"aria-pressed": pressed,
|
|
1064
|
+
"data-state": pressed === void 0 ? void 0 : pressed ? "on" : "off",
|
|
1065
|
+
className: cn("grid place-items-center rounded-control text-fg-muted transition-colors", byDensity(density, "size-8", "size-7"), "hover:bg-raised hover:text-fg disabled:pointer-events-none disabled:opacity-40", pressed && "bg-signal/15 text-signal", focusRing, className),
|
|
1066
|
+
children
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
//#endregion
|
|
1070
|
+
//#region src/components/dropFiles.ts
|
|
1071
|
+
/**
|
|
1072
|
+
* Whether a file matches an `accept` string, as `<input type="file" accept>` reads it:
|
|
1073
|
+
* comma-separated extensions (`.json`), MIME types (`application/json`) and MIME wildcards
|
|
1074
|
+
* (`image/*`). Matching is case-insensitive.
|
|
1075
|
+
*
|
|
1076
|
+
* @param file - The file's name and MIME type.
|
|
1077
|
+
* @param accept - The accept string; empty or absent accepts everything.
|
|
1078
|
+
* @returns Whether the file is accepted.
|
|
1079
|
+
*/
|
|
1080
|
+
function acceptsFile(file, accept) {
|
|
1081
|
+
const tokens = (accept ?? "").split(",").map((token) => token.trim().toLowerCase()).filter((token) => token !== "");
|
|
1082
|
+
if (tokens.length === 0) return true;
|
|
1083
|
+
const name = file.name.toLowerCase();
|
|
1084
|
+
const type = file.type.toLowerCase();
|
|
1085
|
+
return tokens.some((token) => {
|
|
1086
|
+
if (token.startsWith(".")) return name.endsWith(token);
|
|
1087
|
+
if (token.endsWith("/*")) return type.startsWith(token.slice(0, -1));
|
|
1088
|
+
return type === token;
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* Split files by `accept`.
|
|
1093
|
+
*
|
|
1094
|
+
* @param files - The candidates.
|
|
1095
|
+
* @param accept - The accept string.
|
|
1096
|
+
* @returns The accepted files and the rejected ones, each in their original order.
|
|
1097
|
+
*/
|
|
1098
|
+
function partitionFiles(files, accept) {
|
|
1099
|
+
const accepted = [];
|
|
1100
|
+
const rejected = [];
|
|
1101
|
+
for (const file of files) (acceptsFile(file, accept) ? accepted : rejected).push(file);
|
|
1102
|
+
return {
|
|
1103
|
+
accepted,
|
|
1104
|
+
rejected
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Whether a drag carries files (rather than text or a link) — the only drags `FileDrop`
|
|
1109
|
+
* reacts to.
|
|
1110
|
+
*
|
|
1111
|
+
* @param dataTransfer - The drag's data, or `null`.
|
|
1112
|
+
* @returns Whether `Files` is among its types.
|
|
1113
|
+
*/
|
|
1114
|
+
function carriesFiles(dataTransfer) {
|
|
1115
|
+
return dataTransfer !== null && Array.from(dataTransfer.types).includes("Files");
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Every file in a drop, with dropped folders walked recursively.
|
|
1119
|
+
*
|
|
1120
|
+
* Files found inside a folder carry `webkitRelativePath` (`scene/meshes/base.glb`), as the
|
|
1121
|
+
* files of `<input webkitdirectory>` do, so both routes give the same shape. Where the entries
|
|
1122
|
+
* API is missing, the drop's plain file list is returned.
|
|
1123
|
+
*
|
|
1124
|
+
* @param dataTransfer - The drop's data.
|
|
1125
|
+
* @returns The files, in the order the browser listed them.
|
|
1126
|
+
*/
|
|
1127
|
+
async function collectDroppedFiles(dataTransfer) {
|
|
1128
|
+
const plain = Array.from(dataTransfer.files);
|
|
1129
|
+
const entries = Array.from(dataTransfer.items ?? []).filter((item) => item.kind === "file").map((item) => typeof item.webkitGetAsEntry === "function" ? item.webkitGetAsEntry() : null);
|
|
1130
|
+
if (!entries.some((entry) => entry?.isDirectory)) return plain;
|
|
1131
|
+
const files = [];
|
|
1132
|
+
for (const entry of entries) if (entry) await walkEntry(entry, files);
|
|
1133
|
+
return files;
|
|
1134
|
+
}
|
|
1135
|
+
async function walkEntry(entry, out) {
|
|
1136
|
+
if (entry.isFile && entry.file) {
|
|
1137
|
+
const read = entry.file.bind(entry);
|
|
1138
|
+
const file = await new Promise((resolve, reject) => read(resolve, reject));
|
|
1139
|
+
const path = entry.fullPath.replace(/^\//, "");
|
|
1140
|
+
out.push(path.includes("/") ? withRelativePath(file, path) : file);
|
|
1141
|
+
} else if (entry.isDirectory && entry.createReader) {
|
|
1142
|
+
const reader = entry.createReader();
|
|
1143
|
+
for (;;) {
|
|
1144
|
+
const batch = await new Promise((resolve, reject) => reader.readEntries(resolve, reject));
|
|
1145
|
+
if (batch.length === 0) break;
|
|
1146
|
+
for (const child of batch) await walkEntry(child, out);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* The file, carrying `webkitRelativePath` — a read-only accessor on `File.prototype`,
|
|
1152
|
+
* shadowed here on the instance.
|
|
1153
|
+
*/
|
|
1154
|
+
function withRelativePath(file, path) {
|
|
1155
|
+
Object.defineProperty(file, "webkitRelativePath", {
|
|
1156
|
+
value: path,
|
|
1157
|
+
configurable: true
|
|
1158
|
+
});
|
|
1159
|
+
return file;
|
|
1160
|
+
}
|
|
1161
|
+
//#endregion
|
|
1162
|
+
//#region src/components/FileDrop.tsx
|
|
1163
|
+
/**
|
|
1164
|
+
* A drop target plus an "Open files…" button over a hidden `<input type="file">`.
|
|
1165
|
+
*
|
|
1166
|
+
* Inline (the default) it is a dashed zone with a message and the button, carrying
|
|
1167
|
+
* `data-dragging` while files are dragged over it. With `overlay` it renders only the button
|
|
1168
|
+
* and takes drops anywhere in the window, showing a full-window overlay (`aria-hidden`: the
|
|
1169
|
+
* button is the accessible route) while a drag carrying files is over the window. Only drags
|
|
1170
|
+
* that carry files are reacted to; text and links pass through.
|
|
1171
|
+
*
|
|
1172
|
+
* Files are filtered by `accept` on both routes (the browser applies it to neither drops nor,
|
|
1173
|
+
* reliably, the picker); the rest go to `onReject`. A dropped folder is walked recursively.
|
|
1174
|
+
*/
|
|
1175
|
+
function FileDrop({ onFiles, onReject, accept, multiple = true, directory = false, overlay = false, disabled = false, buttonLabel, overlayMessage = "Drop to open", children = "Drop files here, or", className }) {
|
|
1176
|
+
const inputRef = useRef(null);
|
|
1177
|
+
const depthRef = useRef(0);
|
|
1178
|
+
const [dragging, setDragging] = useState(false);
|
|
1179
|
+
const label = buttonLabel ?? (directory ? "Open folder…" : "Open files…");
|
|
1180
|
+
const deliver = (files) => {
|
|
1181
|
+
const { accepted, rejected } = partitionFiles(files, accept);
|
|
1182
|
+
if (accepted.length > 0) onFiles(multiple ? accepted : accepted.slice(0, 1));
|
|
1183
|
+
if (rejected.length > 0) onReject?.(rejected);
|
|
1184
|
+
};
|
|
1185
|
+
const deliverRef = useRef(deliver);
|
|
1186
|
+
useEffect(() => {
|
|
1187
|
+
deliverRef.current = deliver;
|
|
1188
|
+
});
|
|
1189
|
+
useEffect(() => {
|
|
1190
|
+
if (!overlay || disabled) return;
|
|
1191
|
+
const onEnter = (event) => {
|
|
1192
|
+
if (!carriesFiles(event.dataTransfer)) return;
|
|
1193
|
+
depthRef.current += 1;
|
|
1194
|
+
setDragging(true);
|
|
1195
|
+
};
|
|
1196
|
+
const onLeave = (event) => {
|
|
1197
|
+
if (!carriesFiles(event.dataTransfer)) return;
|
|
1198
|
+
depthRef.current = Math.max(0, depthRef.current - 1);
|
|
1199
|
+
if (depthRef.current === 0) setDragging(false);
|
|
1200
|
+
};
|
|
1201
|
+
const onOver = (event) => {
|
|
1202
|
+
if (!carriesFiles(event.dataTransfer)) return;
|
|
1203
|
+
event.preventDefault();
|
|
1204
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = "copy";
|
|
1205
|
+
};
|
|
1206
|
+
const onDrop = (event) => {
|
|
1207
|
+
if (!event.dataTransfer || !carriesFiles(event.dataTransfer)) return;
|
|
1208
|
+
event.preventDefault();
|
|
1209
|
+
depthRef.current = 0;
|
|
1210
|
+
setDragging(false);
|
|
1211
|
+
collectDroppedFiles(event.dataTransfer).then((files) => deliverRef.current(files));
|
|
1212
|
+
};
|
|
1213
|
+
window.addEventListener("dragenter", onEnter);
|
|
1214
|
+
window.addEventListener("dragleave", onLeave);
|
|
1215
|
+
window.addEventListener("dragover", onOver);
|
|
1216
|
+
window.addEventListener("drop", onDrop);
|
|
1217
|
+
return () => {
|
|
1218
|
+
window.removeEventListener("dragenter", onEnter);
|
|
1219
|
+
window.removeEventListener("dragleave", onLeave);
|
|
1220
|
+
window.removeEventListener("dragover", onOver);
|
|
1221
|
+
window.removeEventListener("drop", onDrop);
|
|
1222
|
+
depthRef.current = 0;
|
|
1223
|
+
};
|
|
1224
|
+
}, [overlay, disabled]);
|
|
1225
|
+
const onPick = (event) => {
|
|
1226
|
+
const files = Array.from(event.currentTarget.files ?? []);
|
|
1227
|
+
event.currentTarget.value = "";
|
|
1228
|
+
if (files.length > 0) deliver(files);
|
|
1229
|
+
};
|
|
1230
|
+
const picker = /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("input", {
|
|
1231
|
+
ref: (element) => {
|
|
1232
|
+
inputRef.current = element;
|
|
1233
|
+
if (element) element.webkitdirectory = directory;
|
|
1234
|
+
},
|
|
1235
|
+
type: "file",
|
|
1236
|
+
hidden: true,
|
|
1237
|
+
tabIndex: -1,
|
|
1238
|
+
accept,
|
|
1239
|
+
multiple: multiple || directory,
|
|
1240
|
+
disabled,
|
|
1241
|
+
onChange: onPick
|
|
1242
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
1243
|
+
icon: /* @__PURE__ */ jsx(FolderOpen, {}),
|
|
1244
|
+
disabled,
|
|
1245
|
+
onClick: () => inputRef.current?.click(),
|
|
1246
|
+
className: overlay ? className : void 0,
|
|
1247
|
+
children: label
|
|
1248
|
+
})] });
|
|
1249
|
+
if (overlay) return /* @__PURE__ */ jsxs(Fragment, { children: [picker, dragging && /* @__PURE__ */ jsx("div", {
|
|
1250
|
+
"aria-hidden": true,
|
|
1251
|
+
"data-dragging": "",
|
|
1252
|
+
className: "pointer-events-none fixed inset-0 z-50 grid place-items-center bg-ground/80 p-8 backdrop-blur-sm",
|
|
1253
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
1254
|
+
className: "rounded-panel border-2 border-dashed border-signal bg-surface px-10 py-8 text-sm font-medium text-fg shadow-lg",
|
|
1255
|
+
children: overlayMessage
|
|
1256
|
+
})
|
|
1257
|
+
})] });
|
|
1258
|
+
const zoneDrag = (event, change) => {
|
|
1259
|
+
if (disabled || !carriesFiles(event.dataTransfer)) return;
|
|
1260
|
+
event.preventDefault();
|
|
1261
|
+
if (change === 0) {
|
|
1262
|
+
event.dataTransfer.dropEffect = "copy";
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
depthRef.current = Math.max(0, depthRef.current + change);
|
|
1266
|
+
setDragging(depthRef.current > 0);
|
|
1267
|
+
};
|
|
1268
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1269
|
+
"data-dragging": dragging ? "" : void 0,
|
|
1270
|
+
"data-disabled": disabled ? "" : void 0,
|
|
1271
|
+
onDragEnter: (event) => zoneDrag(event, 1),
|
|
1272
|
+
onDragLeave: (event) => zoneDrag(event, -1),
|
|
1273
|
+
onDragOver: (event) => zoneDrag(event, 0),
|
|
1274
|
+
onDrop: (event) => {
|
|
1275
|
+
if (disabled || !carriesFiles(event.dataTransfer)) return;
|
|
1276
|
+
event.preventDefault();
|
|
1277
|
+
depthRef.current = 0;
|
|
1278
|
+
setDragging(false);
|
|
1279
|
+
collectDroppedFiles(event.dataTransfer).then(deliver);
|
|
1280
|
+
},
|
|
1281
|
+
className: cn("flex flex-col items-center justify-center gap-3 rounded-panel border-2 border-dashed border-line-strong px-6 py-8 text-center text-sm text-fg-muted transition-colors", "data-dragging:border-signal data-dragging:bg-signal/8 data-dragging:text-fg", "data-disabled:border-line", className),
|
|
1282
|
+
children: [/* @__PURE__ */ jsx("p", { children }), picker]
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
//#endregion
|
|
1286
|
+
//#region src/components/toastStore.ts
|
|
1287
|
+
const DEFAULT_DURATION = 5e3;
|
|
1288
|
+
const ERROR_DURATION = 8e3;
|
|
1289
|
+
/**
|
|
1290
|
+
* A new, empty toast store. The package keeps one (behind `toast()` and the default
|
|
1291
|
+
* `<Toaster />`); create another only to isolate a region or a test.
|
|
1292
|
+
*
|
|
1293
|
+
* @returns The store.
|
|
1294
|
+
*/
|
|
1295
|
+
function createToastStore() {
|
|
1296
|
+
let toasts = [];
|
|
1297
|
+
let sequence = 0;
|
|
1298
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1299
|
+
const commit = (next) => {
|
|
1300
|
+
toasts = next;
|
|
1301
|
+
for (const listener of [...listeners]) listener();
|
|
1302
|
+
};
|
|
1303
|
+
return {
|
|
1304
|
+
toast: (options) => {
|
|
1305
|
+
const tone = options.tone ?? "info";
|
|
1306
|
+
sequence += 1;
|
|
1307
|
+
const record = {
|
|
1308
|
+
id: options.id ?? `toast-${sequence}`,
|
|
1309
|
+
title: options.title,
|
|
1310
|
+
description: options.description,
|
|
1311
|
+
tone,
|
|
1312
|
+
duration: options.duration ?? (tone === "error" ? ERROR_DURATION : DEFAULT_DURATION)
|
|
1313
|
+
};
|
|
1314
|
+
const existing = toasts.findIndex((t) => t.id === record.id);
|
|
1315
|
+
commit(existing === -1 ? [...toasts, record] : toasts.map((t, i) => i === existing ? record : t));
|
|
1316
|
+
return record.id;
|
|
1317
|
+
},
|
|
1318
|
+
dismiss: (id) => {
|
|
1319
|
+
if (id === void 0) {
|
|
1320
|
+
if (toasts.length > 0) commit([]);
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
if (toasts.some((t) => t.id === id)) commit(toasts.filter((t) => t.id !== id));
|
|
1324
|
+
},
|
|
1325
|
+
getSnapshot: () => toasts,
|
|
1326
|
+
subscribe: (listener) => {
|
|
1327
|
+
listeners.add(listener);
|
|
1328
|
+
return () => {
|
|
1329
|
+
listeners.delete(listener);
|
|
1330
|
+
};
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
/** The store behind `toast()` and a `<Toaster />` given no `store`. */
|
|
1335
|
+
const defaultToastStore = createToastStore();
|
|
1336
|
+
/**
|
|
1337
|
+
* Raise a notification, from anywhere — shown by the mounted `<Toaster />`.
|
|
1338
|
+
*
|
|
1339
|
+
* `toast({ title: "Scenario loaded", tone: "success" })`. Pass `id` to update a toast in
|
|
1340
|
+
* place (a progress message that becomes a result). `toast.dismiss(id)` removes one early,
|
|
1341
|
+
* `toast.dismiss()` all of them.
|
|
1342
|
+
*
|
|
1343
|
+
* @param options - The message, its tone and its duration.
|
|
1344
|
+
* @returns The toast's id.
|
|
1345
|
+
*/
|
|
1346
|
+
const toast = Object.assign((options) => defaultToastStore.toast(options), { dismiss: (id) => defaultToastStore.dismiss(id) });
|
|
1347
|
+
//#endregion
|
|
1348
|
+
//#region src/components/Toaster.tsx
|
|
1349
|
+
const TONE = {
|
|
1350
|
+
info: {
|
|
1351
|
+
border: "border-signal/30",
|
|
1352
|
+
icon: "text-signal",
|
|
1353
|
+
Icon: Info
|
|
1354
|
+
},
|
|
1355
|
+
success: {
|
|
1356
|
+
border: "border-normal/30",
|
|
1357
|
+
icon: "text-normal",
|
|
1358
|
+
Icon: CircleCheck
|
|
1359
|
+
},
|
|
1360
|
+
warn: {
|
|
1361
|
+
border: "border-warn/30",
|
|
1362
|
+
icon: "text-warn",
|
|
1363
|
+
Icon: TriangleAlert
|
|
1364
|
+
},
|
|
1365
|
+
error: {
|
|
1366
|
+
border: "border-defect/30",
|
|
1367
|
+
icon: "text-defect",
|
|
1368
|
+
Icon: CircleX
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
const NO_TOASTS = [];
|
|
1372
|
+
/**
|
|
1373
|
+
* The stack of notifications raised with `toast()`. Mount it once, near the root.
|
|
1374
|
+
*
|
|
1375
|
+
* Fixed to the bottom-right corner, newest at the bottom. Each toast dismisses itself after
|
|
1376
|
+
* its `duration`; the countdown pauses while the pointer is over the stack or focus is inside
|
|
1377
|
+
* it. A `section` landmark ("Notifications") holding a polite live region, so new toasts are
|
|
1378
|
+
* announced; an `error` toast is a `role="alert"`, announced at once. Each toast carries
|
|
1379
|
+
* `data-tone` and a "Dismiss" button.
|
|
1380
|
+
*/
|
|
1381
|
+
function Toaster({ store = defaultToastStore, limit = 5, className }) {
|
|
1382
|
+
const toasts = useSyncExternalStore(store.subscribe, store.getSnapshot, () => NO_TOASTS);
|
|
1383
|
+
const [hovered, setHovered] = useState(false);
|
|
1384
|
+
const [focused, setFocused] = useState(false);
|
|
1385
|
+
const shown = toasts.slice(-Math.max(1, limit));
|
|
1386
|
+
const onBlur = (event) => {
|
|
1387
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false);
|
|
1388
|
+
};
|
|
1389
|
+
return /* @__PURE__ */ jsx("section", {
|
|
1390
|
+
"aria-label": "Notifications",
|
|
1391
|
+
onPointerEnter: () => setHovered(true),
|
|
1392
|
+
onPointerLeave: () => setHovered(false),
|
|
1393
|
+
onFocus: () => setFocused(true),
|
|
1394
|
+
onBlur,
|
|
1395
|
+
className: cn("pointer-events-none fixed right-4 bottom-4 z-50 w-80 max-w-[calc(100vw-2rem)]", className),
|
|
1396
|
+
children: /* @__PURE__ */ jsx("ol", {
|
|
1397
|
+
"aria-live": "polite",
|
|
1398
|
+
className: "flex flex-col gap-2",
|
|
1399
|
+
children: shown.map((record) => /* @__PURE__ */ jsx(ToastItem, {
|
|
1400
|
+
record,
|
|
1401
|
+
paused: hovered || focused,
|
|
1402
|
+
onDismiss: () => store.dismiss(record.id)
|
|
1403
|
+
}, record.id))
|
|
1404
|
+
})
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
function ToastItem({ record, paused, onDismiss }) {
|
|
1408
|
+
const { border, icon, Icon } = TONE[record.tone];
|
|
1409
|
+
const remainingRef = useRef({
|
|
1410
|
+
record,
|
|
1411
|
+
ms: record.duration
|
|
1412
|
+
});
|
|
1413
|
+
const dismissRef = useRef(onDismiss);
|
|
1414
|
+
useEffect(() => {
|
|
1415
|
+
dismissRef.current = onDismiss;
|
|
1416
|
+
});
|
|
1417
|
+
useEffect(() => {
|
|
1418
|
+
if (remainingRef.current.record !== record) remainingRef.current = {
|
|
1419
|
+
record,
|
|
1420
|
+
ms: record.duration
|
|
1421
|
+
};
|
|
1422
|
+
if (paused || !Number.isFinite(remainingRef.current.ms)) return;
|
|
1423
|
+
const started = Date.now();
|
|
1424
|
+
const timer = setTimeout(() => dismissRef.current(), remainingRef.current.ms);
|
|
1425
|
+
return () => {
|
|
1426
|
+
clearTimeout(timer);
|
|
1427
|
+
remainingRef.current.ms = Math.max(0, remainingRef.current.ms - (Date.now() - started));
|
|
1428
|
+
};
|
|
1429
|
+
}, [paused, record]);
|
|
1430
|
+
return /* @__PURE__ */ jsx("li", {
|
|
1431
|
+
className: "pointer-events-auto",
|
|
1432
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
1433
|
+
role: record.tone === "error" ? "alert" : void 0,
|
|
1434
|
+
"data-tone": record.tone,
|
|
1435
|
+
className: cn("flex items-start gap-2.5 rounded-panel border bg-overlay px-3 py-2.5 text-sm leading-snug shadow-lg shadow-black/25", "transition-[opacity,translate] duration-200 starting:translate-y-2 starting:opacity-0", border),
|
|
1436
|
+
children: [
|
|
1437
|
+
/* @__PURE__ */ jsx(Icon, {
|
|
1438
|
+
className: cn("mt-0.5 size-4 shrink-0", icon),
|
|
1439
|
+
"aria-hidden": true
|
|
1440
|
+
}),
|
|
1441
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1442
|
+
className: "flex min-w-0 flex-1 flex-col gap-0.5",
|
|
1443
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
1444
|
+
className: "font-medium text-fg",
|
|
1445
|
+
children: record.title
|
|
1446
|
+
}), record.description && /* @__PURE__ */ jsx("p", {
|
|
1447
|
+
className: "text-fg-muted",
|
|
1448
|
+
children: record.description
|
|
1449
|
+
})]
|
|
1450
|
+
}),
|
|
1451
|
+
/* @__PURE__ */ jsx("button", {
|
|
1452
|
+
type: "button",
|
|
1453
|
+
"aria-label": "Dismiss",
|
|
1454
|
+
title: "Dismiss",
|
|
1455
|
+
onClick: onDismiss,
|
|
1456
|
+
className: cn("-mr-1 grid size-6 shrink-0 place-items-center rounded-control text-fg-subtle transition-colors hover:bg-raised hover:text-fg", focusRing),
|
|
1457
|
+
children: /* @__PURE__ */ jsx(X, {
|
|
1458
|
+
className: "size-3.5",
|
|
1459
|
+
"aria-hidden": true
|
|
1460
|
+
})
|
|
1461
|
+
})
|
|
1462
|
+
]
|
|
1463
|
+
})
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
//#endregion
|
|
1467
|
+
export { AppShell, DEFAULT_SPEEDS, FileDrop, PlaybackBar, SplitPane, Toaster, TreeView, acceptsFile, advance, ancestorIds, clampIndex, clampSize, collectDroppedFiles, createPlayhead, createToastStore, defaultToastStore, formatSeconds, parentIds, resolveLimits, resolveSize, toast, treeKeyAction, usePlaybackClock, usePlayhead, usePlayheadTimeline, visibleRows };
|
|
1468
|
+
|
|
1469
|
+
//# sourceMappingURL=index.js.map
|