@vitavision/workbench 0.0.0-bootstrap.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +89 -0
- package/dist/index.d.ts +580 -0
- package/dist/index.js +1288 -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/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/treeModel.ts +188 -0
- package/src/components/usePlayback.ts +105 -0
- package/src/index.ts +70 -0
- package/src/styles.css +7 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Where playback is, as a store outside React.
|
|
3
|
+
*
|
|
4
|
+
* A sampled timeline plays at display rate, and the things that follow it — a robot's joint
|
|
5
|
+
* angles, a camera frustum, a highlighted capture — are drawn by a render loop, not by
|
|
6
|
+
* React. If the current frame lived in React state, every frame of playback would re-render
|
|
7
|
+
* every component that reads it, including the whole scene graph. So it lives here: a tiny
|
|
8
|
+
* store a render loop reads with `get()` at no cost, and that React components subscribe to
|
|
9
|
+
* (through `usePlayhead`) only where they display it — the playback bar, a time readout.
|
|
10
|
+
*
|
|
11
|
+
* The store holds an integer sample index and nothing else that changes per frame. The
|
|
12
|
+
* sub-sample position real-time playback needs is the clock's business (`advance`,
|
|
13
|
+
* `usePlaybackClock`), not the store's, so a reader never sees a fractional frame.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A sampled timeline's current position: an external store shared by React and render loops.
|
|
18
|
+
*
|
|
19
|
+
* Every function member may be called detached (`const { get } = playhead`), which is how
|
|
20
|
+
* `useSyncExternalStore` and a render loop hold them.
|
|
21
|
+
*/
|
|
22
|
+
export interface Playhead {
|
|
23
|
+
/** The current sample index, an integer in `0 … count − 1` (0 when `count` is 0). Cheap: call it every frame. */
|
|
24
|
+
readonly get: () => number;
|
|
25
|
+
/**
|
|
26
|
+
* Move to sample `k`. Rounded to an integer and clamped to `0 … count − 1`; listeners are
|
|
27
|
+
* notified only if the index changed.
|
|
28
|
+
*/
|
|
29
|
+
readonly set: (k: number) => void;
|
|
30
|
+
/**
|
|
31
|
+
* Be told when the index or the timeline changes.
|
|
32
|
+
*
|
|
33
|
+
* @returns The unsubscribe function.
|
|
34
|
+
*/
|
|
35
|
+
readonly subscribe: (listener: () => void) => () => void;
|
|
36
|
+
/** How many samples the timeline has. */
|
|
37
|
+
readonly count: number;
|
|
38
|
+
/** The time between samples, in seconds. Sample `k` is at `t = k · dt`. */
|
|
39
|
+
readonly dt: number;
|
|
40
|
+
/**
|
|
41
|
+
* Swap in a new timeline — a newly loaded or re-baked scenario — keeping the one store
|
|
42
|
+
* every subscriber already holds. The index is clamped into the new range (not reset:
|
|
43
|
+
* call `set(0)` for that). Listeners are notified if anything changed.
|
|
44
|
+
*/
|
|
45
|
+
readonly setTimeline: (count: number, dt: number) => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A playhead over `count` samples spaced `dt` seconds apart, starting at sample 0.
|
|
50
|
+
*
|
|
51
|
+
* Create one per app (not per render — keep it in a `useState` initialiser or module
|
|
52
|
+
* scope), hand it to `PlaybackBar` and to the render loop, and replace its timeline with
|
|
53
|
+
* `setTimeline` when a new scenario loads.
|
|
54
|
+
*
|
|
55
|
+
* @param count - The number of samples (a non-negative integer).
|
|
56
|
+
* @param dt - Seconds between samples (positive).
|
|
57
|
+
* @returns The store.
|
|
58
|
+
*/
|
|
59
|
+
export function createPlayhead(count: number, dt: number): Playhead {
|
|
60
|
+
let index = 0;
|
|
61
|
+
let timeline = checkTimeline(count, dt);
|
|
62
|
+
const listeners = new Set<() => void>();
|
|
63
|
+
const notify = () => {
|
|
64
|
+
for (const listener of [...listeners]) listener();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
get: () => index,
|
|
69
|
+
set: (k) => {
|
|
70
|
+
const next = clampIndex(k, timeline.count);
|
|
71
|
+
if (next === index) return;
|
|
72
|
+
index = next;
|
|
73
|
+
notify();
|
|
74
|
+
},
|
|
75
|
+
subscribe: (listener) => {
|
|
76
|
+
listeners.add(listener);
|
|
77
|
+
return () => {
|
|
78
|
+
listeners.delete(listener);
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
get count() {
|
|
82
|
+
return timeline.count;
|
|
83
|
+
},
|
|
84
|
+
get dt() {
|
|
85
|
+
return timeline.dt;
|
|
86
|
+
},
|
|
87
|
+
setTimeline: (nextCount, nextDt) => {
|
|
88
|
+
const next = checkTimeline(nextCount, nextDt);
|
|
89
|
+
const nextIndex = clampIndex(index, next.count);
|
|
90
|
+
if (next.count === timeline.count && next.dt === timeline.dt && nextIndex === index) return;
|
|
91
|
+
timeline = next;
|
|
92
|
+
index = nextIndex;
|
|
93
|
+
notify();
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function checkTimeline(count: number, dt: number): { count: number; dt: number } {
|
|
99
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
100
|
+
throw new RangeError(`playhead: count must be a non-negative integer, got ${count}`);
|
|
101
|
+
}
|
|
102
|
+
if (!(dt > 0) || !Number.isFinite(dt)) {
|
|
103
|
+
throw new RangeError(`playhead: dt must be a positive number of seconds, got ${dt}`);
|
|
104
|
+
}
|
|
105
|
+
return { count, dt };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A sample index made legal: rounded, and clamped to `0 … count − 1`.
|
|
110
|
+
*
|
|
111
|
+
* @param k - Any number (`NaN` reads as 0).
|
|
112
|
+
* @param count - The number of samples.
|
|
113
|
+
* @returns The index; 0 when `count` is 0.
|
|
114
|
+
*/
|
|
115
|
+
export function clampIndex(k: number, count: number): number {
|
|
116
|
+
if (count <= 0 || Number.isNaN(k)) return 0;
|
|
117
|
+
return Math.min(count - 1, Math.max(0, Math.round(k)));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The outcome of one clock step: the new fractional position, and whether playback ran off the end. */
|
|
121
|
+
export interface AdvanceResult {
|
|
122
|
+
/** The new position in samples (fractional); the index to show is its floor. */
|
|
123
|
+
position: number;
|
|
124
|
+
/** True when a non-looping timeline reached its last sample on this step. */
|
|
125
|
+
ended: boolean;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* One step of real-time playback, in samples.
|
|
130
|
+
*
|
|
131
|
+
* Each sample is shown for `dt`: a looping timeline has period `count · dt` and wraps from
|
|
132
|
+
* the last sample to the first; a non-looping one stops on the last sample and reports
|
|
133
|
+
* `ended`. The position is fractional so that playback slower than one sample per frame
|
|
134
|
+
* still advances.
|
|
135
|
+
*
|
|
136
|
+
* @param position - The current position in samples (fractional).
|
|
137
|
+
* @param delta - How many samples to advance (elapsed seconds × speed ÷ dt); not negative.
|
|
138
|
+
* @param count - The number of samples.
|
|
139
|
+
* @param loop - Whether to wrap at the end.
|
|
140
|
+
* @returns The new position and whether playback ended.
|
|
141
|
+
*/
|
|
142
|
+
export function advance(position: number, delta: number, count: number, loop: boolean): AdvanceResult {
|
|
143
|
+
if (count <= 1) return { position: 0, ended: !loop };
|
|
144
|
+
const next = position + Math.max(0, delta);
|
|
145
|
+
if (loop) return { position: next % count, ended: false };
|
|
146
|
+
const last = count - 1;
|
|
147
|
+
return next >= last ? { position: last, ended: true } : { position: next, ended: false };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The default time readout: seconds with millisecond resolution, `"1.234 s"`.
|
|
152
|
+
*
|
|
153
|
+
* @param seconds - The time.
|
|
154
|
+
* @returns The formatted time.
|
|
155
|
+
*/
|
|
156
|
+
export function formatSeconds(seconds: number): string {
|
|
157
|
+
return `${seconds.toFixed(3)} s`;
|
|
158
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The arithmetic behind `SplitPane`: resolving sizes given in pixels or percent, clamping a
|
|
3
|
+
* drag or a key press to the pane's limits, and remembering a size across reloads.
|
|
4
|
+
*
|
|
5
|
+
* Kept out of the component so every rule — which key moves the divider which way, where a
|
|
6
|
+
* collapsible pane snaps shut — is tested without a DOM.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A pane size: CSS pixels as a number, or a share of the split's length as a percentage
|
|
11
|
+
* string (`"30%"`).
|
|
12
|
+
*/
|
|
13
|
+
export type PaneSize = number | `${number}%`;
|
|
14
|
+
|
|
15
|
+
/** The limits a `SplitPane`'s sized pane is held to, resolved to pixels. */
|
|
16
|
+
export interface SplitLimits {
|
|
17
|
+
/** The smallest open size, in pixels. */
|
|
18
|
+
min: number;
|
|
19
|
+
/** The largest size, in pixels. */
|
|
20
|
+
max: number;
|
|
21
|
+
/** Whether the pane may close to 0 below `min`. */
|
|
22
|
+
collapsible: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A size in pixels.
|
|
27
|
+
*
|
|
28
|
+
* @param size - Pixels, or a percentage of `length`.
|
|
29
|
+
* @param length - The split's length along its axis, in pixels.
|
|
30
|
+
* @returns The size in pixels (never negative).
|
|
31
|
+
*/
|
|
32
|
+
export function resolveSize(size: PaneSize, length: number): number {
|
|
33
|
+
const px = typeof size === "number" ? size : (Number.parseFloat(size) / 100) * length;
|
|
34
|
+
return Number.isFinite(px) ? Math.max(0, px) : 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The limits in pixels for a split of `length`. `max` never exceeds `length`, and `min`
|
|
39
|
+
* never exceeds `max`.
|
|
40
|
+
*
|
|
41
|
+
* @param length - The split's length along its axis, in pixels.
|
|
42
|
+
* @param minSize - The smallest open size. Defaults to 0.
|
|
43
|
+
* @param maxSize - The largest size. Defaults to the whole length.
|
|
44
|
+
* @param collapsible - Whether the pane may close below `minSize`.
|
|
45
|
+
* @returns The resolved limits.
|
|
46
|
+
*/
|
|
47
|
+
export function resolveLimits(
|
|
48
|
+
length: number,
|
|
49
|
+
minSize: PaneSize = 0,
|
|
50
|
+
maxSize: PaneSize = "100%",
|
|
51
|
+
collapsible = false,
|
|
52
|
+
): SplitLimits {
|
|
53
|
+
const max = Math.min(resolveSize(maxSize, length), Math.max(0, length));
|
|
54
|
+
const min = Math.min(resolveSize(minSize, length), max);
|
|
55
|
+
return { min, max, collapsible };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Hold a proposed size to the limits. A collapsible pane dragged below half its minimum
|
|
60
|
+
* snaps shut (0); between half the minimum and the minimum it stays at the minimum.
|
|
61
|
+
*
|
|
62
|
+
* @param size - The proposed size, in pixels.
|
|
63
|
+
* @param limits - The resolved limits.
|
|
64
|
+
* @returns The size to apply, in pixels, rounded to whole pixels.
|
|
65
|
+
*/
|
|
66
|
+
export function clampSize(size: number, limits: SplitLimits): number {
|
|
67
|
+
if (limits.collapsible && size < limits.min / 2) return 0;
|
|
68
|
+
return Math.round(Math.min(limits.max, Math.max(limits.min, size)));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Which way a `SplitPane` lays out its two panes. */
|
|
72
|
+
export type SplitOrientation = "horizontal" | "vertical";
|
|
73
|
+
|
|
74
|
+
/** Which of the two panes carries the explicit size; the other takes the rest. */
|
|
75
|
+
export type SizedPane = "start" | "end";
|
|
76
|
+
|
|
77
|
+
/** What a key press on the divider asks for. */
|
|
78
|
+
export type SplitKeyAction =
|
|
79
|
+
| { type: "resize"; size: number }
|
|
80
|
+
| { type: "toggle-collapse" };
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The divider's keyboard (the WAI-ARIA window-splitter pattern).
|
|
84
|
+
*
|
|
85
|
+
* The arrow keys along the split's axis move the divider the way they point — so the sized
|
|
86
|
+
* pane grows or shrinks depending on which side it is on — by `step`, or four steps with
|
|
87
|
+
* Shift. Home gives the sized pane its minimum, End its maximum, and Enter collapses or
|
|
88
|
+
* restores a collapsible pane.
|
|
89
|
+
*
|
|
90
|
+
* @param key - `KeyboardEvent.key`.
|
|
91
|
+
* @param shift - Whether Shift is held.
|
|
92
|
+
* @param size - The sized pane's current size, in pixels.
|
|
93
|
+
* @param options - The layout, the step in pixels and the resolved limits.
|
|
94
|
+
* @returns The action, or `null` when the key is not the divider's.
|
|
95
|
+
*/
|
|
96
|
+
export function splitKeyAction(
|
|
97
|
+
key: string,
|
|
98
|
+
shift: boolean,
|
|
99
|
+
size: number,
|
|
100
|
+
options: { orientation: SplitOrientation; sizedPane: SizedPane; step: number; limits: SplitLimits },
|
|
101
|
+
): SplitKeyAction | null {
|
|
102
|
+
const { orientation, sizedPane, step, limits } = options;
|
|
103
|
+
const [back, forward] =
|
|
104
|
+
orientation === "horizontal" ? ["ArrowLeft", "ArrowRight"] : ["ArrowUp", "ArrowDown"];
|
|
105
|
+
const grow = sizedPane === "start" ? 1 : -1;
|
|
106
|
+
const delta = shift ? step * 4 : step;
|
|
107
|
+
|
|
108
|
+
switch (key) {
|
|
109
|
+
case forward:
|
|
110
|
+
return { type: "resize", size: clampOpen(size + grow * delta, limits) };
|
|
111
|
+
case back:
|
|
112
|
+
return { type: "resize", size: clampOpen(size - grow * delta, limits) };
|
|
113
|
+
case "Home":
|
|
114
|
+
return { type: "resize", size: Math.round(limits.min) };
|
|
115
|
+
case "End":
|
|
116
|
+
return { type: "resize", size: Math.round(limits.max) };
|
|
117
|
+
case "Enter":
|
|
118
|
+
return limits.collapsible ? { type: "toggle-collapse" } : null;
|
|
119
|
+
default:
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** A key step never snaps a pane shut (that is Enter's job): clamp to the open range. */
|
|
125
|
+
function clampOpen(size: number, limits: SplitLimits): number {
|
|
126
|
+
return Math.round(Math.min(limits.max, Math.max(limits.min, size)));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The size stored under `key`, if any.
|
|
131
|
+
*
|
|
132
|
+
* @param key - The `localStorage` key.
|
|
133
|
+
* @returns The stored size in pixels, or `null` when nothing valid is stored or storage
|
|
134
|
+
* cannot be read (private browsing, the server).
|
|
135
|
+
*/
|
|
136
|
+
export function readStoredSize(key: string): number | null {
|
|
137
|
+
try {
|
|
138
|
+
const raw = window.localStorage.getItem(key);
|
|
139
|
+
if (raw === null) return null;
|
|
140
|
+
const value = Number(raw);
|
|
141
|
+
return Number.isFinite(value) && value >= 0 ? value : null;
|
|
142
|
+
} catch {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Remember a size under `key`. Failures (storage full, disabled, the server) are ignored: a
|
|
149
|
+
* size that is not remembered is not worth an error.
|
|
150
|
+
*
|
|
151
|
+
* @param key - The `localStorage` key.
|
|
152
|
+
* @param size - The size in pixels.
|
|
153
|
+
*/
|
|
154
|
+
export function writeStoredSize(key: string, size: number): void {
|
|
155
|
+
try {
|
|
156
|
+
window.localStorage.setItem(key, String(Math.round(size)));
|
|
157
|
+
} catch {
|
|
158
|
+
// Nothing to do: the size simply is not remembered.
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The model behind `TreeView`: which rows are visible, where each sits in its set, and what
|
|
3
|
+
* each key does (the WAI-ARIA tree pattern).
|
|
4
|
+
*
|
|
5
|
+
* Kept out of the component so the keyboard contract — the part a tree most often gets
|
|
6
|
+
* subtly wrong — is tested as a function from (rows, focus, key) to an action.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ReactNode } from "react";
|
|
10
|
+
|
|
11
|
+
/** One node of a `TreeView`. */
|
|
12
|
+
export interface TreeNode {
|
|
13
|
+
/** Identifies the node to `selectedId`, `onSelect` and `expanded`. Unique in the tree. */
|
|
14
|
+
id: string;
|
|
15
|
+
/** The node's name: shown, read out, and matched by type-ahead. */
|
|
16
|
+
label: string;
|
|
17
|
+
/** A leading icon (decorative) — typically a lucide icon at `size-3.5`. */
|
|
18
|
+
icon?: ReactNode;
|
|
19
|
+
/** Child nodes. A node with a (possibly empty) array is a parent; without, a leaf. */
|
|
20
|
+
children?: readonly TreeNode[] | undefined;
|
|
21
|
+
/** A quiet trailing annotation — a type, a count, a frame name. Shown in mono. */
|
|
22
|
+
meta?: ReactNode;
|
|
23
|
+
/** Shown and focusable, but not selectable. */
|
|
24
|
+
disabled?: boolean | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A node as it is laid out: one visible row of the tree. */
|
|
28
|
+
export interface TreeRow {
|
|
29
|
+
/** The node. */
|
|
30
|
+
node: TreeNode;
|
|
31
|
+
/** Depth, from 1 at the root (`aria-level`). */
|
|
32
|
+
level: number;
|
|
33
|
+
/** The parent's id, or `null` at the root. */
|
|
34
|
+
parentId: string | null;
|
|
35
|
+
/** 1-based position among its siblings (`aria-posinset`). */
|
|
36
|
+
posInSet: number;
|
|
37
|
+
/** How many siblings, itself included (`aria-setsize`). */
|
|
38
|
+
setSize: number;
|
|
39
|
+
/** Whether the node has a children array. */
|
|
40
|
+
isParent: boolean;
|
|
41
|
+
/** Whether the node is a parent and expanded. */
|
|
42
|
+
isExpanded: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The rows a reader can see: every root, and the children of every expanded parent, in
|
|
47
|
+
* document order.
|
|
48
|
+
*
|
|
49
|
+
* @param nodes - The roots.
|
|
50
|
+
* @param expanded - The ids of the expanded parents.
|
|
51
|
+
* @returns The visible rows, in order.
|
|
52
|
+
*/
|
|
53
|
+
export function visibleRows(nodes: readonly TreeNode[], expanded: ReadonlySet<string>): TreeRow[] {
|
|
54
|
+
const rows: TreeRow[] = [];
|
|
55
|
+
const walk = (siblings: readonly TreeNode[], level: number, parentId: string | null) => {
|
|
56
|
+
siblings.forEach((node, index) => {
|
|
57
|
+
const isParent = node.children !== undefined;
|
|
58
|
+
const isExpanded = isParent && expanded.has(node.id);
|
|
59
|
+
rows.push({
|
|
60
|
+
node,
|
|
61
|
+
level,
|
|
62
|
+
parentId,
|
|
63
|
+
posInSet: index + 1,
|
|
64
|
+
setSize: siblings.length,
|
|
65
|
+
isParent,
|
|
66
|
+
isExpanded,
|
|
67
|
+
});
|
|
68
|
+
if (isExpanded && node.children) walk(node.children, level + 1, node.id);
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
walk(nodes, 1, null);
|
|
72
|
+
return rows;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Every parent's id — the expanded set for "everything open".
|
|
77
|
+
*
|
|
78
|
+
* @param nodes - The roots.
|
|
79
|
+
* @returns The ids of every node with a children array, at any depth.
|
|
80
|
+
*/
|
|
81
|
+
export function parentIds(nodes: readonly TreeNode[]): string[] {
|
|
82
|
+
const ids: string[] = [];
|
|
83
|
+
const walk = (siblings: readonly TreeNode[]) => {
|
|
84
|
+
for (const node of siblings) {
|
|
85
|
+
if (node.children === undefined) continue;
|
|
86
|
+
ids.push(node.id);
|
|
87
|
+
walk(node.children);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
walk(nodes);
|
|
91
|
+
return ids;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The ancestors of a node, root first — what must be expanded for it to be visible.
|
|
96
|
+
*
|
|
97
|
+
* @param nodes - The roots.
|
|
98
|
+
* @param id - The node.
|
|
99
|
+
* @returns The ancestor ids, root first; empty for a root or an id not in the tree.
|
|
100
|
+
*/
|
|
101
|
+
export function ancestorIds(nodes: readonly TreeNode[], id: string): string[] {
|
|
102
|
+
const walk = (siblings: readonly TreeNode[], path: string[]): string[] | null => {
|
|
103
|
+
for (const node of siblings) {
|
|
104
|
+
if (node.id === id) return path;
|
|
105
|
+
if (node.children) {
|
|
106
|
+
const found = walk(node.children, [...path, node.id]);
|
|
107
|
+
if (found) return found;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
};
|
|
112
|
+
return walk(nodes, []) ?? [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** What a key press on a tree asks for. */
|
|
116
|
+
export type TreeKeyAction =
|
|
117
|
+
| { type: "focus"; id: string }
|
|
118
|
+
| { type: "expand"; id: string }
|
|
119
|
+
| { type: "collapse"; id: string }
|
|
120
|
+
| { type: "select"; id: string }
|
|
121
|
+
| { type: "expand-siblings"; ids: string[] };
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The tree's keyboard (the WAI-ARIA tree pattern), as a pure function.
|
|
125
|
+
*
|
|
126
|
+
* ↓/↑ move to the next/previous visible row; → expands a closed parent, or moves into an
|
|
127
|
+
* open one's first child; ← collapses an open parent, or moves to the parent; Home/End go to
|
|
128
|
+
* the first/last visible row; Enter and Space select; `*` expands every sibling of the
|
|
129
|
+
* focused row; a printable character moves to the next row whose label starts with it
|
|
130
|
+
* (type-ahead, wrapping).
|
|
131
|
+
*
|
|
132
|
+
* @param rows - The visible rows, from `visibleRows`.
|
|
133
|
+
* @param focusedId - The row with focus.
|
|
134
|
+
* @param key - `KeyboardEvent.key`.
|
|
135
|
+
* @returns The action, or `null` when the key does nothing here.
|
|
136
|
+
*/
|
|
137
|
+
export function treeKeyAction(rows: readonly TreeRow[], focusedId: string, key: string): TreeKeyAction | null {
|
|
138
|
+
const index = rows.findIndex((row) => row.node.id === focusedId);
|
|
139
|
+
const row = rows[index];
|
|
140
|
+
if (row === undefined) return null;
|
|
141
|
+
const focus = (target: TreeRow | undefined): TreeKeyAction | null =>
|
|
142
|
+
target === undefined || target === row ? null : { type: "focus", id: target.node.id };
|
|
143
|
+
|
|
144
|
+
switch (key) {
|
|
145
|
+
case "ArrowDown":
|
|
146
|
+
return focus(rows[index + 1]);
|
|
147
|
+
case "ArrowUp":
|
|
148
|
+
return focus(rows[index - 1]);
|
|
149
|
+
case "Home":
|
|
150
|
+
return focus(rows[0]);
|
|
151
|
+
case "End":
|
|
152
|
+
return focus(rows[rows.length - 1]);
|
|
153
|
+
case "ArrowRight":
|
|
154
|
+
if (!row.isParent) return null;
|
|
155
|
+
if (!row.isExpanded) return { type: "expand", id: row.node.id };
|
|
156
|
+
// An open parent's first child is the next row, if it has one.
|
|
157
|
+
return rows[index + 1]?.parentId === row.node.id ? focus(rows[index + 1]) : null;
|
|
158
|
+
case "ArrowLeft":
|
|
159
|
+
if (row.isExpanded) return { type: "collapse", id: row.node.id };
|
|
160
|
+
return row.parentId === null
|
|
161
|
+
? null
|
|
162
|
+
: focus(rows.find((candidate) => candidate.node.id === row.parentId));
|
|
163
|
+
case "Enter":
|
|
164
|
+
case " ":
|
|
165
|
+
return row.node.disabled ? null : { type: "select", id: row.node.id };
|
|
166
|
+
case "*": {
|
|
167
|
+
const ids = rows
|
|
168
|
+
.filter((candidate) => candidate.parentId === row.parentId && candidate.isParent && !candidate.isExpanded)
|
|
169
|
+
.map((candidate) => candidate.node.id);
|
|
170
|
+
return ids.length === 0 ? null : { type: "expand-siblings", ids };
|
|
171
|
+
}
|
|
172
|
+
default:
|
|
173
|
+
return typeAhead(rows, index, key);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** The next row after `from` (wrapping) whose label starts with `key`, for a single printable character. */
|
|
178
|
+
function typeAhead(rows: readonly TreeRow[], from: number, key: string): TreeKeyAction | null {
|
|
179
|
+
if (key.length !== 1 || key.trim() === "") return null;
|
|
180
|
+
const needle = key.toLocaleLowerCase();
|
|
181
|
+
for (let offset = 1; offset <= rows.length; offset += 1) {
|
|
182
|
+
const candidate = rows[(from + offset) % rows.length];
|
|
183
|
+
if (candidate?.node.label.toLocaleLowerCase().startsWith(needle)) {
|
|
184
|
+
return offset === rows.length ? null : { type: "focus", id: candidate.node.id };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The React side of the playhead: subscribing to it, and driving it in real time.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useEffect, useEffectEvent, useSyncExternalStore } from "react";
|
|
6
|
+
|
|
7
|
+
import { advance, type Playhead } from "./playhead";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The playhead's current sample index, re-rendering the calling component when it changes.
|
|
11
|
+
*
|
|
12
|
+
* Use it only where the index is *displayed* (the playback bar, a readout). A render loop
|
|
13
|
+
* reads `playhead.get()` in its own frame callback instead, and re-renders nothing.
|
|
14
|
+
*
|
|
15
|
+
* @param playhead - The store.
|
|
16
|
+
* @returns The current sample index.
|
|
17
|
+
*/
|
|
18
|
+
export function usePlayhead(playhead: Playhead): number {
|
|
19
|
+
return useSyncExternalStore(playhead.subscribe, playhead.get, playhead.get);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The playhead's timeline — `count` and `dt` — re-rendering when `setTimeline` changes it.
|
|
24
|
+
*
|
|
25
|
+
* @param playhead - The store.
|
|
26
|
+
* @returns The number of samples and the seconds between them.
|
|
27
|
+
*/
|
|
28
|
+
export function usePlayheadTimeline(playhead: Playhead): { count: number; dt: number } {
|
|
29
|
+
const count = useSyncExternalStore(playhead.subscribe, () => playhead.count, () => playhead.count);
|
|
30
|
+
const dt = useSyncExternalStore(playhead.subscribe, () => playhead.dt, () => playhead.dt);
|
|
31
|
+
return { count, dt };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Options of `usePlaybackClock`. */
|
|
35
|
+
export interface PlaybackClockOptions {
|
|
36
|
+
/** The store to advance. */
|
|
37
|
+
playhead: Playhead;
|
|
38
|
+
/** Whether the clock runs. */
|
|
39
|
+
playing: boolean;
|
|
40
|
+
/** Playback rate: 1 is real time. Defaults to 1. */
|
|
41
|
+
speed?: number | undefined;
|
|
42
|
+
/** Wrap from the last sample to the first instead of stopping. Defaults to false. */
|
|
43
|
+
loop?: boolean | undefined;
|
|
44
|
+
/** Called once when a non-looping timeline reaches its last sample; set `playing` false here. */
|
|
45
|
+
onEnd?: (() => void) | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The longest wall-clock gap one frame may advance by, in milliseconds. A tab in the
|
|
48
|
+
* background gets no animation frames, and without this cap the first frame back would
|
|
49
|
+
* jump by the whole time away. Defaults to 100.
|
|
50
|
+
*/
|
|
51
|
+
maxFrameMs?: number | undefined;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Advance a playhead in real time × `speed` while `playing`, from `requestAnimationFrame`.
|
|
56
|
+
*
|
|
57
|
+
* Position is accumulated fractionally, so slow playback of a finely sampled timeline still
|
|
58
|
+
* moves; the store only ever holds the integer index. A seek from elsewhere (the scrubber, a
|
|
59
|
+
* marker) is picked up on the next frame. Starting a non-looping timeline that sits on its
|
|
60
|
+
* last sample rewinds it to 0 first. `speed`, `loop` and `onEnd` may change while playing
|
|
61
|
+
* without restarting the clock.
|
|
62
|
+
*
|
|
63
|
+
* Renders nothing and re-renders nothing: call it once, in the component that owns the
|
|
64
|
+
* `playing` state.
|
|
65
|
+
*
|
|
66
|
+
* @param options - The playhead and the transport state.
|
|
67
|
+
*/
|
|
68
|
+
export function usePlaybackClock({
|
|
69
|
+
playhead,
|
|
70
|
+
playing,
|
|
71
|
+
speed = 1,
|
|
72
|
+
loop = false,
|
|
73
|
+
onEnd,
|
|
74
|
+
maxFrameMs = 100,
|
|
75
|
+
}: PlaybackClockOptions): void {
|
|
76
|
+
const step = useEffectEvent((position: number, elapsedMs: number) => {
|
|
77
|
+
const delta = (Math.min(elapsedMs, maxFrameMs) / 1000) * (speed / playhead.dt);
|
|
78
|
+
return advance(position, delta, playhead.count, loop);
|
|
79
|
+
});
|
|
80
|
+
const end = useEffectEvent(() => onEnd?.());
|
|
81
|
+
const rewindIfAtEnd = useEffectEvent(() => {
|
|
82
|
+
if (!loop && playhead.count > 0 && playhead.get() >= playhead.count - 1) playhead.set(0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (!playing) return;
|
|
87
|
+
rewindIfAtEnd();
|
|
88
|
+
let position = playhead.get();
|
|
89
|
+
let last: number | null = null;
|
|
90
|
+
let frame = requestAnimationFrame(function tick(now: number) {
|
|
91
|
+
// A seek since the last frame (scrubber, marker, step button) wins over the clock.
|
|
92
|
+
if (Math.floor(position) !== playhead.get()) position = playhead.get();
|
|
93
|
+
const result = last === null ? { position, ended: false } : step(position, now - last);
|
|
94
|
+
last = now;
|
|
95
|
+
position = result.position;
|
|
96
|
+
playhead.set(Math.floor(position));
|
|
97
|
+
if (result.ended) {
|
|
98
|
+
end();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
frame = requestAnimationFrame(tick);
|
|
102
|
+
});
|
|
103
|
+
return () => cancelAnimationFrame(frame);
|
|
104
|
+
}, [playing, playhead]);
|
|
105
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@vitavision/workbench` — the building blocks of a studio app on `@vitavision/ui`: the
|
|
3
|
+
* shell and its split panes, a tree navigator, a playback transport over a sampled timeline,
|
|
4
|
+
* file opening, and (re-exported from `@vitavision/ui`) notifications.
|
|
5
|
+
*
|
|
6
|
+
* Import `@vitavision/workbench/styles.css` after `@vitavision/ui/styles.css`.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export { AppShell, type AppShellProps, type SidePanelSize } from "./components/AppShell";
|
|
12
|
+
export { SplitPane, type SplitPaneProps } from "./components/SplitPane";
|
|
13
|
+
export {
|
|
14
|
+
type PaneSize,
|
|
15
|
+
type SizedPane,
|
|
16
|
+
type SplitOrientation,
|
|
17
|
+
clampSize,
|
|
18
|
+
resolveLimits,
|
|
19
|
+
resolveSize,
|
|
20
|
+
type SplitLimits,
|
|
21
|
+
} from "./components/splitSize";
|
|
22
|
+
|
|
23
|
+
export { TreeView, type TreeViewProps } from "./components/TreeView";
|
|
24
|
+
export {
|
|
25
|
+
type TreeKeyAction,
|
|
26
|
+
type TreeNode,
|
|
27
|
+
type TreeRow,
|
|
28
|
+
ancestorIds,
|
|
29
|
+
parentIds,
|
|
30
|
+
treeKeyAction,
|
|
31
|
+
visibleRows,
|
|
32
|
+
} from "./components/treeModel";
|
|
33
|
+
|
|
34
|
+
export {
|
|
35
|
+
DEFAULT_SPEEDS,
|
|
36
|
+
PlaybackBar,
|
|
37
|
+
type PlaybackBarProps,
|
|
38
|
+
type PlaybackMarker,
|
|
39
|
+
} from "./components/PlaybackBar";
|
|
40
|
+
export {
|
|
41
|
+
type AdvanceResult,
|
|
42
|
+
advance,
|
|
43
|
+
clampIndex,
|
|
44
|
+
createPlayhead,
|
|
45
|
+
formatSeconds,
|
|
46
|
+
type Playhead,
|
|
47
|
+
} from "./components/playhead";
|
|
48
|
+
export {
|
|
49
|
+
type PlaybackClockOptions,
|
|
50
|
+
usePlaybackClock,
|
|
51
|
+
usePlayhead,
|
|
52
|
+
usePlayheadTimeline,
|
|
53
|
+
} from "./components/usePlayback";
|
|
54
|
+
|
|
55
|
+
export { FileDrop, type FileDropProps } from "./components/FileDrop";
|
|
56
|
+
export { acceptsFile, collectDroppedFiles, type DropEntry } from "./components/dropFiles";
|
|
57
|
+
|
|
58
|
+
// Notifications moved to `@vitavision/ui` (a second app needed them). These are re-exports of
|
|
59
|
+
// the same bindings, so there is still one default store; prefer importing from `@vitavision/ui`.
|
|
60
|
+
export {
|
|
61
|
+
createToastStore,
|
|
62
|
+
defaultToastStore,
|
|
63
|
+
Toaster,
|
|
64
|
+
toast,
|
|
65
|
+
type ToasterProps,
|
|
66
|
+
type ToastOptions,
|
|
67
|
+
type ToastRecord,
|
|
68
|
+
type ToastStore,
|
|
69
|
+
type ToastTone,
|
|
70
|
+
} from "@vitavision/ui";
|
package/src/styles.css
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Nothing to style: this file exists so Tailwind v4 finds this package's class names.
|
|
3
|
+
* `@source` resolves against the stylesheet that declares it, and this file is exported from
|
|
4
|
+
* `src/` (which ships), so it scans the package's sources. Import it after
|
|
5
|
+
* `@vitavision/ui/styles.css`.
|
|
6
|
+
*/
|
|
7
|
+
@source "./";
|