@slotform/react-ui 0.0.0-stage → 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 +21 -0
- package/README.md +44 -2
- package/dist/density.d.ts +4 -0
- package/dist/density.d.ts.map +1 -0
- package/dist/density.js +8 -0
- package/dist/density.js.map +1 -0
- package/dist/index.d.ts +400 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +388 -0
- package/dist/index.js.map +1 -0
- package/dist/node/canvas.d.ts +3 -0
- package/dist/node/canvas.d.ts.map +1 -0
- package/dist/node/canvas.js +13 -0
- package/dist/node/canvas.js.map +1 -0
- package/dist/node/codegen.d.ts +6 -0
- package/dist/node/codegen.d.ts.map +1 -0
- package/dist/node/codegen.js +70 -0
- package/dist/node/codegen.js.map +1 -0
- package/dist/node/css-escape.d.ts +8 -0
- package/dist/node/css-escape.d.ts.map +1 -0
- package/dist/node/css-escape.js +12 -0
- package/dist/node/css-escape.js.map +1 -0
- package/dist/node/fonts.d.ts +6 -0
- package/dist/node/fonts.d.ts.map +1 -0
- package/dist/node/fonts.js +219 -0
- package/dist/node/fonts.js.map +1 -0
- package/dist/node/lint.d.ts +3 -0
- package/dist/node/lint.d.ts.map +1 -0
- package/dist/node/lint.js +23 -0
- package/dist/node/lint.js.map +1 -0
- package/dist/node/mode.d.ts +9 -0
- package/dist/node/mode.d.ts.map +1 -0
- package/dist/node/mode.js +38 -0
- package/dist/node/mode.js.map +1 -0
- package/dist/node/theme.d.ts +13 -0
- package/dist/node/theme.d.ts.map +1 -0
- package/dist/node/theme.js +160 -0
- package/dist/node/theme.js.map +1 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +80 -0
- package/dist/types.js.map +1 -0
- package/guide/acts.md +61 -0
- package/guide/audio.md +77 -0
- package/guide/blocks.md +82 -0
- package/guide/canvas-choosing.md +34 -0
- package/guide/canvas-editing.md +36 -0
- package/guide/canvas-layout.md +46 -0
- package/guide/canvas-opening.md +47 -0
- package/guide/canvas-overview.md +24 -0
- package/guide/canvas-states.md +37 -0
- package/guide/canvas-view.md +46 -0
- package/guide/canvas.md +94 -0
- package/guide/containers.md +60 -0
- package/guide/content.md +40 -0
- package/guide/controls.md +89 -0
- package/guide/density.md +48 -0
- package/guide/dock.md +42 -0
- package/guide/drag.md +38 -0
- package/guide/figures.md +25 -0
- package/guide/forms.md +82 -0
- package/guide/pane-bar.md +11 -0
- package/guide/pane.md +50 -0
- package/guide/reference.md +74 -0
- package/guide/rules.md +109 -0
- package/guide/section.md +29 -0
- package/guide/shell.md +26 -0
- package/guide/shots.md +37 -0
- package/guide/sidebar.md +30 -0
- package/guide/table.md +119 -0
- package/lint/no-arbitrary-value.grit +9 -0
- package/lint/no-class-on-component.grit +12 -0
- package/lint/no-density.grit +8 -0
- package/lint/no-host-look.grit +38 -0
- package/lint/no-img.grit +10 -0
- package/lint/no-navigate-act.grit +22 -0
- package/lint/no-raw-style.grit +8 -0
- package/package.json +150 -3
- package/src/density.ts +9 -0
- package/src/index.ts +503 -0
- package/src/node/canvas.ts +14 -0
- package/src/node/codegen.ts +100 -0
- package/src/node/css-escape.ts +27 -0
- package/src/node/fonts.ts +260 -0
- package/src/node/lint.ts +25 -0
- package/src/node/mode.ts +64 -0
- package/src/node/theme.ts +232 -0
- package/src/types.ts +120 -0
- package/src/ui/atoms/avatar/base.tsx +42 -0
- package/src/ui/atoms/button/glyph.tsx +244 -0
- package/src/ui/atoms/button/link.tsx +81 -0
- package/src/ui/atoms/button/look.ts +32 -0
- package/src/ui/atoms/button/reason.tsx +69 -0
- package/src/ui/atoms/chip/base.tsx +85 -0
- package/src/ui/atoms/count/base.tsx +27 -0
- package/src/ui/atoms/icon/index.tsx +51 -0
- package/src/ui/atoms/message/waiting.tsx +19 -0
- package/src/ui/atoms/prose/painted.tsx +35 -0
- package/src/ui/atoms/row/badges.tsx +226 -0
- package/src/ui/atoms/row/leading.tsx +166 -0
- package/src/ui/atoms/row/lock.tsx +17 -0
- package/src/ui/atoms/row/marks.tsx +79 -0
- package/src/ui/atoms/section/card.tsx +27 -0
- package/src/ui/atoms/spinner/index.tsx +42 -0
- package/src/ui/atoms/status/base.tsx +51 -0
- package/src/ui/atoms/status/change.tsx +29 -0
- package/src/ui/atoms/status/dot.tsx +45 -0
- package/src/ui/atoms/switch/checkbox.tsx +121 -0
- package/src/ui/atoms/turn.tsx +28 -0
- package/src/ui/components/action-bar/base.tsx +276 -0
- package/src/ui/components/action-bar/index.tsx +20 -0
- package/src/ui/components/avatar/index.tsx +21 -0
- package/src/ui/components/banner/index.tsx +207 -0
- package/src/ui/components/banner/pending.tsx +109 -0
- package/src/ui/components/bar-chart/index.tsx +701 -0
- package/src/ui/components/button/base.tsx +214 -0
- package/src/ui/components/button/face.tsx +84 -0
- package/src/ui/components/button/index.tsx +206 -0
- package/src/ui/components/canvas/connection.tsx +26 -0
- package/src/ui/components/canvas/edges.tsx +116 -0
- package/src/ui/components/canvas/elk.ts +260 -0
- package/src/ui/components/canvas/floor.ts +173 -0
- package/src/ui/components/canvas/geometry.ts +772 -0
- package/src/ui/components/canvas/ground.tsx +97 -0
- package/src/ui/components/canvas/group.tsx +156 -0
- package/src/ui/components/canvas/hit.ts +40 -0
- package/src/ui/components/canvas/index.tsx +497 -0
- package/src/ui/components/canvas/label.tsx +53 -0
- package/src/ui/components/canvas/layout.ts +543 -0
- package/src/ui/components/canvas/lift.ts +272 -0
- package/src/ui/components/canvas/look.ts +129 -0
- package/src/ui/components/canvas/node.tsx +689 -0
- package/src/ui/components/canvas/open.ts +243 -0
- package/src/ui/components/canvas/opening.ts +240 -0
- package/src/ui/components/canvas/view.ts +621 -0
- package/src/ui/components/canvas/viewport.ts +417 -0
- package/src/ui/components/canvas/zoom.tsx +113 -0
- package/src/ui/components/card/index.tsx +20 -0
- package/src/ui/components/chip/index.tsx +24 -0
- package/src/ui/components/code/frame.ts +2 -0
- package/src/ui/components/code/index.tsx +234 -0
- package/src/ui/components/columns/index.tsx +51 -0
- package/src/ui/components/count/index.tsx +17 -0
- package/src/ui/components/diff/index.tsx +249 -0
- package/src/ui/components/diff/word.tsx +104 -0
- package/src/ui/components/dock/index.tsx +262 -0
- package/src/ui/components/file-row/index.tsx +213 -0
- package/src/ui/components/form/base.tsx +280 -0
- package/src/ui/components/form/derive.tsx +269 -0
- package/src/ui/components/form/index.tsx +49 -0
- package/src/ui/components/form/saved.tsx +39 -0
- package/src/ui/components/form-field/index.tsx +658 -0
- package/src/ui/components/gallery/index.tsx +83 -0
- package/src/ui/components/gate/index.tsx +183 -0
- package/src/ui/components/image/base.tsx +245 -0
- package/src/ui/components/image/index.tsx +58 -0
- package/src/ui/components/input/base.tsx +246 -0
- package/src/ui/components/input/code.tsx +108 -0
- package/src/ui/components/input/drop.ts +37 -0
- package/src/ui/components/input/file.tsx +154 -0
- package/src/ui/components/input/index.tsx +132 -0
- package/src/ui/components/input/prose.tsx +107 -0
- package/src/ui/components/input/refusal.tsx +30 -0
- package/src/ui/components/item-header/base.tsx +410 -0
- package/src/ui/components/menu/base.tsx +319 -0
- package/src/ui/components/message/attachments.tsx +101 -0
- package/src/ui/components/message/base.tsx +266 -0
- package/src/ui/components/message-input/index.tsx +377 -0
- package/src/ui/components/missing/base.tsx +45 -0
- package/src/ui/components/pane/head.tsx +399 -0
- package/src/ui/components/pane/index.tsx +325 -0
- package/src/ui/components/picker/base.tsx +1551 -0
- package/src/ui/components/picker/index.tsx +224 -0
- package/src/ui/components/picker/list.tsx +423 -0
- package/src/ui/components/picker/segments.tsx +183 -0
- package/src/ui/components/prose/index.tsx +259 -0
- package/src/ui/components/qr-code/index.tsx +65 -0
- package/src/ui/components/row/acts.tsx +126 -0
- package/src/ui/components/row/fact.tsx +54 -0
- package/src/ui/components/row/index.tsx +1370 -0
- package/src/ui/components/row/progress.tsx +38 -0
- package/src/ui/components/row/status.tsx +54 -0
- package/src/ui/components/row/title.ts +7 -0
- package/src/ui/components/rules/index.tsx +290 -0
- package/src/ui/components/section/band.tsx +39 -0
- package/src/ui/components/section/head.tsx +267 -0
- package/src/ui/components/section/index.tsx +409 -0
- package/src/ui/components/section/rows.tsx +356 -0
- package/src/ui/components/sheet/base.tsx +272 -0
- package/src/ui/components/sheet/confirm.tsx +79 -0
- package/src/ui/components/sheet/docked.tsx +218 -0
- package/src/ui/components/sheet/form.tsx +76 -0
- package/src/ui/components/sheet/head.tsx +120 -0
- package/src/ui/components/sheet/index.tsx +45 -0
- package/src/ui/components/sheet/layer.tsx +168 -0
- package/src/ui/components/sheet/outlet.tsx +58 -0
- package/src/ui/components/sheet/task.tsx +41 -0
- package/src/ui/components/shell/cut.tsx +116 -0
- package/src/ui/components/shell/drag.tsx +45 -0
- package/src/ui/components/shell/host.tsx +71 -0
- package/src/ui/components/shell/index.tsx +737 -0
- package/src/ui/components/shell/panes.tsx +76 -0
- package/src/ui/components/shell/preview.ts +230 -0
- package/src/ui/components/shell/rail.tsx +163 -0
- package/src/ui/components/shell/row.ts +11 -0
- package/src/ui/components/shell/switcher.tsx +143 -0
- package/src/ui/components/shell/tree.tsx +252 -0
- package/src/ui/components/slider/base.tsx +139 -0
- package/src/ui/components/slider/index.tsx +57 -0
- package/src/ui/components/stages/index.tsx +198 -0
- package/src/ui/components/stats/count-links.tsx +47 -0
- package/src/ui/components/stats/index.tsx +203 -0
- package/src/ui/components/stats/meter.tsx +213 -0
- package/src/ui/components/stats/parts.ts +10 -0
- package/src/ui/components/step-count/index.tsx +45 -0
- package/src/ui/components/switch/base.tsx +86 -0
- package/src/ui/components/switch/index.tsx +38 -0
- package/src/ui/components/table/columns.tsx +131 -0
- package/src/ui/components/table/index.tsx +2215 -0
- package/src/ui/components/text/index.tsx +37 -0
- package/src/ui/components/thread/index.tsx +503 -0
- package/src/ui/components/thread/latest.tsx +59 -0
- package/src/ui/components/toast/index.tsx +113 -0
- package/src/ui/components/toast/layer.tsx +26 -0
- package/src/ui/components/toolbar/filters.tsx +161 -0
- package/src/ui/components/toolbar/index.tsx +125 -0
- package/src/ui/components/waveform/index.tsx +205 -0
- package/src/ui/globals.css +427 -0
- package/src/ui/lib/act.ts +60 -0
- package/src/ui/lib/age.ts +49 -0
- package/src/ui/lib/bound.ts +16 -0
- package/src/ui/lib/canvas-layout.ts +11 -0
- package/src/ui/lib/caret.ts +9 -0
- package/src/ui/lib/chip-lines.ts +3 -0
- package/src/ui/lib/closed.ts +8 -0
- package/src/ui/lib/code.tsx +184 -0
- package/src/ui/lib/columns.ts +39 -0
- package/src/ui/lib/commit.ts +24 -0
- package/src/ui/lib/container.ts +112 -0
- package/src/ui/lib/copy.ts +29 -0
- package/src/ui/lib/drag.ts +575 -0
- package/src/ui/lib/empty.tsx +85 -0
- package/src/ui/lib/field.ts +39 -0
- package/src/ui/lib/focus.ts +175 -0
- package/src/ui/lib/folds.ts +67 -0
- package/src/ui/lib/follow.ts +220 -0
- package/src/ui/lib/form.ts +19 -0
- package/src/ui/lib/guards.ts +38 -0
- package/src/ui/lib/heading.ts +14 -0
- package/src/ui/lib/ink.ts +26 -0
- package/src/ui/lib/lang.ts +7 -0
- package/src/ui/lib/leave.ts +14 -0
- package/src/ui/lib/lockup.tsx +145 -0
- package/src/ui/lib/measure.ts +22 -0
- package/src/ui/lib/media.ts +63 -0
- package/src/ui/lib/modality.ts +69 -0
- package/src/ui/lib/moment.ts +13 -0
- package/src/ui/lib/more.tsx +76 -0
- package/src/ui/lib/navigate.ts +210 -0
- package/src/ui/lib/noop.ts +3 -0
- package/src/ui/lib/not-found.tsx +40 -0
- package/src/ui/lib/paint.tsx +55 -0
- package/src/ui/lib/parts-line.tsx +54 -0
- package/src/ui/lib/picked.ts +18 -0
- package/src/ui/lib/player-store.ts +38 -0
- package/src/ui/lib/player.ts +139 -0
- package/src/ui/lib/portal.ts +12 -0
- package/src/ui/lib/read.tsx +269 -0
- package/src/ui/lib/region.tsx +49 -0
- package/src/ui/lib/reorder.ts +154 -0
- package/src/ui/lib/rest.ts +16 -0
- package/src/ui/lib/saved.ts +29 -0
- package/src/ui/lib/scramble.tsx +147 -0
- package/src/ui/lib/scrolls.ts +70 -0
- package/src/ui/lib/style.ts +6 -0
- package/src/ui/lib/swap.tsx +107 -0
- package/src/ui/lib/toast.ts +39 -0
- package/src/ui/lib/walk-classes.ts +16 -0
- package/src/ui/lib/window.tsx +376 -0
- package/src/ui/worker.d.ts +4 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import { type PaintRole, paintText, scrambled } from "@slotform/ui-core/paint";
|
|
3
|
+
import { PULSE, paint } from "@slotform/ui-core/variants";
|
|
4
|
+
import { ScrambledGlyphs } from "./scramble.tsx";
|
|
5
|
+
|
|
6
|
+
// One breath for the page: docs/architecture/ui-core/reads.md, "One breath".
|
|
7
|
+
if (typeof document !== "undefined") {
|
|
8
|
+
document.addEventListener(
|
|
9
|
+
"animationstart",
|
|
10
|
+
(event) => {
|
|
11
|
+
if (event.animationName !== "pulse-fill") return;
|
|
12
|
+
const target = event.target;
|
|
13
|
+
if (!(target instanceof Element)) return;
|
|
14
|
+
for (const one of target.getAnimations()) {
|
|
15
|
+
if (one instanceof CSSAnimation && one.animationName === "pulse-fill") {
|
|
16
|
+
one.startTime = 0;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
true,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// A paint fades in after `--wait-delay`: docs/architecture/ui-core/reads.md, "Timing".
|
|
25
|
+
const GLYPHS = cn(
|
|
26
|
+
paint({ kind: "glyphs" }),
|
|
27
|
+
"box-decoration-clone select-none",
|
|
28
|
+
PULSE,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
/** The paint of a part's own box (a mark, a block): its fill, content not drawn. */
|
|
32
|
+
export const PAINT_BOX = cn(paint({ kind: "box" }), PULSE);
|
|
33
|
+
|
|
34
|
+
/** The paint of a control: its own box filled, its content invisible; the control is also `inert`. */
|
|
35
|
+
export const PAINT_CONTROL = cn(paint({ kind: "control" }), PULSE);
|
|
36
|
+
|
|
37
|
+
/** The paint of a control whose children stand in its box (a field's text, a pick's options, a slider's track): the control's paint, each child kept in place and not drawn. */
|
|
38
|
+
export const PAINT_CONTROL_HIDDEN = cn(PAINT_CONTROL, "*:invisible");
|
|
39
|
+
|
|
40
|
+
/** A text region's paint, set inside the region's own text element (no wrapper box): each line box a bar, or, where the theme scrambles the region's kind (`theme.loading`) and the region is one line, the scramble over the same box. It clips where a loaded text ellipsizes (`globals.css`). */
|
|
41
|
+
export function Painted({ role, at = 0 }: { role: PaintRole; at?: number }) {
|
|
42
|
+
if (scrambled(role)) return <ScrambledGlyphs role={role} at={at} />;
|
|
43
|
+
return (
|
|
44
|
+
<span
|
|
45
|
+
aria-hidden
|
|
46
|
+
data-painted=""
|
|
47
|
+
// A figure's leading is under `normal`: its bar stands at the region's own line
|
|
48
|
+
// (`globals.css`), so the waiting figure is no taller than the loaded one.
|
|
49
|
+
data-flush={role === "figure" ? "" : undefined}
|
|
50
|
+
className={GLYPHS}
|
|
51
|
+
>
|
|
52
|
+
{paintText(role, at)}
|
|
53
|
+
</span>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import { META_TAIL } from "@slotform/ui-core/runs";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
// A text line of parts a middle dot apart: docs/architecture/plugins/react-ui.md, "Text".
|
|
6
|
+
|
|
7
|
+
/** Parts a middle dot apart as one text. The parts are the line's children, so the caller draws the line's box and `className` styles each part (the text role). Outside the package's exports. */
|
|
8
|
+
export function PartsLine(props: {
|
|
9
|
+
parts: readonly ReactNode[];
|
|
10
|
+
className?: string;
|
|
11
|
+
}) {
|
|
12
|
+
const { parts, className } = props;
|
|
13
|
+
return parts.map((part, at) => (
|
|
14
|
+
<span key={at} className={cn(className)}>
|
|
15
|
+
{at > 0 ? META_TAIL : null}
|
|
16
|
+
{part}
|
|
17
|
+
</span>
|
|
18
|
+
));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// A trail leading a line: a flex row whose trail is cut at its start, so the
|
|
22
|
+
// nearest place stands, and whose parts after it are cut at their end. The
|
|
23
|
+
// trail's box runs right to left, which puts the ellipsis at its start, and
|
|
24
|
+
// its text is isolated left to right, so the places keep their order. The dot
|
|
25
|
+
// between the two is a flex item of its own, its spaces kept.
|
|
26
|
+
const LINE = "flex min-w-0";
|
|
27
|
+
const TRAIL = "min-w-0 truncate [direction:rtl]";
|
|
28
|
+
const TRAIL_TEXT = "[direction:ltr] [unicode-bidi:isolate]";
|
|
29
|
+
const TRAIL_DOT = "shrink-0 whitespace-pre";
|
|
30
|
+
const REST = "min-w-0 truncate";
|
|
31
|
+
|
|
32
|
+
/** A line led by a trail: its labels a middle dot apart, cut at the start with an ellipsis so the last stands, then the `children` (more parts) after a middle dot, cut at their end. `className` is the line's text role. Outside the package's exports. */
|
|
33
|
+
export function TrailLine(props: {
|
|
34
|
+
trail: readonly string[];
|
|
35
|
+
className?: string;
|
|
36
|
+
children?: ReactNode;
|
|
37
|
+
}) {
|
|
38
|
+
const { trail, className, children } = props;
|
|
39
|
+
return (
|
|
40
|
+
<p className={cn(LINE, className)}>
|
|
41
|
+
<span className={TRAIL}>
|
|
42
|
+
<span className={TRAIL_TEXT}>
|
|
43
|
+
<PartsLine parts={trail} />
|
|
44
|
+
</span>
|
|
45
|
+
</span>
|
|
46
|
+
{children ? (
|
|
47
|
+
<>
|
|
48
|
+
<span className={TRAIL_DOT}>{META_TAIL}</span>
|
|
49
|
+
<span className={REST}>{children}</span>
|
|
50
|
+
</>
|
|
51
|
+
) : null}
|
|
52
|
+
</p>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PickedFile } from "@slotform/ui-core/descriptors";
|
|
2
|
+
|
|
3
|
+
export function pickedFrom(file: File): PickedFile {
|
|
4
|
+
return {
|
|
5
|
+
name: file.name,
|
|
6
|
+
size: file.size,
|
|
7
|
+
type: file.type,
|
|
8
|
+
blob: () => Promise.resolve(file),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// A file attached to a message and its `src`: docs/architecture/plugins/react-ui.md, "A picked file's `src`".
|
|
13
|
+
export function attachedFrom(file: File): PickedFile {
|
|
14
|
+
return {
|
|
15
|
+
...pickedFrom(file),
|
|
16
|
+
...(file.type.startsWith("image/") && { src: URL.createObjectURL(file) }),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createPlayer, type PlayerHost } from "@slotform/ui-core/player";
|
|
2
|
+
import { saveChoice, savedChoice } from "./saved.ts";
|
|
3
|
+
|
|
4
|
+
const VOLUME_KEY = "slotform.player.volume";
|
|
5
|
+
|
|
6
|
+
// The web's side of the store (`createPlayer`, in ui-core): a viewer's volume is
|
|
7
|
+
// theirs from one visit to the next, a convenience, so a store that is blocked or
|
|
8
|
+
// empty falls back to full; the clock follows the animation frame.
|
|
9
|
+
const host: PlayerHost = {
|
|
10
|
+
recall() {
|
|
11
|
+
try {
|
|
12
|
+
const saved: unknown = JSON.parse(savedChoice(VOLUME_KEY) ?? "null");
|
|
13
|
+
if (
|
|
14
|
+
typeof saved === "object" &&
|
|
15
|
+
saved !== null &&
|
|
16
|
+
"volume" in saved &&
|
|
17
|
+
typeof saved.volume === "number" &&
|
|
18
|
+
"muted" in saved &&
|
|
19
|
+
typeof saved.muted === "boolean"
|
|
20
|
+
)
|
|
21
|
+
return {
|
|
22
|
+
volume: Math.min(1, Math.max(0, saved.volume)),
|
|
23
|
+
muted: saved.muted,
|
|
24
|
+
};
|
|
25
|
+
} catch {}
|
|
26
|
+
return undefined;
|
|
27
|
+
},
|
|
28
|
+
keep(volume, muted) {
|
|
29
|
+
saveChoice(VOLUME_KEY, JSON.stringify({ volume, muted }));
|
|
30
|
+
},
|
|
31
|
+
frame(run) {
|
|
32
|
+
const id = requestAnimationFrame(run);
|
|
33
|
+
return () => cancelAnimationFrame(id);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** The page's one player store: `lib/player` gives the app its commands and hooks, and the `Dock` hands it the cover it draws. Outside the package's exports. */
|
|
38
|
+
export const store = createPlayer(host);
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { Peaks, Track } from "@slotform/ui-core/descriptors";
|
|
2
|
+
import {
|
|
3
|
+
type Engine,
|
|
4
|
+
type Output,
|
|
5
|
+
type PlayerCommands,
|
|
6
|
+
type PlayerReport,
|
|
7
|
+
type PlayerState,
|
|
8
|
+
type PlayerTime,
|
|
9
|
+
type PlayerView,
|
|
10
|
+
type QueueProvider,
|
|
11
|
+
type TrackQueue,
|
|
12
|
+
type WindowQueueOptions,
|
|
13
|
+
windowQueue,
|
|
14
|
+
} from "@slotform/ui-core/player";
|
|
15
|
+
import { useSyncExternalStore } from "react";
|
|
16
|
+
import { store } from "./player-store.ts";
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
Engine,
|
|
20
|
+
Output,
|
|
21
|
+
Peaks,
|
|
22
|
+
PlayerCommands,
|
|
23
|
+
PlayerReport,
|
|
24
|
+
PlayerState,
|
|
25
|
+
PlayerTime,
|
|
26
|
+
PlayerView,
|
|
27
|
+
QueueProvider,
|
|
28
|
+
Track,
|
|
29
|
+
TrackQueue,
|
|
30
|
+
WindowQueueOptions,
|
|
31
|
+
};
|
|
32
|
+
export { windowQueue };
|
|
33
|
+
|
|
34
|
+
/** The player, from anywhere in the app (a handler, a loader): `player.play({ src, title, line })`. It plays through the output `connectOutput` was given, and a `Dock` in the `Shell`'s `foot` draws it; the store is the page's, so the sound goes on through navigation. */
|
|
35
|
+
export const player: PlayerCommands = store.player;
|
|
36
|
+
|
|
37
|
+
/** Passes the player its sound: `connectOutput(audioOutput())` once, before the app renders. The output is the app's, not the `Shell`'s, so a screen that plays without a `Dock` takes the same one. */
|
|
38
|
+
export const connectOutput: (engine: Engine) => void = store.connect;
|
|
39
|
+
|
|
40
|
+
/** The browser's audio element as the player's sound: one `Audio`, made when the first track loads and kept, so the sound goes on through navigation. */
|
|
41
|
+
export function audioOutput(): Engine {
|
|
42
|
+
return (report) => {
|
|
43
|
+
const audio = new Audio();
|
|
44
|
+
audio.preload = "metadata";
|
|
45
|
+
audio.addEventListener("timeupdate", report.time);
|
|
46
|
+
audio.addEventListener("durationchange", report.time);
|
|
47
|
+
audio.addEventListener("loadedmetadata", report.metadata);
|
|
48
|
+
audio.addEventListener("play", () => report.state(true));
|
|
49
|
+
audio.addEventListener("pause", () => {
|
|
50
|
+
// A pause while the file loads is the element reloading, not a pause.
|
|
51
|
+
if (audio.readyState > 0) report.state(false);
|
|
52
|
+
});
|
|
53
|
+
audio.addEventListener("volumechange", report.volume);
|
|
54
|
+
audio.addEventListener("ended", report.ended);
|
|
55
|
+
audio.addEventListener("error", report.failed);
|
|
56
|
+
// The system's media controls (none in a browser without them) show the
|
|
57
|
+
// loaded track, its name, its line and its picture once the store hands
|
|
58
|
+
// it over, and act on the sound as the phone's lock screen does; previous
|
|
59
|
+
// and next ask the store, which holds the queue.
|
|
60
|
+
const session =
|
|
61
|
+
"mediaSession" in navigator ? navigator.mediaSession : undefined;
|
|
62
|
+
let shown: Track | undefined;
|
|
63
|
+
const describe = (artwork: string | undefined) => {
|
|
64
|
+
if (!session) return;
|
|
65
|
+
session.metadata = shown
|
|
66
|
+
? new MediaMetadata({
|
|
67
|
+
title: shown.title,
|
|
68
|
+
artist: shown.line ?? "",
|
|
69
|
+
artwork: artwork ? [{ src: artwork }] : [],
|
|
70
|
+
})
|
|
71
|
+
: null;
|
|
72
|
+
};
|
|
73
|
+
const steps = (previous: boolean, next: boolean) => {
|
|
74
|
+
session?.setActionHandler(
|
|
75
|
+
"previoustrack",
|
|
76
|
+
previous ? () => report.step("previous") : null,
|
|
77
|
+
);
|
|
78
|
+
session?.setActionHandler(
|
|
79
|
+
"nexttrack",
|
|
80
|
+
next ? () => report.step("next") : null,
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
const output: Output = {
|
|
84
|
+
load(track) {
|
|
85
|
+
shown = track;
|
|
86
|
+
describe(undefined);
|
|
87
|
+
if (track) audio.src = track.src;
|
|
88
|
+
else {
|
|
89
|
+
audio.pause();
|
|
90
|
+
audio.removeAttribute("src");
|
|
91
|
+
audio.load();
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
// The browser holds back sound that no press asked for: the track waits
|
|
95
|
+
// paused.
|
|
96
|
+
play() {
|
|
97
|
+
audio.play().catch((error: unknown) => {
|
|
98
|
+
const name = error instanceof DOMException ? error.name : "";
|
|
99
|
+
if (name === "NotAllowedError") report.blocked();
|
|
100
|
+
// An `AbortError` is a pause or a new source ending the pending play,
|
|
101
|
+
// which the store asked for: no failure.
|
|
102
|
+
else if (name !== "AbortError") report.failed();
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
pause: () => audio.pause(),
|
|
106
|
+
time: () => audio.currentTime,
|
|
107
|
+
length: () =>
|
|
108
|
+
Number.isFinite(audio.duration) ? audio.duration : undefined,
|
|
109
|
+
ready: () => audio.readyState > 0,
|
|
110
|
+
seek(to) {
|
|
111
|
+
audio.currentTime = to;
|
|
112
|
+
},
|
|
113
|
+
paused: () => audio.paused,
|
|
114
|
+
level: () => ({ volume: audio.volume, muted: audio.muted }),
|
|
115
|
+
apply(volume, muted) {
|
|
116
|
+
audio.volume = volume;
|
|
117
|
+
audio.muted = muted;
|
|
118
|
+
},
|
|
119
|
+
cover: describe,
|
|
120
|
+
steps,
|
|
121
|
+
};
|
|
122
|
+
session?.setActionHandler("play", () => output.play());
|
|
123
|
+
session?.setActionHandler("pause", () => output.pause());
|
|
124
|
+
session?.setActionHandler("seekto", (event) => {
|
|
125
|
+
if (event.seekTime !== undefined) output.seek(event.seekTime);
|
|
126
|
+
});
|
|
127
|
+
return output;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** What the player holds and its commands, drawing again when the track, play state, volume or queue change (not each second: `usePlayerTime` is the clock). */
|
|
132
|
+
export function usePlayer(): PlayerView {
|
|
133
|
+
return useSyncExternalStore(store.subscribe, store.view, store.view);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Where the loaded track stands, drawing again as it moves (on each frame while it plays). */
|
|
137
|
+
export function usePlayerTime(): PlayerTime {
|
|
138
|
+
return useSyncExternalStore(store.subscribeClock, store.clock, store.clock);
|
|
139
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import { NOOP } from "./noop.ts";
|
|
3
|
+
|
|
4
|
+
// Where a popup mounts: docs/architecture/plugins/react-ui.md, "Popups".
|
|
5
|
+
export const PortalContainer = createContext<HTMLElement | null | undefined>(
|
|
6
|
+
undefined,
|
|
7
|
+
);
|
|
8
|
+
|
|
9
|
+
// How the container's `main` hands its portal element to the container host, which
|
|
10
|
+
// names it for every popup under it.
|
|
11
|
+
export const PortalRegion =
|
|
12
|
+
createContext<(node: HTMLElement | null) => void>(NOOP);
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import {
|
|
3
|
+
type Frame,
|
|
4
|
+
FrameHolds,
|
|
5
|
+
useSelections,
|
|
6
|
+
} from "@slotform/ui-core/react/read";
|
|
7
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
8
|
+
import { type Datum, type Drawn, isEnded, lift } from "@slotform/ui-core/read";
|
|
9
|
+
import { ROW_RETRY, row } from "@slotform/ui-core/variants";
|
|
10
|
+
import type { ReactNode } from "react";
|
|
11
|
+
import { IconButtonBase } from "../atoms/button/glyph.tsx";
|
|
12
|
+
import { Icon } from "../atoms/icon/index.tsx";
|
|
13
|
+
import { Spinner } from "../atoms/spinner/index.tsx";
|
|
14
|
+
import { ButtonBase } from "../components/button/base.tsx";
|
|
15
|
+
import { MissingBase } from "../components/missing/base.tsx";
|
|
16
|
+
import { Text } from "../components/text/index.tsx";
|
|
17
|
+
import type { Act } from "./act.ts";
|
|
18
|
+
import { wiringOf } from "./act.ts";
|
|
19
|
+
import { type Empty, EmptyBase, type EmptyFit } from "./empty.tsx";
|
|
20
|
+
|
|
21
|
+
/** The `data-still` a frame's own box carries while its held read has no data and nothing is coming (not asked for, or paused): its painted shape stands, and nothing breathes or scrambles in it. */
|
|
22
|
+
export const stillOf = (frame: Frame): "" | undefined =>
|
|
23
|
+
frame.holds && frame.shape === "still" ? "" : undefined;
|
|
24
|
+
|
|
25
|
+
// A compact control's failed marks stand whole beside it.
|
|
26
|
+
const ENDED_MARKS = "flex shrink-0 items-center";
|
|
27
|
+
const FAILED_MARK = "inline-flex shrink-0 items-center text-danger";
|
|
28
|
+
const RETRY_MARK =
|
|
29
|
+
"relative flex shrink-0 items-center justify-center text-ink-meta";
|
|
30
|
+
// The retry glyph gives way to the spinner after `--act-delay`, as an act's
|
|
31
|
+
// glyph does (`animate-act-out`, `animate-act-in`): a retry the server
|
|
32
|
+
// answers at once never flashes a spinner.
|
|
33
|
+
const RETRY_GLYPH = "opacity-0 animate-act-out";
|
|
34
|
+
const RETRY_SPIN =
|
|
35
|
+
"absolute inset-0 flex items-center justify-center animate-act-in";
|
|
36
|
+
|
|
37
|
+
/** What a failed row draws in its value region: the alert, in the danger ink. With a `label` it is named by it (a mark that stands where a sentence would). */
|
|
38
|
+
export function FailedMark({ label }: { label?: string }) {
|
|
39
|
+
const mark = <Icon name="CircleAlert" />;
|
|
40
|
+
return label === undefined ? (
|
|
41
|
+
<span className={FAILED_MARK}>{mark}</span>
|
|
42
|
+
) : (
|
|
43
|
+
<span role="img" aria-label={label} className={FAILED_MARK}>
|
|
44
|
+
{mark}
|
|
45
|
+
</span>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** What a failed row draws at its end, where a chevron stands: the retry icon, in its own square, a spinner in its place once the retry has run `--act-delay`. Pressing the row retries. */
|
|
50
|
+
export function RetryMark({ busy }: { busy: boolean }) {
|
|
51
|
+
return (
|
|
52
|
+
<span className={cn(ROW_RETRY, RETRY_MARK)}>
|
|
53
|
+
{busy ? (
|
|
54
|
+
<>
|
|
55
|
+
<span className={RETRY_GLYPH}>
|
|
56
|
+
<Icon name="RotateCw" />
|
|
57
|
+
</span>
|
|
58
|
+
<span className={RETRY_SPIN}>
|
|
59
|
+
<Spinner />
|
|
60
|
+
</span>
|
|
61
|
+
</>
|
|
62
|
+
) : (
|
|
63
|
+
<Icon name="RotateCw" />
|
|
64
|
+
)}
|
|
65
|
+
</span>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// The empty shape. Its sentence may be a read of its own (a selection of what
|
|
70
|
+
// the frame holds), subscribed here and only here: a frame whose read failed has
|
|
71
|
+
// no such observer, which would mount on the failed read again each time the
|
|
72
|
+
// body changes shape and retry it, so the failed shape would never settle. The
|
|
73
|
+
// generic sentence stands until the selection lands.
|
|
74
|
+
function EmptyShape(props: {
|
|
75
|
+
empty?: { sentence?: Datum<string>; act?: Empty["act"] };
|
|
76
|
+
fit: EmptyFit;
|
|
77
|
+
fill?: boolean;
|
|
78
|
+
}) {
|
|
79
|
+
const { empty, fit, fill } = props;
|
|
80
|
+
const words = useWords();
|
|
81
|
+
const [said] = useSelections([
|
|
82
|
+
lift<{ sentence?: string }, "sentence">(empty, ["sentence"]),
|
|
83
|
+
] as const);
|
|
84
|
+
const sentence = empty
|
|
85
|
+
? said.painted
|
|
86
|
+
? words.readEmpty
|
|
87
|
+
: said.value?.sentence
|
|
88
|
+
: words.readEmpty;
|
|
89
|
+
return (
|
|
90
|
+
<EmptyBase
|
|
91
|
+
fit={fit}
|
|
92
|
+
tone="rest"
|
|
93
|
+
fill={fill}
|
|
94
|
+
sentence={sentence}
|
|
95
|
+
act={empty?.act}
|
|
96
|
+
/>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** What a frame draws in place of its body, or its body: the failed, missing and empty shapes; ready, waiting and idle are the loaded tree, its leaves painting. */
|
|
101
|
+
export function FrameBody(props: {
|
|
102
|
+
frame: Frame;
|
|
103
|
+
// What the frame says when its read is empty: the default sentence, none (`sentence: undefined` with an act), or its own.
|
|
104
|
+
empty?: { sentence?: Datum<string>; act?: Empty["act"] };
|
|
105
|
+
// Where the shapes stand: inside the card the frame draws (`part`), or on a
|
|
106
|
+
// card of their own, the frame drawing none.
|
|
107
|
+
fit?: EmptyFit;
|
|
108
|
+
// The shapes fill the box they stand in (a canvas's ground).
|
|
109
|
+
fill?: boolean;
|
|
110
|
+
// The kind of record a missing read answers for, wording its sentence (a Pane's).
|
|
111
|
+
noun?: string;
|
|
112
|
+
// The sentence a missing read says, in place of the noun's (a Pane's).
|
|
113
|
+
missing?: string;
|
|
114
|
+
// The failed shape where the title never lands: docs/architecture/ui-core/reads.md, "Missing and failed shapes".
|
|
115
|
+
headed?: boolean;
|
|
116
|
+
children?: ReactNode;
|
|
117
|
+
}) {
|
|
118
|
+
const {
|
|
119
|
+
frame,
|
|
120
|
+
empty,
|
|
121
|
+
fit = "frame",
|
|
122
|
+
fill,
|
|
123
|
+
noun,
|
|
124
|
+
missing,
|
|
125
|
+
headed,
|
|
126
|
+
children,
|
|
127
|
+
} = props;
|
|
128
|
+
const words = useWords();
|
|
129
|
+
if (frame.shape === "failed") {
|
|
130
|
+
return (
|
|
131
|
+
<EmptyBase
|
|
132
|
+
fit={fit}
|
|
133
|
+
tone="failed"
|
|
134
|
+
sentence={words.readFailed}
|
|
135
|
+
act={{ label: words.retry, onAct: frame.retry }}
|
|
136
|
+
fill={fill}
|
|
137
|
+
headed={headed}
|
|
138
|
+
/>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
if (frame.shape === "missing")
|
|
142
|
+
return (
|
|
143
|
+
<MissingBase
|
|
144
|
+
fit={fit}
|
|
145
|
+
fill={fill}
|
|
146
|
+
noun={noun}
|
|
147
|
+
sentence={missing}
|
|
148
|
+
headed={headed}
|
|
149
|
+
/>
|
|
150
|
+
);
|
|
151
|
+
if (frame.shape === "empty") {
|
|
152
|
+
return <EmptyShape empty={empty} fit={fit} fill={fill} />;
|
|
153
|
+
}
|
|
154
|
+
// The reads this frame holds are not drawn again by a frame in its body.
|
|
155
|
+
return <FrameHolds holds={frame.held}>{children}</FrameHolds>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// The mark fades in after `--wait-delay` like the paint does, so a refetch
|
|
159
|
+
// the server answers at once draws nothing a viewer sees; it stands in the
|
|
160
|
+
// heading's ink, the size of the glyphs beside it.
|
|
161
|
+
const BUSY = "inline-flex shrink-0 items-center text-ink-meta animate-paint";
|
|
162
|
+
// The line trails its heading row, so it carries the room under itself.
|
|
163
|
+
const LINE = "flex flex-wrap items-center gap-gap pb-gap";
|
|
164
|
+
// In a form (the form's rows, a group's) the line is a row of the run.
|
|
165
|
+
const LINE_IN_FLOW = cn(row({ state: "rest" }), "flex flex-wrap items-center");
|
|
166
|
+
// A field's failed read stands in its value column in the place of the control: the
|
|
167
|
+
// sentence and Retry on one control-high line, centred as the control would be, so
|
|
168
|
+
// the label beside it shares their centre.
|
|
169
|
+
const LINE_IN_VALUE = "flex flex-wrap items-center gap-gap min-h-control";
|
|
170
|
+
|
|
171
|
+
/** A frame's busy mark, after its heading: a spinner while a read it holds refetches or retries, faded in after `--wait-delay`. A frame that holds no read and a frame with no heading draw none, and of sibling frames over one read only the first draws it, so one refetch stands as one mark. */
|
|
172
|
+
export function BusyMark({ frame }: { frame: Frame }) {
|
|
173
|
+
if (!(frame.holds && frame.flags && frame.busy)) return null;
|
|
174
|
+
return (
|
|
175
|
+
<span data-busy="" className={BUSY}>
|
|
176
|
+
<Spinner />
|
|
177
|
+
</span>
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** One meta sentence with an optional quiet act after it: what a frame says in its head about its read's state. */
|
|
182
|
+
export function StateLine(props: {
|
|
183
|
+
sentence: string;
|
|
184
|
+
act?: Act;
|
|
185
|
+
flow?: boolean;
|
|
186
|
+
/** The line is a field's value, in the control's place. */
|
|
187
|
+
lone?: boolean;
|
|
188
|
+
}) {
|
|
189
|
+
const { sentence, act, flow, lone } = props;
|
|
190
|
+
return (
|
|
191
|
+
<div className={lone ? LINE_IN_VALUE : flow ? LINE_IN_FLOW : LINE}>
|
|
192
|
+
<Text kind="meta">{sentence}</Text>
|
|
193
|
+
{act ? (
|
|
194
|
+
<ButtonBase
|
|
195
|
+
act="quiet"
|
|
196
|
+
label={act.label}
|
|
197
|
+
onAct={act.onAct}
|
|
198
|
+
{...wiringOf(act)}
|
|
199
|
+
/>
|
|
200
|
+
) : null}
|
|
201
|
+
</div>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The line under a control whose options have ended: the failed sentence with Retry, or `words.missing`; nothing while they wait, stand still or answered. A `compact` control (a row's pick, a head's pill) has no line to stand under it, so its failure is the failed row's mark and retry icon at its end, named by the sentence and Retry: one line whatever the width, never past the row. */
|
|
206
|
+
export function OptionsLine(props: {
|
|
207
|
+
drawn: Drawn<unknown>;
|
|
208
|
+
retry: () => Promise<unknown>;
|
|
209
|
+
compact?: boolean;
|
|
210
|
+
/** The field's value: the line stands in the control's place, in the value column. */
|
|
211
|
+
lone?: boolean;
|
|
212
|
+
}) {
|
|
213
|
+
const { drawn, retry, compact, lone } = props;
|
|
214
|
+
const words = useWords();
|
|
215
|
+
if (!drawn.painted) return null;
|
|
216
|
+
if (compact && isEnded(drawn.phase)) {
|
|
217
|
+
const failed = drawn.phase === "failed";
|
|
218
|
+
return (
|
|
219
|
+
<span className={ENDED_MARKS}>
|
|
220
|
+
<FailedMark label={failed ? words.readFailed : words.missing} />
|
|
221
|
+
{failed ? (
|
|
222
|
+
<IconButtonBase
|
|
223
|
+
icon="RotateCw"
|
|
224
|
+
label={words.retry}
|
|
225
|
+
onClick={() => void retry()}
|
|
226
|
+
/>
|
|
227
|
+
) : null}
|
|
228
|
+
</span>
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
if (drawn.phase === "failed") {
|
|
232
|
+
return (
|
|
233
|
+
<StateLine
|
|
234
|
+
flow={!lone}
|
|
235
|
+
lone={lone}
|
|
236
|
+
sentence={words.readFailed}
|
|
237
|
+
act={{ label: words.retry, onAct: retry }}
|
|
238
|
+
/>
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
return drawn.phase === "missing" ? (
|
|
242
|
+
<StateLine flow={!lone} lone={lone} sentence={words.missing} />
|
|
243
|
+
) : null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The line under a frame's heading row when a read it holds has data it could not refresh (stale, with Retry, pending while it runs) or waits for a connection (paused, nothing to press). */
|
|
247
|
+
export function FrameLine({
|
|
248
|
+
frame,
|
|
249
|
+
flow,
|
|
250
|
+
}: {
|
|
251
|
+
frame: Frame;
|
|
252
|
+
/** The line stands among a form's gapped children, which are its room. */
|
|
253
|
+
flow?: boolean;
|
|
254
|
+
}) {
|
|
255
|
+
const words = useWords();
|
|
256
|
+
if (!frame.holds) return null;
|
|
257
|
+
if (frame.stale) {
|
|
258
|
+
return (
|
|
259
|
+
<StateLine
|
|
260
|
+
flow={flow}
|
|
261
|
+
sentence={words.readStale}
|
|
262
|
+
act={{ label: words.retry, onAct: frame.retry }}
|
|
263
|
+
/>
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
return frame.paused ? (
|
|
267
|
+
<StateLine flow={flow} sentence={words.readPaused} />
|
|
268
|
+
) : null;
|
|
269
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { scrambled, tuned } from "@slotform/ui-core/paint";
|
|
2
|
+
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { CodeWhole } from "./code.tsx";
|
|
4
|
+
import { reducedNow } from "./media.ts";
|
|
5
|
+
import { Painted } from "./paint.tsx";
|
|
6
|
+
import { Decoding } from "./scramble.tsx";
|
|
7
|
+
import { Roll } from "./swap.tsx";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A figure or id of a read, in both its forms: `Painted` while `painted`, then
|
|
11
|
+
* its `text`. The region is one instance across the switch, so it knows whether
|
|
12
|
+
* it waited: when the paint stood for `waitDelay` and the theme scrambles its
|
|
13
|
+
* kind, the text decodes as it arrives (otherwise it lands at
|
|
14
|
+
* once), and it rolls when `of`, its number, changes while shown. The layout
|
|
15
|
+
* is the loaded one from the first frame: the decode is paint alone. `text`
|
|
16
|
+
* undefined draws nothing once the read is here. `broken` gives the loaded
|
|
17
|
+
* text its break opportunities after a path's or URL's separators
|
|
18
|
+
* (`CodeWhole`), for an id that wraps whole.
|
|
19
|
+
*/
|
|
20
|
+
export function Decoded(props: {
|
|
21
|
+
role: "figure" | "id";
|
|
22
|
+
painted?: boolean;
|
|
23
|
+
at?: number;
|
|
24
|
+
text?: string;
|
|
25
|
+
of?: number;
|
|
26
|
+
broken?: boolean;
|
|
27
|
+
}) {
|
|
28
|
+
const { role, painted = false, at = 0, text, of, broken = false } = props;
|
|
29
|
+
const [decoding, setDecoding] = useState(false);
|
|
30
|
+
const since = useRef<number>(undefined);
|
|
31
|
+
// Before paint, so the loaded text never shows ahead of its decode.
|
|
32
|
+
useLayoutEffect(() => {
|
|
33
|
+
if (painted) {
|
|
34
|
+
since.current = performance.now();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const waited =
|
|
38
|
+
since.current !== undefined &&
|
|
39
|
+
performance.now() - since.current >= tuned.waitDelay;
|
|
40
|
+
since.current = undefined;
|
|
41
|
+
if (waited && scrambled(role) && !reducedNow()) setDecoding(true);
|
|
42
|
+
}, [painted, role]);
|
|
43
|
+
const done = useCallback(() => setDecoding(false), []);
|
|
44
|
+
if (painted) return <Painted role={role} at={at} />;
|
|
45
|
+
if (text === undefined) return null;
|
|
46
|
+
if (decoding) return <Decoding text={text} onDone={done} />;
|
|
47
|
+
if (broken) return <CodeWhole code={text} />;
|
|
48
|
+
return of === undefined ? text : <Roll text={text} of={of} />;
|
|
49
|
+
}
|