Prune files not in the deploy set (stale build context)
Browse files
web/src/automation/AutomationCanvas.tsx
DELETED
|
@@ -1,396 +0,0 @@
|
|
| 1 |
-
// ---------------------------------------------------------------------------
|
| 2 |
-
// automation/AutomationCanvas.tsx — the flow, drawn (owner ruling R5).
|
| 3 |
-
//
|
| 4 |
-
// ⛔ UNMOUNTED SINCE WAVE 21 (owner ruling R9 — "trigger-first Steps"). Nothing
|
| 5 |
-
// imports this file: `AutomationDetail` renders `AutomationSteps` instead, and
|
| 6 |
-
// the `.auto-canvas* / .auto-node* / .auto-wire* / .auto-zoom*` CSS was removed
|
| 7 |
-
// from `index.css` with the mount. It is kept rather than deleted because R9
|
| 8 |
-
// named a rebuild and not a deletion — but REMOUNTING IT WOULD PAINT AN UNSTYLED
|
| 9 |
-
// FLOW: its stylesheet comes back with it. Booked in the wave-21 mailbox.
|
| 10 |
-
//
|
| 11 |
-
// Hand-rolled SVG + CSS, no new dependency: the repo already draws its own
|
| 12 |
-
// charts and maps this way (`web/src/viz/`), and a flow of four to six nodes is
|
| 13 |
-
// not worth a graph library's bundle, its theming fight, or its opinion about
|
| 14 |
-
// where state lives.
|
| 15 |
-
//
|
| 16 |
-
// ⚠ BRANCHES RENDER; THEY DO NOT EXECUTE (R5, accepted at grill — DEBT D-7).
|
| 17 |
-
// The capture branch is a real fork in BEHAVIOUR — the paid rung answers or the
|
| 18 |
-
// anonymous ladder does — and the engine still walks it linearly. Nothing here
|
| 19 |
-
// should be read as a general branching runtime.
|
| 20 |
-
//
|
| 21 |
-
// TWO TRAPS THIS FILE IS BUILT AROUND, both from [[ui-invisible-to-assertions]]
|
| 22 |
-
// (a thing can be painted, asserted, and still not usable):
|
| 23 |
-
// 1. THE EDGE LAYER MUST NOT EAT CLICKS. It is a full-bleed <svg> sitting over
|
| 24 |
-
// the same area as the nodes, so it carries `pointer-events: none` and the
|
| 25 |
-
// node buttons stay hittable. A canvas whose nodes cannot be clicked passes
|
| 26 |
-
// every DOM assertion ever written about it.
|
| 27 |
-
// 2. A PAN MUST NOT FIRE A CLICK. Dragging the background across a node ends
|
| 28 |
-
// with a click event on whatever is under the pointer, so a drag that moved
|
| 29 |
-
// more than a few pixels suppresses the click it would otherwise produce.
|
| 30 |
-
// ---------------------------------------------------------------------------
|
| 31 |
-
import type { PointerEvent as ReactPointerEvent } from "react";
|
| 32 |
-
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
| 33 |
-
|
| 34 |
-
import type { AutomationGraph, GraphNode } from "./automationApi";
|
| 35 |
-
import type { Placed, Viewport } from "./canvasLayout";
|
| 36 |
-
import {
|
| 37 |
-
IDENTITY,
|
| 38 |
-
MAX_ZOOM,
|
| 39 |
-
MIN_ZOOM,
|
| 40 |
-
NODE_H,
|
| 41 |
-
NODE_W,
|
| 42 |
-
bounds,
|
| 43 |
-
clampZoom,
|
| 44 |
-
fit,
|
| 45 |
-
place,
|
| 46 |
-
wires,
|
| 47 |
-
zoomAt,
|
| 48 |
-
} from "./canvasLayout";
|
| 49 |
-
|
| 50 |
-
interface Props {
|
| 51 |
-
graph: AutomationGraph;
|
| 52 |
-
selected: string;
|
| 53 |
-
onSelect: (nodeId: string) => void;
|
| 54 |
-
/** Flip a node's switch. Absent (or a node with no `toggle`) = no switch drawn. */
|
| 55 |
-
onToggle?: (nodeId: string) => void;
|
| 56 |
-
busyNode?: string;
|
| 57 |
-
/** Shown on the trigger node when the server knows the next fire time. */
|
| 58 |
-
nextRunAt?: string;
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
/** How far a pointer may travel and still count as a click, not a drag. */
|
| 62 |
-
const DRAG_SLOP = 4;
|
| 63 |
-
/** Room left around the flow for the SVG layer, so a curve never clips. */
|
| 64 |
-
const SVG_PAD = 260;
|
| 65 |
-
|
| 66 |
-
function KindMark({ kind }: { kind: GraphNode["kind"] }) {
|
| 67 |
-
// Drawn paths, never emojis (owner constant). Each mark says what the step DOES
|
| 68 |
-
// at a glance: a clock schedules, a globe reaches out, an arrow brings back, a
|
| 69 |
-
// fork chooses, a cylinder stores.
|
| 70 |
-
const common = {
|
| 71 |
-
width: 15,
|
| 72 |
-
height: 15,
|
| 73 |
-
viewBox: "0 0 16 16",
|
| 74 |
-
fill: "none",
|
| 75 |
-
stroke: "currentColor",
|
| 76 |
-
strokeWidth: 1.4,
|
| 77 |
-
strokeLinecap: "round" as const,
|
| 78 |
-
strokeLinejoin: "round" as const,
|
| 79 |
-
"aria-hidden": true,
|
| 80 |
-
};
|
| 81 |
-
if (kind === "trigger")
|
| 82 |
-
return (
|
| 83 |
-
<svg {...common}>
|
| 84 |
-
<circle cx="8" cy="8" r="5.6" />
|
| 85 |
-
<path d="M8 5.2V8l2 1.4" />
|
| 86 |
-
</svg>
|
| 87 |
-
);
|
| 88 |
-
if (kind === "source")
|
| 89 |
-
return (
|
| 90 |
-
<svg {...common}>
|
| 91 |
-
<circle cx="8" cy="8" r="5.6" />
|
| 92 |
-
<path d="M2.6 8h10.8M8 2.4c1.5 1.7 2.2 3.6 2.2 5.6S9.5 12.3 8 13.6C6.5 12.3 5.8 10 5.8 8s.7-3.9 2.2-5.6z" />
|
| 93 |
-
</svg>
|
| 94 |
-
);
|
| 95 |
-
if (kind === "branch")
|
| 96 |
-
return (
|
| 97 |
-
<svg {...common}>
|
| 98 |
-
<path d="M2.6 8h3.2l2.6-3.6h5M8.4 11.6H5.8m2.6 0h5" />
|
| 99 |
-
<path d="M11.6 2.6 13.4 4.4 11.6 6.2M11.6 9.8l1.8 1.8-1.8 1.8" />
|
| 100 |
-
</svg>
|
| 101 |
-
);
|
| 102 |
-
if (kind === "write")
|
| 103 |
-
return (
|
| 104 |
-
<svg {...common}>
|
| 105 |
-
<ellipse cx="8" cy="4.2" rx="4.8" ry="1.9" />
|
| 106 |
-
<path d="M3.2 4.2v7.6c0 1 2.1 1.9 4.8 1.9s4.8-.9 4.8-1.9V4.2M3.2 8c0 1 2.1 1.9 4.8 1.9s4.8-.9 4.8-1.9" />
|
| 107 |
-
</svg>
|
| 108 |
-
);
|
| 109 |
-
return (
|
| 110 |
-
<svg {...common}>
|
| 111 |
-
<path d="M8 2.6v7.2M5.2 7l2.8 2.8L10.8 7M3 12.4h10" />
|
| 112 |
-
</svg>
|
| 113 |
-
);
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
export default function AutomationCanvas({
|
| 117 |
-
graph,
|
| 118 |
-
selected,
|
| 119 |
-
onSelect,
|
| 120 |
-
onToggle,
|
| 121 |
-
busyNode,
|
| 122 |
-
nextRunAt,
|
| 123 |
-
}: Props) {
|
| 124 |
-
const hostRef = useRef<HTMLDivElement | null>(null);
|
| 125 |
-
const [view, setView] = useState<Viewport>(IDENTITY);
|
| 126 |
-
const drag = useRef<{ px: number; py: number; vx: number; vy: number; moved: boolean } | null>(
|
| 127 |
-
null
|
| 128 |
-
);
|
| 129 |
-
|
| 130 |
-
const placed = useMemo(() => place(graph?.nodes || []), [graph]);
|
| 131 |
-
const lines = useMemo(() => wires(graph, placed), [graph, placed]);
|
| 132 |
-
const box = useMemo(() => bounds(placed), [placed]);
|
| 133 |
-
|
| 134 |
-
const fitNow = useCallback(() => {
|
| 135 |
-
const el = hostRef.current;
|
| 136 |
-
if (!el) return;
|
| 137 |
-
const r = el.getBoundingClientRect();
|
| 138 |
-
setView(fit(placed, r.width, r.height));
|
| 139 |
-
}, [placed]);
|
| 140 |
-
|
| 141 |
-
// Fit on mount and whenever the SHAPE changes. Keyed on the node ids rather
|
| 142 |
-
// than the graph object: a status dot moving from idle to ok is a new object
|
| 143 |
-
// every poll, and re-fitting the canvas under someone mid-inspection because a
|
| 144 |
-
// dot changed colour would be its own small betrayal.
|
| 145 |
-
const shape = placed.map((p) => `${p.id}:${p.col}:${p.row}`).join("|");
|
| 146 |
-
useLayoutEffect(() => {
|
| 147 |
-
fitNow();
|
| 148 |
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
| 149 |
-
}, [shape]);
|
| 150 |
-
|
| 151 |
-
// ⚠ THE ZERO-SIZE MOUNT. If this lands before the flex parent has a height —
|
| 152 |
-
// a collapsed rail animating open, a hidden tab becoming visible — the fit
|
| 153 |
-
// above measures 0×0, silently returns the identity viewport, and the flow
|
| 154 |
-
// sits jammed in the top-left corner looking like a layout bug. So: re-fit
|
| 155 |
-
// exactly ONCE, on the transition from no-size to size. Deliberately not a
|
| 156 |
-
// general re-fit-on-resize — that would yank the canvas back to centre every
|
| 157 |
-
// time the window moved, fighting a pan the user had just made.
|
| 158 |
-
useEffect(() => {
|
| 159 |
-
const el = hostRef.current;
|
| 160 |
-
if (!el || typeof ResizeObserver === "undefined") return undefined;
|
| 161 |
-
let hadSize = el.getBoundingClientRect().width > 0;
|
| 162 |
-
const ro = new ResizeObserver(() => {
|
| 163 |
-
const wide = el.getBoundingClientRect().width > 0;
|
| 164 |
-
if (wide && !hadSize) fitNow();
|
| 165 |
-
hadSize = wide;
|
| 166 |
-
});
|
| 167 |
-
ro.observe(el);
|
| 168 |
-
return () => ro.disconnect();
|
| 169 |
-
}, [fitNow]);
|
| 170 |
-
|
| 171 |
-
// Wheel zoom, on a NON-PASSIVE native listener. React attaches wheel handlers
|
| 172 |
-
// passively, so `preventDefault()` inside an `onWheel` prop is ignored and the
|
| 173 |
-
// page scrolls behind the canvas instead of the canvas zooming.
|
| 174 |
-
useEffect(() => {
|
| 175 |
-
const el = hostRef.current;
|
| 176 |
-
if (!el) return undefined;
|
| 177 |
-
const onWheel = (e: WheelEvent) => {
|
| 178 |
-
e.preventDefault();
|
| 179 |
-
const r = el.getBoundingClientRect();
|
| 180 |
-
setView((v) =>
|
| 181 |
-
zoomAt(v, v.k * (e.deltaY < 0 ? 1.12 : 1 / 1.12), e.clientX - r.left, e.clientY - r.top)
|
| 182 |
-
);
|
| 183 |
-
};
|
| 184 |
-
el.addEventListener("wheel", onWheel, { passive: false });
|
| 185 |
-
return () => el.removeEventListener("wheel", onWheel);
|
| 186 |
-
}, []);
|
| 187 |
-
|
| 188 |
-
const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {
|
| 189 |
-
if (e.button !== 0) return;
|
| 190 |
-
(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
|
| 191 |
-
drag.current = { px: e.clientX, py: e.clientY, vx: view.x, vy: view.y, moved: false };
|
| 192 |
-
};
|
| 193 |
-
|
| 194 |
-
const onPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {
|
| 195 |
-
const d = drag.current;
|
| 196 |
-
if (!d) return;
|
| 197 |
-
const dx = e.clientX - d.px;
|
| 198 |
-
const dy = e.clientY - d.py;
|
| 199 |
-
if (!d.moved && Math.abs(dx) + Math.abs(dy) < DRAG_SLOP) return;
|
| 200 |
-
d.moved = true;
|
| 201 |
-
setView((v) => ({ ...v, x: d.vx + dx, y: d.vy + dy }));
|
| 202 |
-
};
|
| 203 |
-
|
| 204 |
-
const endDrag = (e: ReactPointerEvent<HTMLDivElement>) => {
|
| 205 |
-
if (drag.current) {
|
| 206 |
-
try {
|
| 207 |
-
(e.currentTarget as HTMLElement).releasePointerCapture(e.pointerId);
|
| 208 |
-
} catch {
|
| 209 |
-
// The pointer was already released (a lost capture on window blur). Not
|
| 210 |
-
// a failure — the drag is over either way.
|
| 211 |
-
}
|
| 212 |
-
}
|
| 213 |
-
// Trap 2: leave the "did it move" flag readable for the click that follows.
|
| 214 |
-
window.setTimeout(() => {
|
| 215 |
-
drag.current = null;
|
| 216 |
-
}, 0);
|
| 217 |
-
};
|
| 218 |
-
|
| 219 |
-
const swallowClick = () => drag.current?.moved === true;
|
| 220 |
-
|
| 221 |
-
const zoomBy = (factor: number) => {
|
| 222 |
-
const el = hostRef.current;
|
| 223 |
-
if (!el) return;
|
| 224 |
-
const r = el.getBoundingClientRect();
|
| 225 |
-
setView((v) => zoomAt(v, v.k * factor, r.width / 2, r.height / 2));
|
| 226 |
-
};
|
| 227 |
-
|
| 228 |
-
return (
|
| 229 |
-
<div className="auto-canvas-wrap">
|
| 230 |
-
<div
|
| 231 |
-
className="auto-canvas"
|
| 232 |
-
ref={hostRef}
|
| 233 |
-
// `group`, NOT `application`. Every node here is a real <button>, so a
|
| 234 |
-
// screen reader should keep browsing them normally; `application` would
|
| 235 |
-
// switch AT into forms mode and hand it a pan/zoom surface it cannot
|
| 236 |
-
// drive instead of the list of steps it can.
|
| 237 |
-
role="group"
|
| 238 |
-
aria-label="Automation flow"
|
| 239 |
-
onPointerDown={onPointerDown}
|
| 240 |
-
onPointerMove={onPointerMove}
|
| 241 |
-
onPointerUp={endDrag}
|
| 242 |
-
onPointerCancel={endDrag}
|
| 243 |
-
>
|
| 244 |
-
<div
|
| 245 |
-
className="auto-scene"
|
| 246 |
-
style={{ transform: `translate(${view.x}px, ${view.y}px) scale(${view.k})` }}
|
| 247 |
-
>
|
| 248 |
-
{/* Trap 1: this layer is decorative and must never intercept a click. */}
|
| 249 |
-
<svg
|
| 250 |
-
className="auto-wires"
|
| 251 |
-
style={{
|
| 252 |
-
left: box.minX - SVG_PAD,
|
| 253 |
-
top: box.minY - SVG_PAD,
|
| 254 |
-
width: box.w + SVG_PAD * 2,
|
| 255 |
-
height: box.h + SVG_PAD * 2,
|
| 256 |
-
}}
|
| 257 |
-
aria-hidden="true"
|
| 258 |
-
>
|
| 259 |
-
<g transform={`translate(${SVG_PAD - box.minX}, ${SVG_PAD - box.minY})`}>
|
| 260 |
-
{lines.map((w) => (
|
| 261 |
-
<g key={`${w.from}->${w.to}`} className={"auto-wire" + (w.dim ? " is-dim" : "")}>
|
| 262 |
-
<path d={w.d} />
|
| 263 |
-
{w.label ? (
|
| 264 |
-
<text x={w.mid[0]} y={w.mid[1]} textAnchor="middle">
|
| 265 |
-
{w.label}
|
| 266 |
-
</text>
|
| 267 |
-
) : null}
|
| 268 |
-
</g>
|
| 269 |
-
))}
|
| 270 |
-
</g>
|
| 271 |
-
</svg>
|
| 272 |
-
|
| 273 |
-
{placed.map((p: Placed, i) => (
|
| 274 |
-
<div
|
| 275 |
-
key={p.id}
|
| 276 |
-
className={
|
| 277 |
-
"auto-node" +
|
| 278 |
-
(p.id === selected ? " is-selected" : "") +
|
| 279 |
-
(p.enabled ? "" : " is-off") +
|
| 280 |
-
` is-${p.kind}`
|
| 281 |
-
}
|
| 282 |
-
style={{ left: p.x, top: p.y, width: NODE_W, height: NODE_H }}
|
| 283 |
-
>
|
| 284 |
-
<button
|
| 285 |
-
type="button"
|
| 286 |
-
className="auto-node-hit"
|
| 287 |
-
aria-pressed={p.id === selected}
|
| 288 |
-
title={`${p.title} — ${p.subtitle}`}
|
| 289 |
-
onClick={() => {
|
| 290 |
-
if (swallowClick()) return;
|
| 291 |
-
onSelect(p.id);
|
| 292 |
-
}}
|
| 293 |
-
>
|
| 294 |
-
<span className="auto-node-head">
|
| 295 |
-
<span className="auto-node-mark">
|
| 296 |
-
<KindMark kind={p.kind} />
|
| 297 |
-
</span>
|
| 298 |
-
<span className="auto-node-title">{p.title}</span>
|
| 299 |
-
</span>
|
| 300 |
-
<span className="auto-node-sub">{p.subtitle}</span>
|
| 301 |
-
<span className="auto-node-detail">
|
| 302 |
-
{p.id === "trigger" && nextRunAt && p.enabled ? `Next ${nextRunAt}` : p.detail}
|
| 303 |
-
</span>
|
| 304 |
-
<span className="auto-node-foot">
|
| 305 |
-
<span className="auto-node-step">{i + 1}</span>
|
| 306 |
-
<span
|
| 307 |
-
className={`auto-dot is-${p.status}`}
|
| 308 |
-
title={`Last run: ${p.status}`}
|
| 309 |
-
aria-label={`Last run: ${p.status}`}
|
| 310 |
-
/>
|
| 311 |
-
</span>
|
| 312 |
-
</button>
|
| 313 |
-
{p.toggle && onToggle ? (
|
| 314 |
-
<button
|
| 315 |
-
type="button"
|
| 316 |
-
className={"auto-node-switch" + (p.enabled ? " is-on" : "")}
|
| 317 |
-
disabled={busyNode === p.id}
|
| 318 |
-
aria-pressed={p.enabled}
|
| 319 |
-
aria-label={`${p.enabled ? "Turn off" : "Turn on"} ${p.title}`}
|
| 320 |
-
title={`${p.enabled ? "Turn off" : "Turn on"} ${p.title}`}
|
| 321 |
-
onClick={(e) => {
|
| 322 |
-
e.stopPropagation();
|
| 323 |
-
if (swallowClick()) return;
|
| 324 |
-
onToggle(p.id);
|
| 325 |
-
}}
|
| 326 |
-
>
|
| 327 |
-
<span className="auto-node-switch-knob" />
|
| 328 |
-
</button>
|
| 329 |
-
) : (
|
| 330 |
-
// A step with no honest switch says so, rather than carrying a
|
| 331 |
-
// control that would do nothing (R5's "every node toggleable"
|
| 332 |
-
// ends where a real behaviour change ends).
|
| 333 |
-
<span className="auto-node-always" title="This step is what the automation is">
|
| 334 |
-
Always
|
| 335 |
-
</span>
|
| 336 |
-
)}
|
| 337 |
-
</div>
|
| 338 |
-
))}
|
| 339 |
-
</div>
|
| 340 |
-
</div>
|
| 341 |
-
|
| 342 |
-
<div className="auto-zoom" role="group" aria-label="Canvas zoom">
|
| 343 |
-
<button type="button" className="auto-zoom-btn" title="Fit the flow" onClick={fitNow}>
|
| 344 |
-
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
| 345 |
-
<path
|
| 346 |
-
d="M2.6 5.8V2.6h3.2M13.4 5.8V2.6h-3.2M2.6 10.2v3.2h3.2M13.4 10.2v3.2h-3.2"
|
| 347 |
-
stroke="currentColor"
|
| 348 |
-
strokeWidth="1.4"
|
| 349 |
-
strokeLinecap="round"
|
| 350 |
-
strokeLinejoin="round"
|
| 351 |
-
/>
|
| 352 |
-
</svg>
|
| 353 |
-
</button>
|
| 354 |
-
<button
|
| 355 |
-
type="button"
|
| 356 |
-
className="auto-zoom-btn"
|
| 357 |
-
title="Zoom out"
|
| 358 |
-
aria-label="Zoom out"
|
| 359 |
-
disabled={view.k <= MIN_ZOOM + 0.001}
|
| 360 |
-
onClick={() => zoomBy(1 / 1.2)}
|
| 361 |
-
>
|
| 362 |
-
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
| 363 |
-
<path d="M3.6 8h8.8" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
|
| 364 |
-
</svg>
|
| 365 |
-
</button>
|
| 366 |
-
<button
|
| 367 |
-
type="button"
|
| 368 |
-
className="auto-zoom-pct"
|
| 369 |
-
title="Back to 100%"
|
| 370 |
-
onClick={() => setView((v) => ({ ...v, k: clampZoom(1) }))}
|
| 371 |
-
>
|
| 372 |
-
{Math.round(view.k * 100)}%
|
| 373 |
-
</button>
|
| 374 |
-
<button
|
| 375 |
-
type="button"
|
| 376 |
-
className="auto-zoom-btn"
|
| 377 |
-
title="Zoom in"
|
| 378 |
-
aria-label="Zoom in"
|
| 379 |
-
disabled={view.k >= MAX_ZOOM - 0.001}
|
| 380 |
-
onClick={() => zoomBy(1.2)}
|
| 381 |
-
>
|
| 382 |
-
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
| 383 |
-
<path
|
| 384 |
-
d="M8 3.6v8.8M3.6 8h8.8"
|
| 385 |
-
stroke="currentColor"
|
| 386 |
-
strokeWidth="1.5"
|
| 387 |
-
strokeLinecap="round"
|
| 388 |
-
/>
|
| 389 |
-
</svg>
|
| 390 |
-
</button>
|
| 391 |
-
</div>
|
| 392 |
-
</div>
|
| 393 |
-
);
|
| 394 |
-
}
|
| 395 |
-
|
| 396 |
-
export { NODE_H, NODE_W };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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------------------------------------------------------------------------
|
| 2 |
-
// automation/canvasLayout.ts — pixels for the node canvas, and NOTHING else.
|
| 3 |
-
//
|
| 4 |
-
// THE DIVISION OF LABOUR, stated once: the SERVER owns the topology (which nodes
|
| 5 |
-
// exist, what they are called, which edges join them, what each node's dot says
|
| 6 |
-
// — `engine.graph()`), and this file owns where they land on screen. That split
|
| 7 |
-
// is why the canvas cannot describe a step the engine stopped running: there is
|
| 8 |
-
// no second opinion about what the steps are, only about their coordinates.
|
| 9 |
-
//
|
| 10 |
-
// Pure functions over plain data — no React, no DOM, no measurement. Everything
|
| 11 |
-
// here is deterministic from `col`/`row`, which is what makes fit-to-view and
|
| 12 |
-
// zoom-about-the-cursor arithmetic rather than layout thrash.
|
| 13 |
-
// ---------------------------------------------------------------------------
|
| 14 |
-
import type { AutomationGraph, GraphEdge, GraphNode } from "./automationApi";
|
| 15 |
-
|
| 16 |
-
/** One node's box. Tuned against `reference/Sola 1.png`: wide enough for a
|
| 17 |
-
* sentence-case title plus two muted lines, short enough that a four-node flow
|
| 18 |
-
* fits a laptop viewport at 100%. */
|
| 19 |
-
export const NODE_W = 208;
|
| 20 |
-
export const NODE_H = 96;
|
| 21 |
-
/** The gap a `col` step costs. The edge draws in it, so it must fit a label. */
|
| 22 |
-
export const COL_GAP = 104;
|
| 23 |
-
/** The gap a `row` step costs. Rows are −1 / 0 / +1 around the spine. */
|
| 24 |
-
export const ROW_GAP = 132;
|
| 25 |
-
/** Breathing room around the whole flow when fitting it to the viewport. */
|
| 26 |
-
export const PAD = 56;
|
| 27 |
-
|
| 28 |
-
export const MIN_ZOOM = 0.35;
|
| 29 |
-
export const MAX_ZOOM = 2;
|
| 30 |
-
|
| 31 |
-
export interface Placed extends GraphNode {
|
| 32 |
-
x: number;
|
| 33 |
-
y: number;
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
export interface Viewport {
|
| 37 |
-
/** Scene-space translation, in screen pixels (applied before scale). */
|
| 38 |
-
x: number;
|
| 39 |
-
y: number;
|
| 40 |
-
/** Scale. `1` = 100%. */
|
| 41 |
-
k: number;
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
export const IDENTITY: Viewport = { x: 0, y: 0, k: 1 };
|
| 45 |
-
|
| 46 |
-
/** Grid coordinates → pixels. The only place the two are related. */
|
| 47 |
-
export function place(nodes: GraphNode[]): Placed[] {
|
| 48 |
-
return nodes.map((n) => ({
|
| 49 |
-
...n,
|
| 50 |
-
x: n.col * (NODE_W + COL_GAP),
|
| 51 |
-
y: n.row * ROW_GAP,
|
| 52 |
-
}));
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
export interface Box {
|
| 56 |
-
minX: number;
|
| 57 |
-
minY: number;
|
| 58 |
-
maxX: number;
|
| 59 |
-
maxY: number;
|
| 60 |
-
w: number;
|
| 61 |
-
h: number;
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
export function bounds(placed: Placed[]): Box {
|
| 65 |
-
if (!placed.length) return { minX: 0, minY: 0, maxX: NODE_W, maxY: NODE_H, w: NODE_W, h: NODE_H };
|
| 66 |
-
const minX = Math.min(...placed.map((p) => p.x));
|
| 67 |
-
const minY = Math.min(...placed.map((p) => p.y));
|
| 68 |
-
const maxX = Math.max(...placed.map((p) => p.x + NODE_W));
|
| 69 |
-
const maxY = Math.max(...placed.map((p) => p.y + NODE_H));
|
| 70 |
-
return { minX, minY, maxX, maxY, w: maxX - minX, h: maxY - minY };
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
export function clampZoom(k: number): number {
|
| 74 |
-
return Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, k));
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
/**
|
| 78 |
-
* The viewport that centres the whole flow in a `w × h` viewport.
|
| 79 |
-
*
|
| 80 |
-
* Capped at 1: a two-node automation blown up to 200% looks broken, not roomy.
|
| 81 |
-
* Zooming IN past 100% is something a user asks for; it is never a default.
|
| 82 |
-
*/
|
| 83 |
-
export function fit(placed: Placed[], w: number, h: number): Viewport {
|
| 84 |
-
const b = bounds(placed);
|
| 85 |
-
if (w <= 0 || h <= 0) return IDENTITY;
|
| 86 |
-
const k = clampZoom(Math.min(1, (w - PAD * 2) / b.w, (h - PAD * 2) / b.h));
|
| 87 |
-
return {
|
| 88 |
-
k,
|
| 89 |
-
x: (w - b.w * k) / 2 - b.minX * k,
|
| 90 |
-
y: (h - b.h * k) / 2 - b.minY * k,
|
| 91 |
-
};
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
/**
|
| 95 |
-
* Zoom about a fixed screen point — the pixel under the cursor stays under the
|
| 96 |
-
* cursor. Without this a wheel-zoom walks the flow off screen and every zoom
|
| 97 |
-
* needs a pan to undo it.
|
| 98 |
-
*/
|
| 99 |
-
export function zoomAt(v: Viewport, k: number, px: number, py: number): Viewport {
|
| 100 |
-
const next = clampZoom(k);
|
| 101 |
-
const ratio = next / v.k;
|
| 102 |
-
return { k: next, x: px - (px - v.x) * ratio, y: py - (py - v.y) * ratio };
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
/** A node's output port, in scene space. */
|
| 106 |
-
export function outPort(p: Placed): [number, number] {
|
| 107 |
-
return [p.x + NODE_W, p.y + NODE_H / 2];
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
/** A node's input port, in scene space. */
|
| 111 |
-
export function inPort(p: Placed): [number, number] {
|
| 112 |
-
return [p.x, p.y + NODE_H / 2];
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
/**
|
| 116 |
-
* The wire between two nodes.
|
| 117 |
-
*
|
| 118 |
-
* A cubic with HORIZONTAL control handles, so an edge always leaves a node
|
| 119 |
-
* sideways and arrives sideways — that is what makes a branch read as one thing
|
| 120 |
-
* splitting rather than two unrelated lines crossing. Same-row edges come out
|
| 121 |
-
* straight because both control points sit on the same y.
|
| 122 |
-
*/
|
| 123 |
-
export function edgePath(from: Placed, to: Placed): string {
|
| 124 |
-
const [x1, y1] = outPort(from);
|
| 125 |
-
const [x2, y2] = inPort(to);
|
| 126 |
-
const bend = Math.max(36, Math.abs(x2 - x1) * 0.45);
|
| 127 |
-
return `M ${x1} ${y1} C ${x1 + bend} ${y1}, ${x2 - bend} ${y2}, ${x2} ${y2}`;
|
| 128 |
-
}
|
| 129 |
-
|
| 130 |
-
/** The midpoint of an edge, for its label. */
|
| 131 |
-
export function edgeMid(from: Placed, to: Placed): [number, number] {
|
| 132 |
-
const [x1, y1] = outPort(from);
|
| 133 |
-
const [x2, y2] = inPort(to);
|
| 134 |
-
return [(x1 + x2) / 2, (y1 + y2) / 2 - 8];
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
export interface Wire extends GraphEdge {
|
| 138 |
-
d: string;
|
| 139 |
-
mid: [number, number];
|
| 140 |
-
/** An edge is dim when EITHER end is switched off — a wire into a disabled
|
| 141 |
-
* step is not a path this automation takes. */
|
| 142 |
-
dim: boolean;
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
export function wires(graph: AutomationGraph, placed: Placed[]): Wire[] {
|
| 146 |
-
const by = new Map(placed.map((p) => [p.id, p]));
|
| 147 |
-
const out: Wire[] = [];
|
| 148 |
-
for (const e of graph.edges || []) {
|
| 149 |
-
const a = by.get(e.from);
|
| 150 |
-
const b = by.get(e.to);
|
| 151 |
-
// An edge naming a node that is not there is dropped rather than drawn to
|
| 152 |
-
// (0,0) — a wire into the corner looks like a bug in the flow, when it is a
|
| 153 |
-
// bug in the payload.
|
| 154 |
-
if (!a || !b) continue;
|
| 155 |
-
out.push({
|
| 156 |
-
...e,
|
| 157 |
-
d: edgePath(a, b),
|
| 158 |
-
mid: edgeMid(a, b),
|
| 159 |
-
dim: !a.enabled || !b.enabled,
|
| 160 |
-
});
|
| 161 |
-
}
|
| 162 |
-
return out;
|
| 163 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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