/** * The `d` string every graph edge draws, with no React Flow runtime and no * store. GraphEdge reads obstacles from the React Flow store and calls this; * a read-only layout passes its own node boxes (FZ-8149), so both draw the * same path and reroute the same way. */ import { getBezierPath, getSmoothStepPath, getStraightPath } from "../react-flow/xyPaths"; import type { Position } from "../react-flow/xyPaths"; import { pathIntrudes, routeAroundBoxes, routeOnTrack } from "./edgeRoute"; import type { EdgeEnds, EdgeObstacles, EdgeSide } from "./edgeRoute"; import type { EdgePathKind } from "./edgeStyle"; export interface PathGeometry { readonly sourceX: number; readonly sourceY: number; readonly targetX: number; readonly targetY: number; readonly sourcePosition: Position; readonly targetPosition: Position; } /** * `step` is `getSmoothStepPath` with a zero corner radius — React Flow has no * separate step builder, and the design's table names four path SHAPES, not * four functions. */ export function buildEdgePath( path: EdgePathKind, geometry: PathGeometry, obstacles?: EdgeObstacles, ): [d: string, labelX: number, labelY: number] { const built = buildFlowPath(path, geometry); if (!obstacles || obstacles.boxes.length === 0) return built; const ends: EdgeEnds = { sourceX: geometry.sourceX, sourceY: geometry.sourceY, targetX: geometry.targetX, targetY: geometry.targetY, sourceSide: geometry.sourcePosition as EdgeSide, targetSide: geometry.targetPosition as EdgeSide, }; const tracked = routeOnTrack(path, ends, obstacles); if (tracked) return tracked; if (!pathIntrudes(built[0], ends, obstacles)) return built; return routeAroundBoxes(path, ends, obstacles) ?? built; } export function buildFlowPath( path: EdgePathKind, geometry: PathGeometry, ): [d: string, labelX: number, labelY: number] { const { sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition } = geometry; switch (path) { case "bezier": { const [d, labelX, labelY] = getBezierPath({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, }); return [d, labelX, labelY]; } case "straight": { const [d, labelX, labelY] = getStraightPath({ sourceX, sourceY, targetX, targetY }); return [d, labelX, labelY]; } case "step": case "smoothstep": { const [d, labelX, labelY] = getSmoothStepPath({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, borderRadius: path === "step" ? 0 : undefined, }); return [d, labelX, labelY]; } } }