strata-react 1.3.16 → 1.3.18
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/README.md +2 -2
- package/UPGRADING.md +2 -0
- package/dist/components/box-overlay.d.ts +3 -1
- package/dist/components/boxed-picture.d.ts +34 -0
- package/dist/components/thumbnail.d.ts +6 -1
- package/dist/{errors-D0TsKLwJ.js → errors-jep2voNI.js} +42 -3
- package/dist/errors-jep2voNI.js.map +1 -0
- package/dist/{format-BNkXVwFx.js → field-t4Hoer0d.js} +327 -2
- package/dist/field-t4Hoer0d.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +89 -325
- package/dist/index.js.map +1 -1
- package/dist/lib/boxes.d.ts +23 -0
- package/dist/map.js +2 -3
- package/dist/map.js.map +1 -1
- package/dist/microphone-B5ITwm4G.js +697 -0
- package/dist/microphone-B5ITwm4G.js.map +1 -0
- package/dist/panorama.js +3 -2
- package/dist/panorama.js.map +1 -1
- package/dist/testing.js +4 -2
- package/dist/testing.js.map +1 -1
- package/dist/{theme-CFBAYaiu.js → theme-DXacprlw.js} +2 -2
- package/dist/{theme-CFBAYaiu.js.map → theme-DXacprlw.js.map} +1 -1
- package/dist/{value-controls-WRXbtUp6.js → value-controls-DB5l5Qe2.js} +4 -44
- package/dist/value-controls-DB5l5Qe2.js.map +1 -0
- package/package.json +1 -1
- package/dist/errors-D0TsKLwJ.js.map +0 -1
- package/dist/field-jfhLD5XX.js +0 -328
- package/dist/field-jfhLD5XX.js.map +0 -1
- package/dist/format-BNkXVwFx.js.map +0 -1
- package/dist/microphone-CZy0bNEx.js +0 -339
- package/dist/microphone-CZy0bNEx.js.map +0 -1
- package/dist/value-controls-WRXbtUp6.js.map +0 -1
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import { g as cn, m as format } from "./field-t4Hoer0d.js";
|
|
2
|
+
import { d as isDev, f as useInView, o as readErrorBody, r as describeHttpError, s as Button$1, t as HttpError } from "./errors-jep2voNI.js";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { Button, Text, UNSTABLE_Toast, UNSTABLE_ToastContent, UNSTABLE_ToastQueue, UNSTABLE_ToastRegion } from "react-aria-components";
|
|
5
|
+
import { CircleAlert, CircleCheck, History, ImageOff, Info, RotateCcw, TriangleAlert, X } from "lucide-react";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
|
|
8
|
+
// src/lib/overlay.ts
|
|
9
|
+
var overlaySurface = "rounded-overlay border border-border bg-popover text-popover-foreground shadow-overlay enter-overlay";
|
|
10
|
+
// src/query/poll.ts
|
|
11
|
+
/** The pause state and last-read time of a polled screen: pass it to `useLoad({ poll })` and to `PauseButton`. */
|
|
12
|
+
function usePoll({ every, paused: initiallyPaused = false }) {
|
|
13
|
+
const [paused, setPaused] = React.useState(initiallyPaused);
|
|
14
|
+
const [at, setAt] = React.useState(0);
|
|
15
|
+
const toggle = React.useCallback(() => setPaused((p) => !p), []);
|
|
16
|
+
const note = React.useCallback((dataUpdatedAt) => setAt((previous) => Math.max(previous, dataUpdatedAt)), []);
|
|
17
|
+
return React.useMemo(() => {
|
|
18
|
+
const poll = {
|
|
19
|
+
every,
|
|
20
|
+
paused,
|
|
21
|
+
toggle,
|
|
22
|
+
updatedAt: at ? new Date(at) : null,
|
|
23
|
+
note,
|
|
24
|
+
with: (override) => ({
|
|
25
|
+
...poll,
|
|
26
|
+
every: override.every,
|
|
27
|
+
with: (again) => poll.with(again)
|
|
28
|
+
})
|
|
29
|
+
};
|
|
30
|
+
return poll;
|
|
31
|
+
}, [
|
|
32
|
+
every,
|
|
33
|
+
paused,
|
|
34
|
+
toggle,
|
|
35
|
+
at,
|
|
36
|
+
note
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
/** Wires a query to its `Poll`: the time of each read, and one read when the poll is resumed. */
|
|
40
|
+
function usePollBinding(poll, query) {
|
|
41
|
+
const note = poll?.note;
|
|
42
|
+
React.useEffect(() => {
|
|
43
|
+
if (note && query.dataUpdatedAt) note(query.dataUpdatedAt);
|
|
44
|
+
}, [note, query.dataUpdatedAt]);
|
|
45
|
+
const paused = poll?.paused;
|
|
46
|
+
const wasPaused = React.useRef(paused);
|
|
47
|
+
React.useEffect(() => {
|
|
48
|
+
if (wasPaused.current && paused === false) query.refetch();
|
|
49
|
+
wasPaused.current = paused;
|
|
50
|
+
}, [paused]);
|
|
51
|
+
}
|
|
52
|
+
// src/query/index.tsx
|
|
53
|
+
/** A QueryClient with Strata's defaults: one retry, 30 s stale time, no refetch on window focus. */
|
|
54
|
+
function createQueryClient(overrides) {
|
|
55
|
+
return new QueryClient({
|
|
56
|
+
...overrides,
|
|
57
|
+
defaultOptions: {
|
|
58
|
+
...overrides?.defaultOptions,
|
|
59
|
+
queries: {
|
|
60
|
+
retry: 1,
|
|
61
|
+
staleTime: 3e4,
|
|
62
|
+
refetchOnWindowFocus: false,
|
|
63
|
+
...overrides?.defaultOptions?.queries
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Provides a query client (Strata's defaults unless one is passed) to the app. */
|
|
69
|
+
function StrataQueryProvider({ client, children }) {
|
|
70
|
+
const [fallback] = React.useState(() => client ?? createQueryClient());
|
|
71
|
+
return /* @__PURE__ */ jsx(QueryClientProvider, {
|
|
72
|
+
client: client ?? fallback,
|
|
73
|
+
children
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Turns a TanStack query result into the `Load<T>` that `DataView` takes; errors are described for a person by `describe`.
|
|
78
|
+
* A refetch that fails after data arrived stays `ready`, with `stale` saying why and when the data was read.
|
|
79
|
+
*/
|
|
80
|
+
function toLoad(query, describe = describeHttpError) {
|
|
81
|
+
if (query.status === "success") return {
|
|
82
|
+
status: "ready",
|
|
83
|
+
data: query.data
|
|
84
|
+
};
|
|
85
|
+
if (query.status === "error" && query.data !== void 0) return {
|
|
86
|
+
status: "ready",
|
|
87
|
+
data: query.data,
|
|
88
|
+
stale: staleOf(query, describe)
|
|
89
|
+
};
|
|
90
|
+
if (query.status === "error") return {
|
|
91
|
+
status: "error",
|
|
92
|
+
message: describe(query.error),
|
|
93
|
+
retry: () => void query.refetch()
|
|
94
|
+
};
|
|
95
|
+
return { status: "loading" };
|
|
96
|
+
}
|
|
97
|
+
/** The `Stale` of a query whose refetch failed after a good read. Shared by `toLoad` and `useInfiniteLoad`. */
|
|
98
|
+
function staleOf(query, describe = describeHttpError) {
|
|
99
|
+
return {
|
|
100
|
+
message: describe(query.error),
|
|
101
|
+
readAt: new Date(query.dataUpdatedAt),
|
|
102
|
+
retry: () => void query.refetch(),
|
|
103
|
+
retrying: query.isFetching
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** `useQuery`, returned as a `Load<T>` for `DataView`. With `poll` (from `usePoll`) it reads again every `poll.every` ms until the person pauses it. */
|
|
107
|
+
function useLoad(options) {
|
|
108
|
+
const { describeError, poll, ...rest } = options;
|
|
109
|
+
const query = useQuery(poll ? {
|
|
110
|
+
...rest,
|
|
111
|
+
refetchInterval: poll.paused ? false : poll.every
|
|
112
|
+
} : rest);
|
|
113
|
+
usePollBinding(poll, query);
|
|
114
|
+
return toLoad(query, describeError);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* fetch() that throws an HttpError on a non-2xx response and parses JSON: the queryFn default, and the usual commit for a write.
|
|
118
|
+
* `init.json` sends a JSON body without building the headers. A 204 or empty body resolves to undefined. An error response's body is read onto the HttpError (`body`, `reason`, `fieldErrors`).
|
|
119
|
+
*/
|
|
120
|
+
async function fetchJson(input, init, { readError = readErrorBody } = {}) {
|
|
121
|
+
const { json, ...rest } = init ?? {};
|
|
122
|
+
const headers = new Headers(rest.headers);
|
|
123
|
+
if (!headers.has("Accept")) headers.set("Accept", "application/json");
|
|
124
|
+
if (json !== void 0) {
|
|
125
|
+
if (!headers.has("Content-Type")) headers.set("Content-Type", "application/json");
|
|
126
|
+
rest.body = JSON.stringify(json);
|
|
127
|
+
rest.method ??= "POST";
|
|
128
|
+
}
|
|
129
|
+
const res = await fetch(input, {
|
|
130
|
+
...rest,
|
|
131
|
+
headers
|
|
132
|
+
});
|
|
133
|
+
if (!res.ok) {
|
|
134
|
+
const raw = await res.text().catch(() => "");
|
|
135
|
+
let body = raw || void 0;
|
|
136
|
+
try {
|
|
137
|
+
if (raw) body = JSON.parse(raw);
|
|
138
|
+
} catch {}
|
|
139
|
+
throw new HttpError(res.status, res.statusText, String(input), body, readError(body));
|
|
140
|
+
}
|
|
141
|
+
const text = res.status === 204 ? "" : await res.text();
|
|
142
|
+
return text ? JSON.parse(text) : void 0;
|
|
143
|
+
}
|
|
144
|
+
// src/components/data-view.tsx
|
|
145
|
+
/** The default failed-read panel: the reason on the danger tint, with Try again. Its parent carries `role="alert"`. */
|
|
146
|
+
function LoadError({ message, retry, className }) {
|
|
147
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
148
|
+
className: cn("flex flex-wrap items-start gap-3 rounded-surface bg-danger-subtle p-4 text-danger", className),
|
|
149
|
+
children: [
|
|
150
|
+
/* @__PURE__ */ jsx(CircleAlert, {
|
|
151
|
+
"aria-hidden": true,
|
|
152
|
+
className: "mt-0.5 size-5 shrink-0"
|
|
153
|
+
}),
|
|
154
|
+
/* @__PURE__ */ jsx("p", {
|
|
155
|
+
className: "min-w-0 flex-1 type-body",
|
|
156
|
+
children: message
|
|
157
|
+
}),
|
|
158
|
+
/* @__PURE__ */ jsxs(Button$1, {
|
|
159
|
+
size: "sm",
|
|
160
|
+
onClick: retry,
|
|
161
|
+
children: [/* @__PURE__ */ jsx(RotateCcw, { "aria-hidden": true }), "Try again"]
|
|
162
|
+
})
|
|
163
|
+
]
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
var defaultError = (failure) => /* @__PURE__ */ jsx(LoadError, { ...failure });
|
|
167
|
+
/** Every view of remote data: renders `skeleton` while loading, an alert with Try again on error, `empty` when `isEmpty`, else `children(data)`, under a `StaleNotice` when a refresh failed. The skeleton and empty state are required. */
|
|
168
|
+
function DataView({ state, isEmpty, skeleton, empty, children, staleNotice = (stale) => /* @__PURE__ */ jsx(StaleNotice, { stale }), error = defaultError, className }) {
|
|
169
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
170
|
+
className: cn("min-w-0", className),
|
|
171
|
+
"aria-busy": state.status === "loading" || void 0,
|
|
172
|
+
children: [
|
|
173
|
+
state.status === "loading" && skeleton,
|
|
174
|
+
state.status === "error" && /* @__PURE__ */ jsx("div", {
|
|
175
|
+
role: "alert",
|
|
176
|
+
children: error({
|
|
177
|
+
message: state.message,
|
|
178
|
+
retry: state.retry
|
|
179
|
+
})
|
|
180
|
+
}),
|
|
181
|
+
state.status === "ready" && /* @__PURE__ */ jsxs(Fragment, { children: [state.stale && staleNotice(state.stale), isEmpty(state.data) ? empty : children(state.data)] })
|
|
182
|
+
]
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/** When `readAt` was, for a person: the clock time today, else the weekday and time. */
|
|
186
|
+
function readTime(readAt, now = /* @__PURE__ */ new Date()) {
|
|
187
|
+
return readAt.toDateString() === now.toDateString() ? format.clock.format(readAt) : format.shortDay.format(readAt);
|
|
188
|
+
}
|
|
189
|
+
/** The notice over data whose refresh failed: what happened, how old the data shown is, and Try again. A polite status, not an alert: nothing was lost. */
|
|
190
|
+
function StaleNotice({ stale, shown, className }) {
|
|
191
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
192
|
+
role: "status",
|
|
193
|
+
className: cn("mb-4 flex flex-wrap items-start gap-3 rounded-surface bg-warning-subtle p-3 text-warning", className),
|
|
194
|
+
children: [
|
|
195
|
+
/* @__PURE__ */ jsx(History, {
|
|
196
|
+
"aria-hidden": true,
|
|
197
|
+
className: "mt-0.5 size-4 shrink-0"
|
|
198
|
+
}),
|
|
199
|
+
/* @__PURE__ */ jsxs("p", {
|
|
200
|
+
className: "min-w-0 flex-1 type-body-sm",
|
|
201
|
+
children: [
|
|
202
|
+
stale.message,
|
|
203
|
+
" ",
|
|
204
|
+
shown ? shown(readTime(stale.readAt)) : `Showing data from ${readTime(stale.readAt)}.`
|
|
205
|
+
]
|
|
206
|
+
}),
|
|
207
|
+
/* @__PURE__ */ jsxs(Button$1, {
|
|
208
|
+
size: "sm",
|
|
209
|
+
onClick: stale.retry,
|
|
210
|
+
disabled: stale.retrying,
|
|
211
|
+
children: [/* @__PURE__ */ jsx(RotateCcw, { "aria-hidden": true }), stale.retrying ? "Trying again…" : "Try again"]
|
|
212
|
+
})
|
|
213
|
+
]
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/** An empty state that teaches the next action: a title, a sentence and an action. No illustration slot, on purpose. */
|
|
217
|
+
function EmptyState({ title, children, action }) {
|
|
218
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
219
|
+
className: "grid justify-items-start gap-2 border-y border-border py-6",
|
|
220
|
+
children: [
|
|
221
|
+
/* @__PURE__ */ jsx("p", {
|
|
222
|
+
className: "type-heading",
|
|
223
|
+
children: title
|
|
224
|
+
}),
|
|
225
|
+
children && /* @__PURE__ */ jsx("p", {
|
|
226
|
+
className: "max-w-prose type-body text-muted-foreground",
|
|
227
|
+
children
|
|
228
|
+
}),
|
|
229
|
+
action && /* @__PURE__ */ jsx("div", {
|
|
230
|
+
className: "mt-2",
|
|
231
|
+
children: action
|
|
232
|
+
})
|
|
233
|
+
]
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/** A placeholder block for a loading layout. Size it with layout classes so it mirrors the final content. */
|
|
237
|
+
function Skeleton({ className }) {
|
|
238
|
+
return /* @__PURE__ */ jsx("div", {
|
|
239
|
+
"aria-hidden": true,
|
|
240
|
+
className: cn("rounded-control bg-muted motion-safe:animate-pulse", className)
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
// src/components/toast.tsx
|
|
244
|
+
var queue = new UNSTABLE_ToastQueue({ maxVisibleToasts: 3 });
|
|
245
|
+
/** Milliseconds a toast stays: 5 s plus 40 ms a character, to at most 15 s, and at least 8 s with an action. */
|
|
246
|
+
function toastTimeout(content) {
|
|
247
|
+
if (content.timeout !== void 0) return content.timeout;
|
|
248
|
+
const chars = content.title.length + (content.description?.length ?? 0) + (content.action?.label.length ?? 0);
|
|
249
|
+
return Math.max(content.action ? 8e3 : 0, Math.min(15e3, 5e3 + 40 * chars));
|
|
250
|
+
}
|
|
251
|
+
/** The toast queue: `toast.show({ title })` from anywhere; `toast.close(key)`; `toast.clear()`. */
|
|
252
|
+
var toast = {
|
|
253
|
+
/** Shows a toast and returns its key. */
|
|
254
|
+
show(content) {
|
|
255
|
+
if (mounted.length === 0 && !warnedNoRegion && isDev()) {
|
|
256
|
+
warnedNoRegion = true;
|
|
257
|
+
console.warn("strata-react: toast.show() was called but no StrataToastRegion is mounted, so nothing will appear. Render <StrataToastRegion /> once at the app root.");
|
|
258
|
+
}
|
|
259
|
+
return queue.add(content, { timeout: toastTimeout(content) });
|
|
260
|
+
},
|
|
261
|
+
close(key) {
|
|
262
|
+
queue.close(key);
|
|
263
|
+
},
|
|
264
|
+
clear() {
|
|
265
|
+
queue.clear();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
var marks = {
|
|
269
|
+
info: {
|
|
270
|
+
icon: Info,
|
|
271
|
+
color: "text-info"
|
|
272
|
+
},
|
|
273
|
+
success: {
|
|
274
|
+
icon: CircleCheck,
|
|
275
|
+
color: "text-success"
|
|
276
|
+
},
|
|
277
|
+
warning: {
|
|
278
|
+
icon: TriangleAlert,
|
|
279
|
+
color: "text-warning"
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
var mounted = [];
|
|
283
|
+
var warnedNoRegion = false;
|
|
284
|
+
var watchers = /* @__PURE__ */ new Set();
|
|
285
|
+
var changed = () => watchers.forEach((w) => w());
|
|
286
|
+
var watch = (w) => {
|
|
287
|
+
watchers.add(w);
|
|
288
|
+
return () => void watchers.delete(w);
|
|
289
|
+
};
|
|
290
|
+
/** True while this region is the one that draws. The server and the first client render say yes, so hydration matches. */
|
|
291
|
+
function useOwnsQueue() {
|
|
292
|
+
const id = React.useId();
|
|
293
|
+
React.useLayoutEffect(() => {
|
|
294
|
+
mounted.push(id);
|
|
295
|
+
changed();
|
|
296
|
+
return () => {
|
|
297
|
+
mounted.splice(mounted.indexOf(id), 1);
|
|
298
|
+
changed();
|
|
299
|
+
};
|
|
300
|
+
}, [id]);
|
|
301
|
+
const owns = React.useSyncExternalStore(watch, () => mounted[0] === id, () => true);
|
|
302
|
+
const [warned, setWarned] = React.useState(false);
|
|
303
|
+
React.useEffect(() => {
|
|
304
|
+
if (owns || warned || mounted.indexOf(id) < 1) return;
|
|
305
|
+
setWarned(true);
|
|
306
|
+
if (isDev()) console.warn("strata-react: a StrataToastRegion is already mounted, so this one draws nothing. Render it once at the app root; in a test of a route tree that draws its own, pass `toasts: false` to renderRoutes.");
|
|
307
|
+
}, [
|
|
308
|
+
owns,
|
|
309
|
+
warned,
|
|
310
|
+
id
|
|
311
|
+
]);
|
|
312
|
+
return owns;
|
|
313
|
+
}
|
|
314
|
+
/** The one place toasts appear. Render it once at the app root; a second one mounted at the same time draws nothing and warns in development. A labelled landmark (F6), polite, above panes and the undo bar. */
|
|
315
|
+
function StrataToastRegion({ label = "Notifications", className }) {
|
|
316
|
+
if (!useOwnsQueue()) return null;
|
|
317
|
+
return /* @__PURE__ */ jsx(UNSTABLE_ToastRegion, {
|
|
318
|
+
queue,
|
|
319
|
+
"aria-label": label,
|
|
320
|
+
className: cn("layer-bar fixed inset-x-4 top-4 grid justify-items-end gap-2 outline-none sm:inset-x-auto sm:top-auto sm:right-4 sm:bottom-4", className),
|
|
321
|
+
children: ({ toast: t }) => /* @__PURE__ */ jsxs(UNSTABLE_Toast, {
|
|
322
|
+
toast: t,
|
|
323
|
+
className: cn("flex w-full max-w-sm items-start gap-3 p-3 outline-none focus-visible:outline-2 focus-visible:outline-ring", overlaySurface),
|
|
324
|
+
children: [
|
|
325
|
+
(() => {
|
|
326
|
+
const { icon: Icon, color } = marks[t.content.tone ?? "info"];
|
|
327
|
+
return /* @__PURE__ */ jsx(Icon, {
|
|
328
|
+
"aria-hidden": true,
|
|
329
|
+
className: cn("mt-0.5 size-4 shrink-0", color)
|
|
330
|
+
});
|
|
331
|
+
})(),
|
|
332
|
+
/* @__PURE__ */ jsxs(UNSTABLE_ToastContent, {
|
|
333
|
+
className: "grid min-w-0 flex-1 gap-0.5",
|
|
334
|
+
children: [/* @__PURE__ */ jsx(Text, {
|
|
335
|
+
slot: "title",
|
|
336
|
+
className: "type-label",
|
|
337
|
+
children: t.content.title
|
|
338
|
+
}), t.content.description && /* @__PURE__ */ jsx(Text, {
|
|
339
|
+
slot: "description",
|
|
340
|
+
className: "type-body-sm text-muted-foreground",
|
|
341
|
+
children: t.content.description
|
|
342
|
+
})]
|
|
343
|
+
}),
|
|
344
|
+
t.content.action && /* @__PURE__ */ jsx(Button$1, {
|
|
345
|
+
size: "sm",
|
|
346
|
+
onPress: () => {
|
|
347
|
+
t.content.action?.onPress();
|
|
348
|
+
queue.close(t.key);
|
|
349
|
+
},
|
|
350
|
+
children: t.content.action.label
|
|
351
|
+
}),
|
|
352
|
+
/* @__PURE__ */ jsx(Button, {
|
|
353
|
+
slot: "close",
|
|
354
|
+
"aria-label": "Dismiss",
|
|
355
|
+
className: "grid size-6 shrink-0 cursor-pointer place-items-center rounded-control text-muted-foreground transition-state hover:bg-muted hover:text-foreground",
|
|
356
|
+
children: /* @__PURE__ */ jsx(X, {
|
|
357
|
+
"aria-hidden": true,
|
|
358
|
+
className: "size-4"
|
|
359
|
+
})
|
|
360
|
+
})
|
|
361
|
+
]
|
|
362
|
+
})
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
// src/lib/viewport.ts
|
|
366
|
+
/** The picture fitted in its frame, not panned. */
|
|
367
|
+
var FIT = {
|
|
368
|
+
scale: 1,
|
|
369
|
+
x: 0,
|
|
370
|
+
y: 0
|
|
371
|
+
};
|
|
372
|
+
/** The least zoom: the whole picture fitted. */
|
|
373
|
+
var MIN_SCALE = 1;
|
|
374
|
+
/** The most zoom, 32 times the fit. */
|
|
375
|
+
var MAX_SCALE = 32;
|
|
376
|
+
/** Keeps the picture from being dragged out of its frame: at most half the extra width or height past each edge. */
|
|
377
|
+
function clampViewport(v, frame) {
|
|
378
|
+
const scale = Math.min(32, Math.max(1, v.scale));
|
|
379
|
+
if (scale === 1) return FIT;
|
|
380
|
+
const mx = (scale - 1) * frame.width / 2;
|
|
381
|
+
const my = (scale - 1) * frame.height / 2;
|
|
382
|
+
return {
|
|
383
|
+
scale,
|
|
384
|
+
x: Math.min(mx, Math.max(-mx, v.x)),
|
|
385
|
+
y: Math.min(my, Math.max(-my, v.y))
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/** A new scale with the point under `at` (pixels from the frame's centre) staying under it. */
|
|
389
|
+
function zoomAt(v, nextScale, at, frame) {
|
|
390
|
+
const scale = Math.min(32, Math.max(1, nextScale));
|
|
391
|
+
const k = scale / v.scale;
|
|
392
|
+
return clampViewport({
|
|
393
|
+
scale,
|
|
394
|
+
x: at.x - k * (at.x - v.x),
|
|
395
|
+
y: at.y - k * (at.y - v.y)
|
|
396
|
+
}, frame);
|
|
397
|
+
}
|
|
398
|
+
/** The scale at which the picture's own pixels are drawn one to one: its natural width over the width it is fitted at. */
|
|
399
|
+
function actualPixelsScale(naturalWidth, fittedWidth) {
|
|
400
|
+
if (!naturalWidth || !fittedWidth) return 1;
|
|
401
|
+
return Math.min(32, Math.max(1, naturalWidth / fittedWidth));
|
|
402
|
+
}
|
|
403
|
+
/** The CSS transform for a viewport. */
|
|
404
|
+
function viewportTransform(v) {
|
|
405
|
+
return `translate(${v.x}px, ${v.y}px) scale(${v.scale})`;
|
|
406
|
+
}
|
|
407
|
+
/** A region as shares of the frame, so it lands on the same part of every candidate whatever its pixel size. */
|
|
408
|
+
function regionShares(region) {
|
|
409
|
+
const [x, y, w, h] = region.box;
|
|
410
|
+
if (!region.of) return [
|
|
411
|
+
x,
|
|
412
|
+
y,
|
|
413
|
+
w,
|
|
414
|
+
h
|
|
415
|
+
];
|
|
416
|
+
const [ow, oh] = region.of;
|
|
417
|
+
return [
|
|
418
|
+
x / ow,
|
|
419
|
+
y / oh,
|
|
420
|
+
w / ow,
|
|
421
|
+
h / oh
|
|
422
|
+
];
|
|
423
|
+
}
|
|
424
|
+
/** The viewport that fills the frame with a box (as much of it as fits, centred): the scale that fits the box, the offset that puts its centre at the frame's centre. */
|
|
425
|
+
function boxViewport(box, frame) {
|
|
426
|
+
const [x, y, w, h] = box;
|
|
427
|
+
const scale = Math.min(32, Math.max(1, Math.min(1 / Math.max(w, 1e-6), 1 / Math.max(h, 1e-6))));
|
|
428
|
+
return clampViewport({
|
|
429
|
+
scale,
|
|
430
|
+
x: -scale * (x + w / 2 - .5) * frame.width,
|
|
431
|
+
y: -scale * (y + h / 2 - .5) * frame.height
|
|
432
|
+
}, frame);
|
|
433
|
+
}
|
|
434
|
+
/** A rectangle drawn on the frame (pixels from its top left) as shares of the picture under the viewport now. */
|
|
435
|
+
function frameBoxToShares(rect, v, frame) {
|
|
436
|
+
const toShare = (p, offset, size) => size ? (p - size / 2 - offset) / v.scale / size + .5 : 0;
|
|
437
|
+
const clamp = (n) => Math.min(1, Math.max(0, n));
|
|
438
|
+
const xa = clamp(toShare(Math.min(rect.x0, rect.x1), v.x, frame.width));
|
|
439
|
+
const xb = clamp(toShare(Math.max(rect.x0, rect.x1), v.x, frame.width));
|
|
440
|
+
const ya = clamp(toShare(Math.min(rect.y0, rect.y1), v.y, frame.height));
|
|
441
|
+
const yb = clamp(toShare(Math.max(rect.y0, rect.y1), v.y, frame.height));
|
|
442
|
+
return [
|
|
443
|
+
xa,
|
|
444
|
+
ya,
|
|
445
|
+
xb - xa,
|
|
446
|
+
yb - ya
|
|
447
|
+
];
|
|
448
|
+
}
|
|
449
|
+
// src/lib/boxes.ts
|
|
450
|
+
/**
|
|
451
|
+
* The boxes in reading order: top to bottom in rows, left to right within a row. A box joins the
|
|
452
|
+
* row above when its centre falls inside that row's first box vertically, so lines of text that
|
|
453
|
+
* wobble a few pixels still read in order and two columns read row by row, not column by column.
|
|
454
|
+
* Returns ids, so a caller keeps its own objects.
|
|
455
|
+
*/
|
|
456
|
+
function readingOrder(boxes) {
|
|
457
|
+
const byTop = [...boxes].sort((a, b) => a.y + a.h / 2 - (b.y + b.h / 2) || a.x - b.x);
|
|
458
|
+
const rows = [];
|
|
459
|
+
for (const box of byTop) {
|
|
460
|
+
const row = rows[rows.length - 1];
|
|
461
|
+
const centre = box.y + box.h / 2;
|
|
462
|
+
if (row && centre >= row[0].y && centre <= row[0].y + row[0].h) row.push(box);
|
|
463
|
+
else rows.push([box]);
|
|
464
|
+
}
|
|
465
|
+
return rows.flatMap((row) => row.sort((a, b) => a.x - b.x).map((b) => b.id));
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* The viewport that fills a frame with one box of the picture. The picture sits in the frame the way
|
|
469
|
+
* `object-fit: contain` puts it (a frame of another shape leaves bars), which is also how
|
|
470
|
+
* `BoxOverlay` draws it, so the same numbers agree. `margin` is the share of the box added on every
|
|
471
|
+
* side so the box is not touching the frame (default 0.25).
|
|
472
|
+
*/
|
|
473
|
+
function zoomToBox(box, of, frame, margin = .25) {
|
|
474
|
+
const s = Math.min(frame.width / of.w, frame.height / of.h);
|
|
475
|
+
const ox = (frame.width - of.w * s) / 2;
|
|
476
|
+
const oy = (frame.height - of.h * s) / 2;
|
|
477
|
+
const mx = box.w * margin;
|
|
478
|
+
const my = box.h * margin;
|
|
479
|
+
return boxViewport([
|
|
480
|
+
(ox + (box.x - mx) * s) / frame.width,
|
|
481
|
+
(oy + (box.y - my) * s) / frame.height,
|
|
482
|
+
(box.w + 2 * mx) * s / frame.width,
|
|
483
|
+
(box.h + 2 * my) * s / frame.height
|
|
484
|
+
], frame);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Where the whole picture goes so that one box of it fills a frame of `aspect` (width over height), as
|
|
488
|
+
* percentages of the frame: the crop is the box grown `pad` times about its centre, then widened or
|
|
489
|
+
* heightened to the frame's shape, so the box is never squashed and stays in the middle. A crop that
|
|
490
|
+
* runs past the picture's edge shows the frame's own ground there, as a face at the edge of a photo
|
|
491
|
+
* is shown by a server crop that pads. No canvas and no second request: the browser draws the one
|
|
492
|
+
* picture, which a list of faces from the same photo shares from its cache.
|
|
493
|
+
*/
|
|
494
|
+
function regionStyle({ of, box, pad = 1.6 }, aspect) {
|
|
495
|
+
let cw = Math.max(box.w * pad, 1);
|
|
496
|
+
let ch = Math.max(box.h * pad, 1);
|
|
497
|
+
if (cw / ch < aspect) cw = ch * aspect;
|
|
498
|
+
else ch = cw / aspect;
|
|
499
|
+
const x0 = box.x + box.w / 2 - cw / 2;
|
|
500
|
+
const y0 = box.y + box.h / 2 - ch / 2;
|
|
501
|
+
const pct = (n) => `${+(n * 100).toFixed(4) || 0}%`;
|
|
502
|
+
return {
|
|
503
|
+
left: pct(-x0 / cw),
|
|
504
|
+
top: pct(-y0 / ch),
|
|
505
|
+
width: pct(of.w / cw),
|
|
506
|
+
height: pct(of.h / ch)
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
// src/components/thumbnail.tsx
|
|
510
|
+
var loadedBefore = /* @__PURE__ */ new Map();
|
|
511
|
+
var LOADED_LIMIT = 2e3;
|
|
512
|
+
function remember(key, index) {
|
|
513
|
+
loadedBefore.delete(key);
|
|
514
|
+
loadedBefore.set(key, index);
|
|
515
|
+
if (loadedBefore.size > LOADED_LIMIT) loadedBefore.delete(loadedBefore.keys().next().value);
|
|
516
|
+
}
|
|
517
|
+
/** Forgets which pictures `Thumbnail` has loaded, so a test that checks the loading state starts from nothing. `renderWithStrata` and `renderRoutes` call it. */
|
|
518
|
+
function forgetLoadedThumbnails() {
|
|
519
|
+
loadedBefore.clear();
|
|
520
|
+
}
|
|
521
|
+
var ratioNumber = (aspect) => {
|
|
522
|
+
if (typeof aspect === "number") return aspect;
|
|
523
|
+
const [w, h] = aspect.split("/").map(Number);
|
|
524
|
+
return w / h;
|
|
525
|
+
};
|
|
526
|
+
var ratio = (aspect) => typeof aspect === "number" ? String(aspect) : aspect.replace("/", " / ");
|
|
527
|
+
/** A box of fixed aspect ratio for a remote image: a skeleton until it loads, the `missing` words (or a `fallback`) if it cannot, a request only when it nears the screen, and `children` overlaid in its corners. `alt` and `missing` or `fallback` are required. */
|
|
528
|
+
function Thumbnail({ src, alt, missing, aspect = 1, fit = "cover", srcSet, sizes, eager = false, rootMargin = "200px", className, fallback, onLoad, onMissing, region, children }) {
|
|
529
|
+
const sources = React.useMemo(() => src === null ? [] : typeof src === "string" ? src ? [src] : [] : src.filter(Boolean), [src]);
|
|
530
|
+
const key = sources.length ? `${sources.join("\n")} ${srcSet ?? ""}` : null;
|
|
531
|
+
const [outcome, setOutcome] = React.useState(null);
|
|
532
|
+
const before = key === null ? void 0 : loadedBefore.get(key);
|
|
533
|
+
const mine = key !== null && outcome?.key === key ? outcome : null;
|
|
534
|
+
const index = mine?.index ?? before ?? 0;
|
|
535
|
+
const phase = key === null ? "missing" : mine?.done ?? "loading";
|
|
536
|
+
const current = sources[index];
|
|
537
|
+
const near = useInView({
|
|
538
|
+
active: !eager && sources.length > 0,
|
|
539
|
+
rootMargin,
|
|
540
|
+
once: true,
|
|
541
|
+
unsupported: true
|
|
542
|
+
});
|
|
543
|
+
const requested = eager || near.inView || before !== void 0;
|
|
544
|
+
const fail = () => setOutcome(index + 1 < sources.length ? {
|
|
545
|
+
key,
|
|
546
|
+
index: index + 1,
|
|
547
|
+
done: null
|
|
548
|
+
} : {
|
|
549
|
+
key,
|
|
550
|
+
index,
|
|
551
|
+
done: "missing"
|
|
552
|
+
});
|
|
553
|
+
const told = React.useRef(null);
|
|
554
|
+
const tell = React.useRef(onMissing);
|
|
555
|
+
tell.current = onMissing;
|
|
556
|
+
React.useEffect(() => {
|
|
557
|
+
if (phase !== "missing") told.current = null;
|
|
558
|
+
else if (key !== null && told.current !== key) {
|
|
559
|
+
told.current = key;
|
|
560
|
+
tell.current?.();
|
|
561
|
+
}
|
|
562
|
+
}, [key, phase]);
|
|
563
|
+
const name = [alt, missing].filter(Boolean).join(": ");
|
|
564
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
565
|
+
ref: near.ref,
|
|
566
|
+
className: cn("relative overflow-hidden rounded-control bg-muted", className),
|
|
567
|
+
style: { aspectRatio: ratio(aspect) },
|
|
568
|
+
"aria-busy": phase === "loading" || void 0,
|
|
569
|
+
children: [
|
|
570
|
+
phase === "loading" && /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0" }),
|
|
571
|
+
key !== null && phase !== "missing" && requested && /* @__PURE__ */ jsx("img", {
|
|
572
|
+
ref: (el) => {
|
|
573
|
+
if (el && phase === "loading" && before !== void 0 && el.complete && el.naturalWidth > 0) setOutcome({
|
|
574
|
+
key,
|
|
575
|
+
index,
|
|
576
|
+
done: "loaded"
|
|
577
|
+
});
|
|
578
|
+
},
|
|
579
|
+
src: current,
|
|
580
|
+
srcSet: index === 0 ? srcSet : void 0,
|
|
581
|
+
sizes,
|
|
582
|
+
alt,
|
|
583
|
+
decoding: "async",
|
|
584
|
+
onLoad: (e) => onLoad?.(e.currentTarget, index) === false ? fail() : (remember(key, index), setOutcome({
|
|
585
|
+
key,
|
|
586
|
+
index,
|
|
587
|
+
done: "loaded"
|
|
588
|
+
})),
|
|
589
|
+
onError: fail,
|
|
590
|
+
style: region ? regionStyle(region, ratioNumber(aspect)) : void 0,
|
|
591
|
+
className: cn("absolute", region ? "max-w-none" : ["inset-0 size-full", fit === "cover" ? "object-cover" : "object-contain"], phase === "loading" && "opacity-0")
|
|
592
|
+
}, `${key}#${index}`),
|
|
593
|
+
phase === "missing" && /* @__PURE__ */ jsx("div", {
|
|
594
|
+
...name ? {
|
|
595
|
+
role: "img",
|
|
596
|
+
"aria-label": name
|
|
597
|
+
} : { "aria-hidden": true },
|
|
598
|
+
className: cn("absolute inset-0 grid", fallback === void 0 && "content-center justify-items-center gap-0.5 p-1 text-center text-muted-foreground"),
|
|
599
|
+
children: fallback === void 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ImageOff, {
|
|
600
|
+
"aria-hidden": true,
|
|
601
|
+
className: "size-4 shrink-0"
|
|
602
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
603
|
+
className: "type-caption text-balance",
|
|
604
|
+
children: missing
|
|
605
|
+
})] }) : /* @__PURE__ */ jsx("div", {
|
|
606
|
+
"aria-hidden": true,
|
|
607
|
+
className: "grid size-full min-h-0 min-w-0",
|
|
608
|
+
children: fallback
|
|
609
|
+
})
|
|
610
|
+
}),
|
|
611
|
+
children
|
|
612
|
+
]
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
/** The loading shape of a `Thumbnail`, for a `DataView` skeleton: the same box and aspect ratio, so nothing moves when the list arrives. */
|
|
616
|
+
function ThumbnailSkeleton({ aspect = 1, className }) {
|
|
617
|
+
return /* @__PURE__ */ jsx("div", {
|
|
618
|
+
"aria-hidden": true,
|
|
619
|
+
className: cn("relative overflow-hidden rounded-control bg-muted", className),
|
|
620
|
+
style: { aspectRatio: ratio(aspect) },
|
|
621
|
+
children: /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0" })
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
// src/lib/microphone.ts
|
|
625
|
+
/** A microphone that would not open, with a sentence a person can act on. */
|
|
626
|
+
var MicrophoneError = class extends Error {};
|
|
627
|
+
/** The sentence for what getUserMedia threw. */
|
|
628
|
+
function explainMicrophoneError(e) {
|
|
629
|
+
const name = e instanceof DOMException ? e.name : "";
|
|
630
|
+
if (name === "NotAllowedError") return new MicrophoneError("The browser was not allowed to use the microphone. Allow it from the address bar, then try again.");
|
|
631
|
+
if (name === "NotFoundError") return new MicrophoneError("No microphone was found. Plug one in, then try again.");
|
|
632
|
+
if (name === "NotReadableError") return new MicrophoneError("The microphone is in use by another program. Close it, then try again.");
|
|
633
|
+
return new MicrophoneError(`The microphone could not be opened (${e instanceof DOMException || e instanceof Error ? e.message : String(e)}).`);
|
|
634
|
+
}
|
|
635
|
+
var WORKLET = `
|
|
636
|
+
class StrataTap extends AudioWorkletProcessor {
|
|
637
|
+
constructor() { super(); this.buf = new Float32Array(2048); this.n = 0 }
|
|
638
|
+
process(inputs) {
|
|
639
|
+
const ch = inputs[0] && inputs[0][0]
|
|
640
|
+
if (ch) for (let i = 0; i < ch.length; i++) {
|
|
641
|
+
this.buf[this.n++] = ch[i]
|
|
642
|
+
if (this.n === this.buf.length) { this.port.postMessage(this.buf.slice()); this.n = 0 }
|
|
643
|
+
}
|
|
644
|
+
return true
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
registerProcessor('strata-tap', StrataTap)
|
|
648
|
+
`;
|
|
649
|
+
/** The browser's own microphone, with its processing off. */
|
|
650
|
+
var browserMicrophone = { async open(onSamples) {
|
|
651
|
+
if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia) throw new MicrophoneError("The microphone needs a secure page: open the app at http://localhost or over https.");
|
|
652
|
+
let media;
|
|
653
|
+
try {
|
|
654
|
+
media = await navigator.mediaDevices.getUserMedia({ audio: {
|
|
655
|
+
channelCount: 1,
|
|
656
|
+
echoCancellation: false,
|
|
657
|
+
noiseSuppression: false,
|
|
658
|
+
autoGainControl: false
|
|
659
|
+
} });
|
|
660
|
+
} catch (e) {
|
|
661
|
+
throw explainMicrophoneError(e);
|
|
662
|
+
}
|
|
663
|
+
let context;
|
|
664
|
+
const close = () => {
|
|
665
|
+
for (const t of media.getTracks()) t.stop();
|
|
666
|
+
context?.close();
|
|
667
|
+
};
|
|
668
|
+
try {
|
|
669
|
+
context = new AudioContext();
|
|
670
|
+
const url = URL.createObjectURL(new Blob([WORKLET], { type: "text/javascript" }));
|
|
671
|
+
try {
|
|
672
|
+
await context.audioWorklet.addModule(url);
|
|
673
|
+
} finally {
|
|
674
|
+
URL.revokeObjectURL(url);
|
|
675
|
+
}
|
|
676
|
+
const tap = new AudioWorkletNode(context, "strata-tap");
|
|
677
|
+
tap.port.onmessage = (event) => onSamples(event.data);
|
|
678
|
+
const mute = context.createGain();
|
|
679
|
+
mute.gain.value = 0;
|
|
680
|
+
context.createMediaStreamSource(media).connect(tap).connect(mute).connect(context.destination);
|
|
681
|
+
return {
|
|
682
|
+
sampleRate: context.sampleRate,
|
|
683
|
+
device: media.getAudioTracks()[0]?.label || null,
|
|
684
|
+
close
|
|
685
|
+
};
|
|
686
|
+
} catch (e) {
|
|
687
|
+
close();
|
|
688
|
+
throw explainMicrophoneError(e);
|
|
689
|
+
}
|
|
690
|
+
} };
|
|
691
|
+
/** The microphone screens record from: the browser's, unless a provider gives another (a fake in tests and stories). */
|
|
692
|
+
var MicrophoneContext = React.createContext(browserMicrophone);
|
|
693
|
+
/** The microphone from the nearest `MicrophoneContext`. */
|
|
694
|
+
var useMicrophone = () => React.useContext(MicrophoneContext);
|
|
695
|
+
export { StrataQueryProvider as A, toast as C, LoadError as D, EmptyState as E, useLoad as F, usePoll as I, overlaySurface as L, fetchJson as M, staleOf as N, Skeleton as O, toLoad as P, StrataToastRegion as S, DataView as T, clampViewport as _, useMicrophone as a, viewportTransform as b, forgetLoadedThumbnails as c, zoomToBox as d, FIT as f, boxViewport as g, actualPixelsScale as h, explainMicrophoneError as i, createQueryClient as j, StaleNotice as k, readingOrder as l, MIN_SCALE as m, MicrophoneError as n, Thumbnail as o, MAX_SCALE as p, browserMicrophone as r, ThumbnailSkeleton as s, MicrophoneContext as t, regionStyle as u, frameBoxToShares as v, toastTimeout as w, zoomAt as x, regionShares as y };
|
|
696
|
+
|
|
697
|
+
//# sourceMappingURL=microphone-B5ITwm4G.js.map
|