/** * The GraphNode measure recipe — pure. No React, no DOM, no `@xyflow`. * * A node is a CONTAINER, not a control (design §11 Size). Everything here * exists to keep it off the control-height ladder: the width rides a per-kind * table, the radius caps at the node's own padding, and the minimum height is * floored by how many ports have to fit down one edge. * * Pure on purpose. `graphNode.spec.ts` runs it in Node with no renderer, so * the laws are falsifiable before a pixel exists. */ import { capContainerRadius, defaultScaleInputs, generateScale, getActiveScaleTokens, type Knobs, MIN_PRESS_TARGET, panelPaddingPx, resolveRadiusForPart, sizeTokenStop, } from "@multiplatform.one/theme"; /** * The row of the per-kind tables a node reads: its size token's side of the * default control size (`sizeTokenStop`), never the control-height recipe. */ export type GraphNodeSizeKnob = "small" | "medium" | "large"; export type GraphNodeRadiusKnob = Knobs["borderRadius"]; /** The table row for a resolved size token. */ export function graphNodeSize(sizeToken: string): GraphNodeSizeKnob { return sizeTokenStop(sizeToken); } export type GraphPortSide = "left" | "right" | "top" | "bottom"; /** * The structural subset of `PortSpec` this module needs to measure a node. * * TODO(task 1.3): replace with `PortSpec` from `../model` once the schema * lands. It is declared structurally compatible on purpose — `PortSpec` adds * `index`, `dataType` and `arity`, none of which change a measurement — so the * swap is an import change, not a rewrite. */ export interface GraphNodePort { readonly id: string; readonly side: GraphPortSide; readonly direction: "in" | "out"; readonly label?: string; } /** `NodeKindSpec.measure` (design §4.3): width fixed per kind, height knob-driven. */ export interface GraphNodeMeasure { readonly width: number; readonly minHeight: number; } /** * The default per-kind measure table (design §11 Size: "default 172 / 190 / 216"). * * A kind that declares its own `measure` overrides this entirely; the table is * what an undeclared kind falls back to. `minHeight` anchors on design §5's * measured node heights (62 px compact, 72 px default) with `large` one step * above, rather than a number invented here. */ export const defaultNodeMeasure: Record = { small: { width: 172, minHeight: 62 }, medium: { width: 190, minHeight: 72 }, large: { width: 216, minHeight: 84 }, }; /** * Vertical pitch between two ports on the same node edge. * * Coarse pointers get the press floor itself (design §7.1 H4: two ports closer * than the hit diameter would have overlapping discs and the wrong one would * win). Fine pointers get 24, which is H3's fine-pointer hit diameter and is * already above every painted stop (8 / 10 / 12), so the pitch never has to * consult the painted ladder — that is what keeps this module independent of * `handleGeometry.ts` rather than circular with it. */ export const FINE_PORT_PITCH = 24; export function portSpacing(opts: { readonly coarsePointer: boolean }): number { return opts.coarsePointer ? MIN_PRESS_TARGET : FINE_PORT_PITCH; } /** * §11 Size, GraphNode frame: `remap` to `NodeKindSpec.measure.width`. * * The size token reaches the width only through the per-kind table. It never * reaches the control-height recipe (`knobProps.control.height`), because a * node is a container and a container that steps with a control ladder is a * control wearing a frame. */ export function graphNodeWidth(opts: { readonly size: GraphNodeSizeKnob; readonly measure?: Partial; }): number { return opts.measure?.width ?? defaultNodeMeasure[opts.size].width; } /** * How many ports stack down the tallest vertical edge. * * Height crowding is per EDGE, not per node: `left` and `right` ports stack * vertically and can collide with their own side's neighbours, never with the * opposite side's. `top` / `bottom` ports stack horizontally and constrain * width, so they do not enter a height floor at all. */ export function verticalPortCrowd(ports: readonly GraphNodePort[]): number { let left = 0; let right = 0; for (const port of ports) { if (port.side === "left") left += 1; else if (port.side === "right") right += 1; } return Math.max(left, right); } /** * §7.1 H4, the floor half: a node is tall enough for every port on its * busiest edge to be a real target, instead of ports silently stealing hits * from each other. */ export function graphNodeMinHeight(opts: { readonly size: GraphNodeSizeKnob; readonly ports: readonly GraphNodePort[]; readonly portSpacingPx: number; readonly measure?: Partial; }): number { const declared = opts.measure?.minHeight ?? defaultNodeMeasure[opts.size].minHeight; return Math.max(declared, verticalPortCrowd(opts.ports) * opts.portSpacingPx); } /** * §11 Radius, GraphNode frame: CONTAINER-CAP. The Tamagui value to paint — * the radius token while it fits inside the node's own padding, the padding px * once it would not. */ export function graphNodeRadius(knobs: { readonly borderRadius: GraphNodeRadiusKnob; }): string | number { return capContainerRadius(knobs.borderRadius); } /** The default scale's radius tokens; rendered nodes read the active scale. */ export const nodeRadiusTokenPx: Readonly> = { ...generateScale(defaultScaleInputs).tokens.radius, }; /** px for a capped number or a radius token on the active scale. */ export function containerRadiusPx(value: string | number): number { if (typeof value === "number") return value; const radius = getActiveScaleTokens().radius; if (!Object.hasOwn(radius, value)) { throw new Error(`containerRadiusPx: "${value}" is not a radius token on the active scale`); } return radius[value as keyof typeof radius]; } /** * The same radius in px, resolved through the DG-RAD doctrine table rather * than the resolver. * * Two independent implementations of one law: `graphNodeRadius` goes through * `capContainerRadius` (resolver), this goes through `resolveRadiusForPart` * (doctrine). `graphNode.spec.ts` asserts they agree at every stop, so a * doctrine/resolver drift is a failing test rather than a corner that quietly * clips a child. */ export function graphNodeRadiusPx(knobs: { readonly borderRadius: GraphNodeRadiusKnob }): number { return resolveRadiusForPart({ radiusClass: "CONTAINER-CAP", stop: knobs.borderRadius, paddingPx: panelPaddingPx(), }).px; } /** The complete measure a node is seeded with BEFORE first paint (design §5). */ export function graphNodeMeasure(opts: { readonly size: GraphNodeSizeKnob; readonly ports: readonly GraphNodePort[]; readonly portSpacingPx: number; readonly measure?: Partial; }): GraphNodeMeasure { return { width: graphNodeWidth(opts), minHeight: graphNodeMinHeight(opts), }; }