/** * How an edge paints, design §4.4 and §11. * * Pure: knob names in, a style out. No React, no `@xyflow`, no DOM — so the * table can be asserted without mounting anything, which is what makes * `edgeStyle.spec.ts` a cheap test rather than a render. * * Stroke width comes from the `borderWidth` knob, so one flip moves every edge * on the canvas. The kind picks only a WEIGHT on top of it: control edges * (flow, error) stroke `controlEdgeWeight` times the value edges (data, * conditional), because a one-step shade apart the two read as one line * (FZ-9012). Task 1.7's * `theme/graphVars.ts` reads `edgeStrokeWidth` from here rather than repeating * the ladder — §11's borderWidth row cites that file, and one ladder in two * places is one ladder that can drift. */ import type { Animation, BorderWidth } from "@multiplatform.one/theme"; import type { BuiltinEdgeKind } from "./edgeKinds"; export type EdgePathKind = "bezier" | "smoothstep" | "step" | "straight"; export type EdgeMarker = "arrow" | "arrowclosed"; export type EdgeWeight = "control" | "value"; export interface EdgeAppearance { readonly path: EdgePathKind; /** A token expression. A hex literal here fails `edgeStyle.spec.ts`. */ readonly stroke: string; /** SVG `stroke-dasharray`. Absent means a solid line. */ readonly dash?: string; readonly marker: EdgeMarker; readonly weight: EdgeWeight; } /** Design §4.4's table, verbatim. */ export const edgeAppearances: Readonly> = { flow: { path: "smoothstep", stroke: "$color-9", marker: "arrowclosed", weight: "control" }, data: { path: "bezier", stroke: "$color-8", marker: "arrow", weight: "value" }, error: { path: "smoothstep", stroke: "$red-9", dash: "6 4", marker: "arrowclosed", weight: "control", }, conditional: { path: "step", stroke: "$color-8", dash: "2 4", marker: "arrow", weight: "value" }, }; /** * How much heavier a control edge strokes than a value edge, at every * `borderWidth` stop. A ratio rather than a second ladder, so the knob still * moves both together and the gap between them never closes. */ export const controlEdgeWeight = 1.5; /** * §11: an edge is a stroke, not a border, and a 0-width edge erases the graph's * primary information. The theme's own border widths are 0 / hairline / 1 / 2 * (`resolveKnobs.ts` `borderWidthMap`, LC-148), so this is a remap keyed by the * resolved stop (`knobProps.borderWidthStop`), and * deliberately not a floor to `hairlineWidth` — that token is dividers-only by * its own header, and a stroke that stays legible while the canvas zooms out is * not a divider. */ export const edgeStrokeWidths: Readonly> = { none: 1, small: 1.5, medium: 2, large: 3, }; export function edgeStrokeWidth(borderWidth: BorderWidth, weight: EdgeWeight = "value"): number { const base = edgeStrokeWidths[borderWidth]; return weight === "control" ? base * controlEdgeWeight : base; } /** The keyframes live in `css/graph.css`, which lands with task 1.7. */ export const dashAnimation = "mp-graph-dash 0.5s linear infinite"; function isBuiltin(name: string): name is BuiltinEdgeKind { return name in edgeAppearances; } /** * The marker TYPE for an edge object, for a consumer that builds React Flow * edges and lets React Flow draw the head. * * The house canvas does not: React Flow paints its own `` defs in * `defaultMarkerColor`, a grey that matches no edge kind, so `GraphEdge` draws * the head itself from `resolveEdgeStyle(...).marker` in the edge's own stroke * (`edgeArrowhead` below), and `flowAdapter` leaves `markerEnd` unset. */ export function edgeMarkerEnd(input: { readonly kind: string; readonly appearance?: string }): { readonly type: EdgeMarker; } { const named = input.appearance ?? input.kind; return { type: edgeAppearances[isBuiltin(named) ? named : "flow"].marker }; } /** * The arrowhead `GraphEdge` draws, in marker units. `markerUnits` is * `strokeWidth`, so the head scales with the edge and a control edge's head is * heavier with its line. * * The tip is the marker's origin (`refX` / `refY` 0) and every point runs back * from it, so the head ends exactly where the path ends: React Flow ends a path * at the handle box's outer edge, which is the outside of the port's ring. */ export const edgeArrowhead = { viewBox: "-10 -10 20 20", size: 12.5, refX: 0, refY: 0, points: { arrow: "-5,-4 0,0 -5,4", arrowclosed: "-5,-4 0,0 -5,4 -5,-4", }, } as const satisfies { viewBox: string; size: number; refX: number; refY: number; points: Record; }; /** How far the head reaches back from the tip, in px, for a stroke width. */ export function edgeArrowheadLength(strokeWidth: number): number { const xs = edgeArrowhead.points.arrowclosed.split(" ").map((pair) => Number(pair.split(",")[0])); const reach = Math.max(...xs) - Math.min(...xs); const unitsPerViewBox = Number(edgeArrowhead.viewBox.split(" ")[2]); return (reach / unitsPerViewBox) * edgeArrowhead.size * strokeWidth; } export interface EdgeStyleInput { /** `edge.kind` from the model, not React Flow's `edge.type`. */ readonly kind: string; /** `EdgeKindSpec.appearance`, when the kind is not one of the four. */ readonly appearance?: string; readonly borderWidth: BorderWidth; readonly animation: Animation; } /** * `"$color-9"` -> `"color-9"`. The theme key, so a renderer can look the token up * without the appearance table ever holding a resolved colour — which is what * keeps `edgeStyle.spec.ts`'s hex ban meaningful. */ export function themeTokenName(stroke: string): string { return stroke.startsWith("$") ? stroke.slice(1) : stroke; } export interface EdgeStyle { readonly path: EdgePathKind; readonly stroke: string; readonly strokeWidth: number; readonly strokeDasharray?: string; readonly marker: EdgeMarker; /** A CSS `animation` shorthand. `"none"` when the knob says so. */ readonly animation: string; } export function resolveEdgeStyle(input: EdgeStyleInput): EdgeStyle { const named = input.appearance ?? input.kind; // An edge that does not paint is worse than one that paints plainly, so an // unknown kind reads as `flow` rather than disappearing. const appearance = edgeAppearances[isBuiltin(named) ? named : "flow"]; return { path: appearance.path, stroke: appearance.stroke, strokeWidth: edgeStrokeWidth(input.borderWidth, appearance.weight), strokeDasharray: appearance.dash, marker: appearance.marker, // Stopping the march is not the same as erasing the dashes: the PATTERN is // how a reader tells an error edge from a flow edge, so it survives // `animation: "none"` and a reduced-motion user keeps the channel. animation: appearance.dash !== undefined && input.animation !== "none" ? dashAnimation : "none", }; }