import type { EdgeObstacle } from "../edges/edgeRoute"; import { edgeClearance } from "../edges/edgeRoute"; import { buildEdgePath } from "../edges/edgePath"; import type { PathGeometry } from "../edges/edgePath"; import { resolveEdgeStyle } from "../edges/edgeStyle"; import { layoutWithDagre } from "../layout/dagre"; import type { XY } from "../model/document"; import { Position } from "../react-flow/xyPaths"; export type GraphDirection = "LR" | "TB"; export interface GraphLayoutNode { readonly id: string; readonly width: number; readonly height: number; } export interface GraphLayoutEdge { readonly id?: string; readonly source: string; readonly target: string; readonly kind?: string; readonly emphasis?: boolean; } export interface GraphPlanInput { readonly nodes: readonly GraphLayoutNode[]; readonly edges: readonly GraphLayoutEdge[]; readonly direction: GraphDirection; readonly nodeSpacing: number; readonly rankSpacing: number; readonly margin: number; readonly strokeWidth: number; readonly wrapWidth?: number; readonly positions?: Readonly>; } export interface PlannedNode extends GraphLayoutNode { readonly x: number; readonly y: number; } export interface PlannedEdge { readonly id: string; readonly source: string; readonly target: string; readonly d: string; readonly head: string; readonly tip: XY; readonly stroke: string; readonly dash?: string; readonly emphasis: boolean; } export interface GraphPlan { readonly width: number; readonly height: number; readonly nodes: readonly PlannedNode[]; readonly edges: readonly PlannedEdge[]; } const sides: Record = { LR: { out: Position.Right, in: Position.Left }, TB: { out: Position.Bottom, in: Position.Top }, }; const inward: Record = { [Position.Left]: { x: 1, y: 0 }, [Position.Right]: { x: -1, y: 0 }, [Position.Top]: { x: 0, y: 1 }, [Position.Bottom]: { x: 0, y: -1 }, }; export function graphEdgeId(edge: GraphLayoutEdge): string { return edge.id ?? `${edge.source}\n${edge.target}`; } export function usableEdges( nodes: readonly GraphLayoutNode[], edges: readonly GraphLayoutEdge[], ): GraphLayoutEdge[] { const known = new Set(nodes.map((node) => node.id)); const seen = new Set(); const kept: GraphLayoutEdge[] = []; for (const edge of edges) { if (edge.source === edge.target || !known.has(edge.source) || !known.has(edge.target)) continue; const id = graphEdgeId(edge); if (seen.has(id)) continue; seen.add(id); kept.push(edge); } return kept; } function placeNodes(input: GraphPlanInput, edges: readonly GraphLayoutEdge[]): Map { const placed = new Map(); if (input.positions) { for (const node of input.nodes) placed.set(node.id, input.positions[node.id] ?? { x: 0, y: 0 }); return placed; } const linked = new Set(edges.flatMap((edge) => [edge.source, edge.target])); const graphNodes = input.nodes.filter((node) => linked.has(node.id)); const isolated = input.nodes.filter((node) => !linked.has(node.id)); let dagWidth = 0; let dagHeight = 0; if (graphNodes.length > 0) { const result = layoutWithDagre({ nodes: graphNodes.map((node) => ({ ...node, position: { x: 0, y: 0 } })), edges: edges.map((edge) => ({ id: graphEdgeId(edge), source: edge.source, target: edge.target, })), options: { direction: input.direction, nodeSpacing: input.nodeSpacing, rankSpacing: input.rankSpacing, }, }); for (const node of graphNodes) { const at = result.positions[node.id] ?? { x: 0, y: 0 }; placed.set(node.id, at); dagWidth = Math.max(dagWidth, at.x + node.width); dagHeight = Math.max(dagHeight, at.y + node.height); } } const wrap = Math.max(dagWidth, input.wrapWidth ?? 0, ...isolated.map((node) => node.width)); let x = 0; let y = graphNodes.length > 0 ? dagHeight + input.rankSpacing : 0; let rowHeight = 0; for (const node of isolated) { if (x > 0 && x + node.width > wrap) { x = 0; y += rowHeight + input.nodeSpacing; rowHeight = 0; } placed.set(node.id, { x, y }); x += node.width + input.nodeSpacing; rowHeight = Math.max(rowHeight, node.height); } return placed; } function anchor(node: PlannedNode, side: Position): XY { switch (side) { case Position.Left: return { x: node.x, y: node.y + node.height / 2 }; case Position.Right: return { x: node.x + node.width, y: node.y + node.height / 2 }; case Position.Top: return { x: node.x + node.width / 2, y: node.y }; default: return { x: node.x + node.width / 2, y: node.y + node.height }; } } function round(value: number): number { return Math.round(value * 100) / 100; } export function arrowHead(tip: XY, side: Position, strokeWidth: number): string { const into = inward[side]; const length = strokeWidth * 4; const half = (strokeWidth * 3) / 2; const base = { x: tip.x - into.x * length, y: tip.y - into.y * length }; const across = { x: -into.y, y: into.x }; const a = { x: base.x + across.x * half, y: base.y + across.y * half }; const b = { x: base.x - across.x * half, y: base.y - across.y * half }; return `M ${round(tip.x)} ${round(tip.y)} L ${round(a.x)} ${round(a.y)} L ${round(b.x)} ${round(b.y)} Z`; } export function planGraphLayout(input: GraphPlanInput): GraphPlan { const edges = usableEdges(input.nodes, input.edges); const placed = placeNodes(input, edges); const { margin } = input; const nodes: PlannedNode[] = input.nodes.map((node) => { const at = placed.get(node.id) ?? { x: 0, y: 0 }; return { ...node, x: round(at.x + margin), y: round(at.y + margin) }; }); const byId = new Map(nodes.map((node) => [node.id, node])); const boxes: EdgeObstacle[] = nodes.map((node) => ({ id: node.id, x: node.x, y: node.y, width: node.width, height: node.height, })); const side = sides[input.direction]; const planned: PlannedEdge[] = edges.map((edge) => { const source = byId.get(edge.source)!; const target = byId.get(edge.target)!; const from = anchor(source, side.out); const tip = anchor(target, side.in); const style = resolveEdgeStyle({ kind: edge.kind ?? "flow", borderWidth: "small", animation: "none", }); const geometry: PathGeometry = { sourceX: from.x, sourceY: from.y, targetX: tip.x, targetY: tip.y, sourcePosition: side.out, targetPosition: side.in, }; const [d] = buildEdgePath(style.path, geometry, { boxes, clearance: edgeClearance }); return { id: graphEdgeId(edge), source: edge.source, target: edge.target, d, head: arrowHead(tip, side.in, input.strokeWidth * (edge.emphasis ? 2 : 1)), tip, stroke: edge.emphasis ? "$accent-color" : style.stroke, ...(style.strokeDasharray ? { dash: style.strokeDasharray } : {}), emphasis: edge.emphasis ?? false, }; }); const width = nodes.reduce((most, node) => Math.max(most, node.x + node.width), 0) + margin; const height = nodes.reduce((most, node) => Math.max(most, node.y + node.height), 0) + margin; return { width: round(width), height: round(height), nodes, edges: planned }; }