@vitavision/workbench 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,6 +32,13 @@ export interface SidePanelSize {
32
32
  export interface AppShellProps {
33
33
  /** The top bar: the app's name, the file actions, the theme toggle. */
34
34
  header?: ReactNode;
35
+ /**
36
+ * A fixed-width column at the far left, outside the resizable panels: an icon rail of
37
+ * workspaces. It takes its content's width and is a `<nav>` named by `railLabel`.
38
+ */
39
+ rail?: ReactNode;
40
+ /** Names the rail's landmark (`<nav>`). Defaults to "Workspaces". */
41
+ railLabel?: string | undefined;
35
42
  /** The panel left of the main surface — a navigator such as a `TreeView`. */
36
43
  left?: ReactNode;
37
44
  /** The main surface — the viewport. Always present. */
@@ -61,17 +68,19 @@ const LEFT_DEFAULTS = { defaultSize: 280, minSize: 180, maxSize: "50%" } as cons
61
68
  const RIGHT_DEFAULTS = { defaultSize: 320, minSize: 220, maxSize: "50%" } as const;
62
69
 
63
70
  /**
64
- * The full-viewport frame of a studio app: `header` across the top, `left` | `main` |
65
- * `right` in the middle with resizable side panels, `bottom` across the foot. Every slot but
66
- * `main` is optional, and an absent slot takes no space.
71
+ * The full-viewport frame of a studio app: `header` across the top, then `rail` | `left` |
72
+ * `main` | `right` with resizable side panels and a fixed-width rail, and `bottom` across the
73
+ * foot. Every slot but `main` is optional, and an absent slot takes no space.
67
74
  *
68
- * Landmarks: the header is a `<header>`, the main surface a `<main>`, the side panels
69
- * `<aside>`s named by `leftLabel` and `rightLabel`. Each side panel is divided from the main
75
+ * Landmarks: the header is a `<header>`, the rail a `<nav>` named by `railLabel`, the main
76
+ * surface a `<main>`, the side panels `<aside>`s named by `leftLabel` and `rightLabel`. Each side panel is divided from the main
70
77
  * surface by a `SplitPane` divider (drag, or focus it and use the arrow keys). Only the
71
78
  * insides of the panels scroll; the shell itself is fixed to the viewport (`h-dvh`).
72
79
  */
73
80
  export function AppShell({
74
81
  header,
82
+ rail,
83
+ railLabel = "Workspaces",
75
84
  left,
76
85
  main,
77
86
  right,
@@ -87,6 +96,7 @@ export function AppShell({
87
96
  const rightSizing = { ...RIGHT_DEFAULTS, ...rightSize };
88
97
  const hasLeft = left !== undefined && left !== null && left !== false;
89
98
  const hasRight = right !== undefined && right !== null && right !== false;
99
+ const hasRail = rail !== undefined && rail !== null && rail !== false;
90
100
 
91
101
  const mainSurface = <main className="relative h-full min-h-0 min-w-0 overflow-hidden">{main}</main>;
92
102
 
@@ -134,7 +144,16 @@ export function AppShell({
134
144
  ) : (
135
145
  <div aria-hidden />
136
146
  )}
137
- <div className="min-h-0 min-w-0">{middle}</div>
147
+ {hasRail ? (
148
+ <div className="flex min-h-0 min-w-0">
149
+ <nav aria-label={railLabel} className="h-full shrink-0 overflow-y-auto border-r border-line bg-surface">
150
+ {rail}
151
+ </nav>
152
+ <div className="min-h-0 min-w-0 flex-1">{middle}</div>
153
+ </div>
154
+ ) : (
155
+ <div className="min-h-0 min-w-0">{middle}</div>
156
+ )}
138
157
  {bottom ? <div className="min-w-0 border-t border-line bg-surface">{bottom}</div> : <div aria-hidden />}
139
158
  </div>
140
159
  );
@@ -13,12 +13,38 @@ import type { ChangeEvent, DragEvent, ReactNode } from "react";
13
13
 
14
14
  import { Button, cn } from "@vitavision/ui";
15
15
 
16
- import { carriesFiles, collectDroppedFiles, partitionFiles } from "./dropFiles";
16
+ import { acceptsPath, carriesFiles, collectDroppedFiles, partitionFiles } from "./dropFiles";
17
+
18
+ /**
19
+ * Where paths come from in a desktop shell whose native drops and dialogs yield paths, not
20
+ * `File`s (Tauri, Electron). The app implements it over its shell's APIs; the package stays
21
+ * shell-agnostic. See the README for a Tauri implementation.
22
+ */
23
+ export interface PathSource {
24
+ /** Opens the native picker; resolves with the chosen paths, `[]` if cancelled. */
25
+ pick: (options: { multiple: boolean; directory: boolean; accept?: string | undefined }) => Promise<string[]>;
26
+ /**
27
+ * Subscribes to native drags of files over the window: `onEnter` and `onLeave` while a drag
28
+ * hovers, `onDrop` with its paths. Returns the unsubscribe. Optional: without it, only the
29
+ * picker offers paths.
30
+ */
31
+ subscribe?: (handlers: { onEnter: () => void; onLeave: () => void; onDrop: (paths: string[]) => void }) => () => void;
32
+ }
17
33
 
18
34
  /** Props of `FileDrop`. */
19
35
  export interface FileDropProps {
20
36
  /** Called with the accepted files of a drop or a pick (never with an empty list). */
21
- onFiles: (files: File[]) => void;
37
+ onFiles?: ((files: File[]) => void) | undefined;
38
+ /**
39
+ * The desktop route: paths from `pathSource`'s picker and native drops, filtered by
40
+ * `accept` on their extensions. With a `pathSource`, the picker and drops go through it
41
+ * instead of the browser's `File` route.
42
+ */
43
+ pathSource?: PathSource | undefined;
44
+ /** Called with the accepted paths of a native drop or pick (never with an empty list). */
45
+ onPaths?: ((paths: string[]) => void) | undefined;
46
+ /** Called with the paths `accept` turned away. */
47
+ onRejectPaths?: ((paths: string[]) => void) | undefined;
22
48
  /** Called with the files a drop or a pick brought that `accept` turned away. */
23
49
  onReject?: ((files: File[]) => void) | undefined;
24
50
  /**
@@ -62,9 +88,15 @@ export interface FileDropProps {
62
88
  *
63
89
  * Files are filtered by `accept` on both routes (the browser applies it to neither drops nor,
64
90
  * reliably, the picker); the rest go to `onReject`. A dropped folder is walked recursively.
91
+ *
92
+ * In a desktop shell, pass a `pathSource`: the picker and native drops then yield paths to
93
+ * `onPaths`, filtered by `accept` on their extensions, and the browser `File` route is off.
65
94
  */
66
95
  export function FileDrop({
67
96
  onFiles,
97
+ pathSource,
98
+ onPaths,
99
+ onRejectPaths,
68
100
  onReject,
69
101
  accept,
70
102
  multiple = true,
@@ -83,18 +115,40 @@ export function FileDrop({
83
115
 
84
116
  const deliver = (files: readonly File[]) => {
85
117
  const { accepted, rejected } = partitionFiles(files, accept);
86
- if (accepted.length > 0) onFiles(multiple ? accepted : accepted.slice(0, 1));
118
+ if (accepted.length > 0) onFiles?.(multiple ? accepted : accepted.slice(0, 1));
87
119
  if (rejected.length > 0) onReject?.(rejected);
88
120
  };
121
+ const deliverPaths = (paths: readonly string[]) => {
122
+ const accepted = paths.filter((path) => acceptsPath(path, accept));
123
+ const rejected = paths.filter((path) => !acceptsPath(path, accept));
124
+ if (accepted.length > 0) onPaths?.(multiple ? accepted : accepted.slice(0, 1));
125
+ if (rejected.length > 0) onRejectPaths?.(rejected);
126
+ };
89
127
  const deliverRef = useRef(deliver);
128
+ const deliverPathsRef = useRef(deliverPaths);
90
129
  useEffect(() => {
91
130
  deliverRef.current = deliver;
131
+ deliverPathsRef.current = deliverPaths;
92
132
  });
93
133
 
134
+ // The desktop route: native drags report paths through the source, for the overlay and the
135
+ // inline zone alike (a webview with native drag-drop on fires no DOM drop with files).
136
+ useEffect(() => {
137
+ if (!pathSource?.subscribe || disabled) return;
138
+ return pathSource.subscribe({
139
+ onEnter: () => setDragging(true),
140
+ onLeave: () => setDragging(false),
141
+ onDrop: (paths) => {
142
+ setDragging(false);
143
+ deliverPathsRef.current(paths);
144
+ },
145
+ });
146
+ }, [pathSource, disabled]);
147
+
94
148
  // The window-wide target. `dragenter`/`dragleave` fire for every element crossed, so the
95
149
  // overlay's visibility is a depth count rather than the last event seen.
96
150
  useEffect(() => {
97
- if (!overlay || disabled) return;
151
+ if (!overlay || disabled || pathSource) return;
98
152
  const onEnter = (event: globalThis.DragEvent) => {
99
153
  if (!carriesFiles(event.dataTransfer)) return;
100
154
  depthRef.current += 1;
@@ -128,7 +182,7 @@ export function FileDrop({
128
182
  window.removeEventListener("drop", onDrop);
129
183
  depthRef.current = 0;
130
184
  };
131
- }, [overlay, disabled]);
185
+ }, [overlay, disabled, pathSource]);
132
186
 
133
187
  const onPick = (event: ChangeEvent<HTMLInputElement>) => {
134
188
  const files = Array.from(event.currentTarget.files ?? []);
@@ -155,7 +209,15 @@ export function FileDrop({
155
209
  <Button
156
210
  icon={<FolderOpen />}
157
211
  disabled={disabled}
158
- onClick={() => inputRef.current?.click()}
212
+ onClick={() => {
213
+ if (pathSource) {
214
+ void pathSource.pick({ multiple: multiple || directory, directory, accept }).then((paths) => {
215
+ if (paths.length > 0) deliverPathsRef.current(paths);
216
+ });
217
+ return;
218
+ }
219
+ inputRef.current?.click();
220
+ }}
159
221
  className={overlay ? className : undefined}
160
222
  >
161
223
  {label}
@@ -0,0 +1,165 @@
1
+ /*
2
+ * Which item of an ordered set is current — a frame of a capture, a page of a scan — as a
3
+ * strip of thumbnails with previous/next and `[` / `]`.
4
+ *
5
+ * A capture is an ordered set and a person steps through it one item at a time, which a
6
+ * dropdown hides and a library page makes a round trip. The strip keeps the neighbours in
7
+ * view, and the keys work from anywhere on the screen.
8
+ */
9
+
10
+ import { ChevronLeft, ChevronRight } from "lucide-react";
11
+ import { useEffect, useRef, type ReactNode } from "react";
12
+
13
+ import { Button, Kbd, cn, focusRing } from "@vitavision/ui";
14
+
15
+ import { stepIndex } from "./sequence";
16
+
17
+ /** One item of a `SequenceNavigator`. */
18
+ export interface SequenceItem {
19
+ /** Its identity, unique in the sequence. */
20
+ id: string;
21
+ /** Its name: the thumbnail's alt text and tooltip. */
22
+ label: string;
23
+ /** A thumbnail URL, loaded lazily. Without it, the default rendering is the label. */
24
+ thumbnail?: string | undefined;
25
+ }
26
+
27
+ /** Props of `SequenceNavigator`. */
28
+ export interface SequenceNavigatorProps {
29
+ /** The items, in order. */
30
+ items: readonly SequenceItem[];
31
+ /** The current item's id, or `null` for none. */
32
+ value: string | null;
33
+ /** Called with the id of the item chosen by click, by previous/next, or by `[` / `]`. */
34
+ onValueChange: (id: string) => void;
35
+ /** Draws an item's thumbnail, e.g. one whose URL must be fetched. Defaults to a lazy `<img>`. */
36
+ renderThumbnail?: ((item: SequenceItem) => ReactNode) | undefined;
37
+ /** Step with `[` and `]` from anywhere outside a text field. Defaults to true. */
38
+ keys?: boolean | undefined;
39
+ /** Wrap from the last item to the first. Defaults to false. */
40
+ wrap?: boolean | undefined;
41
+ /** Names the strip (`aria-label`). */
42
+ "aria-label": string;
43
+ /** Merged with the row's own classes through `cn`. */
44
+ className?: string | undefined;
45
+ }
46
+
47
+ /**
48
+ * A horizontal strip of thumbnails between previous and next buttons, with the position
49
+ * ("3 / 50").
50
+ *
51
+ * The current thumbnail carries `aria-current="true"` and `data-current`, has an outline,
52
+ * and is scrolled into view when it changes. `[` and `]` step from anywhere outside a text
53
+ * field. Thumbnails load lazily, so a capture of thousands of frames opens at once.
54
+ */
55
+ export function SequenceNavigator({
56
+ items,
57
+ value,
58
+ onValueChange,
59
+ renderThumbnail,
60
+ keys = true,
61
+ wrap = false,
62
+ "aria-label": ariaLabel,
63
+ className,
64
+ }: SequenceNavigatorProps) {
65
+ const index = items.findIndex((item) => item.id === value);
66
+ const listRef = useRef<HTMLOListElement>(null);
67
+
68
+ const step = (delta: 1 | -1) => {
69
+ const next = stepIndex(items.length, index, delta, wrap);
70
+ const item = next === null ? undefined : items[next];
71
+ if (item && item.id !== value) onValueChange(item.id);
72
+ };
73
+ const stepRef = useRef(step);
74
+ useEffect(() => {
75
+ stepRef.current = step;
76
+ });
77
+
78
+ useEffect(() => {
79
+ if (!keys) return;
80
+ const onKey = (event: KeyboardEvent) => {
81
+ if (event.metaKey || event.ctrlKey || event.altKey || isTypingTarget(event.target)) return;
82
+ if (event.key === "[") stepRef.current(-1);
83
+ else if (event.key === "]") stepRef.current(1);
84
+ };
85
+ window.addEventListener("keydown", onKey);
86
+ return () => window.removeEventListener("keydown", onKey);
87
+ }, [keys]);
88
+
89
+ useEffect(() => {
90
+ listRef.current?.querySelector("[data-current]")?.scrollIntoView({ block: "nearest", inline: "nearest" });
91
+ }, [value]);
92
+
93
+ const atStart = !wrap && index <= 0;
94
+ const atEnd = !wrap && (index < 0 || index >= items.length - 1);
95
+
96
+ return (
97
+ <div className={cn("flex min-w-0 items-center gap-1.5", className)}>
98
+ <Button
99
+ size="sm"
100
+ variant="ghost"
101
+ icon={<ChevronLeft />}
102
+ aria-label="Previous"
103
+ aria-keyshortcuts={keys ? "[" : undefined}
104
+ disabled={items.length < 2 || atStart}
105
+ onClick={() => step(-1)}
106
+ />
107
+ <ol ref={listRef} aria-label={ariaLabel} className="flex min-w-0 flex-1 gap-1 overflow-x-auto py-1">
108
+ {items.map((item) => {
109
+ const current = item.id === value;
110
+ return (
111
+ <li key={item.id} className="shrink-0">
112
+ <button
113
+ type="button"
114
+ title={item.label}
115
+ aria-label={item.label}
116
+ aria-current={current ? "true" : undefined}
117
+ data-current={current ? "" : undefined}
118
+ onClick={() => {
119
+ if (!current) onValueChange(item.id);
120
+ }}
121
+ className={cn(
122
+ "block h-10 w-14 overflow-hidden rounded-control border border-line bg-raised text-fg",
123
+ "data-current:ring-2 data-current:ring-signal",
124
+ focusRing,
125
+ )}
126
+ >
127
+ {renderThumbnail ? (
128
+ renderThumbnail(item)
129
+ ) : item.thumbnail ? (
130
+ <img src={item.thumbnail} alt="" loading="lazy" decoding="async" className="h-full w-full object-cover" />
131
+ ) : (
132
+ <span className="block truncate px-1 font-mono text-[10px]">{item.label}</span>
133
+ )}
134
+ </button>
135
+ </li>
136
+ );
137
+ })}
138
+ </ol>
139
+ <Button
140
+ size="sm"
141
+ variant="ghost"
142
+ icon={<ChevronRight />}
143
+ aria-label="Next"
144
+ aria-keyshortcuts={keys ? "]" : undefined}
145
+ disabled={items.length < 2 || atEnd}
146
+ onClick={() => step(1)}
147
+ />
148
+ <span className="shrink-0 font-mono text-xs text-fg-muted tabular-nums">
149
+ {index >= 0 ? index + 1 : "–"} / {items.length}
150
+ </span>
151
+ {keys && (
152
+ <span className="hidden shrink-0 items-center gap-0.5 text-xs text-fg-muted sm:flex" aria-hidden>
153
+ <Kbd>[</Kbd>
154
+ <Kbd>]</Kbd>
155
+ </span>
156
+ )}
157
+ </div>
158
+ );
159
+ }
160
+
161
+ function isTypingTarget(target: EventTarget | null): boolean {
162
+ if (!(target instanceof HTMLElement)) return false;
163
+ const tag = target.tagName;
164
+ return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || target.isContentEditable;
165
+ }
@@ -32,6 +32,38 @@ export function acceptsFile(file: { name: string; type: string }, accept: string
32
32
  });
33
33
  }
34
34
 
35
+ /** MIME types by extension, for matching a bare path against `type/*` and MIME tokens. */
36
+ const MIME_BY_EXTENSION: Readonly<Record<string, string>> = {
37
+ png: "image/png",
38
+ jpg: "image/jpeg",
39
+ jpeg: "image/jpeg",
40
+ bmp: "image/bmp",
41
+ gif: "image/gif",
42
+ webp: "image/webp",
43
+ tif: "image/tiff",
44
+ tiff: "image/tiff",
45
+ pgm: "image/x-portable-graymap",
46
+ json: "application/json",
47
+ csv: "text/csv",
48
+ txt: "text/plain",
49
+ };
50
+
51
+ /**
52
+ * Whether a path matches an `accept` string, for desktop shells whose native drops and
53
+ * dialogs yield paths rather than `File`s. Extensions match the path's ending; MIME types and
54
+ * wildcards match through the extension of common image, JSON and text formats.
55
+ *
56
+ * @param path - A file path, with either separator.
57
+ * @param accept - The accept string; empty or absent accepts everything.
58
+ * @returns Whether the path is accepted.
59
+ */
60
+ export function acceptsPath(path: string, accept: string | undefined): boolean {
61
+ const name = path.split(/[\\/]/).pop() ?? path;
62
+ const dot = name.lastIndexOf(".");
63
+ const type = dot >= 0 ? (MIME_BY_EXTENSION[name.slice(dot + 1).toLowerCase()] ?? "") : "";
64
+ return acceptsFile({ name, type }, accept);
65
+ }
66
+
35
67
  /**
36
68
  * Split files by `accept`.
37
69
  *
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Stepping through an ordered set, as a pure function so its edge cases (an empty set, no
3
+ * current item, wrapping) are tested without a DOM.
4
+ */
5
+
6
+ /**
7
+ * The index one step from `index`.
8
+ *
9
+ * @param length - The number of items.
10
+ * @param index - The current index, or -1 for none.
11
+ * @param delta - +1 for next, -1 for previous.
12
+ * @param wrap - Wrap from the last item to the first and back.
13
+ * @returns The next index; the first (or, stepping back, the last) item when there is no
14
+ * current one; the same index at an end without `wrap`; `null` for an empty set.
15
+ */
16
+ export function stepIndex(length: number, index: number, delta: 1 | -1, wrap = false): number | null {
17
+ if (length <= 0) return null;
18
+ if (index < 0 || index >= length) return delta > 0 ? 0 : length - 1;
19
+ const next = index + delta;
20
+ if (wrap) return (next + length) % length;
21
+ return Math.min(length - 1, Math.max(0, next));
22
+ }
package/src/index.ts CHANGED
@@ -52,8 +52,15 @@ export {
52
52
  usePlayheadTimeline,
53
53
  } from "./components/usePlayback";
54
54
 
55
- export { FileDrop, type FileDropProps } from "./components/FileDrop";
56
- export { acceptsFile, collectDroppedFiles, type DropEntry } from "./components/dropFiles";
55
+ export { FileDrop, type FileDropProps, type PathSource } from "./components/FileDrop";
56
+ export { acceptsFile, acceptsPath, collectDroppedFiles, type DropEntry } from "./components/dropFiles";
57
+
58
+ export {
59
+ SequenceNavigator,
60
+ type SequenceItem,
61
+ type SequenceNavigatorProps,
62
+ } from "./components/SequenceNavigator";
63
+ export { stepIndex } from "./components/sequence";
57
64
 
58
65
  // Notifications moved to `@vitavision/ui` (a second app needed them). These are re-exports of
59
66
  // the same bindings, so there is still one default store; prefer importing from `@vitavision/ui`.