@gemboss/ui 0.57.0 → 0.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +221 -0
- package/README.md +238 -0
- package/catalog/editor.html +1 -1
- package/catalog/index.html +4 -4
- package/gemboss-ui.css +3 -3
- package/node/skill-flow/layout.js +194 -0
- package/node/skill-flow/model.js +279 -0
- package/package.json +31 -1
- package/parts/components.css +1 -1
- package/parts/core.css +1 -1
- package/parts/shell.css +1 -1
- package/src/assistant/host.tsx +21 -5
- package/src/assistant/sessions.tsx +36 -18
- package/src/auth-sso-plain.js +1 -1
- package/src/auth.tsx +2 -0
- package/src/collections.css +1 -1
- package/src/collections.tsx +5 -2
- package/src/dialog.tsx +4 -1
- package/src/editor/controls.tsx +18 -7
- package/src/editor/index.ts +1 -1
- package/src/editor/picker.tsx +9 -3
- package/src/editor/repeater.tsx +26 -14
- package/src/editor/swatch.ts +86 -0
- package/src/editor-color.css +1 -1
- package/src/editor-picker.css +1 -1
- package/src/editor-plain.js +1 -1
- package/src/editor-popover.css +1 -1
- package/src/editor.css +1 -1
- package/src/index.ts +17 -0
- package/src/job-card.css +1 -0
- package/src/job-card.tsx +130 -0
- package/src/list-page.css +1 -1
- package/src/members-data.ts +278 -0
- package/src/members.css +1 -0
- package/src/members.tsx +868 -0
- package/src/menu-extras.css +1 -1
- package/src/menu-select.css +1 -0
- package/src/menu.tsx +39 -12
- package/src/narration.css +1 -0
- package/src/save-bar.css +1 -0
- package/src/save-bar.tsx +50 -0
- package/src/skill-card.css +1 -0
- package/src/skill-card.tsx +146 -0
- package/src/skill-flow/canvas.tsx +593 -0
- package/src/skill-flow/index.ts +12 -0
- package/src/skill-flow/layout.ts +238 -0
- package/src/skill-flow/model.ts +412 -0
- package/src/skill-flow.css +1 -0
- package/src/skill-stats.css +1 -0
- package/src/skill-stats.tsx +213 -0
- package/src/skill-view.css +1 -0
- package/src/skill-view.tsx +249 -0
- package/src/theme-dark.css +1 -1
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SkillFlow — a skill's flow chart as a read-only canvas (03/11/2026, lifted from content-radar's SkillFlow.client.jsx: Chris
|
|
5
|
+
* 24/10/2026, "cần cái canvas để hiển thị flow chart của mỗi cái skills xem luồng tư duy sơ đồ ntn"). Nothing is dragged or wired:
|
|
6
|
+
* click a box (or Tab to it, then Enter) and its details open: what happens there, the tools the AI calls, 1–3 examples, and where
|
|
7
|
+
* it goes next.
|
|
8
|
+
*
|
|
9
|
+
* Load it LAZILY: React Flow and dagre are optional peers of the kit, and only `@gemboss/ui/skill-flow` reaches them. In Next:
|
|
10
|
+
* const SkillFlow = dynamic(() => import("@gemboss/ui/skill-flow").then((m) => m.SkillFlow), { ssr: false });
|
|
11
|
+
* (no server render: the canvas measures its frame before it places a box).
|
|
12
|
+
*
|
|
13
|
+
* Layout by the frame's real width (ResizeObserver), not the screen's:
|
|
14
|
+
* ≥ 1100px: the phases are lanes side by side, the whole flow at once; the details float at the canvas's right.
|
|
15
|
+
* narrower (a 390px phone): one stream top down, the phases as bands; the canvas at most ~72% of the screen high, so there is
|
|
16
|
+
* room left to scroll the page; the details sit under it.
|
|
17
|
+
* Zoom: a bar stuck to the canvas's top right (− · 100% · + · Fit · back to the start · Expand), 10%–400%; a plain wheel scrolls
|
|
18
|
+
* the page until you zoom in; Ctrl + wheel and a pinch zoom (React Flow), ⌘ + wheel zooms round the pointer; + / − / 0 with focus
|
|
19
|
+
* in the canvas. Expand: the same canvas in a ReaderDialog nearly the whole window (Esc / ✕ / Close to come back).
|
|
20
|
+
* Colours are GemBoss tokens, so `data-theme="dark"` follows; React Flow's colorMode follows the page's real ground.
|
|
21
|
+
*/
|
|
22
|
+
import "@xyflow/react/dist/style.css";
|
|
23
|
+
import "../skill-flow.css";
|
|
24
|
+
import React, { Fragment, useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
|
|
25
|
+
import {
|
|
26
|
+
ReactFlow, ReactFlowProvider, Background, Handle, Position, BaseEdge, EdgeLabelRenderer, getSmoothStepPath, MarkerType,
|
|
27
|
+
useReactFlow, useViewport, useStore, type EdgeProps, type NodeChange, type NodeProps, type NodeTypes, type EdgeTypes,
|
|
28
|
+
} from "@xyflow/react";
|
|
29
|
+
import { ReaderDialog } from "../reader-dialog";
|
|
30
|
+
import { FLOW_ZOOM, flowWheelPans, flowZoomKey, phasesIn, shortTool, validateFlow, wheelZoom, zoomLabel, type Flow, type FlowEdgeKind, type FlowMessages, type FlowNode, type FlowPhase } from "./model";
|
|
31
|
+
import { layoutFlow, type FlowLayout } from "./layout";
|
|
32
|
+
|
|
33
|
+
/** The words around the canvas. English by default; a surface passes its own. */
|
|
34
|
+
export interface SkillFlowLabels {
|
|
35
|
+
kinds: Record<FlowNode["kind"], string>;
|
|
36
|
+
person: string;
|
|
37
|
+
yes: string; no: string;
|
|
38
|
+
next: Record<FlowEdgeKind, string>;
|
|
39
|
+
examples: Record<string, string>;
|
|
40
|
+
legendDecision: string; legendStop: string; legendExit: string; legendNo: string; legendLoop: string;
|
|
41
|
+
details: string; toolsHeading: string; examplesHeading: string; examplesNote: string; answers: string; nextHeading: string; from: string;
|
|
42
|
+
emptyPanel: string; closeDetails: string;
|
|
43
|
+
zoomBar: string; zoomIn: string; zoomOut: string; zoomLevel: string; fit: string; home: string; expand: string; close: string; closeFull: string;
|
|
44
|
+
/** The Expand button's tooltip. Left out: the surface's `expand` (the words it borrowed before 04/11/2026). */
|
|
45
|
+
expandTip?: string;
|
|
46
|
+
canvas: string; fullTitle: string; fullSubtitle: string; cantDraw: string;
|
|
47
|
+
/** The legend's name for a screen reader. Left out: the surface's `fullTitle` (the words it borrowed before 04/11/2026). */
|
|
48
|
+
legend?: string;
|
|
49
|
+
hint: (o: { full: boolean; touch: boolean; key: string }) => string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const SKILL_FLOW_LABELS: SkillFlowLabels = {
|
|
53
|
+
kinds: { start: "Start", step: "Step", decision: "Decision", stop: "You confirm", exit: "Leaves this skill", end: "Done" },
|
|
54
|
+
person: "A person",
|
|
55
|
+
yes: "Yes", no: "No",
|
|
56
|
+
next: { normal: "Then", yes: "Yes", no: "No", loop: "Back" },
|
|
57
|
+
examples: { input: "Input", output: "Output", good: "Do", bad: "Don't", tool: "Tool call" },
|
|
58
|
+
legendDecision: "Decision", legendStop: "You confirm", legendExit: "Leaves this skill", legendNo: "No", legendLoop: "Back to fix",
|
|
59
|
+
details: "Step details", toolsHeading: "Tools the AI calls", examplesHeading: "Examples", examplesNote: "made up: names and numbers are not real",
|
|
60
|
+
answers: "Answers", nextHeading: "Next", from: "From",
|
|
61
|
+
emptyPanel: "Tap a box to see what happens there, which tools the AI calls, an example, and where it goes next.",
|
|
62
|
+
closeDetails: "Close details",
|
|
63
|
+
zoomBar: "Zoom the flow", zoomIn: "Zoom in", zoomOut: "Zoom out", zoomLevel: "Zoom level", fit: "Fit the whole flow", home: "Back to the start",
|
|
64
|
+
expand: "Expand", expandTip: "Expand", close: "Close", closeFull: "Close full screen (Esc)",
|
|
65
|
+
canvas: "Flow canvas. Plus and minus zoom, 0 goes back to the start.",
|
|
66
|
+
fullTitle: "Skill flow", fullSubtitle: "Every step this skill takes, where it decides, and where it stops for you.", legend: "Skill flow",
|
|
67
|
+
cantDraw: "Couldn't draw this flow",
|
|
68
|
+
hint: ({ full, touch, key }) => (full
|
|
69
|
+
? (touch ? "Pinch to zoom, drag to move. Tap a box for what happens there." : `Scroll or drag to move, ${key} + scroll or a pinch to zoom. Click a box for what happens there.`)
|
|
70
|
+
: touch ? "Drag to move, pinch to zoom, or Expand for full screen. Tap a box for what happens there."
|
|
71
|
+
: `Drag to move. ${key} + scroll or a pinch zooms. Click a box (or Tab to it, then Enter) for what happens there.`),
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export interface SkillFlowProps {
|
|
75
|
+
/** A checked flow (validateFlow); examples may carry server-rendered `html`. */
|
|
76
|
+
flow: Flow;
|
|
77
|
+
/** The lanes, in order. Every node's `phase` is one of their keys. */
|
|
78
|
+
phases: readonly FlowPhase[];
|
|
79
|
+
labels?: Partial<SkillFlowLabels>;
|
|
80
|
+
/** One line above the canvas (where the flow came from, a problem with it). */
|
|
81
|
+
note?: ReactNode;
|
|
82
|
+
/** "bad": the note is a problem (the flow file is broken and a stand-in is drawn): the danger tint, said as an alert. */
|
|
83
|
+
noteTone?: "bad";
|
|
84
|
+
/** A phase key to light; the other lanes fade (where a job using this skill stands now). The lit lane carries `data-lit`
|
|
85
|
+
* and a ring; its boxes carry `data-lit` too. */
|
|
86
|
+
highlight?: string | null;
|
|
87
|
+
/** The words of a problem when `flow` does not pass validateFlow ("Couldn't draw this flow (…)"); model.ts FLOW_MESSAGES is the English. */
|
|
88
|
+
messages?: Partial<FlowMessages>;
|
|
89
|
+
/** Prefixes cut off a tool's name in a box (`gemboss_get_skill` → `get_skill`). */
|
|
90
|
+
toolPrefixes?: readonly string[];
|
|
91
|
+
/** The surface's class for rendered Markdown (an example's `html`). */
|
|
92
|
+
mdClassName?: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const WIDE = 1100;
|
|
96
|
+
// Under this the 13px words in a box drop under ~10px: better to let the reader drag sideways than shrink further.
|
|
97
|
+
const READ_ZOOM = 0.78;
|
|
98
|
+
const PAD = 16;
|
|
99
|
+
// Kept free at the top for the zoom bar (10px padding + 38px bar + 8px): the first view puts no box under it.
|
|
100
|
+
const TOP = 56;
|
|
101
|
+
const PANEL_W = 360;
|
|
102
|
+
const LANE_TONES = 6;
|
|
103
|
+
// Every class a box, a line, a chip or an example wears, written whole (never built from the kind): a search for a class finds where
|
|
104
|
+
// it is drawn, and the admin's orphan-rule gate (a-tap-target-rule-outlived-its-markup.test.ts) has no runtime stem to exempt.
|
|
105
|
+
const NODE_CLASS: Record<FlowNode["kind"], string> = {
|
|
106
|
+
start: "skf-node skf-node--start", step: "skf-node", decision: "skf-node skf-node--decision",
|
|
107
|
+
stop: "skf-node skf-node--stop", exit: "skf-node skf-node--exit", end: "skf-node skf-node--end",
|
|
108
|
+
};
|
|
109
|
+
const EDGE_CLASS: Record<FlowEdgeKind, string> = { normal: "skf-edge", yes: "skf-edge skf-edge--yes", no: "skf-edge skf-edge--no", loop: "skf-edge skf-edge--loop" };
|
|
110
|
+
const ELABEL_CLASS: Record<FlowEdgeKind, string> = { normal: "skf-elabel", yes: "skf-elabel skf-elabel--yes", no: "skf-elabel skf-elabel--no", loop: "skf-elabel skf-elabel--loop" };
|
|
111
|
+
const CHIP_CLASS: Record<FlowEdgeKind, string> = { normal: "skf-chip", yes: "skf-chip skf-chip--yes", no: "skf-chip skf-chip--no", loop: "skf-chip skf-chip--loop" };
|
|
112
|
+
const EX_CLASS: Record<string, string> = {
|
|
113
|
+
input: "skf-ex skf-ex--input", output: "skf-ex skf-ex--output", good: "skf-ex skf-ex--good", bad: "skf-ex skf-ex--bad", tool: "skf-ex skf-ex--tool",
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const SIDES: Array<[string, Position]> = [["t", Position.Top], ["b", Position.Bottom], ["l", Position.Left], ["r", Position.Right]];
|
|
117
|
+
// Eight hidden handles: layoutFlow picks a side of the box for each line (s-<side> / t-<side>).
|
|
118
|
+
const Handles = () => (
|
|
119
|
+
<>
|
|
120
|
+
{SIDES.map(([k, p]) => (
|
|
121
|
+
<Fragment key={k}>
|
|
122
|
+
<Handle id={`s-${k}`} type="source" position={p} isConnectable={false} className="skf-h" />
|
|
123
|
+
<Handle id={`t-${k}`} type="target" position={p} isConnectable={false} className="skf-h" />
|
|
124
|
+
</Fragment>
|
|
125
|
+
))}
|
|
126
|
+
</>
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const PersonIcon = () => (
|
|
130
|
+
<svg viewBox="0 0 16 16" width="11" height="11" aria-hidden="true"><circle cx="8" cy="5" r="3" fill="currentColor" /><path d="M2.5 14.5c.6-3 2.8-4.6 5.5-4.6s4.9 1.6 5.5 4.6" fill="currentColor" /></svg>
|
|
131
|
+
);
|
|
132
|
+
const HomeIcon = () => (
|
|
133
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 1.8 1.5 7.2l1 1.2L3.5 7.6V14h3.6v-4h1.8v4h3.6V7.6l1 .8 1-1.2z" fill="currentColor" /></svg>
|
|
134
|
+
);
|
|
135
|
+
const Stroke = ({ d }: { d: string }) => (
|
|
136
|
+
<svg viewBox="0 0 16 16" aria-hidden="true"><path d={d} fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" /></svg>
|
|
137
|
+
);
|
|
138
|
+
const MinusIcon = () => <Stroke d="M3.5 8h9" />;
|
|
139
|
+
const PlusIcon = () => <Stroke d="M8 3.5v9M3.5 8h9" />;
|
|
140
|
+
const FitIcon = () => <Stroke d="M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10" />;
|
|
141
|
+
const ExpandIcon = () => <Stroke d="M9.5 2.5h4v4M13.5 2.5 9 7M6.5 13.5h-4v-4M2.5 13.5 7 9" />;
|
|
142
|
+
const CollapseIcon = () => <Stroke d="M13 7H9V3M9 7l4.5-4.5M3 9h4v4M7 9l-4.5 4.5" />;
|
|
143
|
+
|
|
144
|
+
/** The surface's words over the English ones. `expandTip` and `legend` were the `expand` and `fullTitle` words until 04/11/2026: a
|
|
145
|
+
* surface that renamed those and not these keeps what it had. */
|
|
146
|
+
export function skillFlowLabels(labels?: Partial<SkillFlowLabels>): SkillFlowLabels {
|
|
147
|
+
const L = { ...SKILL_FLOW_LABELS, ...labels };
|
|
148
|
+
return { ...L, expandTip: labels?.expandTip ?? labels?.expand ?? SKILL_FLOW_LABELS.expandTip, legend: labels?.legend ?? labels?.fullTitle ?? SKILL_FLOW_LABELS.legend };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** `**bold**` and `code` inside a line of an example, as elements (no HTML string, nothing to sanitise). */
|
|
152
|
+
function inline(text: string): ReactNode[] {
|
|
153
|
+
return text.split(/(\*\*[^*]+\*\*|`[^`]+`)/g).filter(Boolean).map((p, i) => (p.startsWith("**") && p.endsWith("**") && p.length > 4
|
|
154
|
+
? <strong key={i}>{p.slice(2, -2)}</strong> : p.startsWith("`") && p.endsWith("`") && p.length > 2 ? <code key={i}>{p.slice(1, -1)}</code> : p));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* An example's Markdown when the surface sent no `html` (flowWithHtml): the few shapes the flow files use, drawn as elements:
|
|
159
|
+
* paragraphs, `- ` and `1. ` lists, `> ` quotes, ``` fences, **bold**, `code`. Anything else stays text, never markup.
|
|
160
|
+
*/
|
|
161
|
+
export function ExampleMd({ md }: { md: string }) {
|
|
162
|
+
const out: ReactNode[] = [];
|
|
163
|
+
const lines = md.replace(/\r\n?/g, "\n").split("\n");
|
|
164
|
+
for (let i = 0; i < lines.length;) {
|
|
165
|
+
const line = lines[i]!;
|
|
166
|
+
if (/^```/.test(line)) {
|
|
167
|
+
const body: string[] = [];
|
|
168
|
+
for (i += 1; i < lines.length && !/^```/.test(lines[i]!); i += 1) body.push(lines[i]!);
|
|
169
|
+
i += 1;
|
|
170
|
+
out.push(<pre key={out.length}><code>{body.join("\n")}</code></pre>);
|
|
171
|
+
} else if (/^\s*[-*] /.test(line) || /^\s*\d+\. /.test(line)) {
|
|
172
|
+
const ordered = /^\s*\d+\. /.test(line);
|
|
173
|
+
const items: string[] = [];
|
|
174
|
+
for (; i < lines.length && (ordered ? /^\s*\d+\. /.test(lines[i]!) : /^\s*[-*] /.test(lines[i]!)); i += 1) items.push(lines[i]!.replace(/^\s*(?:[-*]|\d+\.) /, ""));
|
|
175
|
+
const li = items.map((t, k) => <li key={k}>{inline(t)}</li>);
|
|
176
|
+
out.push(ordered ? <ol key={out.length}>{li}</ol> : <ul key={out.length}>{li}</ul>);
|
|
177
|
+
} else if (/^> /.test(line)) {
|
|
178
|
+
const quote: string[] = [];
|
|
179
|
+
for (; i < lines.length && /^> /.test(lines[i]!); i += 1) quote.push(lines[i]!.slice(2));
|
|
180
|
+
out.push(<blockquote key={out.length}>{inline(quote.join(" "))}</blockquote>);
|
|
181
|
+
} else if (!line.trim()) {
|
|
182
|
+
i += 1;
|
|
183
|
+
} else {
|
|
184
|
+
const para: string[] = [];
|
|
185
|
+
for (; i < lines.length && lines[i]!.trim() && !/^(```|> |\s*[-*] |\s*\d+\. )/.test(lines[i]!); i += 1) para.push(lines[i]!.trim());
|
|
186
|
+
out.push(<p key={out.length}>{inline(para.join(" "))}</p>);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return <>{out}</>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
type Ctx = { L: SkillFlowLabels; prefixes?: readonly string[]; dim: (phase: string) => boolean; lit: (phase: string) => boolean; messages?: Partial<FlowMessages> };
|
|
193
|
+
const FlowCtx = React.createContext<Ctx>({ L: SKILL_FLOW_LABELS, dim: () => false, lit: () => false });
|
|
194
|
+
|
|
195
|
+
type NodeData = { node: FlowNode; lane: number };
|
|
196
|
+
function FlowNodeBox({ data, selected }: NodeProps) {
|
|
197
|
+
const { L, prefixes, dim, lit } = React.useContext(FlowCtx);
|
|
198
|
+
const { node: n, lane } = data as unknown as NodeData;
|
|
199
|
+
const eye = n.kind === "step" ? (n.human ? L.person : null) : L.kinds[n.kind];
|
|
200
|
+
return (
|
|
201
|
+
<div className={NODE_CLASS[n.kind] ?? "skf-node"} data-lane={lane % LANE_TONES} data-on={selected ? "" : undefined} data-dim={dim(n.phase) ? "" : undefined}
|
|
202
|
+
data-lit={lit(n.phase) ? "" : undefined}>
|
|
203
|
+
<Handles />
|
|
204
|
+
{eye ? <span className="skf-node__eye">{n.kind === "stop" || n.human ? <PersonIcon /> : null}{eye}</span> : null}
|
|
205
|
+
<span className="skf-node__label">{n.label}</span>
|
|
206
|
+
{n.tools.length ? <span className="skf-node__tools">{n.tools.map((t) => <code key={t} title={t}>{shortTool(t, prefixes)}</code>)}</span> : null}
|
|
207
|
+
</div>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
type BandData = { label: string; lanes: boolean; lane: number; phase: string };
|
|
212
|
+
function Band({ data }: NodeProps) {
|
|
213
|
+
const { dim, lit } = React.useContext(FlowCtx);
|
|
214
|
+
const b = data as unknown as BandData;
|
|
215
|
+
return (
|
|
216
|
+
<div className={`skf-band${b.lanes ? " skf-band--lane" : ""}`} data-lane={b.lane % LANE_TONES} data-dim={dim(b.phase) ? "" : undefined}
|
|
217
|
+
data-lit={lit(b.phase) ? "" : undefined}>
|
|
218
|
+
<span className="skf-band__label">{b.label}</span>
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const NEAR: Record<string, [number, number, string]> = { bottom: [6, 3, "0, 0"], top: [6, -3, "0, -100%"], right: [6, -3, "0, -100%"], left: [-6, -3, "-100%, -100%"] };
|
|
224
|
+
type EdgeData = { kind: FlowEdgeKind; label: string; offset?: number; centerX?: number; centerY?: number };
|
|
225
|
+
function FlowEdge({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, data, markerEnd }: EdgeProps) {
|
|
226
|
+
const { L } = React.useContext(FlowCtx);
|
|
227
|
+
const d = data as unknown as EdgeData;
|
|
228
|
+
const [path, lx, ly] = getSmoothStepPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, borderRadius: 10,
|
|
229
|
+
offset: d.offset ?? 20, centerX: d.centerX, centerY: d.centerY });
|
|
230
|
+
const word = d.kind === "yes" ? L.yes : d.kind === "no" ? L.no : null;
|
|
231
|
+
// A short line (a branch that leaves the skill right beside its decision): only Yes / No; the long label is in the details.
|
|
232
|
+
const short = (Math.abs(sourceY - targetY) < 4 && Math.abs(targetX - sourceX) < 90) || (Math.abs(sourceX - targetX) < 4 && Math.abs(targetY - sourceY) < 30);
|
|
233
|
+
const words = short ? "" : d.label;
|
|
234
|
+
// Yes / No sit right at the decision's way out (in the middle of a long line they would sit on another box); other labels (a
|
|
235
|
+
// loop) in the middle of the line, i.e. in the gap between two lanes.
|
|
236
|
+
const near = word ? NEAR[sourcePosition] : null;
|
|
237
|
+
// A loop that runs almost flat: its label goes under the line, off the arrow and the target box.
|
|
238
|
+
const flat = Math.abs(sourceY - targetY) < 30;
|
|
239
|
+
const transform = near ? `translate(${sourceX + near[0]}px, ${sourceY + near[1]}px) translate(${near[2]})`
|
|
240
|
+
: `translate(-50%, ${flat ? "6px" : "-50%"}) translate(${lx}px, ${ly}px)`;
|
|
241
|
+
return (
|
|
242
|
+
<>
|
|
243
|
+
<BaseEdge id={id} path={path} markerEnd={markerEnd} className={EDGE_CLASS[d.kind] ?? EDGE_CLASS.normal} interactionWidth={0} />
|
|
244
|
+
{word || words ? (
|
|
245
|
+
<EdgeLabelRenderer>
|
|
246
|
+
<span className={ELABEL_CLASS[d.kind] ?? ELABEL_CLASS.normal} title={d.label || undefined} style={{ transform }}>
|
|
247
|
+
{word ? <b>{word}</b> : null}{word && words ? " · " : null}{words}
|
|
248
|
+
</span>
|
|
249
|
+
</EdgeLabelRenderer>
|
|
250
|
+
) : null}
|
|
251
|
+
</>
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const NODE_TYPES = { flow: FlowNodeBox, band: Band } as unknown as NodeTypes;
|
|
256
|
+
const EDGE_TYPES = { flow: FlowEdge } as unknown as EdgeTypes;
|
|
257
|
+
const STROKE: Record<FlowEdgeKind, string> = { normal: "var(--skf-edge)", yes: "var(--skf-edge)", no: "var(--skf-no)", loop: "var(--skf-loop)" };
|
|
258
|
+
|
|
259
|
+
// The page's real ground: the kit's data-theme when set; else the --gemboss-surface token measured (a page without the dark
|
|
260
|
+
// palette stays light on a dark machine, and the canvas must not go dark alone); else the machine's.
|
|
261
|
+
function useColorMode(): "light" | "dark" {
|
|
262
|
+
const [mode, setMode] = useState<"light" | "dark">("light");
|
|
263
|
+
useEffect(() => {
|
|
264
|
+
const root = document.documentElement;
|
|
265
|
+
const mq = window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
266
|
+
const read = (): "light" | "dark" => {
|
|
267
|
+
const t = root.getAttribute("data-theme");
|
|
268
|
+
if (t === "dark" || t === "light") return t;
|
|
269
|
+
const m = getComputedStyle(root).getPropertyValue("--gemboss-surface").trim().match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
|
|
270
|
+
if (!m) return mq && mq.matches ? "dark" : "light";
|
|
271
|
+
const hex = m[1]!.length === 3 ? m[1]!.replace(/./g, "$&$&") : m[1]!;
|
|
272
|
+
const [r, g, b] = [0, 2, 4].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255) as [number, number, number];
|
|
273
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b < 0.45 ? "dark" : "light";
|
|
274
|
+
};
|
|
275
|
+
const update = () => setMode(read());
|
|
276
|
+
update();
|
|
277
|
+
const mo = new MutationObserver(update);
|
|
278
|
+
mo.observe(root, { attributes: true, attributeFilter: ["data-theme", "class", "style"] });
|
|
279
|
+
mq?.addEventListener?.("change", update);
|
|
280
|
+
return () => { mo.disconnect(); mq?.removeEventListener?.("change", update); };
|
|
281
|
+
}, []);
|
|
282
|
+
return mode;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function Details({ node, flow, lane, phaseLabel, floating, onPick, onClose, mdClassName }: {
|
|
286
|
+
node: FlowNode; flow: Flow; lane: number; phaseLabel: string; floating: boolean; onPick: (id: string) => void; onClose: () => void; mdClassName?: string;
|
|
287
|
+
}) {
|
|
288
|
+
const { L } = React.useContext(FlowCtx);
|
|
289
|
+
const label = (id: string) => flow.nodes.find((n) => n.id === id)?.label || id;
|
|
290
|
+
const outs = flow.edges.filter((e) => e.from === node.id);
|
|
291
|
+
const ins = flow.edges.filter((e) => e.to === node.id && e.kind !== "loop");
|
|
292
|
+
return (
|
|
293
|
+
<section className="skf-panel" data-lane={lane % LANE_TONES} aria-label={L.details}>
|
|
294
|
+
<div className="skf-panel__head">
|
|
295
|
+
<p className="skf-panel__eye"><i className="skf-dot" data-lane={lane % LANE_TONES} />{phaseLabel} · {L.kinds[node.kind]}</p>
|
|
296
|
+
{floating ? <button type="button" className="skf-panel__x" onClick={onClose} aria-label={L.closeDetails}>×</button> : null}
|
|
297
|
+
</div>
|
|
298
|
+
<h4>{node.label}</h4>
|
|
299
|
+
{node.detail ? <p className="skf-panel__detail">{node.detail}</p> : null}
|
|
300
|
+
{node.tools.length ? (
|
|
301
|
+
<>
|
|
302
|
+
<p className="skf-panel__h">{L.toolsHeading}</p>
|
|
303
|
+
<div className="skf-panel__tools">{node.tools.map((t) => <code key={t}>{t}</code>)}</div>
|
|
304
|
+
</>
|
|
305
|
+
) : null}
|
|
306
|
+
{node.examples?.length ? (
|
|
307
|
+
<>
|
|
308
|
+
<p className="skf-panel__h">{L.examplesHeading} <span>{L.examplesNote}</span></p>
|
|
309
|
+
<div className="skf-panel__ex">
|
|
310
|
+
{node.examples.map((x, i) => (
|
|
311
|
+
<article key={`${i}:${x.title}`} className={EX_CLASS[x.kind] ?? "skf-ex"}>
|
|
312
|
+
<p className="skf-ex__head"><span className="skf-ex__kind">{L.examples[x.kind] || x.kind}</span>{x.title}</p>
|
|
313
|
+
{x.html
|
|
314
|
+
? <div className={`skf-ex__md${mdClassName ? ` ${mdClassName}` : ""}`} data-content="" dangerouslySetInnerHTML={{ __html: x.html }} />
|
|
315
|
+
: <div className="skf-ex__md"><ExampleMd md={x.md} /></div>}
|
|
316
|
+
</article>
|
|
317
|
+
))}
|
|
318
|
+
</div>
|
|
319
|
+
</>
|
|
320
|
+
) : null}
|
|
321
|
+
{outs.length ? (
|
|
322
|
+
<>
|
|
323
|
+
<p className="skf-panel__h">{node.kind === "decision" ? L.answers : L.nextHeading}</p>
|
|
324
|
+
<ul className="skf-panel__next">
|
|
325
|
+
{outs.map((e) => (
|
|
326
|
+
<li key={e.to}>
|
|
327
|
+
<button type="button" className="skf-panel__go" onClick={() => onPick(e.to)}>
|
|
328
|
+
<span className={CHIP_CLASS[e.kind] ?? CHIP_CLASS.normal}>{L.next[e.kind]}</span>
|
|
329
|
+
<span>{e.label ? <em>{e.label} → </em> : null}{label(e.to)}</span>
|
|
330
|
+
</button>
|
|
331
|
+
</li>
|
|
332
|
+
))}
|
|
333
|
+
</ul>
|
|
334
|
+
</>
|
|
335
|
+
) : null}
|
|
336
|
+
{ins.length && node.kind !== "start" ? (
|
|
337
|
+
<p className="skf-panel__from">{L.from} {ins.map((e, i) => (
|
|
338
|
+
<Fragment key={e.from}>{i ? ", " : ""}<button type="button" className="skf-panel__back" onClick={() => onPick(e.from)}>{label(e.from)}</button></Fragment>
|
|
339
|
+
))}</p>
|
|
340
|
+
) : null}
|
|
341
|
+
</section>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// The zoom bar on its own: useViewport redraws on every frame of a drag or a zoom, and only the bar needs that number. It sits in
|
|
346
|
+
// `.skf__tools`, stuck (sticky) to the canvas's top right, so a tall canvas scrolled past in a popup still shows it.
|
|
347
|
+
function ZoomBar({ full, onFit, onReset, onExpand }: { full: boolean; onFit: () => void; onReset: () => void; onExpand?: () => void }) {
|
|
348
|
+
const { L } = React.useContext(FlowCtx);
|
|
349
|
+
const rf = useReactFlow();
|
|
350
|
+
const { zoom } = useViewport();
|
|
351
|
+
return (
|
|
352
|
+
<div className="skf-zoom" role="toolbar" aria-label={L.zoomBar}>
|
|
353
|
+
<button type="button" className="skf-zoom__btn" onClick={() => rf.zoomOut({ duration: 160 })} disabled={zoom <= FLOW_ZOOM.min + 0.001} title={`${L.zoomOut} (−)`} aria-label={L.zoomOut}><MinusIcon /></button>
|
|
354
|
+
<output className="skf-zoom__pct" aria-live="off" title={L.zoomLevel}>{zoomLabel(zoom)}</output>
|
|
355
|
+
<button type="button" className="skf-zoom__btn" onClick={() => rf.zoomIn({ duration: 160 })} disabled={zoom >= FLOW_ZOOM.max - 0.001} title={`${L.zoomIn} (+)`} aria-label={L.zoomIn}><PlusIcon /></button>
|
|
356
|
+
<i className="skf-zoom__sep" aria-hidden="true" />
|
|
357
|
+
<button type="button" className="skf-zoom__btn" onClick={onFit} title={L.fit} aria-label={L.fit}><FitIcon /></button>
|
|
358
|
+
<button type="button" className="skf-zoom__btn" onClick={onReset} title={`${L.home} (0)`} aria-label={L.home}><HomeIcon /></button>
|
|
359
|
+
{onExpand ? (
|
|
360
|
+
<>
|
|
361
|
+
<i className="skf-zoom__sep" aria-hidden="true" />
|
|
362
|
+
<button type="button" className="skf-zoom__btn skf-zoom__wide" onClick={onExpand} title={full ? L.closeFull : L.expandTip}>
|
|
363
|
+
{full ? <CollapseIcon /> : <ExpandIcon />}<span>{full ? L.close : L.expand}</span>
|
|
364
|
+
</button>
|
|
365
|
+
</>
|
|
366
|
+
) : null}
|
|
367
|
+
</div>
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function Canvas({ flow, phases, sel, setSel, full = false, onExpand, note, noteTone, mdClassName }: {
|
|
372
|
+
flow: Flow; phases: readonly FlowPhase[]; sel: string | null; setSel: React.Dispatch<React.SetStateAction<string | null>>;
|
|
373
|
+
full?: boolean; onExpand?: () => void; note?: ReactNode; noteTone?: "bad"; mdClassName?: string;
|
|
374
|
+
}) {
|
|
375
|
+
const { L, messages } = React.useContext(FlowCtx);
|
|
376
|
+
const rf = useReactFlow();
|
|
377
|
+
const box = useRef<HTMLDivElement>(null);
|
|
378
|
+
const stage = useRef<HTMLDivElement>(null);
|
|
379
|
+
const [pane, setPane] = useState<HTMLDivElement | null>(null);
|
|
380
|
+
const [w, setW] = useState(0);
|
|
381
|
+
const [sh, setSh] = useState(0);
|
|
382
|
+
const [vh, setVh] = useState(800);
|
|
383
|
+
const [touch, setTouch] = useState(false);
|
|
384
|
+
const [mac, setMac] = useState(false);
|
|
385
|
+
const mode = useColorMode();
|
|
386
|
+
|
|
387
|
+
useEffect(() => {
|
|
388
|
+
const el = box.current;
|
|
389
|
+
if (!el) return undefined;
|
|
390
|
+
const ro = new ResizeObserver(([e]) => setW(Math.round(e!.contentRect.width)));
|
|
391
|
+
ro.observe(el);
|
|
392
|
+
const onResize = () => setVh(window.innerHeight);
|
|
393
|
+
onResize();
|
|
394
|
+
setTouch(Boolean(window.matchMedia && window.matchMedia("(pointer: coarse)").matches));
|
|
395
|
+
setMac(/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent || ""));
|
|
396
|
+
window.addEventListener("resize", onResize);
|
|
397
|
+
return () => { ro.disconnect(); window.removeEventListener("resize", onResize); };
|
|
398
|
+
}, []);
|
|
399
|
+
|
|
400
|
+
const wide = w >= WIDE;
|
|
401
|
+
const lay = useMemo((): { L?: FlowLayout; err?: string } => {
|
|
402
|
+
try { return { L: layoutFlow(flow, { phases, lanes: wide, messages }) }; } catch (e) { return { err: (e as Error).message }; }
|
|
403
|
+
}, [flow, phases, wide, messages]);
|
|
404
|
+
const G = lay.L;
|
|
405
|
+
|
|
406
|
+
// Full screen: the canvas is as tall as the popup's frame, measured, for the first view.
|
|
407
|
+
useEffect(() => {
|
|
408
|
+
const el = stage.current;
|
|
409
|
+
if (!full || !el) return undefined;
|
|
410
|
+
const ro = new ResizeObserver(([e]) => setSh(Math.round(e!.contentRect.height)));
|
|
411
|
+
ro.observe(el);
|
|
412
|
+
return () => ro.disconnect();
|
|
413
|
+
}, [full, Boolean(G)]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
414
|
+
|
|
415
|
+
// The first view (also "back to the start", key 0): the width fitted at a size still readable; the canvas as tall as the flow
|
|
416
|
+
// (wide) or at most ~72% of the screen (narrow, room left to scroll the page). Full screen: the whole flow in the frame if it is
|
|
417
|
+
// still readable (≥ 55%), else as above.
|
|
418
|
+
const view = useMemo(() => {
|
|
419
|
+
if (!G || !w || (full && !sh)) return null;
|
|
420
|
+
const fit = (w - 2 * PAD) / G.width;
|
|
421
|
+
if (full) {
|
|
422
|
+
const all = Math.min(1, fit, (sh - TOP - PAD) / G.height);
|
|
423
|
+
if (all >= 0.55) return { x: Math.round((w - G.width * all) / 2), y: Math.round(TOP + (sh - TOP - PAD - G.height * all) / 2), zoom: all, h: sh };
|
|
424
|
+
}
|
|
425
|
+
const zoom = Math.min(1, Math.max(fit, READ_ZOOM));
|
|
426
|
+
const h = full ? sh : Math.round(Math.min(Math.max(G.height * zoom + TOP + PAD, 320), wide ? 1200 : Math.max(360, vh * 0.72)));
|
|
427
|
+
let x = (w - G.width * zoom) / 2;
|
|
428
|
+
// Too wide for the frame: a narrow stream puts the start box in the middle (branches either side, drag to see them); lanes
|
|
429
|
+
// start from the left edge (drag right for the last lane).
|
|
430
|
+
if (fit < READ_ZOOM) {
|
|
431
|
+
const s = G.nodes.find((n) => n.type === "start")!;
|
|
432
|
+
x = wide ? PAD : w / 2 - (s.position.x + s.width / 2) * zoom;
|
|
433
|
+
}
|
|
434
|
+
return { x: Math.round(x), y: TOP, zoom, h };
|
|
435
|
+
}, [G, w, vh, wide, full, sh]);
|
|
436
|
+
|
|
437
|
+
useEffect(() => { if (view) void rf.setViewport({ x: view.x, y: view.y, zoom: view.zoom }); }, [view, rf]);
|
|
438
|
+
useEffect(() => { setSel((cur) => (cur && G && G.nodes.some((n) => n.id === cur) ? cur : null)); }, [G, setSel]);
|
|
439
|
+
|
|
440
|
+
const nodes = useMemo(() => (!G ? [] : [
|
|
441
|
+
...G.bands.map((b) => ({ id: `band:${b.phase}`, type: "band", position: { x: b.x, y: b.y }, width: b.width, height: b.height,
|
|
442
|
+
data: { phase: b.phase, label: b.label, lanes: G.lanes, lane: b.lane }, draggable: false, selectable: false, focusable: false, zIndex: -1 })),
|
|
443
|
+
...G.nodes.map((n) => ({ id: n.id, type: "flow", position: n.position, width: n.width, data: { node: n.data, lane: n.lane }, draggable: false,
|
|
444
|
+
selected: n.id === sel, ariaLabel: `${L.kinds[n.type]}: ${n.data.label}` })),
|
|
445
|
+
]), [G, sel, L]);
|
|
446
|
+
const edges = useMemo(() => (!G ? [] : G.edges.map((e) => ({ ...e, selectable: false, focusable: false,
|
|
447
|
+
markerEnd: { type: MarkerType.ArrowClosed, width: 14, height: 14, color: STROKE[e.data.kind] },
|
|
448
|
+
className: sel && (e.source === sel || e.target === sel) ? "is-on" : undefined }))), [G, sel]);
|
|
449
|
+
|
|
450
|
+
// The box just picked must be seen: not under the floating details (wide), not off the canvas. Moves only, keeps the zoom.
|
|
451
|
+
const reveal = useCallback((id: string, scroll: boolean) => {
|
|
452
|
+
const n = G?.nodes.find((x) => x.id === id);
|
|
453
|
+
if (!n || !view) return;
|
|
454
|
+
const vp = rf.getViewport();
|
|
455
|
+
const left = n.position.x * vp.zoom + vp.x;
|
|
456
|
+
const top = n.position.y * vp.zoom + vp.y;
|
|
457
|
+
const right = left + n.width * vp.zoom;
|
|
458
|
+
const bottom = top + n.height * vp.zoom;
|
|
459
|
+
const maxX = (wide ? w - PANEL_W - 24 : w) - 12;
|
|
460
|
+
const dx = right > maxX ? maxX - right : left < 12 ? 12 - left : 0;
|
|
461
|
+
const dy = bottom > view.h - 12 ? view.h - 12 - bottom : top < 12 ? 12 - top : 0;
|
|
462
|
+
if (dx || dy) void rf.setViewport({ x: vp.x + dx, y: vp.y + dy, zoom: vp.zoom }, { duration: 260 });
|
|
463
|
+
if (scroll) box.current?.querySelector(`.react-flow__node[data-id="${CSS.escape(id)}"]`)?.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
|
464
|
+
}, [G, view, rf, w, wide]);
|
|
465
|
+
|
|
466
|
+
// Mouse, touch and keyboard (Tab to a box, then Enter / Space; Esc to drop it) all arrive as React Flow's "select" changes.
|
|
467
|
+
const onNodesChange = useCallback((changes: NodeChange[]) => {
|
|
468
|
+
const on = changes.find((c) => c.type === "select" && c.selected && !c.id.startsWith("band:"));
|
|
469
|
+
if (on && "id" in on) setSel(on.id);
|
|
470
|
+
else if (changes.some((c) => c.type === "select" && !c.selected)) {
|
|
471
|
+
setSel((cur) => (changes.some((c) => c.type === "select" && c.id === cur && !c.selected) ? null : cur));
|
|
472
|
+
}
|
|
473
|
+
}, [setSel]);
|
|
474
|
+
useEffect(() => { if (sel) reveal(sel, false); }, [sel, reveal]);
|
|
475
|
+
const pick = useCallback((id: string) => { setSel(id); reveal(id, true); }, [reveal, setSel]);
|
|
476
|
+
const home = useCallback(() => { if (view) void rf.setViewport({ x: view.x, y: view.y, zoom: view.zoom }, { duration: 260 }); }, [view, rf]);
|
|
477
|
+
const fit = useCallback(() => {
|
|
478
|
+
void rf.fitView({ padding: { top: `${TOP}px`, right: `${PAD}px`, bottom: `${PAD}px`, left: `${PAD}px` }, duration: 260, minZoom: FLOW_ZOOM.min, maxZoom: 1 });
|
|
479
|
+
}, [rf]);
|
|
480
|
+
|
|
481
|
+
// Zoomed in past the fit: a boolean from the store, so it redraws only when it crosses, not on every frame of a drag.
|
|
482
|
+
const wheelPans = useStore((s) => flowWheelPans({ zoom: s.transform[2], home: view?.zoom || 0, full }));
|
|
483
|
+
// ⌘ + wheel (Mac): React Flow zooms only with ctrl+wheel when preventScrolling is off, so ⌘ takes its own way, caught in the
|
|
484
|
+
// canvas frame's capture phase before React Flow. Ctrl + wheel and a pinch (also ctrl+wheel) are React Flow's.
|
|
485
|
+
useEffect(() => {
|
|
486
|
+
if (!pane) return undefined;
|
|
487
|
+
const onWheel = (e: WheelEvent) => {
|
|
488
|
+
if (!e.metaKey || e.ctrlKey) return;
|
|
489
|
+
e.preventDefault();
|
|
490
|
+
e.stopPropagation();
|
|
491
|
+
const r = (pane.querySelector(".react-flow") || pane).getBoundingClientRect();
|
|
492
|
+
void rf.setViewport(wheelZoom(rf.getViewport(), { px: e.clientX - r.left, py: e.clientY - r.top, deltaY: e.deltaY, deltaMode: e.deltaMode }));
|
|
493
|
+
};
|
|
494
|
+
pane.addEventListener("wheel", onWheel, { passive: false, capture: true });
|
|
495
|
+
return () => pane.removeEventListener("wheel", onWheel, { capture: true });
|
|
496
|
+
}, [pane, rf]);
|
|
497
|
+
|
|
498
|
+
// + / − / 0 with focus in the canvas (the frame, a box, or a button of the zoom bar).
|
|
499
|
+
const onKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
500
|
+
const act = flowZoomKey(e);
|
|
501
|
+
if (!act || (e.target as Element).closest?.("input, textarea, select, [contenteditable=\"true\"]")) return;
|
|
502
|
+
e.preventDefault();
|
|
503
|
+
if (act === "in") void rf.zoomIn({ duration: 160 });
|
|
504
|
+
else if (act === "out") void rf.zoomOut({ duration: 160 });
|
|
505
|
+
else home();
|
|
506
|
+
}, [rf, home]);
|
|
507
|
+
// A press on the canvas's ground focuses the frame (d3-zoom cancels mousedown's default), so the zoom keys work at once.
|
|
508
|
+
const focusPane = (e: React.PointerEvent) => { if (!(e.target as Element).closest?.("button, a, .react-flow__node")) pane?.focus({ preventScroll: true }); };
|
|
509
|
+
|
|
510
|
+
const node = sel ? flow.nodes.find((n) => n.id === sel) ?? null : null;
|
|
511
|
+
const laneOf = (key: string) => Math.max(0, phases.findIndex((p) => p.key === key));
|
|
512
|
+
const shown = phasesIn(flow, phases);
|
|
513
|
+
const hint = L.hint({ full, touch, key: mac ? "⌘" : "Ctrl" });
|
|
514
|
+
const details = (floating: boolean) => (node ? (
|
|
515
|
+
<Details node={node} flow={flow} lane={laneOf(node.phase)} phaseLabel={phases.find((p) => p.key === node.phase)?.label ?? node.phase}
|
|
516
|
+
floating={floating} onPick={pick} onClose={() => setSel(null)} mdClassName={mdClassName} />
|
|
517
|
+
) : null);
|
|
518
|
+
|
|
519
|
+
return (
|
|
520
|
+
<div className={`skf${full ? " skf--full" : ""}`} ref={box}>
|
|
521
|
+
<div className="skf-top">
|
|
522
|
+
<ul className="skf-legend" aria-label={L.legend}>
|
|
523
|
+
{shown.map((p) => <li key={p.key}><i className="skf-dot" data-lane={laneOf(p.key) % LANE_TONES} />{p.label}</li>)}
|
|
524
|
+
<li><i className="skf-key skf-key--decision" />{L.legendDecision}</li>
|
|
525
|
+
<li><i className="skf-key skf-key--stop" />{L.legendStop}</li>
|
|
526
|
+
{flow.nodes.some((n) => n.kind === "exit") ? <li><i className="skf-key skf-key--exit" />{L.legendExit}</li> : null}
|
|
527
|
+
{flow.edges.some((e) => e.kind === "no") ? <li><i className="skf-line skf-line--no" />{L.legendNo}</li> : null}
|
|
528
|
+
{flow.edges.some((e) => e.kind === "loop") ? <li><i className="skf-line skf-line--loop" />{L.legendLoop}</li> : null}
|
|
529
|
+
</ul>
|
|
530
|
+
<p className="skf-hint">{hint}</p>
|
|
531
|
+
</div>
|
|
532
|
+
{!full && note ? (noteTone === "bad" ? <div className="skf-note skf-note--bad" role="alert">{note}</div> : <div className="skf-note">{note}</div>) : null}
|
|
533
|
+
{lay.err ? <p className="skf-note skf-note--bad" role="alert">{L.cantDraw} ({lay.err}).</p> : null}
|
|
534
|
+
{G && (view || full) ? (
|
|
535
|
+
<div className={`skf__stage${wide ? " is-wide" : ""}`} ref={stage} style={full || !view ? undefined : { height: view.h }} onKeyDown={onKeyDown}>
|
|
536
|
+
{view ? (
|
|
537
|
+
<>
|
|
538
|
+
<div className="skf__tools"><ZoomBar full={full} onFit={fit} onReset={home} onExpand={onExpand} /></div>
|
|
539
|
+
<div className="skf__canvas" ref={setPane} tabIndex={0} role="group" aria-label={L.canvas} onPointerDown={focusPane}>
|
|
540
|
+
<ReactFlow nodes={nodes} edges={edges} nodeTypes={NODE_TYPES} edgeTypes={EDGE_TYPES} onNodesChange={onNodesChange}
|
|
541
|
+
onPaneClick={() => setSel(null)} colorMode={mode} defaultViewport={{ x: view.x, y: view.y, zoom: view.zoom }}
|
|
542
|
+
minZoom={FLOW_ZOOM.min} maxZoom={FLOW_ZOOM.max} translateExtent={[[-160, -160], [G.width + 160, G.height + 160]]}
|
|
543
|
+
nodesDraggable={false} nodesConnectable={false} elementsSelectable edgesFocusable={false} edgesReconnectable={false}
|
|
544
|
+
selectNodesOnDrag={false} multiSelectionKeyCode={null} selectionKeyCode={null} deleteKeyCode={null} zoomActivationKeyCode={null}
|
|
545
|
+
// As the GemBoss admin's canvases: drag with the left or middle button to move; ⌘ / Ctrl + wheel and a pinch zoom; a
|
|
546
|
+
// plain wheel moves the canvas in full screen or once zoomed in past the fit, and scrolls the page otherwise.
|
|
547
|
+
panOnDrag={[0, 1]} zoomOnPinch zoomOnScroll={false} panOnScroll={wheelPans} zoomOnDoubleClick={false} preventScrolling={wheelPans}>
|
|
548
|
+
<Background gap={22} size={1} />
|
|
549
|
+
</ReactFlow>
|
|
550
|
+
</div>
|
|
551
|
+
{wide && node ? <div className="skf__float">{details(true)}</div> : null}
|
|
552
|
+
</>
|
|
553
|
+
) : null}
|
|
554
|
+
</div>
|
|
555
|
+
) : !lay.err ? <div className="skf__stage skf__stage--wait" aria-hidden="true" /> : null}
|
|
556
|
+
{!wide && G ? (node ? details(full) : full ? null : <p className="skf-panel skf-panel--empty">{L.emptyPanel}</p>) : null}
|
|
557
|
+
</div>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* A read-only canvas for one flow. The picked box is shared between the canvas in the page and the full-screen one (Expand): it
|
|
563
|
+
* opens on the box you were reading.
|
|
564
|
+
*/
|
|
565
|
+
export function SkillFlow({ flow: input, phases, labels, note, noteTone, highlight = null, toolPrefixes, mdClassName, messages }: SkillFlowProps) {
|
|
566
|
+
const [sel, setSel] = useState<string | null>(null);
|
|
567
|
+
const [full, setFull] = useState(false);
|
|
568
|
+
const L = useMemo(() => skillFlowLabels(labels), [labels]);
|
|
569
|
+
// Checked once here, with the defaults filled (a node without `tools`, an edge without `kind`) and a server's example html kept:
|
|
570
|
+
// the boxes, the details and the layout all read this one copy, never the raw input.
|
|
571
|
+
const checked = useMemo((): { flow?: Flow; err?: string } => {
|
|
572
|
+
try { return { flow: validateFlow(input, { phases, allowHtml: true, messages }) }; } catch (e) { return { err: (e as Error).message }; }
|
|
573
|
+
}, [input, phases, messages]);
|
|
574
|
+
const ctx = useMemo<Ctx>(() => ({ L, prefixes: toolPrefixes, messages, dim: (phase: string) => !!highlight && phase !== highlight,
|
|
575
|
+
lit: (phase: string) => !!highlight && phase === highlight }), [L, toolPrefixes, highlight, messages]);
|
|
576
|
+
if (!checked.flow) return <div className="skf"><p className="skf-note skf-note--bad" role="alert">{L.cantDraw} ({checked.err}).</p></div>;
|
|
577
|
+
const flow = checked.flow;
|
|
578
|
+
return (
|
|
579
|
+
<FlowCtx.Provider value={ctx}>
|
|
580
|
+
<ReactFlowProvider>
|
|
581
|
+
<Canvas flow={flow} phases={phases} sel={sel} setSel={setSel} onExpand={() => setFull(true)} note={note} noteTone={noteTone} mdClassName={mdClassName} />
|
|
582
|
+
</ReactFlowProvider>
|
|
583
|
+
{full ? (
|
|
584
|
+
// The kit's ReaderDialog, opened up to nearly the whole window (skill-flow.css): Esc / ✕ close it alone, the popup behind stays.
|
|
585
|
+
<ReaderDialog open onClose={() => setFull(false)} className="skf-pop" title={L.fullTitle} subtitle={L.fullSubtitle}>
|
|
586
|
+
<ReactFlowProvider>
|
|
587
|
+
<Canvas full flow={flow} phases={phases} sel={sel} setSel={setSel} onExpand={() => setFull(false)} mdClassName={mdClassName} />
|
|
588
|
+
</ReactFlowProvider>
|
|
589
|
+
</ReaderDialog>
|
|
590
|
+
) : null}
|
|
591
|
+
</FlowCtx.Provider>
|
|
592
|
+
);
|
|
593
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@gemboss/ui/skill-flow` (03/11/2026): a skill's flow chart. Its OWN entry, never re-exported by the kit's index: the canvas
|
|
3
|
+
* brings React Flow and the layout brings dagre (both optional peers of the kit), and a surface that never draws a flow must not
|
|
4
|
+
* load either. Import it lazily (next/dynamic with `ssr: false`, React.lazy), and only where a flow is drawn.
|
|
5
|
+
*
|
|
6
|
+
* The data side alone (checking a flow.json on a server, the zoom maths in a test) is `@gemboss/ui/skill-flow/model`: no peer.
|
|
7
|
+
* `@gemboss/ui/skill-flow/layout` is `layoutFlow` alone (dagre, no React). The published package carries both as plain JavaScript
|
|
8
|
+
* too, under the `node` condition, so Node without a TypeScript step imports them (scripts/pack-lib.mjs NODE_COPIES, 04/11/2026).
|
|
9
|
+
*/
|
|
10
|
+
export { ExampleMd, SkillFlow, SKILL_FLOW_LABELS, skillFlowLabels, type SkillFlowLabels, type SkillFlowProps } from "./canvas";
|
|
11
|
+
export { layoutFlow, nodeSize, type FlowBand, type FlowLayout, type LaidEdge, type LaidNode } from "./layout";
|
|
12
|
+
export * from "./model";
|