@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,234 @@
|
|
|
1
|
+
import { Button as BaseButton } from "@base-ui/react/button";
|
|
2
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
3
|
+
import type { Run, Runs } from "@slotform/ui-core/descriptors";
|
|
4
|
+
import { PAINT_ROWS } from "@slotform/ui-core/paint";
|
|
5
|
+
import { useSelections } from "@slotform/ui-core/react/read";
|
|
6
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
7
|
+
import { type Datum, paints } from "@slotform/ui-core/read";
|
|
8
|
+
import { partRuns, partText } from "@slotform/ui-core/runs";
|
|
9
|
+
import { counted, named } from "@slotform/ui-core/tokens";
|
|
10
|
+
import {
|
|
11
|
+
CARD,
|
|
12
|
+
CODE_ACT,
|
|
13
|
+
CODE_FOLD,
|
|
14
|
+
CODE_HEAD,
|
|
15
|
+
CODE_UNDER_HEAD,
|
|
16
|
+
codeText,
|
|
17
|
+
lineBox,
|
|
18
|
+
PRESS_WASH,
|
|
19
|
+
text,
|
|
20
|
+
} from "@slotform/ui-core/variants";
|
|
21
|
+
import { Fragment, useId, useState } from "react";
|
|
22
|
+
import { IconButtonBase } from "../../atoms/button/glyph.tsx";
|
|
23
|
+
import { Icon } from "../../atoms/icon/index.tsx";
|
|
24
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
25
|
+
import { CodeWhole, RunText } from "../../lib/code.tsx";
|
|
26
|
+
import { useCopy } from "../../lib/copy.ts";
|
|
27
|
+
import { Painted } from "../../lib/paint.tsx";
|
|
28
|
+
import { CODE_FRAME } from "./frame.ts";
|
|
29
|
+
|
|
30
|
+
const HEAD = "flex items-center";
|
|
31
|
+
const TITLE = "grow min-w-0 truncate";
|
|
32
|
+
const BODY = "flex min-w-0";
|
|
33
|
+
// The text keeps its line breaks and wraps a long token at its path and URL
|
|
34
|
+
// separators (`CodeWhole`), or anywhere where one segment alone is wider, so an
|
|
35
|
+
// argument reads whole at any width. Focusable by script (the fold lands on
|
|
36
|
+
// it), its ring inward inside the frame's clip.
|
|
37
|
+
const TEXT =
|
|
38
|
+
"whitespace-pre-wrap in-data-[modality=keyboard]:focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring";
|
|
39
|
+
const TEXT_BESIDE = "grow min-w-0";
|
|
40
|
+
const ACT = "shrink-0";
|
|
41
|
+
const LINE = "flex items-center h-lh";
|
|
42
|
+
const ACTS = "gap-gap";
|
|
43
|
+
const FOLD = cn(
|
|
44
|
+
"flex items-center w-full",
|
|
45
|
+
PRESS_WASH,
|
|
46
|
+
"in-data-[modality=keyboard]:focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
/** Text a machine reads, in mono, whole. */
|
|
50
|
+
export interface CodeProps extends Closed {
|
|
51
|
+
/** The text, its lines split on newlines (text; wraps at any width). A selection paints `PAINT_ROWS` lines until its read lands. */
|
|
52
|
+
text: Datum<string>;
|
|
53
|
+
/** What the text is (a file's name, the tool it goes into), in a head over it; it names the text and its acts (a short phrase; truncates). A command or a machine name is a `{ code }` run, in the mono. */
|
|
54
|
+
title?: Run | Runs;
|
|
55
|
+
/** Shows only the last lines, this many, behind an act that reveals the earlier ones. */
|
|
56
|
+
tail?: number;
|
|
57
|
+
/** Adds the copy act, named by the title (else by the download's file, else just Copy): in the head with a title, else in a column beside the first line. */
|
|
58
|
+
copy?: boolean;
|
|
59
|
+
/** Adds the download act, saving the text as a file of this name (`recovery-codes.txt`), named by the title (else by the file): after the copy act, in the same place. */
|
|
60
|
+
download?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The download act: the text as a file of the given name, saved through an
|
|
64
|
+
// anchor.
|
|
65
|
+
function DownloadAct(props: {
|
|
66
|
+
name: string;
|
|
67
|
+
text: string;
|
|
68
|
+
file: string;
|
|
69
|
+
painted: boolean;
|
|
70
|
+
}) {
|
|
71
|
+
const words = useWords();
|
|
72
|
+
return (
|
|
73
|
+
<IconButtonBase
|
|
74
|
+
inset
|
|
75
|
+
painted={props.painted}
|
|
76
|
+
icon="Download"
|
|
77
|
+
label={named(words.download, props.name)}
|
|
78
|
+
onClick={() => {
|
|
79
|
+
const url = URL.createObjectURL(
|
|
80
|
+
new Blob([props.text], { type: "text/plain" }),
|
|
81
|
+
);
|
|
82
|
+
const link = document.createElement("a");
|
|
83
|
+
link.href = url;
|
|
84
|
+
link.download = props.file;
|
|
85
|
+
link.click();
|
|
86
|
+
// No event marks the download taking the URL; the click starts it in
|
|
87
|
+
// its own task, so the next one may revoke it.
|
|
88
|
+
setTimeout(() => URL.revokeObjectURL(url));
|
|
89
|
+
}}
|
|
90
|
+
/>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The copy act: a check and the word Copied for two seconds once copied.
|
|
95
|
+
function CopyAct(props: { name?: string; text: string; painted: boolean }) {
|
|
96
|
+
const words = useWords();
|
|
97
|
+
const [done, copy] = useCopy();
|
|
98
|
+
return (
|
|
99
|
+
<IconButtonBase
|
|
100
|
+
inset
|
|
101
|
+
painted={props.painted}
|
|
102
|
+
icon={done ? "Check" : "Copy"}
|
|
103
|
+
label={done ? words.copied : named(words.copy, props.name)}
|
|
104
|
+
onClick={() => copy(props.text)}
|
|
105
|
+
/>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Mono at the code role in the frame Code, Diff share; keeps its line breaks and wraps a long line at any width, never scrolling sideways: a path or a URL breaks after its slashes and query signs first, a flag keeps its hyphens bound. A head names it (`title`) and carries the copy and download acts; without a title they stand side by side in a column beside the first line. `tail` folds the earlier lines behind a one-way act that reveals them and leaves, the focus landing on the text. */
|
|
110
|
+
export function Code({
|
|
111
|
+
text: datum,
|
|
112
|
+
title: titled,
|
|
113
|
+
tail,
|
|
114
|
+
copy,
|
|
115
|
+
download,
|
|
116
|
+
}: CodeProps) {
|
|
117
|
+
// The title's words name the text and its acts; its runs draw in the head.
|
|
118
|
+
const title =
|
|
119
|
+
titled === undefined ? undefined : partText(titled) || undefined;
|
|
120
|
+
const [drawn] = useSelections([datum] as const);
|
|
121
|
+
const painted = paints(drawn);
|
|
122
|
+
const source = drawn.painted ? "" : drawn.value;
|
|
123
|
+
const words = useWords();
|
|
124
|
+
const id = useId();
|
|
125
|
+
const [textNode, setTextNode] = useState<HTMLPreElement | null>(null);
|
|
126
|
+
const [unfolded, setUnfolded] = useState(false);
|
|
127
|
+
// The earlier lines come in as a block: the frame is named, so it grows by
|
|
128
|
+
// the page's block move and what follows moves with it.
|
|
129
|
+
const name = title ?? words.code;
|
|
130
|
+
// The acts' name: the title, else the file the download saves.
|
|
131
|
+
const actName = title ?? download;
|
|
132
|
+
const acts = (
|
|
133
|
+
<>
|
|
134
|
+
{copy ? <CopyAct name={actName} text={source} painted={painted} /> : null}
|
|
135
|
+
{download ? (
|
|
136
|
+
<DownloadAct
|
|
137
|
+
name={title ?? download}
|
|
138
|
+
text={source}
|
|
139
|
+
file={download}
|
|
140
|
+
painted={painted}
|
|
141
|
+
/>
|
|
142
|
+
) : null}
|
|
143
|
+
</>
|
|
144
|
+
);
|
|
145
|
+
const head =
|
|
146
|
+
titled && title ? (
|
|
147
|
+
<div className={cn(CODE_HEAD, HEAD)}>
|
|
148
|
+
<span className={cn(text({ role: "meta" }), TITLE)}>
|
|
149
|
+
<RunText runs={partRuns(titled)} fit="name" />
|
|
150
|
+
</span>
|
|
151
|
+
{acts}
|
|
152
|
+
</div>
|
|
153
|
+
) : null;
|
|
154
|
+
// Painted, the text is `PAINT_ROWS` lines of filler and never folds.
|
|
155
|
+
const lines = painted ? [] : source.split("\n");
|
|
156
|
+
const hidden =
|
|
157
|
+
tail !== undefined && !unfolded ? Math.max(0, lines.length - tail) : 0;
|
|
158
|
+
const shown = hidden > 0 ? lines.slice(hidden) : lines;
|
|
159
|
+
const beside = (copy === true || download !== undefined) && !title;
|
|
160
|
+
const fold =
|
|
161
|
+
hidden > 0 ? (
|
|
162
|
+
<BaseButton
|
|
163
|
+
aria-expanded={false}
|
|
164
|
+
aria-controls={id}
|
|
165
|
+
onClick={() => {
|
|
166
|
+
// The act leaves with the lines it folded: the focus moves to the
|
|
167
|
+
// text, mounted already, before the act goes.
|
|
168
|
+
textNode?.focus();
|
|
169
|
+
setUnfolded(true);
|
|
170
|
+
}}
|
|
171
|
+
className={cn(CODE_FOLD, title && CODE_UNDER_HEAD, FOLD)}
|
|
172
|
+
>
|
|
173
|
+
<Icon name="ChevronUp" fit="meta" />
|
|
174
|
+
<span className={text({ role: "meta" })}>
|
|
175
|
+
{counted(words.earlierLines, hidden)}
|
|
176
|
+
</span>
|
|
177
|
+
</BaseButton>
|
|
178
|
+
) : null;
|
|
179
|
+
const body = (
|
|
180
|
+
// biome-ignore lint/a11y/useSemanticElements: a named text, not a form's fieldset; a group adds no landmark per block
|
|
181
|
+
<pre
|
|
182
|
+
ref={setTextNode}
|
|
183
|
+
id={id}
|
|
184
|
+
// Focusable by script only (the fold lands on it), out of the tab order.
|
|
185
|
+
tabIndex={-1}
|
|
186
|
+
role="group"
|
|
187
|
+
aria-label={name}
|
|
188
|
+
className={cn(
|
|
189
|
+
text({ role: "code" }),
|
|
190
|
+
codeText({ act: beside ? "beside" : "none" }),
|
|
191
|
+
title && !fold && CODE_UNDER_HEAD,
|
|
192
|
+
TEXT,
|
|
193
|
+
beside && TEXT_BESIDE,
|
|
194
|
+
)}
|
|
195
|
+
>
|
|
196
|
+
<code>
|
|
197
|
+
{painted ? (
|
|
198
|
+
Array.from({ length: PAINT_ROWS }, (_, at) => (
|
|
199
|
+
<Fragment key={at}>
|
|
200
|
+
{at > 0 ? "\n" : null}
|
|
201
|
+
<Painted role="sentence" at={at} />
|
|
202
|
+
</Fragment>
|
|
203
|
+
))
|
|
204
|
+
) : (
|
|
205
|
+
<CodeWhole code={shown.join("\n")} />
|
|
206
|
+
)}
|
|
207
|
+
</code>
|
|
208
|
+
</pre>
|
|
209
|
+
);
|
|
210
|
+
// An ended read draws no text; the frame that holds it says why.
|
|
211
|
+
if (drawn.painted && !painted) return null;
|
|
212
|
+
return (
|
|
213
|
+
<div
|
|
214
|
+
data-wide=""
|
|
215
|
+
inert={painted || undefined}
|
|
216
|
+
className={cn(CARD, CODE_FRAME)}
|
|
217
|
+
>
|
|
218
|
+
{head}
|
|
219
|
+
{fold}
|
|
220
|
+
{beside ? (
|
|
221
|
+
<div className={BODY}>
|
|
222
|
+
{body}
|
|
223
|
+
<span className={cn(CODE_ACT, ACT)}>
|
|
224
|
+
<span className={cn(lineBox({ role: "code" }), LINE, ACTS)}>
|
|
225
|
+
{acts}
|
|
226
|
+
</span>
|
|
227
|
+
</span>
|
|
228
|
+
</div>
|
|
229
|
+
) : (
|
|
230
|
+
body
|
|
231
|
+
)}
|
|
232
|
+
</div>
|
|
233
|
+
);
|
|
234
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import { type ColumnsFit, columns } from "@slotform/ui-core/variants";
|
|
3
|
+
import { type ReactNode, use } from "react";
|
|
4
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
5
|
+
import { PageTitle } from "../../lib/container.ts";
|
|
6
|
+
|
|
7
|
+
// The board is one flex row: each child stands at the column width and does
|
|
8
|
+
// not shrink, so no wrapper holds a track. The row
|
|
9
|
+
// bleeds to the Pane's edge and insets its columns back to the page inset, so
|
|
10
|
+
// it scrolls sideways edge to edge; the body clips past that edge, so its
|
|
11
|
+
// focus ring draws inside it.
|
|
12
|
+
const BOARD =
|
|
13
|
+
"flex items-start overflow-x-auto -mx-page *:w-(--container-column) *:max-w-full *:shrink-0 focus-visible:-outline-offset-2";
|
|
14
|
+
// The half fit stacks in one column, then stands two to a row from the
|
|
15
|
+
// Pane's desktop width. The row aligns its columns to the start, as the board
|
|
16
|
+
// does: a Section is a grid of auto rows, so one stretched to the taller
|
|
17
|
+
// column's height would spread the extra into its rows and draw an empty floor
|
|
18
|
+
// above its content.
|
|
19
|
+
const HALF =
|
|
20
|
+
"flex flex-col page-desktop:flex-row page-desktop:items-start page-desktop:*:grow page-desktop:*:basis-0";
|
|
21
|
+
|
|
22
|
+
/** Sections side by side: a board's columns scrolling sideways, or two to a row. */
|
|
23
|
+
export interface ColumnsProps extends Closed {
|
|
24
|
+
/** `board` (the default): each section at the column width, the row scrolling sideways. `half`: two to a row filling the body from the Pane's desktop width, stacking in order below it. */
|
|
25
|
+
fit?: ColumnsFit;
|
|
26
|
+
/** The sections, one Section each. */
|
|
27
|
+
children?: ReactNode;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** At `board`, each child in a column at its width, the row scrolling sideways from the page inset. At `half`, the children two to a row filling the body from the desktop width, stacked below it. */
|
|
31
|
+
export function Columns({ fit, children }: ColumnsProps) {
|
|
32
|
+
const title = use(PageTitle);
|
|
33
|
+
if (fit === "half")
|
|
34
|
+
return <div className={cn(columns({ fit }), HALF)}>{children}</div>;
|
|
35
|
+
// The row scrolls, so it takes focus for the keyboard to scroll it even
|
|
36
|
+
// when its columns hold nothing focusable, named by its Pane's title as a
|
|
37
|
+
// group, not a second region of that name.
|
|
38
|
+
return (
|
|
39
|
+
// biome-ignore lint/a11y/useSemanticElements: a fieldset groups form controls; this is a row of sections that scrolls
|
|
40
|
+
<div
|
|
41
|
+
data-wide=""
|
|
42
|
+
role="group"
|
|
43
|
+
aria-labelledby={title}
|
|
44
|
+
// biome-ignore lint/a11y/noNoninteractiveTabindex: a scrolling region is reached by the keyboard (WCAG 2.1.1, axe scrollable-region-focusable)
|
|
45
|
+
tabIndex={0}
|
|
46
|
+
className={cn(columns({ fit }), BOARD)}
|
|
47
|
+
>
|
|
48
|
+
{children}
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useSelections } from "@slotform/ui-core/react/read";
|
|
2
|
+
import { type Datum, paints } from "@slotform/ui-core/read";
|
|
3
|
+
import { CountBase } from "../../atoms/count/base.tsx";
|
|
4
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
5
|
+
|
|
6
|
+
interface CountProps extends Closed {
|
|
7
|
+
/** The number, or its drawn form where it is capped (`tabCount`). A selection paints the figure, and the Count is inert, until its read lands. */
|
|
8
|
+
value: Datum<number | string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A number in the meta ink at the caption size, its figures at one width. */
|
|
12
|
+
export function Count({ value }: CountProps) {
|
|
13
|
+
const [figure] = useSelections([value]);
|
|
14
|
+
if (paints(figure)) return <CountBase value="" painted />;
|
|
15
|
+
// An ended read draws no figure; the frame that holds it says why.
|
|
16
|
+
return figure.painted ? null : <CountBase value={figure.value} />;
|
|
17
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import type { DiffLine, Hunk } from "@slotform/ui-core/descriptors";
|
|
3
|
+
import {
|
|
4
|
+
BOTH,
|
|
5
|
+
type Elision,
|
|
6
|
+
elidedBefore,
|
|
7
|
+
hunkContext,
|
|
8
|
+
hunkKey,
|
|
9
|
+
indented,
|
|
10
|
+
lineHunks,
|
|
11
|
+
lineKey,
|
|
12
|
+
type Numbers,
|
|
13
|
+
numbersAt,
|
|
14
|
+
numbersOf,
|
|
15
|
+
tokenParts,
|
|
16
|
+
} from "@slotform/ui-core/line-diff";
|
|
17
|
+
import { PAINT_ROWS } from "@slotform/ui-core/paint";
|
|
18
|
+
import { useFrame, useSelections } from "@slotform/ui-core/react/read";
|
|
19
|
+
import { useWords } from "@slotform/ui-core/react/words";
|
|
20
|
+
import { type Datum, isSelection } from "@slotform/ui-core/read";
|
|
21
|
+
import { counted } from "@slotform/ui-core/tokens";
|
|
22
|
+
import {
|
|
23
|
+
CARD,
|
|
24
|
+
DIFF_CODE,
|
|
25
|
+
DIFF_GUTTER,
|
|
26
|
+
DIFF_HANG,
|
|
27
|
+
DIFF_HUNK,
|
|
28
|
+
DIFF_MARK,
|
|
29
|
+
DIFF_MARKS,
|
|
30
|
+
diffLine,
|
|
31
|
+
} from "@slotform/ui-core/variants";
|
|
32
|
+
import { Fragment, useMemo } from "react";
|
|
33
|
+
import type { Closed } from "../../lib/closed.ts";
|
|
34
|
+
import { useTouch } from "../../lib/media.ts";
|
|
35
|
+
import { Painted } from "../../lib/paint.tsx";
|
|
36
|
+
import { FrameBody, FrameLine, stillOf } from "../../lib/read.tsx";
|
|
37
|
+
import { CODE_FRAME } from "../code/frame.ts";
|
|
38
|
+
import { WordDiff } from "./word.tsx";
|
|
39
|
+
|
|
40
|
+
const TABLE = "w-full border-collapse";
|
|
41
|
+
// A line stands its indent and then the rest beside it (`indented`), so a
|
|
42
|
+
// wrapped line hangs one inset under its first visual line at its own indent,
|
|
43
|
+
// never left of the key it continues; the negative indent pulls the first
|
|
44
|
+
// line back to the indent's end. It wraps at its spaces and between tokens
|
|
45
|
+
// (`tokenParts`), so a name is never broken; only a run with no place to
|
|
46
|
+
// break that is wider than the line itself breaks inside it.
|
|
47
|
+
const LINE = "flex min-w-0";
|
|
48
|
+
const INDENT = "shrink-0 whitespace-pre";
|
|
49
|
+
const HANG = "grow min-w-0 -indent-pad whitespace-pre-wrap";
|
|
50
|
+
// The elided band's words (how many lines go unshown, then the enclosing
|
|
51
|
+
// function) stand on one line.
|
|
52
|
+
const CONTEXT = "truncate";
|
|
53
|
+
// The mark of lines a diff does not show, in the marker's column: a broken
|
|
54
|
+
// rule, a glyph no act wears.
|
|
55
|
+
const ELIDED = "\u2506";
|
|
56
|
+
const GUTTER = "text-end align-top";
|
|
57
|
+
const MARK = "align-top";
|
|
58
|
+
const CODE = "w-full align-top";
|
|
59
|
+
|
|
60
|
+
interface DiffBase extends Closed {
|
|
61
|
+
/** What the diff shows (the file's path), its table's accessible name (a short phrase; read aloud, never drawn). */
|
|
62
|
+
label: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A unified diff, as hunks, or as the two texts it compares, diffed here by line or by word. */
|
|
66
|
+
export type DiffProps =
|
|
67
|
+
| (DiffBase & {
|
|
68
|
+
/** The hunks, each its lines (a `header`'s ranges are never drawn: an elided mark parts two hunks, and stands before a first that starts past line 1, with the function git names after the ranges). A selection paints `PAINT_ROWS` lines until its read lands, and is the diff's own frame: it holds its read and draws the failed, missing and empty shapes in its own box whatever stands above it. */
|
|
69
|
+
hunks: Datum<readonly Hunk[]>;
|
|
70
|
+
before?: never;
|
|
71
|
+
after?: never;
|
|
72
|
+
by?: never;
|
|
73
|
+
})
|
|
74
|
+
| (DiffBase & {
|
|
75
|
+
hunks?: never;
|
|
76
|
+
/** How the two texts are compared: by line (the default), as a unified diff of code, or by word, as body text that keeps its line breaks with its edit marked in place, a removed run struck through and an added run underlined dotted in the `ok` ink (a cue besides its ground's hue, never a link's line), in a card at the measure. */
|
|
77
|
+
by?: "line" | "word";
|
|
78
|
+
/** The text before the change (text; its lines wrap). */
|
|
79
|
+
before: Datum<string>;
|
|
80
|
+
/** The text after it (text; its lines wrap). */
|
|
81
|
+
after: Datum<string>;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Painted, a line's gutters are empty cells.
|
|
85
|
+
function Gutters(props: { line?: DiffLine; numbers: Numbers }) {
|
|
86
|
+
return (
|
|
87
|
+
<>
|
|
88
|
+
{props.numbers.before ? (
|
|
89
|
+
<td className={cn(DIFF_GUTTER, GUTTER)}>{props.line?.before}</td>
|
|
90
|
+
) : null}
|
|
91
|
+
{props.numbers.after ? (
|
|
92
|
+
<td className={cn(DIFF_GUTTER, GUTTER)}>{props.line?.after}</td>
|
|
93
|
+
) : null}
|
|
94
|
+
<td className={cn(DIFF_MARK, MARK)}>
|
|
95
|
+
{DIFF_MARKS[props.line?.kind ?? "context"]}
|
|
96
|
+
</td>
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// A line's code, its indent and then the rest hung at it, with its break
|
|
102
|
+
// opportunities between tokens.
|
|
103
|
+
function Code(props: { text: string }) {
|
|
104
|
+
const { indent, rest } = indented(props.text);
|
|
105
|
+
return (
|
|
106
|
+
<div className={LINE}>
|
|
107
|
+
{indent === "" ? null : <span className={INDENT}>{indent}</span>}
|
|
108
|
+
<span className={cn(DIFF_HANG, HANG)}>
|
|
109
|
+
{tokenParts(rest).map((part, at) => (
|
|
110
|
+
<Fragment key={at}>
|
|
111
|
+
{at > 0 ? <wbr /> : null}
|
|
112
|
+
{part}
|
|
113
|
+
</Fragment>
|
|
114
|
+
))}
|
|
115
|
+
</span>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Where lines go unshown: the marker's column holds the mark, the code's the
|
|
121
|
+
// words that say so (how many, where the numbers count them) and the function
|
|
122
|
+
// git names, a middle dot apart, on the group ground in the meta ink. The row
|
|
123
|
+
// is named by those words.
|
|
124
|
+
function Elided(props: {
|
|
125
|
+
numbers: Numbers;
|
|
126
|
+
elision: Elision;
|
|
127
|
+
context: string | undefined;
|
|
128
|
+
}) {
|
|
129
|
+
const { numbers, elision, context } = props;
|
|
130
|
+
const words = useWords();
|
|
131
|
+
const said =
|
|
132
|
+
elision.lines === undefined
|
|
133
|
+
? words.linesUnshown
|
|
134
|
+
: counted(words.unshownLines, elision.lines);
|
|
135
|
+
return (
|
|
136
|
+
<tr className={diffLine({ kind: "elided" })}>
|
|
137
|
+
{numbers.before ? <td className={DIFF_GUTTER} /> : null}
|
|
138
|
+
{numbers.after ? <td className={DIFF_GUTTER} /> : null}
|
|
139
|
+
<td aria-hidden className={cn(DIFF_MARK, DIFF_HUNK, MARK)}>
|
|
140
|
+
{ELIDED}
|
|
141
|
+
</td>
|
|
142
|
+
<td className={cn(DIFF_CODE, DIFF_HUNK, CODE)}>
|
|
143
|
+
<div className={CONTEXT}>
|
|
144
|
+
{context === undefined ? said : `${said} · ${context}`}
|
|
145
|
+
</div>
|
|
146
|
+
</td>
|
|
147
|
+
</tr>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// The elided band before the hunk at `at`, where lines go unshown there.
|
|
152
|
+
function ElidedBefore(props: {
|
|
153
|
+
hunks: readonly Hunk[];
|
|
154
|
+
at: number;
|
|
155
|
+
numbers: Numbers;
|
|
156
|
+
}) {
|
|
157
|
+
const elision = elidedBefore(props.hunks, props.at);
|
|
158
|
+
const hunk = props.hunks[props.at];
|
|
159
|
+
if (elision === undefined || hunk === undefined) return null;
|
|
160
|
+
return (
|
|
161
|
+
<Elided
|
|
162
|
+
numbers={props.numbers}
|
|
163
|
+
elision={elision}
|
|
164
|
+
context={hunkContext(hunk)}
|
|
165
|
+
/>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A unified diff at the code role in the frame Code shares: its lines, added on ok-soft and removed on danger-soft, the whole row, each with its line numbers (the old and the new, or the one side a diff that only adds or only removes has; on touch one column, the new side or the only one) and its `+` or `−` marker. No hunk header's ranges are drawn: where lines go unshown (before a first hunk that starts past line 1, between two hunks) an elided band stands on the group ground, its mark (`┆`) in the marker's column and in the code's the words that say so ("12 lines not shown" where the numbers count them, else "Lines not shown": `words.unshownLines`, `words.linesUnshown`), then the function git names after the ranges, a middle dot apart. A long line wraps under itself at every width, at its spaces and between tokens, hung one inset past its own indent, its numbers on its first line. Given hunks by a selection it is a frame of them: the failed shape with Retry, the missing shape and the empty shape stand in its box with the generic sentence, and the stale and paused line over it while it holds its read. */
|
|
170
|
+
function LineDiff({ label, hunks, before, after }: DiffProps) {
|
|
171
|
+
const [h, b, a] = useSelections([hunks, before, after] as const);
|
|
172
|
+
// The hunks are the diff's frame: two texts are given plain or by a
|
|
173
|
+
// frame above, and a diff of them is no read's empty.
|
|
174
|
+
const frame = useFrame(isSelection(hunks) ? hunks : undefined);
|
|
175
|
+
const painted = h.painted || b.painted || a.painted;
|
|
176
|
+
// The hunks derive once per text pair, and not while the diff paints.
|
|
177
|
+
const shown = useMemo(() => {
|
|
178
|
+
if (h.painted || b.painted || a.painted) return [];
|
|
179
|
+
// `DiffProps` gives both texts wherever it gives no hunks; the read's
|
|
180
|
+
// tuple keeps no link between its members.
|
|
181
|
+
return h.value ?? lineHunks(b.value as string, a.value as string);
|
|
182
|
+
}, [h.painted, b.painted, a.painted, h.value, b.value, a.value]);
|
|
183
|
+
// On touch one number column stands, the new side or the only one.
|
|
184
|
+
const touch = useTouch();
|
|
185
|
+
const numbers = numbersAt(numbersOf(shown), touch);
|
|
186
|
+
const body = painted ? (
|
|
187
|
+
<div
|
|
188
|
+
data-wide=""
|
|
189
|
+
inert
|
|
190
|
+
data-still={stillOf(frame)}
|
|
191
|
+
className={cn(CARD, CODE_FRAME)}
|
|
192
|
+
>
|
|
193
|
+
<table className={TABLE}>
|
|
194
|
+
<tbody>
|
|
195
|
+
{Array.from({ length: PAINT_ROWS }, (_, at) => (
|
|
196
|
+
<tr key={at} className={diffLine({ kind: "context" })}>
|
|
197
|
+
<Gutters numbers={numbersAt(BOTH, touch)} />
|
|
198
|
+
<td className={cn(DIFF_CODE, CODE)}>
|
|
199
|
+
<div className={LINE}>
|
|
200
|
+
<span className={cn(DIFF_HANG, HANG)}>
|
|
201
|
+
<Painted role="sentence" at={at} />
|
|
202
|
+
</span>
|
|
203
|
+
</div>
|
|
204
|
+
</td>
|
|
205
|
+
</tr>
|
|
206
|
+
))}
|
|
207
|
+
</tbody>
|
|
208
|
+
</table>
|
|
209
|
+
</div>
|
|
210
|
+
) : (
|
|
211
|
+
<div data-wide="" className={cn(CARD, CODE_FRAME)}>
|
|
212
|
+
<table aria-label={label} className={TABLE}>
|
|
213
|
+
<tbody>
|
|
214
|
+
{shown.map((hunk, at) => (
|
|
215
|
+
<Fragment key={hunkKey(hunk, at)}>
|
|
216
|
+
<ElidedBefore hunks={shown} at={at} numbers={numbers} />
|
|
217
|
+
{hunk.lines.map((line) => (
|
|
218
|
+
<tr
|
|
219
|
+
key={lineKey(line)}
|
|
220
|
+
className={diffLine({ kind: line.kind })}
|
|
221
|
+
>
|
|
222
|
+
<Gutters line={line} numbers={numbers} />
|
|
223
|
+
<td className={cn(DIFF_CODE, CODE)}>
|
|
224
|
+
<Code text={line.text} />
|
|
225
|
+
</td>
|
|
226
|
+
</tr>
|
|
227
|
+
))}
|
|
228
|
+
</Fragment>
|
|
229
|
+
))}
|
|
230
|
+
</tbody>
|
|
231
|
+
</table>
|
|
232
|
+
</div>
|
|
233
|
+
);
|
|
234
|
+
return (
|
|
235
|
+
<FrameBody frame={frame}>
|
|
236
|
+
<FrameLine frame={frame} />
|
|
237
|
+
{body}
|
|
238
|
+
</FrameBody>
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** A diff of hunks, or of two texts by line or by word. */
|
|
243
|
+
export function Diff(props: DiffProps) {
|
|
244
|
+
return props.by === "word" ? (
|
|
245
|
+
<WordDiff label={props.label} before={props.before} after={props.after} />
|
|
246
|
+
) : (
|
|
247
|
+
<LineDiff {...props} />
|
|
248
|
+
);
|
|
249
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { cn } from "@slotform/ui-core/cn";
|
|
2
|
+
import { PAINT_ROWS } from "@slotform/ui-core/paint";
|
|
3
|
+
import { useSelections } from "@slotform/ui-core/react/read";
|
|
4
|
+
import { type Datum, paints } from "@slotform/ui-core/read";
|
|
5
|
+
import {
|
|
6
|
+
CARD,
|
|
7
|
+
PROSE_DIFF_BODY,
|
|
8
|
+
PROSE_DIFF_TEXT,
|
|
9
|
+
proseDiffRun,
|
|
10
|
+
text,
|
|
11
|
+
} from "@slotform/ui-core/variants";
|
|
12
|
+
import { diffWords } from "diff";
|
|
13
|
+
import { Fragment, type ReactNode, useMemo } from "react";
|
|
14
|
+
import { Painted } from "../../lib/paint.tsx";
|
|
15
|
+
import { CODE_FRAME } from "../code/frame.ts";
|
|
16
|
+
|
|
17
|
+
// The text keeps its line breaks, so a text of lines (a prompt, a list of
|
|
18
|
+
// steps) compares line by line; runs of spaces still collapse. A native text
|
|
19
|
+
// keeps them on its own.
|
|
20
|
+
const LINES = "whitespace-pre-line";
|
|
21
|
+
|
|
22
|
+
// Body text at the measure in the frame Code and Diff share, its line breaks kept, its edit marked in place: a removed run struck through on danger-soft, an added run on ok-soft underlined dotted in the `ok` ink, so it never reads as a link. `label` names the group.
|
|
23
|
+
export function WordDiff(props: {
|
|
24
|
+
label: string;
|
|
25
|
+
before: Datum<string>;
|
|
26
|
+
after: Datum<string>;
|
|
27
|
+
}) {
|
|
28
|
+
const { label, before, after } = props;
|
|
29
|
+
const [from, to] = useSelections([before, after] as const);
|
|
30
|
+
const painted = paints(from) || paints(to);
|
|
31
|
+
// An ended read draws no text; the frame that holds it says why.
|
|
32
|
+
const ended = from.painted || to.painted;
|
|
33
|
+
// The runs derive once per text pair, and not while the text paints.
|
|
34
|
+
const runs = useMemo(() => {
|
|
35
|
+
if (from.painted || to.painted) return [];
|
|
36
|
+
// A run marks its words alone, its edge whitespace standing outside it;
|
|
37
|
+
// each run is keyed by where it starts in the two texts read together.
|
|
38
|
+
const parts = diffWords(from.value, to.value);
|
|
39
|
+
let at = 0;
|
|
40
|
+
const drawn: ReactNode[] = [];
|
|
41
|
+
parts.forEach((part, index) => {
|
|
42
|
+
const key = at;
|
|
43
|
+
at += part.value.length;
|
|
44
|
+
const lead = part.value.match(/^\s*/)?.[0] ?? "";
|
|
45
|
+
const trail = part.value.slice(lead.length).match(/\s*$/)?.[0] ?? "";
|
|
46
|
+
const marked = part.value.slice(
|
|
47
|
+
lead.length,
|
|
48
|
+
part.value.length - trail.length,
|
|
49
|
+
);
|
|
50
|
+
if ((!part.added && !part.removed) || !marked) {
|
|
51
|
+
drawn.push(part.value);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// A removed run and the added run beside it stand a space apart.
|
|
55
|
+
const previous = parts[index - 1];
|
|
56
|
+
const touching =
|
|
57
|
+
previous !== undefined &&
|
|
58
|
+
(previous.added || previous.removed) &&
|
|
59
|
+
previous.added !== part.added &&
|
|
60
|
+
!/\s$/.test(previous.value) &&
|
|
61
|
+
!lead;
|
|
62
|
+
drawn.push(touching ? " " : lead);
|
|
63
|
+
drawn.push(
|
|
64
|
+
part.removed ? (
|
|
65
|
+
<del key={key} className={proseDiffRun({ kind: "removed" })}>
|
|
66
|
+
{marked}
|
|
67
|
+
</del>
|
|
68
|
+
) : (
|
|
69
|
+
<ins key={key} className={proseDiffRun({ kind: "added" })}>
|
|
70
|
+
{marked}
|
|
71
|
+
</ins>
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
drawn.push(trail);
|
|
75
|
+
});
|
|
76
|
+
return drawn;
|
|
77
|
+
}, [from.painted, to.painted, from.value, to.value]);
|
|
78
|
+
if (painted)
|
|
79
|
+
return (
|
|
80
|
+
<div inert className={cn(CARD, CODE_FRAME)}>
|
|
81
|
+
<div className={PROSE_DIFF_BODY}>
|
|
82
|
+
<p className={cn(text({ role: "body" }), PROSE_DIFF_TEXT)}>
|
|
83
|
+
{Array.from({ length: PAINT_ROWS }, (_, at) => (
|
|
84
|
+
<Fragment key={at}>
|
|
85
|
+
{at > 0 ? <br /> : null}
|
|
86
|
+
<Painted role="sentence" at={at} />
|
|
87
|
+
</Fragment>
|
|
88
|
+
))}
|
|
89
|
+
</p>
|
|
90
|
+
</div>
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
if (ended) return null;
|
|
94
|
+
return (
|
|
95
|
+
// biome-ignore lint/a11y/useSemanticElements: a card holding one paragraph, no form group
|
|
96
|
+
<div role="group" aria-label={label} className={cn(CARD, CODE_FRAME)}>
|
|
97
|
+
<div className={PROSE_DIFF_BODY}>
|
|
98
|
+
<p className={cn(text({ role: "body" }), PROSE_DIFF_TEXT, LINES)}>
|
|
99
|
+
{runs}
|
|
100
|
+
</p>
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
}
|