import { buildConnectorGraph } from "./connectors.ts"; import { getRadialSignalEndpoints } from "./gridAnimations.ts"; import { sameBoundary, type TopologyBoundaryDefinition } from "./topologyBoundary.ts"; import { resolveTopologyNodeOperationalState } from "./nodeStatus.ts"; import type { ConnectorRoutingMode } from "./connectorRouting.ts"; import { axialToPixel, getGridBounds, type AxialCoord, type HexCell } from "./hex.ts"; import { resolveTopologyIcon } from "./topologyIconRegistry.ts"; import type { PreparedRenderOccupant, TopologyGridCellState, TopologyRenderFrame, } from "./renderPipeline.ts"; export interface TopologyGridMarkupOptions { frame: TopologyRenderFrame; radius: number; activeCellIds?: string[]; cellStates?: Record; showNodes?: boolean; className?: string; connectorRouting?: ConnectorRoutingMode; boundary?: TopologyBoundaryDefinition | null; radialSignal?: { origin?: AxialCoord; innerRadius?: number; outerRadius?: number; endpointCount?: number; } | boolean; } function escapeMarkup(value: unknown) { return String(value) .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); } function imageClipId(node: PreparedRenderOccupant) { return `occupant-image-clip-${node.id.replace(/[^a-zA-Z0-9_-]/g, "-")}-${node.q}-${node.r}`; } function occupantWithCell( node: PreparedRenderOccupant, cellMap: Map, ) { const cell = cellMap.get(node.cellId); return cell ? { ...node, cell } : null; } export function renderTopologyGridMarkup({ frame, radius, activeCellIds = [], cellStates = {}, showNodes = true, className = "", connectorRouting = "straight", boundary = frame.boundary ?? null, radialSignal = false, }: TopologyGridMarkupOptions) { const { cells } = frame; const bounds = getGridBounds(cells, radius); const cellMap = new Map(cells.map((cell) => [cell.id, cell])); const activeSet = new Set(activeCellIds); const occupiedEntries = frame.occupants .map((node) => occupantWithCell(node, cellMap)) .filter((node): node is NonNullable => Boolean(node)); const transitionOrder = new Map(occupiedEntries.map((node, index) => [node.id, index])); const gridOffset = cells[0] ? { x: cells[0].center.x - axialToPixel(cells[0], radius).x, y: cells[0].center.y - axialToPixel(cells[0], radius).y, } : { x: 0, y: 0 }; const connectorGraph = buildConnectorGraph( occupiedEntries, frame.relationships, radius, gridOffset, { routingMode: connectorRouting, cells, sameBoundary: (sourceId, targetId) => sameBoundary(sourceId, targetId, { entities: occupiedEntries, relationships: frame.relationships, boundary, }), }, ); const imageClips = occupiedEntries .filter((node) => typeof node.image === "string" && node.image.length > 0) .map((node) => ` `) .join(""); const connectorMarkup = connectorGraph.routes.map((edge) => { const signalState = frame.signalEdgeStates[`${edge.from}->${edge.to}`] ?? frame.signalEdgeStates[`${edge.to}->${edge.from}`] ?? "idle"; return ` `; }).join(""); const radialConfig = radialSignal === true ? {} : radialSignal || null; const radialEndpoints = radialConfig ? getRadialSignalEndpoints(cells, radialConfig.origin ?? { q: 0, r: 0 }, radialConfig.innerRadius ?? 4, radialConfig.outerRadius ?? 8, radialConfig.endpointCount ?? 12) : []; const radialOrigin = radialConfig ? cellMap.get("hex-" + (radialConfig.origin?.q ?? 0) + "-" + (radialConfig.origin?.r ?? 0)) : null; const radialConnectorMarkup = radialOrigin ? radialEndpoints.map((endpoint) => ` `).join("") : ""; const lines = cells.map((cell) => { const state = cellStates[cell.id]; const isActive = state ? state.state === "active" : activeSet.has(cell.id); const isTrail = state?.state === "trail"; const isConverted = state?.state === "converted"; const isReceiving = state?.type === "receiving"; const isTileAlt = ((cell.q + cell.r) & 1) !== 0; const intensity = state?.intensity ?? (isActive ? 1 : activeSet.has(cell.id) ? 0.7 : 0.24); const edgeFactor = cell.edgeFactor ?? 1; const stateName = state?.state ?? (isActive ? "active" : "idle"); const signalType = state?.type ?? ""; const receivingStyle = isReceiving ? "fill:none;" : ""; return ` ${isActive || isTrail || isConverted || isReceiving ? ` ` : ""} `; }).join(""); const nodes = showNodes ? cells.map((cell) => { const state = cellStates[cell.id]; const isActive = state ? state.state === "active" : activeSet.has(cell.id); const isTrail = state?.state === "trail"; const isConverted = state?.state === "converted"; const isReceiving = state?.type === "receiving"; const intensity = state?.intensity ?? (isActive ? 1 : activeSet.has(cell.id) ? 0.7 : 0.24); const edgeFactor = cell.edgeFactor ?? 1; const stateName = state?.state ?? (isActive ? "active" : "idle"); const signalType = state?.type ?? ""; const receivingStyle = isReceiving ? "fill:none;" : ""; return ` ${isActive || isTrail || isConverted || isReceiving ? ` ` : ""} `; }).join("") : ""; const occupants = occupiedEntries.map((node, nodeIndex) => { const size = node.size ?? "md"; const isDisabled = Boolean(node.disabled); const isActive = Boolean(node.active); const isSelected = Boolean(node.selected); const icon = resolveTopologyIcon(node.icon); const labelSize = Math.max(12, radius * 0.26); const subtitleSize = Math.max(8, radius * 0.13); const titleOffset = node.subtitle ? Math.max(4, radius * 0.08) : Math.max(2, radius * 0.04); const subtitleOffset = Math.max(12, radius * 0.24); const imageSource = node.image ?? icon?.image; const imageScale = Math.max(0.1, Number(node.imageScale ?? icon?.imageScale ?? 1)); const imageAlign = node.imageAlign ?? icon?.imageAlign ?? "center"; const imageWidth = Math.sqrt(3) * radius * imageScale; const imageHeight = radius * 2 * imageScale; const imageOffsetY = Number(node.imageOffsetY ?? 0) * radius; const imageX = node.cell.center.x - imageWidth / 2; const imageY = imageAlign === "top" ? node.cell.center.y - radius + radius * 0.12 + imageOffsetY : node.cell.center.y - imageHeight / 2 + imageOffsetY; const ariaLabel = node.subtitle ? `${node.label}. ${node.subtitle}` : node.label; const operationalState = resolveTopologyNodeOperationalState(node); return ` ${imageSource ? `` : ""} ${escapeMarkup(node.label)} ${node.subtitle ? `${escapeMarkup(node.subtitle)}` : ""} `; }).join(""); const hasOccupants = occupiedEntries.length > 0; return ` ${imageClips ? `${imageClips}` : ""} ${connectorMarkup ? `` : ""} ${radialConnectorMarkup ? `` : ""} ${showNodes ? `` : ""} ${occupants} `; }