mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(merman): route visible flowchart subgraph edges (#60100)
This commit is contained in:
committed by
GitHub
Unverified
parent
0ab13468fd
commit
6118c83b01
@@ -29,6 +29,7 @@ import {
|
||||
import type {
|
||||
FlowchartDiagram,
|
||||
FlowchartEdgeRoute,
|
||||
FlowchartEndpointBounds,
|
||||
FlowchartNode,
|
||||
FlowchartNodeBounds,
|
||||
FlowchartPoint,
|
||||
@@ -256,7 +257,7 @@ function fadeSourcePath(
|
||||
function drawSourceConnectors(
|
||||
grid: FlowchartGrid,
|
||||
diagram: FlowchartDiagram,
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
routes: readonly FlowchartEdgeRoute[],
|
||||
): void {
|
||||
const nodesById = new Map(diagram.nodes.map((node) => [node.id, node]))
|
||||
@@ -275,7 +276,7 @@ function drawSourceConnectors(
|
||||
}
|
||||
|
||||
for (const route of routes) {
|
||||
const from = bounds.get(route.edge.from)
|
||||
const from = endpoints.get(route.edge.from)
|
||||
const sourcePoint = route.points[0]
|
||||
if (!from || !sourcePoint) continue
|
||||
const styles = sourceFadeStyles(flowchartNodeStyle(nodesById.get(route.edge.from)))
|
||||
@@ -328,7 +329,8 @@ export function drawFlowchartDiagramGrid(
|
||||
const bound = bounds.get(node.id)
|
||||
if (bound) drawNode(grid, node, bound, borderStyle)
|
||||
}
|
||||
drawSourceConnectors(grid, diagram, bounds, routes)
|
||||
const endpoints = new Map<string, FlowchartEndpointBounds>([...subgraphBounds, ...bounds])
|
||||
drawSourceConnectors(grid, diagram, endpoints, routes)
|
||||
for (const subgraph of diagram.subgraphs ?? []) {
|
||||
const bound = subgraphBounds.get(subgraph.id)
|
||||
if (bound) drawSubgraphLabel(grid, bound)
|
||||
|
||||
@@ -1596,6 +1596,113 @@ flowchart TD
|
||||
expect(output).toContain("After — merged this week")
|
||||
})
|
||||
|
||||
test("routes visible subgraph-to-subgraph chains without creating phantom nodes", () => {
|
||||
const content = `flowchart TD
|
||||
subgraph S1["1. provider.transform (seed)"]
|
||||
A1["Catalog model → copy catalog variants"]
|
||||
A2["Custom model → apply package/modelID/limit + seed default variants"]
|
||||
end
|
||||
subgraph S2["2. plugin model.transform"]
|
||||
B["Plugin can replace variants, clear to [], or ignore"]
|
||||
end
|
||||
subgraph S3["3. config model.transform (post)"]
|
||||
C1{"Config sets reasoning or variants?"}
|
||||
C1 -- yes --> C2["Apply explicit config"]
|
||||
C1 -- no --> C3{"Config changed package (or recreated removed model)?"}
|
||||
C3 -- yes --> C4["Respell / reseed variants"]
|
||||
C3 -- no --> C5["Keep current variants"]
|
||||
end
|
||||
S1 --> S2 --> S3`
|
||||
const diagram = parseMermaidFlowchartDiagram(content)
|
||||
expect(diagram.nodes.some((node) => node.id === "S1" || node.id === "S2" || node.id === "S3")).toBe(false)
|
||||
|
||||
for (const layoutMaxWidth of [60, 80, 120]) {
|
||||
const layout = layoutParsedFlowchartDiagram(diagram, { compact: true, layoutMaxWidth })
|
||||
const grid = drawParsedFlowchartDiagramGrid(diagram, { compact: true, layoutMaxWidth })
|
||||
const s1 = layout.subgraphBounds.get("S1")!
|
||||
const s2 = layout.subgraphBounds.get("S2")!
|
||||
const s3 = layout.subgraphBounds.get("S3")!
|
||||
|
||||
expect(s1.top + s1.height).toBeLessThan(s2.top)
|
||||
expect(s2.top + s2.height).toBeLessThan(s3.top)
|
||||
|
||||
for (const [fromId, toId, fromFrame, toFrame] of [
|
||||
["S1", "S2", s1, s2],
|
||||
["S2", "S3", s2, s3],
|
||||
] as const) {
|
||||
const route = layout.routes.find((candidate) => candidate.edge.from === fromId && candidate.edge.to === toId)!
|
||||
const start = route.points[0]!
|
||||
const end = route.points.at(-1)!
|
||||
expect(start.y).toBe(fromFrame.top + fromFrame.height)
|
||||
expect(end.y).toBe(toFrame.top - 1)
|
||||
expect(grid.getCell(start.x, fromFrame.top + fromFrame.height - 1)?.char).toBe("┬")
|
||||
expect(grid.getCell(end.x, end.y)?.char).toBe("▼")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
test("resolves forward, nested, labeled, and upward subgraph endpoints while keeping explicit same-id nodes", () => {
|
||||
const forward = parseMermaidFlowchartDiagram(`flowchart TD
|
||||
Start[Start] --> S1
|
||||
S1 -->|done| Done[Done]
|
||||
subgraph S1["Stage"]
|
||||
W1[Step 1] --> W2[Step 2]
|
||||
end`)
|
||||
expect(forward.nodes.map((node) => node.id)).toEqual(["Start", "Done", "W1", "W2"])
|
||||
const forwardOutput = renderFlowchartDiagram(`flowchart TD
|
||||
Start[Start] --> S1
|
||||
S1 -->|done| Done[Done]
|
||||
subgraph S1["Stage"]
|
||||
W1[Step 1] --> W2[Step 2]
|
||||
end`)
|
||||
expect(forwardOutput).toContain("done")
|
||||
|
||||
const btGrid = drawParsedFlowchartDiagramGrid(
|
||||
parseMermaidFlowchartDiagram(`flowchart BT
|
||||
subgraph S1["First group title"]
|
||||
A1[Alpha]
|
||||
end
|
||||
subgraph S2["Second group title"]
|
||||
B1[Beta]
|
||||
end
|
||||
S1 --> S2`),
|
||||
{ compact: true },
|
||||
)
|
||||
const btText = btGrid.toString({ trimTop: true, trimBottom: true })
|
||||
expect(btText).toContain("First group title")
|
||||
expect(btText).toContain("┴")
|
||||
expect(btText).toContain("▲")
|
||||
|
||||
const nestedLayout = layoutFlowchartDiagram(`flowchart TD
|
||||
subgraph Outer["Outer"]
|
||||
Entry[Entry] --> Inner
|
||||
subgraph Inner["Inner"]
|
||||
A[One] --> B[Two]
|
||||
end
|
||||
end`)
|
||||
const outer = nestedLayout.subgraphBounds.get("Outer")!
|
||||
const entryRoute = nestedLayout.routes.find((route) => route.edge.from === "Entry" && route.edge.to === "Inner")!
|
||||
expect(
|
||||
entryRoute.points.every(
|
||||
(point) =>
|
||||
point.x > outer.left &&
|
||||
point.x < outer.left + outer.width - 1 &&
|
||||
point.y > outer.top &&
|
||||
point.y < outer.top + outer.height - 1,
|
||||
),
|
||||
).toBe(true)
|
||||
|
||||
const sameId = parseMermaidFlowchartDiagram(`flowchart LR
|
||||
subgraph API
|
||||
API[API Gateway] --> DB[(Postgres)]
|
||||
end`)
|
||||
expect(sameId.nodes.map((node) => node.id)).toEqual(["API", "DB"])
|
||||
const sameIdLayout = layoutParsedFlowchartDiagram(sameId, { compact: true })
|
||||
const apiNode = sameIdLayout.bounds.get("API")!
|
||||
const apiRoute = sameIdLayout.routes[0]!
|
||||
expect(apiRoute.points[0]!.x).toBe(apiNode.left + apiNode.width)
|
||||
})
|
||||
|
||||
test("keeps every node and edge in a long chain distinct and in its subgraph", () => {
|
||||
const diagram = parseMermaidFlowchartDiagram(`flowchart LR
|
||||
subgraph Pipeline
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
flowchartVerticalBranchLabelGap,
|
||||
} from "./labels.js"
|
||||
import type { FlowchartDiagramRenderOptions } from "./options.js"
|
||||
import { avoidFlowchartFrameBorders, routeFlowchartEdges } from "./routing.js"
|
||||
import { avoidFlowchartFrameBorders, flowchartSourceConnector, routeFlowchartEdges } from "./routing.js"
|
||||
import type {
|
||||
FlowchartDiagram,
|
||||
FlowchartDirection,
|
||||
@@ -24,6 +24,7 @@ import type {
|
||||
FlowchartNodeBounds,
|
||||
FlowchartNodeSize,
|
||||
FlowchartPoint,
|
||||
FlowchartSubgraph,
|
||||
FlowchartSubgraphBounds,
|
||||
} from "./types.js"
|
||||
|
||||
@@ -37,6 +38,8 @@ export const COMPACT_MIN_VERTICAL_RANK_GAP = 2
|
||||
const SUBGRAPH_PADDING_X = 2
|
||||
const SUBGRAPH_PADDING_TOP = 1
|
||||
const SUBGRAPH_PADDING_BOTTOM = 1
|
||||
const MIN_SUBGRAPH_VERTICAL_EDGE_GAP = 3
|
||||
const MIN_SUBGRAPH_HORIZONTAL_EDGE_GAP = 4
|
||||
|
||||
export interface FlowchartLayout {
|
||||
diagram: FlowchartDiagram
|
||||
@@ -49,6 +52,61 @@ export interface FlowchartLayout {
|
||||
|
||||
type FlowchartBounds = Pick<FlowchartSubgraphBounds, "left" | "top" | "width" | "height" | "centerX" | "centerY">
|
||||
|
||||
function endpointRankSpan(
|
||||
diagram: FlowchartDiagram,
|
||||
edge: FlowchartEdge,
|
||||
normalizedRanks: ReadonlyMap<string, number>,
|
||||
nodeIds: ReadonlySet<string>,
|
||||
):
|
||||
| { startRank: number; endRank: number; hasSubgraphEndpoint: boolean; unsharedSubgraphs: FlowchartSubgraph[] }
|
||||
| undefined {
|
||||
const fromRanks = (nodeIds.has(edge.from) ? [edge.from] : [...collectSubgraphNodeIds(diagram, edge.from)]).flatMap(
|
||||
(id) => {
|
||||
const rank = normalizedRanks.get(id)
|
||||
return rank === undefined ? [] : [rank]
|
||||
},
|
||||
)
|
||||
const toRanks = (nodeIds.has(edge.to) ? [edge.to] : [...collectSubgraphNodeIds(diagram, edge.to)]).flatMap((id) => {
|
||||
const rank = normalizedRanks.get(id)
|
||||
return rank === undefined ? [] : [rank]
|
||||
})
|
||||
if (fromRanks.length === 0 || toRanks.length === 0) return undefined
|
||||
const forwardStart = Math.max(...fromRanks)
|
||||
const forwardEnd = Math.min(...toRanks)
|
||||
const reverseStart = Math.max(...toRanks)
|
||||
const reverseEnd = Math.min(...fromRanks)
|
||||
const span =
|
||||
forwardStart < forwardEnd
|
||||
? { startRank: forwardStart, endRank: forwardEnd }
|
||||
: reverseStart < reverseEnd
|
||||
? { startRank: reverseStart, endRank: reverseEnd }
|
||||
: undefined
|
||||
if (!span) return undefined
|
||||
|
||||
const hasSubgraphEndpoint = !nodeIds.has(edge.from) || !nodeIds.has(edge.to)
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
const unsharedSubgraphs = hasSubgraphEndpoint
|
||||
? subgraphs.filter((subgraph) => {
|
||||
const containsFrom = subgraph.id === edge.from || subgraphContainsEndpoint(diagram, subgraph, edge.from)
|
||||
const containsTo = subgraph.id === edge.to || subgraphContainsEndpoint(diagram, subgraph, edge.to)
|
||||
return containsFrom !== containsTo
|
||||
})
|
||||
: []
|
||||
return { ...span, hasSubgraphEndpoint, unsharedSubgraphs }
|
||||
}
|
||||
|
||||
function ensureRankSpanGap(gaps: number[], startIndex: number, endIndex: number, required: number): void {
|
||||
const low = Math.min(startIndex, endIndex)
|
||||
const high = Math.max(startIndex, endIndex)
|
||||
if (low === high) return
|
||||
if (high - low === 1) {
|
||||
gaps[low] = Math.max(gaps[low]!, required)
|
||||
return
|
||||
}
|
||||
const current = gaps.slice(low, high).reduce((sum, gap) => sum + gap, 0)
|
||||
if (current < required) gaps[high - 1] = gaps[high - 1]! + (required - current)
|
||||
}
|
||||
|
||||
function horizontalRankGaps(
|
||||
diagram: FlowchartDiagram,
|
||||
normalizedRanks: ReadonlyMap<string, number>,
|
||||
@@ -57,17 +115,37 @@ function horizontalRankGaps(
|
||||
): number[] {
|
||||
const gaps = Array.from({ length: Math.max(0, rankKeys.length - 1) }, () => fallback)
|
||||
const rankIndexes = new Map(rankKeys.map((rank, index) => [rank, index]))
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
|
||||
for (const edge of diagram.edges) {
|
||||
if (!edge.label) continue
|
||||
const fromIndex = rankIndexes.get(normalizedRanks.get(edge.from) ?? -1)
|
||||
const toIndex = rankIndexes.get(normalizedRanks.get(edge.to) ?? -1)
|
||||
if (fromIndex === undefined || toIndex === undefined || fromIndex === toIndex) continue
|
||||
if (edge.orderOnly) continue
|
||||
const hasSubgraphEndpoint = !nodeIds.has(edge.from) || !nodeIds.has(edge.to)
|
||||
if (!edge.label && !hasSubgraphEndpoint) continue
|
||||
|
||||
const labelGap = flowchartHorizontalLabelRankGap(flowchartLabelWidth(edge.label, visualLength))
|
||||
for (let index = Math.min(fromIndex, toIndex); index < Math.max(fromIndex, toIndex); index++) {
|
||||
gaps[index] = Math.max(gaps[index]!, labelGap)
|
||||
if (!hasSubgraphEndpoint) {
|
||||
const fromIndex = rankIndexes.get(normalizedRanks.get(edge.from) ?? -1)
|
||||
const toIndex = rankIndexes.get(normalizedRanks.get(edge.to) ?? -1)
|
||||
if (fromIndex === undefined || toIndex === undefined || fromIndex === toIndex) continue
|
||||
const labelGap = flowchartHorizontalLabelRankGap(flowchartLabelWidth(edge.label, visualLength))
|
||||
for (let index = Math.min(fromIndex, toIndex); index < Math.max(fromIndex, toIndex); index++) {
|
||||
gaps[index] = Math.max(gaps[index]!, labelGap)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const span = endpointRankSpan(diagram, edge, normalizedRanks, nodeIds)
|
||||
if (!span) continue
|
||||
const startIndex = rankIndexes.get(span.startRank)
|
||||
const endIndex = rankIndexes.get(span.endRank)
|
||||
if (startIndex === undefined || endIndex === undefined) continue
|
||||
const padding = span.unsharedSubgraphs.length * SUBGRAPH_PADDING_X
|
||||
const clearance = edge.label
|
||||
? Math.max(
|
||||
MIN_SUBGRAPH_HORIZONTAL_EDGE_GAP,
|
||||
flowchartHorizontalLabelRankGap(flowchartLabelWidth(edge.label, visualLength)),
|
||||
)
|
||||
: MIN_SUBGRAPH_HORIZONTAL_EDGE_GAP
|
||||
ensureRankSpanGap(gaps, startIndex, endIndex, padding + clearance)
|
||||
}
|
||||
|
||||
return gaps
|
||||
@@ -81,17 +159,37 @@ function verticalRankGaps(
|
||||
): number[] {
|
||||
const gaps = Array.from({ length: Math.max(0, rankKeys.length - 1) }, () => fallback)
|
||||
const rankIndexes = new Map(rankKeys.map((rank, index) => [rank, index]))
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
|
||||
for (const edge of diagram.edges) {
|
||||
if (!edge.label) continue
|
||||
const fromIndex = rankIndexes.get(normalizedRanks.get(edge.from) ?? -1)
|
||||
const toIndex = rankIndexes.get(normalizedRanks.get(edge.to) ?? -1)
|
||||
if (fromIndex === undefined || toIndex === undefined || fromIndex === toIndex) continue
|
||||
if (edge.orderOnly) continue
|
||||
const hasSubgraphEndpoint = !nodeIds.has(edge.from) || !nodeIds.has(edge.to)
|
||||
if (!edge.label && !hasSubgraphEndpoint) continue
|
||||
|
||||
const labelHeight = splitDiagramLines(edge.label).length
|
||||
for (let index = Math.min(fromIndex, toIndex); index < Math.max(fromIndex, toIndex); index++) {
|
||||
gaps[index] = Math.max(gaps[index]!, labelHeight + 2)
|
||||
if (!hasSubgraphEndpoint) {
|
||||
const fromIndex = rankIndexes.get(normalizedRanks.get(edge.from) ?? -1)
|
||||
const toIndex = rankIndexes.get(normalizedRanks.get(edge.to) ?? -1)
|
||||
if (fromIndex === undefined || toIndex === undefined || fromIndex === toIndex) continue
|
||||
const labelHeight = splitDiagramLines(edge.label).length
|
||||
for (let index = Math.min(fromIndex, toIndex); index < Math.max(fromIndex, toIndex); index++) {
|
||||
gaps[index] = Math.max(gaps[index]!, labelHeight + 2)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const span = endpointRankSpan(diagram, edge, normalizedRanks, nodeIds)
|
||||
if (!span) continue
|
||||
const startIndex = rankIndexes.get(span.startRank)
|
||||
const endIndex = rankIndexes.get(span.endRank)
|
||||
if (startIndex === undefined || endIndex === undefined) continue
|
||||
const padding = span.unsharedSubgraphs.reduce(
|
||||
(sum, subgraph) => sum + Math.max(SUBGRAPH_PADDING_TOP, splitDiagramLines(subgraph.label).length),
|
||||
0,
|
||||
)
|
||||
const clearance = edge.label
|
||||
? Math.max(MIN_SUBGRAPH_VERTICAL_EDGE_GAP, splitDiagramLines(edge.label).length + 2)
|
||||
: MIN_SUBGRAPH_VERTICAL_EDGE_GAP
|
||||
ensureRankSpanGap(gaps, startIndex, endIndex, padding + clearance)
|
||||
}
|
||||
|
||||
return gaps
|
||||
@@ -133,10 +231,40 @@ function rankNodes(diagram: FlowchartDiagram): Map<string, number> {
|
||||
const incomingCounts = new Map(diagram.nodes.map((node) => [node.id, 0]))
|
||||
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
const subgraphById = new Map(subgraphs.map((subgraph) => [subgraph.id, subgraph]))
|
||||
const insideSubgraph = (endpointId: string, subgraphId: string): boolean => {
|
||||
const subgraph = subgraphById.get(subgraphId)
|
||||
return Boolean(subgraph && subgraphContainsEndpoint(diagram, subgraph, endpointId))
|
||||
}
|
||||
const subgraphExitNodes = (subgraphId: string): string[] => {
|
||||
const members = collectSubgraphNodeIds(diagram, subgraphId)
|
||||
const hasInternalOutgoing = new Set<string>()
|
||||
for (const edge of diagram.edges) {
|
||||
if (edge.from === edge.to || !insideSubgraph(edge.from, subgraphId) || !insideSubgraph(edge.to, subgraphId))
|
||||
continue
|
||||
const sources = nodeIds.has(edge.from) ? [edge.from] : [...collectSubgraphNodeIds(diagram, edge.from)]
|
||||
for (const source of sources) hasInternalOutgoing.add(source)
|
||||
}
|
||||
const exits = [...members].filter((id) => !hasInternalOutgoing.has(id))
|
||||
return exits.length > 0 ? exits : [...members]
|
||||
}
|
||||
const subgraphEntryNodes = (subgraphId: string): string[] => {
|
||||
const members = collectSubgraphNodeIds(diagram, subgraphId)
|
||||
const hasInternalIncoming = new Set<string>()
|
||||
for (const edge of diagram.edges) {
|
||||
if (edge.from === edge.to || !insideSubgraph(edge.from, subgraphId) || !insideSubgraph(edge.to, subgraphId))
|
||||
continue
|
||||
const targets = nodeIds.has(edge.to) ? [edge.to] : [...collectSubgraphNodeIds(diagram, edge.to)]
|
||||
for (const target of targets) hasInternalIncoming.add(target)
|
||||
}
|
||||
const entries = [...members].filter((id) => !hasInternalIncoming.has(id))
|
||||
return entries.length > 0 ? entries : [...members]
|
||||
}
|
||||
const rankEdges = diagram.edges.flatMap((edge) => {
|
||||
if (!edge.orderOnly) return [edge]
|
||||
const fromIds = nodeIds.has(edge.from) ? [edge.from] : [...collectSubgraphNodeIds(diagram, edge.from)]
|
||||
const toIds = nodeIds.has(edge.to) ? [edge.to] : [...collectSubgraphNodeIds(diagram, edge.to)]
|
||||
if (nodeIds.has(edge.from) && nodeIds.has(edge.to)) return [edge]
|
||||
const fromIds = nodeIds.has(edge.from) ? [edge.from] : subgraphExitNodes(edge.from)
|
||||
const toIds = nodeIds.has(edge.to) ? [edge.to] : subgraphEntryNodes(edge.to)
|
||||
return fromIds.flatMap((from) => toIds.map((to) => ({ ...edge, from, to })))
|
||||
})
|
||||
|
||||
@@ -288,7 +416,23 @@ function segmentOverlapsSlot(
|
||||
)
|
||||
}
|
||||
|
||||
function routeOverlapsSlot(route: FlowchartEdgeRoute, slot: FlowchartBounds): boolean {
|
||||
function routeOverlapsSlot(
|
||||
route: FlowchartEdgeRoute,
|
||||
bounds: FlowchartSubgraphBounds,
|
||||
slot: FlowchartBounds,
|
||||
nodeIds: ReadonlySet<string>,
|
||||
): boolean {
|
||||
if (route.edge.from === bounds.id && !nodeIds.has(bounds.id) && route.points[0]) {
|
||||
const connector = flowchartSourceConnector(bounds, route.points[0])
|
||||
if (
|
||||
connector.x >= slot.left &&
|
||||
connector.x < slot.left + slot.width &&
|
||||
connector.y >= slot.top &&
|
||||
connector.y < slot.top + slot.height
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for (let index = 1; index < route.points.length; index++) {
|
||||
if (segmentOverlapsSlot(route.points[index - 1]!, route.points[index]!, slot)) return true
|
||||
}
|
||||
@@ -316,12 +460,13 @@ function routeOverlapsSlot(route: FlowchartEdgeRoute, slot: FlowchartBounds): bo
|
||||
function chooseSubgraphLabelSide(
|
||||
bounds: FlowchartSubgraphBounds,
|
||||
routes: readonly FlowchartEdgeRoute[],
|
||||
nodeIds: ReadonlySet<string>,
|
||||
): FlowchartSubgraphBounds["labelSide"] {
|
||||
const topSlot = labelSlot(bounds, "top")
|
||||
if (!routes.some((route) => routeOverlapsSlot(route, topSlot))) return "top"
|
||||
if (!routes.some((route) => routeOverlapsSlot(route, bounds, topSlot, nodeIds))) return "top"
|
||||
|
||||
const bottomSlot = labelSlot(bounds, "bottom")
|
||||
return routes.some((route) => routeOverlapsSlot(route, bottomSlot)) ? "top" : "bottom"
|
||||
return routes.some((route) => routeOverlapsSlot(route, bounds, bottomSlot, nodeIds)) ? "top" : "bottom"
|
||||
}
|
||||
|
||||
function pathBounds(points: readonly { x: number; y: number }[]): FlowchartBounds | undefined {
|
||||
@@ -661,7 +806,12 @@ function layoutLocalSubgraphDirections(
|
||||
const itemByEndpoint = new Map<string, string>()
|
||||
for (const item of items) {
|
||||
for (const nodeId of item.nodeIds) itemByEndpoint.set(nodeId, item.id)
|
||||
if (item.childId) itemByEndpoint.set(item.childId, item.id)
|
||||
if (item.childId) {
|
||||
itemByEndpoint.set(item.childId, item.id)
|
||||
for (const nestedId of collectDescendantSubgraphIds(diagram, item.childId)) {
|
||||
itemByEndpoint.set(nestedId, item.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
const nodes = items.map((item): FlowchartNode => ({ id: item.id, label: item.id, shape: "box" }))
|
||||
const localDiagram: FlowchartDiagram = {
|
||||
@@ -757,10 +907,28 @@ function layoutLocalSubgraphDirections(
|
||||
return wrapped
|
||||
}
|
||||
|
||||
function collectDescendantSubgraphIds(diagram: FlowchartDiagram, subgraphId: string): Set<string> {
|
||||
const ids = new Set<string>()
|
||||
for (const child of diagram.subgraphs ?? []) {
|
||||
if (child.parentId !== subgraphId) continue
|
||||
ids.add(child.id)
|
||||
for (const nested of collectDescendantSubgraphIds(diagram, child.id)) ids.add(nested)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
function subgraphContainsEndpoint(diagram: FlowchartDiagram, subgraph: FlowchartSubgraph, endpointId: string): boolean {
|
||||
return (
|
||||
collectSubgraphNodeIds(diagram, subgraph.id).has(endpointId) ||
|
||||
collectDescendantSubgraphIds(diagram, subgraph.id).has(endpointId)
|
||||
)
|
||||
}
|
||||
|
||||
function edgeDirection(diagram: FlowchartDiagram, edge: FlowchartEdge): FlowchartDirection {
|
||||
for (const subgraph of [...(diagram.subgraphs ?? [])].reverse()) {
|
||||
if (!subgraph.direction) continue
|
||||
if (subgraph.nodeIds.includes(edge.from) && subgraph.nodeIds.includes(edge.to)) return subgraph.direction
|
||||
if (subgraphContainsEndpoint(diagram, subgraph, edge.from) && subgraphContainsEndpoint(diagram, subgraph, edge.to))
|
||||
return subgraph.direction
|
||||
}
|
||||
return diagram.direction
|
||||
}
|
||||
@@ -896,6 +1064,29 @@ function separateTopLevelItems(
|
||||
if (bounds) translateBounds(bounds, dx, dy)
|
||||
}
|
||||
}
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
const minSubgraphEdgeGap = (fromItemId: string, toItemId: string): number => {
|
||||
const matching = diagram.edges.filter(
|
||||
(edge) =>
|
||||
!edge.orderOnly &&
|
||||
(!nodeIds.has(edge.from) || !nodeIds.has(edge.to)) &&
|
||||
itemByEndpoint.get(edge.from) === fromItemId &&
|
||||
itemByEndpoint.get(edge.to) === toItemId,
|
||||
)
|
||||
if (matching.length === 0) return gap
|
||||
const labelRequirements = matching.map((edge) =>
|
||||
!edge.label
|
||||
? 0
|
||||
: horizontal
|
||||
? flowchartHorizontalLabelRankGap(flowchartLabelWidth(edge.label, visualLength))
|
||||
: splitDiagramLines(edge.label).length + 2,
|
||||
)
|
||||
return Math.max(
|
||||
gap,
|
||||
horizontal ? MIN_SUBGRAPH_HORIZONTAL_EDGE_GAP : MIN_SUBGRAPH_VERTICAL_EDGE_GAP,
|
||||
...labelRequirements,
|
||||
)
|
||||
}
|
||||
if (hasLocalDirection) {
|
||||
const outgoing = new Map(items.map((item) => [item.id, new Set<string>()]))
|
||||
for (const edge of diagram.edges) {
|
||||
@@ -913,24 +1104,63 @@ function separateTopLevelItems(
|
||||
(horizontal ? a.bounds.centerX - b.bounds.centerX : a.bounds.centerY - b.bounds.centerY),
|
||||
)
|
||||
let cursor: number | undefined
|
||||
let previousId: string | undefined
|
||||
let moved = false
|
||||
for (const item of items) {
|
||||
const start = horizontal ? item.bounds.left : item.bounds.top
|
||||
const size = horizontal ? item.bounds.width : item.bounds.height
|
||||
if (cursor === undefined) {
|
||||
cursor = start + size + gap
|
||||
cursor = start + size
|
||||
previousId = item.id
|
||||
continue
|
||||
}
|
||||
const shift = cursor - start
|
||||
const pairGap = previousId
|
||||
? Math.max(minSubgraphEdgeGap(previousId, item.id), minSubgraphEdgeGap(item.id, previousId))
|
||||
: gap
|
||||
const shift = cursor + pairGap - start
|
||||
moved ||= shift !== 0
|
||||
moveItem(item, horizontal ? shift : 0, horizontal ? 0 : shift)
|
||||
cursor = start + shift + size + gap
|
||||
cursor = start + shift + size
|
||||
previousId = item.id
|
||||
}
|
||||
return moved
|
||||
}
|
||||
|
||||
const rankedItems = items.filter((item) => topLevelIds.has(item.id))
|
||||
if (rankedItems.length < 2) return false
|
||||
const reversed = diagram.direction === "RL" || diagram.direction === "BT"
|
||||
const primaryStart = (bound: FlowchartBounds): number => {
|
||||
const start = horizontal ? bound.left : bound.top
|
||||
const size = horizontal ? bound.width : bound.height
|
||||
return reversed ? -(start + size) : start
|
||||
}
|
||||
const primaryEnd = (bound: FlowchartBounds): number => primaryStart(bound) + (horizontal ? bound.width : bound.height)
|
||||
const ensureSubgraphEdgeItemClearance = (): boolean => {
|
||||
const allItemById = new Map(items.map((item) => [item.id, item]))
|
||||
const endpoints = new Map<string, FlowchartBounds>([...subgraphBounds, ...nodeBounds])
|
||||
let adjusted = false
|
||||
for (const edge of diagram.edges) {
|
||||
if (edge.orderOnly || (nodeIds.has(edge.from) && nodeIds.has(edge.to))) continue
|
||||
const fromItem = allItemById.get(itemByEndpoint.get(edge.from) ?? "")
|
||||
const toItem = allItemById.get(itemByEndpoint.get(edge.to) ?? "")
|
||||
const fromBound = endpoints.get(edge.from)
|
||||
const toBound = endpoints.get(edge.to)
|
||||
if (!fromItem || !toItem || fromItem === toItem || !fromBound || !toBound) continue
|
||||
if (primaryStart(toBound) <= primaryStart(fromBound)) continue
|
||||
const required = minSubgraphEdgeGap(fromItem.id, toItem.id)
|
||||
const deficit = primaryEnd(fromBound) + required - primaryStart(toBound)
|
||||
if (deficit <= 0) continue
|
||||
adjusted = true
|
||||
const toStart = primaryStart(toItem.bounds)
|
||||
const offset = reversed ? -deficit : deficit
|
||||
for (const candidate of items) {
|
||||
if (candidate !== toItem && primaryStart(candidate.bounds) < toStart) continue
|
||||
if (candidate === fromItem) continue
|
||||
moveItem(candidate, horizontal ? offset : 0, horizontal ? 0 : offset)
|
||||
}
|
||||
}
|
||||
return adjusted
|
||||
}
|
||||
if (rankedItems.length < 2) return ensureSubgraphEdgeItemClearance()
|
||||
|
||||
const itemById = new Map(rankedItems.map((item) => [item.id, item]))
|
||||
const outgoing = new Map(rankedItems.map((item) => [item.id, new Set<string>()]))
|
||||
@@ -947,27 +1177,25 @@ function separateTopLevelItems(
|
||||
)
|
||||
for (const item of rankedItems) item.rank = ranks.get(item.id)!
|
||||
|
||||
const reversed = diagram.direction === "RL" || diagram.direction === "BT"
|
||||
const primaryStart = (item: (typeof items)[number]): number => {
|
||||
const start = horizontal ? item.bounds.left : item.bounds.top
|
||||
const size = horizontal ? item.bounds.width : item.bounds.height
|
||||
return reversed ? -(start + size) : start
|
||||
}
|
||||
const itemsByRank = Map.groupBy(rankedItems, (item) => item.rank)
|
||||
const rankKeys = [...itemsByRank.keys()].sort((a, b) => a - b)
|
||||
let cursor: number | undefined
|
||||
let previousEnd: number | undefined
|
||||
let previousRankItems: (typeof items)[number][] = []
|
||||
let moved = false
|
||||
for (const rank of rankKeys) {
|
||||
const rankItems = itemsByRank.get(rank)!
|
||||
const start = Math.min(...rankItems.map(primaryStart))
|
||||
const end = Math.max(
|
||||
...rankItems.map((item) => primaryStart(item) + (horizontal ? item.bounds.width : item.bounds.height)),
|
||||
)
|
||||
if (cursor === undefined) {
|
||||
cursor = end + gap
|
||||
const start = Math.min(...rankItems.map((item) => primaryStart(item.bounds)))
|
||||
const end = Math.max(...rankItems.map((item) => primaryEnd(item.bounds)))
|
||||
if (previousEnd === undefined) {
|
||||
previousEnd = end
|
||||
previousRankItems = rankItems
|
||||
continue
|
||||
}
|
||||
const shift = Math.max(0, cursor - start)
|
||||
const rankGap = Math.max(
|
||||
gap,
|
||||
...previousRankItems.flatMap((prev) => rankItems.map((curr) => minSubgraphEdgeGap(prev.id, curr.id))),
|
||||
)
|
||||
const shift = Math.max(0, previousEnd + rankGap - start)
|
||||
if (shift > 0) {
|
||||
moved = true
|
||||
for (const item of rankItems) {
|
||||
@@ -975,7 +1203,8 @@ function separateTopLevelItems(
|
||||
moveItem(item, horizontal ? offset : 0, horizontal ? 0 : offset)
|
||||
}
|
||||
}
|
||||
cursor = end + shift + gap
|
||||
previousEnd = end + shift
|
||||
previousRankItems = rankItems
|
||||
}
|
||||
|
||||
for (const rank of rankKeys) {
|
||||
@@ -1027,6 +1256,7 @@ function separateTopLevelItems(
|
||||
}
|
||||
}
|
||||
}
|
||||
moved = ensureSubgraphEdgeItemClearance() || moved
|
||||
return moved
|
||||
}
|
||||
|
||||
@@ -1037,6 +1267,7 @@ function layoutSubgraphs(
|
||||
): Map<string, FlowchartSubgraphBounds> {
|
||||
const subgraphBounds = new Map<string, FlowchartSubgraphBounds>()
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
|
||||
for (const subgraph of [...subgraphs].reverse()) {
|
||||
const children: FlowchartBounds[] = []
|
||||
@@ -1052,7 +1283,7 @@ function layoutSubgraphs(
|
||||
}
|
||||
if (children.length > 0) {
|
||||
const bound = subgraphBoundFromChildren(subgraph.id, subgraph.label, children)
|
||||
bound.labelSide = chooseSubgraphLabelSide(bound, routes)
|
||||
bound.labelSide = chooseSubgraphLabelSide(bound, routes, nodeIds)
|
||||
subgraphBounds.set(subgraph.id, bound)
|
||||
}
|
||||
}
|
||||
@@ -1088,14 +1319,33 @@ function layoutFlowchartWithDirection(
|
||||
const bounds = ranked.bounds
|
||||
const responsive = layoutLocalSubgraphDirections(diagram, bounds, minNodeGap, requestedMinRankGap, targetWidth)
|
||||
const directionAligned = responsiveFallback || responsive || ranked.wrapped
|
||||
const route = (frames?: ReadonlyMap<string, FlowchartSubgraphBounds>) =>
|
||||
routeFlowchartEdges(diagram, bounds, (edge) => edgeDirection(diagram, edge), frames, targetWidth, directionAligned)
|
||||
const nodeIds = new Set(diagram.nodes.map((node) => node.id))
|
||||
const hasSubgraphEndpoints = diagram.edges.some(
|
||||
(edge) => !edge.orderOnly && (!nodeIds.has(edge.from) || !nodeIds.has(edge.to)),
|
||||
)
|
||||
const memberDiagram = hasSubgraphEndpoints
|
||||
? { ...diagram, edges: diagram.edges.filter((edge) => nodeIds.has(edge.from) && nodeIds.has(edge.to)) }
|
||||
: diagram
|
||||
const route = (
|
||||
frames?: ReadonlyMap<string, FlowchartSubgraphBounds>,
|
||||
targetDiagram: FlowchartDiagram = memberDiagram,
|
||||
lockedRoutes?: ReadonlyMap<FlowchartEdge, FlowchartEdgeRoute>,
|
||||
) =>
|
||||
routeFlowchartEdges(
|
||||
targetDiagram,
|
||||
bounds,
|
||||
(edge) => edgeDirection(diagram, edge),
|
||||
frames,
|
||||
targetWidth,
|
||||
directionAligned,
|
||||
lockedRoutes,
|
||||
)
|
||||
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
let subgraphBounds = new Map<string, FlowchartSubgraphBounds>()
|
||||
let routes: FlowchartEdgeRoute[]
|
||||
if (subgraphs.length === 0) {
|
||||
routes = route()
|
||||
routes = route(undefined, diagram)
|
||||
} else {
|
||||
routes = route()
|
||||
subgraphBounds = layoutSubgraphs(diagram, bounds, routes)
|
||||
@@ -1122,6 +1372,31 @@ function layoutFlowchartWithDirection(
|
||||
targetWidth,
|
||||
)
|
||||
if (expanded) routes = route(subgraphBounds)
|
||||
if (hasSubgraphEndpoints) {
|
||||
const locked = new Map(routes.map((memberRoute) => [memberRoute.edge, memberRoute]))
|
||||
for (const edge of diagram.edges) {
|
||||
if (edge.orderOnly || nodeIds.has(edge.from)) continue
|
||||
const sourceFrame = subgraphBounds.get(edge.from)
|
||||
const targetBound = bounds.get(edge.to) ?? subgraphBounds.get(edge.to)
|
||||
if (!sourceFrame || !targetBound || sourceFrame.labelSide !== "top") continue
|
||||
if (!isHorizontalDirection(edgeDirection(diagram, edge)) && targetBound.centerY < sourceFrame.centerY) {
|
||||
const bottomSlot = labelSlot(sourceFrame, "bottom")
|
||||
if (!routes.some((route) => routeOverlapsSlot(route, sourceFrame, bottomSlot, nodeIds))) {
|
||||
sourceFrame.labelSide = "bottom"
|
||||
}
|
||||
}
|
||||
}
|
||||
routes = route(subgraphBounds, diagram, locked)
|
||||
let labelSideChanged = false
|
||||
for (const bound of subgraphBounds.values()) {
|
||||
const nextSide = chooseSubgraphLabelSide(bound, routes, nodeIds)
|
||||
if (nextSide !== bound.labelSide) {
|
||||
bound.labelSide = nextSide
|
||||
labelSideChanged = true
|
||||
}
|
||||
}
|
||||
if (labelSideChanged) routes = route(subgraphBounds, diagram, locked)
|
||||
}
|
||||
avoidFlowchartFrameBorders(routes, bounds, subgraphBounds)
|
||||
}
|
||||
freezeRouteLabelPoints(routes)
|
||||
|
||||
@@ -274,6 +274,61 @@ function hasInternalStatementSeparator(line: string): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
function resolveSubgraphEndpoints(
|
||||
nodes: Map<string, FlowchartNode>,
|
||||
edges: readonly FlowchartEdge[],
|
||||
subgraphs: FlowchartSubgraph[],
|
||||
explicitShapeIds: ReadonlySet<string>,
|
||||
referencedInOpenSubgraphs: ReadonlyMap<string, ReadonlySet<string>>,
|
||||
): void {
|
||||
const resolved = new Set<string>()
|
||||
const descendantsOf = (subgraphId: string): Set<string> => {
|
||||
const ids = new Set<string>()
|
||||
for (const child of subgraphs) {
|
||||
if (child.parentId !== subgraphId) continue
|
||||
ids.add(child.id)
|
||||
for (const nested of descendantsOf(child.id)) ids.add(nested)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
const membersOf = (subgraphId: string, descendants: ReadonlySet<string>): Set<string> => {
|
||||
const ids = new Set<string>()
|
||||
for (const candidate of subgraphs) {
|
||||
if (candidate.id !== subgraphId && !descendants.has(candidate.id)) continue
|
||||
for (const nodeId of candidate.nodeIds) {
|
||||
if (nodeId === subgraphId || descendants.has(nodeId) || resolved.has(nodeId)) continue
|
||||
ids.add(nodeId)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
for (const subgraph of [...subgraphs].reverse()) {
|
||||
subgraph.nodeIds = subgraph.nodeIds.filter((id) => !resolved.has(id))
|
||||
const descendants = descendantsOf(subgraph.id)
|
||||
const members = membersOf(subgraph.id, descendants)
|
||||
const linksSelfOrDescendant = edges.some(
|
||||
(edge) =>
|
||||
(edge.from === subgraph.id && (edge.to === subgraph.id || members.has(edge.to) || descendants.has(edge.to))) ||
|
||||
(edge.to === subgraph.id && (members.has(edge.from) || descendants.has(edge.from))),
|
||||
)
|
||||
if (
|
||||
explicitShapeIds.has(subgraph.id) ||
|
||||
referencedInOpenSubgraphs.get(subgraph.id)?.has(subgraph.id) ||
|
||||
members.size === 0 ||
|
||||
linksSelfOrDescendant
|
||||
) {
|
||||
continue
|
||||
}
|
||||
resolved.add(subgraph.id)
|
||||
nodes.delete(subgraph.id)
|
||||
}
|
||||
|
||||
for (const subgraph of subgraphs) {
|
||||
subgraph.nodeIds = subgraph.nodeIds.filter((id) => !resolved.has(id))
|
||||
}
|
||||
}
|
||||
|
||||
export function isMermaidFlowchartDiagram(content: string): boolean {
|
||||
return FLOWCHART_HEADER_RE.test(firstMeaningfulMermaidLine(content) ?? "")
|
||||
}
|
||||
@@ -283,6 +338,18 @@ export function parseMermaidFlowchartDiagram(content: string): FlowchartDiagram
|
||||
const edges: FlowchartEdge[] = []
|
||||
const subgraphs: FlowchartSubgraph[] = []
|
||||
const subgraphStack: Array<{ subgraph: FlowchartSubgraph; lineNumber: number; sourceLine: string }> = []
|
||||
const explicitShapeIds = new Set<string>()
|
||||
const referencedInOpenSubgraphs = new Map<string, Set<string>>()
|
||||
const trackNodeToken = (stripped: string): string => {
|
||||
const node = ensureNode(nodes, stripped)
|
||||
if (hasExplicitNodeShape(stripped)) explicitShapeIds.add(node.id)
|
||||
for (const open of subgraphStack) {
|
||||
const set = referencedInOpenSubgraphs.get(node.id) ?? new Set<string>()
|
||||
set.add(open.subgraph.id)
|
||||
referencedInOpenSubgraphs.set(node.id, set)
|
||||
}
|
||||
return node.id
|
||||
}
|
||||
let direction: FlowchartDirection = DEFAULT_DIRECTION
|
||||
|
||||
for (const source of meaningfulNumberedMermaidLines(content)) {
|
||||
@@ -348,22 +415,11 @@ export function parseMermaidFlowchartDiagram(content: string): FlowchartDiagram
|
||||
].map((group) => splitNodeGroup(group).map(stripNodeToken))
|
||||
|
||||
if (nodeGroups.every((group) => group.every((token) => token.length > 0))) {
|
||||
const unsupportedEndpoint = nodeGroups.find((group, index) => {
|
||||
const orderOnlyEndpoint = edgeOperators[index - 1]?.orderOnly || edgeOperators[index]?.orderOnly
|
||||
return group.some(
|
||||
(stripped) =>
|
||||
!(orderOnlyEndpoint && subgraphs.some((subgraph) => subgraph.id === stripped)) &&
|
||||
!isSupportedNodeToken(stripped),
|
||||
)
|
||||
})
|
||||
const unsupportedEndpoint = nodeGroups.find((group) =>
|
||||
group.some((stripped) => !isSupportedNodeToken(stripped)),
|
||||
)
|
||||
if (unsupportedEndpoint) throw new MermaidSyntaxError("flowchart", source.lineNumber, line)
|
||||
const chainNodeIds = nodeGroups.map((group, index) => {
|
||||
const orderOnlyEndpoint = edgeOperators[index - 1]?.orderOnly || edgeOperators[index]?.orderOnly
|
||||
return group.map((stripped) => {
|
||||
if (orderOnlyEndpoint && subgraphs.some((subgraph) => subgraph.id === stripped)) return stripped
|
||||
return ensureNode(nodes, stripped).id
|
||||
})
|
||||
})
|
||||
const chainNodeIds = nodeGroups.map((group) => group.map(trackNodeToken))
|
||||
for (const nodeId of chainNodeIds.flat()) {
|
||||
if (nodes.has(nodeId)) addNodeToSubgraph(currentSubgraph, nodeId)
|
||||
}
|
||||
@@ -389,7 +445,7 @@ export function parseMermaidFlowchartDiagram(content: string): FlowchartDiagram
|
||||
|
||||
const nodeGroup = splitNodeGroup(line)
|
||||
if (nodeGroup.every(isSupportedNodeToken)) {
|
||||
for (const token of nodeGroup) addNodeToSubgraph(currentSubgraph, ensureNode(nodes, stripNodeToken(token)).id)
|
||||
for (const token of nodeGroup) addNodeToSubgraph(currentSubgraph, trackNodeToken(stripNodeToken(token)))
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -406,5 +462,6 @@ export function parseMermaidFlowchartDiagram(content: string): FlowchartDiagram
|
||||
)
|
||||
}
|
||||
|
||||
resolveSubgraphEndpoints(nodes, edges, subgraphs, explicitShapeIds, referencedInOpenSubgraphs)
|
||||
return { direction, nodes: [...nodes.values()], edges, subgraphs }
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import type {
|
||||
FlowchartDirection,
|
||||
FlowchartEdge,
|
||||
FlowchartEdgeRoute,
|
||||
FlowchartEndpointBounds,
|
||||
FlowchartNodeBounds,
|
||||
FlowchartPoint,
|
||||
FlowchartSubgraph,
|
||||
@@ -57,13 +58,13 @@ function isVerticalDirection(direction: FlowchartDirection): boolean {
|
||||
return direction === "TB" || direction === "TD" || direction === "BT"
|
||||
}
|
||||
|
||||
function verticalTravel(from: FlowchartNodeBounds, to: FlowchartNodeBounds): VerticalTravel {
|
||||
function verticalTravel(from: FlowchartEndpointBounds, to: FlowchartEndpointBounds): VerticalTravel {
|
||||
return centerCoordinate(to, "y") >= centerCoordinate(from, "y") ? "down" : "up"
|
||||
}
|
||||
|
||||
function isVerticalBackEdge(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
direction: FlowchartDirection,
|
||||
): boolean {
|
||||
return direction === "BT"
|
||||
@@ -72,8 +73,8 @@ function isVerticalBackEdge(
|
||||
}
|
||||
|
||||
function isHorizontalBackEdge(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
direction: FlowchartDirection,
|
||||
): boolean {
|
||||
return direction === "RL"
|
||||
@@ -82,8 +83,8 @@ function isHorizontalBackEdge(
|
||||
}
|
||||
|
||||
function horizontalTravel(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
direction: FlowchartDirection,
|
||||
): HorizontalTravel {
|
||||
const targetIsRight = centerCoordinate(to, "x") > centerCoordinate(from, "x")
|
||||
@@ -91,13 +92,37 @@ function horizontalTravel(
|
||||
return direction === "RL" ? (targetIsRight ? "right" : "left") : targetIsSameOrRight ? "right" : "left"
|
||||
}
|
||||
|
||||
function isSubgraphBounds(bounds: FlowchartEndpointBounds): bounds is FlowchartSubgraphBounds {
|
||||
return "labelSide" in bounds
|
||||
}
|
||||
|
||||
function clearSubgraphTitlePort(bounds: FlowchartEndpointBounds, port: FlowchartPoint, role: PortRole): FlowchartPoint {
|
||||
if (role !== "source" || !isSubgraphBounds(bounds) || bounds.label.length === 0) return port
|
||||
const connector = flowchartSourceConnector(bounds, port)
|
||||
const title = subgraphTitleBounds(bounds)
|
||||
const overlapsTitle =
|
||||
connector.y >= title.top &&
|
||||
connector.y < title.top + title.height &&
|
||||
connector.x >= title.left &&
|
||||
connector.x < title.left + title.width
|
||||
if (!overlapsTitle) return port
|
||||
const afterTitleX = title.left + title.width
|
||||
if (afterTitleX <= bounds.left + bounds.width - 2) return { x: afterTitleX, y: port.y }
|
||||
if (title.left - 1 >= bounds.left + 1) return { x: title.left - 1, y: port.y }
|
||||
return port
|
||||
}
|
||||
|
||||
function endpointSidePoint(bounds: FlowchartEndpointBounds, side: DiagramSide, role: PortRole): FlowchartPoint {
|
||||
return clearSubgraphTitlePort(bounds, boundsSidePoint(bounds, side), role)
|
||||
}
|
||||
|
||||
function verticalBackEdgePath(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
leftBoundary?: number,
|
||||
): FlowchartPoint[] {
|
||||
const start = boundsSidePoint(from, "left")
|
||||
const end = boundsSidePoint(to, "left")
|
||||
const start = endpointSidePoint(from, "left", "source")
|
||||
const end = endpointSidePoint(to, "left", "target")
|
||||
const busX = Math.min(
|
||||
afterFarthestCoordinate([start, end], "x", "left", BUS_CLEARANCE),
|
||||
leftBoundary === undefined ? Number.POSITIVE_INFINITY : leftBoundary - BUS_CLEARANCE * 2,
|
||||
@@ -105,27 +130,32 @@ function verticalBackEdgePath(
|
||||
return pathViaLane(start, lane("x", busX), end)
|
||||
}
|
||||
|
||||
function verticalForwardEdgePath(from: FlowchartNodeBounds, to: FlowchartNodeBounds): FlowchartPoint[] {
|
||||
function verticalForwardEdgePath(from: FlowchartEndpointBounds, to: FlowchartEndpointBounds): FlowchartPoint[] {
|
||||
const travel = verticalTravel(from, to)
|
||||
const startSide = sideForDirection(travel)
|
||||
const endSide = oppositeSide(startSide)
|
||||
const sourceCenter = boundsCenter(from)
|
||||
const targetCenter = boundsCenter(to)
|
||||
const start = withCoordinate(boundsSidePoint(from, startSide), "x", snapCoordinate(sourceCenter.x, targetCenter.x, 1))
|
||||
const end = boundsSidePoint(to, endSide)
|
||||
const snapped = withCoordinate(
|
||||
boundsSidePoint(from, startSide),
|
||||
"x",
|
||||
snapCoordinate(sourceCenter.x, targetCenter.x, 1),
|
||||
)
|
||||
const start = clearSubgraphTitlePort(from, snapped, "source")
|
||||
const end = endpointSidePoint(to, endSide, "target")
|
||||
return orthogonalPath(start, end, { preferredAxis: "y" })
|
||||
}
|
||||
|
||||
function horizontalBackEdgePath(from: FlowchartNodeBounds, to: FlowchartNodeBounds): FlowchartPoint[] {
|
||||
const start = boundsSidePoint(from, "top")
|
||||
const end = boundsSidePoint(to, "top")
|
||||
function horizontalBackEdgePath(from: FlowchartEndpointBounds, to: FlowchartEndpointBounds): FlowchartPoint[] {
|
||||
const start = endpointSidePoint(from, "top", "source")
|
||||
const end = endpointSidePoint(to, "top", "target")
|
||||
const busY = afterFarthestCoordinate([start, end], "y", "up", BUS_CLEARANCE)
|
||||
return pathViaLane(start, lane("y", busY), end)
|
||||
}
|
||||
|
||||
function horizontalEdgePath(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
direction: FlowchartDirection,
|
||||
): FlowchartPoint[] {
|
||||
const overlapsHorizontally = from.left < to.left + to.width && to.left < from.left + from.width
|
||||
@@ -135,33 +165,37 @@ function horizontalEdgePath(
|
||||
|
||||
const travel = horizontalTravel(from, to, direction)
|
||||
const startSide = sideForDirection(travel)
|
||||
return orthogonalPath(boundsSidePoint(from, startSide), boundsSidePoint(to, oppositeSide(startSide)), {
|
||||
preferredAxis: "x",
|
||||
})
|
||||
return orthogonalPath(
|
||||
endpointSidePoint(from, startSide, "source"),
|
||||
endpointSidePoint(to, oppositeSide(startSide), "target"),
|
||||
{
|
||||
preferredAxis: "x",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
function selfEdgePath(bounds: FlowchartNodeBounds, laneOffset = 0): FlowchartPoint[] {
|
||||
const start = boundsSidePoint(bounds, "right")
|
||||
const end = boundsSidePoint(bounds, "bottom")
|
||||
function selfEdgePath(bounds: FlowchartEndpointBounds, laneOffset = 0): FlowchartPoint[] {
|
||||
const start = endpointSidePoint(bounds, "right", "source")
|
||||
const end = endpointSidePoint(bounds, "bottom", "target")
|
||||
const rightLaneX = bounds.left + bounds.width + BUS_CLEARANCE + laneOffset
|
||||
const bottomLaneY = bounds.top + bounds.height + 1 + laneOffset
|
||||
return [start, { x: rightLaneX, y: start.y }, { x: rightLaneX, y: bottomLaneY }, { x: end.x, y: bottomLaneY }, end]
|
||||
}
|
||||
|
||||
function parallelEdgePath(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
axis: DiagramAxis,
|
||||
laneCoordinate: number,
|
||||
): FlowchartPoint[] {
|
||||
if (axis === "y") {
|
||||
const start = boundsSidePoint(from, "bottom")
|
||||
const end = boundsSidePoint(to, "bottom")
|
||||
const start = endpointSidePoint(from, "bottom", "source")
|
||||
const end = endpointSidePoint(to, "bottom", "target")
|
||||
return pathViaLane(start, lane("y", laneCoordinate), end)
|
||||
}
|
||||
|
||||
const start = boundsSidePoint(from, "right")
|
||||
const end = boundsSidePoint(to, "right")
|
||||
const start = endpointSidePoint(from, "right", "source")
|
||||
const end = endpointSidePoint(to, "right", "target")
|
||||
return pathViaLane(start, lane("x", laneCoordinate), end)
|
||||
}
|
||||
|
||||
@@ -178,13 +212,13 @@ function rightRenderExtent(route: FlowchartEdgeRoute): number {
|
||||
return right
|
||||
}
|
||||
|
||||
function parallelLaneAxis(from: FlowchartNodeBounds, to: FlowchartNodeBounds): DiagramAxis {
|
||||
function parallelLaneAxis(from: FlowchartEndpointBounds, to: FlowchartEndpointBounds): DiagramAxis {
|
||||
return Math.abs(to.centerX - from.centerX) >= Math.abs(to.centerY - from.centerY) ? "y" : "x"
|
||||
}
|
||||
|
||||
function edgePath(
|
||||
from: FlowchartNodeBounds,
|
||||
to: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
to: FlowchartEndpointBounds,
|
||||
direction: FlowchartDirection,
|
||||
leftBoundary?: number,
|
||||
): FlowchartPoint[] {
|
||||
@@ -194,8 +228,8 @@ function edgePath(
|
||||
if (overlapsVertically) {
|
||||
const travel: HorizontalTravel = centerCoordinate(to, "x") >= centerCoordinate(from, "x") ? "right" : "left"
|
||||
return orthogonalPath(
|
||||
boundsSidePoint(from, sideForDirection(travel)),
|
||||
boundsSidePoint(to, oppositeSide(sideForDirection(travel))),
|
||||
endpointSidePoint(from, sideForDirection(travel), "source"),
|
||||
endpointSidePoint(to, oppositeSide(sideForDirection(travel)), "target"),
|
||||
)
|
||||
}
|
||||
return isVerticalBackEdge(from, to, direction)
|
||||
@@ -273,21 +307,36 @@ function targetFanInLane(
|
||||
return keepBefore(unclamped, advanceCoordinate(targetCoordinate, travel, -1), travel)
|
||||
}
|
||||
|
||||
function portForTravel(bounds: FlowchartNodeBounds, travel: DiagramDirection, role: PortRole): FlowchartPoint {
|
||||
function portForTravel(bounds: FlowchartEndpointBounds, travel: DiagramDirection, role: PortRole): FlowchartPoint {
|
||||
const side = role === "source" ? sideForDirection(travel) : oppositeSide(sideForDirection(travel))
|
||||
return boundsSidePoint(bounds, side)
|
||||
return endpointSidePoint(bounds, side, role)
|
||||
}
|
||||
|
||||
function subgraphContainsMember(
|
||||
subgraphs: readonly FlowchartSubgraph[],
|
||||
subgraph: FlowchartSubgraph,
|
||||
endpointId: string,
|
||||
): boolean {
|
||||
return (
|
||||
subgraph.nodeIds.includes(endpointId) ||
|
||||
subgraphs.some(
|
||||
(child) =>
|
||||
child.parentId === subgraph.id &&
|
||||
(child.id === endpointId || subgraphContainsMember(subgraphs, child, endpointId)),
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function horizontalForwardRecords(
|
||||
edges: FlowchartEdge[],
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
direction: FlowchartDirection,
|
||||
): EdgeRecord[] {
|
||||
const travel = direction === "RL" ? "left" : "right"
|
||||
const records: EdgeRecord[] = []
|
||||
for (const edge of edges) {
|
||||
const source = bounds.get(edge.from)
|
||||
const target = bounds.get(edge.to)
|
||||
const source = endpoints.get(edge.from)
|
||||
const target = endpoints.get(edge.to)
|
||||
if (!source || !target) continue
|
||||
const forward =
|
||||
direction === "RL"
|
||||
@@ -305,14 +354,14 @@ function horizontalForwardRecords(
|
||||
|
||||
function verticalForwardRecords(
|
||||
edges: FlowchartEdge[],
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
direction: FlowchartDirection,
|
||||
): EdgeRecord[] {
|
||||
const travel = direction === "BT" ? "up" : "down"
|
||||
const records: EdgeRecord[] = []
|
||||
for (const edge of edges) {
|
||||
const source = bounds.get(edge.from)
|
||||
const target = bounds.get(edge.to)
|
||||
const source = endpoints.get(edge.from)
|
||||
const target = endpoints.get(edge.to)
|
||||
if (!source || !target) continue
|
||||
const forward =
|
||||
direction === "BT"
|
||||
@@ -329,17 +378,23 @@ function verticalForwardRecords(
|
||||
}
|
||||
|
||||
function horizontalExitSubgraph(diagram: FlowchartDiagram, edge: FlowchartEdge): FlowchartSubgraph | undefined {
|
||||
for (const subgraph of [...(diagram.subgraphs ?? [])].reverse()) {
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
for (const subgraph of [...subgraphs].reverse()) {
|
||||
if (subgraph.direction !== "LR" && subgraph.direction !== "RL") continue
|
||||
if (subgraph.nodeIds.includes(edge.from) && !subgraph.nodeIds.includes(edge.to)) return subgraph
|
||||
if (subgraph.id === edge.from || subgraph.id === edge.to) continue
|
||||
if (subgraphContainsMember(subgraphs, subgraph, edge.from) && !subgraphContainsMember(subgraphs, subgraph, edge.to))
|
||||
return subgraph
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function horizontalEntrySubgraph(diagram: FlowchartDiagram, edge: FlowchartEdge): FlowchartSubgraph | undefined {
|
||||
for (const subgraph of [...(diagram.subgraphs ?? [])].reverse()) {
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
for (const subgraph of [...subgraphs].reverse()) {
|
||||
if (subgraph.direction !== "LR" && subgraph.direction !== "RL") continue
|
||||
if (subgraph.nodeIds.includes(edge.to) && !subgraph.nodeIds.includes(edge.from)) return subgraph
|
||||
if (subgraph.id === edge.from || subgraph.id === edge.to) continue
|
||||
if (subgraphContainsMember(subgraphs, subgraph, edge.to) && !subgraphContainsMember(subgraphs, subgraph, edge.from))
|
||||
return subgraph
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
@@ -416,7 +471,7 @@ function alignClusteredVerticalSources(records: readonly EdgeRecord[]): EdgeReco
|
||||
|
||||
function routeHorizontalFanOut(
|
||||
records: readonly EdgeRecord[],
|
||||
bounds: ReadonlyMap<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
direction: FlowchartDirection,
|
||||
handled: Set<FlowchartEdge>,
|
||||
routes: FlowchartEdgeRoute[],
|
||||
@@ -433,7 +488,7 @@ function routeHorizontalFanOut(
|
||||
for (const record of sourceRecords) {
|
||||
const targetOwner = targetOwners.get(record.edge.to)
|
||||
targetOwners.set(record.edge.to, targetOwner ?? sourceId)
|
||||
const target = bounds.get(record.edge.to)
|
||||
const target = endpoints.get(record.edge.to)
|
||||
if (!targetOwner || targetOwner === sourceId || !target) {
|
||||
routes.push(fanRoute(record.edge, sourcePort, record.targetPort, lane("x", busX)))
|
||||
handled.add(record.edge)
|
||||
@@ -441,7 +496,7 @@ function routeHorizontalFanOut(
|
||||
}
|
||||
|
||||
const targetSide = sourcePort.y < record.targetPort.y ? "top" : "bottom"
|
||||
const targetPoint = boundsSidePoint(target, targetSide)
|
||||
const targetPoint = endpointSidePoint(target, targetSide, "target")
|
||||
const approach = shiftPoint(targetPoint, targetSide === "top" ? "up" : "down")
|
||||
routes.push({
|
||||
edge: record.edge,
|
||||
@@ -523,7 +578,7 @@ function routeVerticalFanIn(
|
||||
|
||||
function routeParallelEdges(
|
||||
diagram: FlowchartDiagram,
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
directionForEdge: (edge: FlowchartEdge) => FlowchartDirection,
|
||||
directionAligned: boolean,
|
||||
handled: Set<FlowchartEdge>,
|
||||
@@ -532,8 +587,8 @@ function routeParallelEdges(
|
||||
const groups = groupRecords(diagram.edges, (edge) => `${edge.from}:${edge.to}`)
|
||||
for (const edges of groups.values()) {
|
||||
if (edges.length < 2) continue
|
||||
const from = bounds.get(edges[0]!.from)
|
||||
const to = bounds.get(edges[0]!.to)
|
||||
const from = endpoints.get(edges[0]!.from)
|
||||
const to = endpoints.get(edges[0]!.to)
|
||||
if (!from || !to) continue
|
||||
if (from.id === to.id) {
|
||||
let laneOffset = 0
|
||||
@@ -571,19 +626,22 @@ function routeParallelEdges(
|
||||
|
||||
function routeHorizontalSubgraphExitFanIn(
|
||||
diagram: FlowchartDiagram,
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds> | undefined,
|
||||
handled: Set<FlowchartEdge>,
|
||||
routes: FlowchartEdgeRoute[],
|
||||
): void {
|
||||
if (!subgraphBounds) return
|
||||
|
||||
const groups = new Map<string, { edge: FlowchartEdge; subgraph: FlowchartSubgraph; source: FlowchartNodeBounds }[]>()
|
||||
const groups = new Map<
|
||||
string,
|
||||
{ edge: FlowchartEdge; subgraph: FlowchartSubgraph; source: FlowchartEndpointBounds }[]
|
||||
>()
|
||||
for (const edge of diagram.edges) {
|
||||
if (handled.has(edge)) continue
|
||||
const subgraph = horizontalExitSubgraph(diagram, edge)
|
||||
const source = bounds.get(edge.from)
|
||||
const target = bounds.get(edge.to)
|
||||
const source = endpoints.get(edge.from)
|
||||
const target = endpoints.get(edge.to)
|
||||
if (!subgraph || !source || !target) continue
|
||||
|
||||
const key = `${subgraph.id}:${edge.to}`
|
||||
@@ -595,7 +653,7 @@ function routeHorizontalSubgraphExitFanIn(
|
||||
for (const group of groups.values()) {
|
||||
const subgraph = group[0]!.subgraph
|
||||
const subgraphBound = subgraphBounds.get(subgraph.id)
|
||||
const target = bounds.get(group[0]!.edge.to)
|
||||
const target = endpoints.get(group[0]!.edge.to)
|
||||
if (!subgraphBound || !target) continue
|
||||
|
||||
const travel: HorizontalTravel = subgraph.direction === "RL" ? "left" : "right"
|
||||
@@ -608,7 +666,7 @@ function routeHorizontalSubgraphExitFanIn(
|
||||
const targetBelow = target.centerY >= subgraphBound.centerY
|
||||
const targetPort = targetSubgraph
|
||||
? portForTravel(target, horizontalSubgraphEntryTravel(targetSubgraph), "target")
|
||||
: boundsSidePoint(target, targetBelow ? "top" : "bottom")
|
||||
: endpointSidePoint(target, targetBelow ? "top" : "bottom", "target")
|
||||
const joinY = targetSubgraphBound
|
||||
? horizontalSubgraphJoinY(subgraphBound, targetSubgraphBound)
|
||||
: horizontalSubgraphExitJoinY(subgraphBound, targetPort, targetBelow)
|
||||
@@ -637,7 +695,7 @@ function routeHorizontalSubgraphExitFanIn(
|
||||
|
||||
function routeHorizontalSubgraphEntries(
|
||||
diagram: FlowchartDiagram,
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds> | undefined,
|
||||
handled: Set<FlowchartEdge>,
|
||||
routes: FlowchartEdgeRoute[],
|
||||
@@ -648,8 +706,8 @@ function routeHorizontalSubgraphEntries(
|
||||
if (handled.has(edge)) continue
|
||||
const subgraph = horizontalEntrySubgraph(diagram, edge)
|
||||
const subgraphBound = subgraph ? subgraphBounds.get(subgraph.id) : undefined
|
||||
const from = bounds.get(edge.from)
|
||||
const to = bounds.get(edge.to)
|
||||
const from = endpoints.get(edge.from)
|
||||
const to = endpoints.get(edge.to)
|
||||
if (!subgraph || !subgraphBound || !from || !to) continue
|
||||
|
||||
const targetPort = portForTravel(to, horizontalSubgraphEntryTravel(subgraph), "target")
|
||||
@@ -875,7 +933,8 @@ function subgraphTitleBounds(bounds: FlowchartSubgraphBounds): {
|
||||
function avoidNodeObstacles(
|
||||
route: FlowchartEdgeRoute,
|
||||
routes: readonly FlowchartEdgeRoute[],
|
||||
bounds: Map<string, FlowchartNodeBounds>,
|
||||
bounds: ReadonlyMap<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds> | undefined,
|
||||
routeIndex: number,
|
||||
diagram: FlowchartDiagram,
|
||||
@@ -883,16 +942,19 @@ function avoidNodeObstacles(
|
||||
const allNodeBounds = [...bounds.values()]
|
||||
const allSubgraphBounds = [...(subgraphBounds?.values() ?? [])]
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
const contains = (subgraph: FlowchartSubgraph, nodeId: string): boolean =>
|
||||
subgraph.nodeIds.includes(nodeId) ||
|
||||
subgraphs.some((child) => child.parentId === subgraph.id && contains(child, nodeId))
|
||||
const contains = (subgraph: FlowchartSubgraph, endpointId: string): boolean =>
|
||||
subgraphContainsMember(subgraphs, subgraph, endpointId)
|
||||
const owner = [...subgraphs].reverse().find((subgraph) => {
|
||||
if (!contains(subgraph, route.edge.from) || !contains(subgraph, route.edge.to)) return false
|
||||
const children = subgraphs.filter((child) => child.parentId === subgraph.id)
|
||||
return (
|
||||
children.some((child) => contains(child, route.edge.from)) &&
|
||||
children.some((child) => contains(child, route.edge.to)) &&
|
||||
!children.some((child) => contains(child, route.edge.from) && contains(child, route.edge.to))
|
||||
children.some((child) => child.id === route.edge.from || contains(child, route.edge.from)) &&
|
||||
children.some((child) => child.id === route.edge.to || contains(child, route.edge.to)) &&
|
||||
!children.some(
|
||||
(child) =>
|
||||
(child.id === route.edge.from || contains(child, route.edge.from)) &&
|
||||
(child.id === route.edge.to || contains(child, route.edge.to)),
|
||||
)
|
||||
)
|
||||
})
|
||||
const ownerBounds = owner ? subgraphBounds?.get(owner.id) : undefined
|
||||
@@ -918,8 +980,21 @@ function avoidNodeObstacles(
|
||||
const allowedContact = isSource && isTarget ? "both" : isSource ? "source" : isTarget ? "target" : undefined
|
||||
return pathIntersectsBounds(candidate.points, bound, allowedContact)
|
||||
})
|
||||
const sourceSubgraph = bounds.has(route.edge.from) ? undefined : subgraphBounds?.get(route.edge.from)
|
||||
const intersectsSourceTitle = (candidate: FlowchartEdgeRoute): boolean => {
|
||||
if (!sourceSubgraph || sourceSubgraph.label.length === 0 || !candidate.points[0]) return false
|
||||
const connector = flowchartSourceConnector(sourceSubgraph, candidate.points[0])
|
||||
const title = subgraphTitleBounds(sourceSubgraph)
|
||||
return (
|
||||
connector.y >= title.top &&
|
||||
connector.y < title.top + title.height &&
|
||||
connector.x >= title.left &&
|
||||
connector.x < title.left + title.width
|
||||
)
|
||||
}
|
||||
const intersectsStructuralObstacle = (candidate: FlowchartEdgeRoute): boolean =>
|
||||
intersectsNode(candidate) ||
|
||||
intersectsSourceTitle(candidate) ||
|
||||
allSubgraphBounds.some(
|
||||
(bound) =>
|
||||
pathRunsAlongFrame(candidate.points, bound) ||
|
||||
@@ -945,8 +1020,8 @@ function avoidNodeObstacles(
|
||||
}
|
||||
if (!intersectsObstacle(route)) return route
|
||||
|
||||
const from = bounds.get(route.edge.from)
|
||||
const to = bounds.get(route.edge.to)
|
||||
const from = endpoints.get(route.edge.from)
|
||||
const to = endpoints.get(route.edge.to)
|
||||
if (!from || !to) return route
|
||||
const routingBounds = [...allNodeBounds, ...allSubgraphBounds]
|
||||
const rightBusX = Math.max(...routingBounds.map((bound) => bound.left + bound.width - 1)) + BUS_CLEARANCE
|
||||
@@ -1090,28 +1165,44 @@ function avoidNodeObstacles(
|
||||
(busX): FlowchartEdgeRoute => ({
|
||||
...route,
|
||||
labelAxis: route.labelAxis === undefined ? undefined : "y",
|
||||
points: pathViaLane(boundsSidePoint(from, "right"), lane("x", busX), boundsSidePoint(to, "right")),
|
||||
points: pathViaLane(
|
||||
endpointSidePoint(from, "right", "source"),
|
||||
lane("x", busX),
|
||||
endpointSidePoint(to, "right", "target"),
|
||||
),
|
||||
}),
|
||||
),
|
||||
...candidateLeftBusXs.map(
|
||||
(busX): FlowchartEdgeRoute => ({
|
||||
...route,
|
||||
labelAxis: route.labelAxis === undefined ? undefined : "y",
|
||||
points: pathViaLane(boundsSidePoint(from, "left"), lane("x", busX), boundsSidePoint(to, "left")),
|
||||
points: pathViaLane(
|
||||
endpointSidePoint(from, "left", "source"),
|
||||
lane("x", busX),
|
||||
endpointSidePoint(to, "left", "target"),
|
||||
),
|
||||
}),
|
||||
),
|
||||
...candidateTopBusYs.map(
|
||||
(busY): FlowchartEdgeRoute => ({
|
||||
...route,
|
||||
labelAxis: route.labelAxis === undefined ? undefined : "x",
|
||||
points: pathViaLane(boundsSidePoint(from, "top"), lane("y", busY), boundsSidePoint(to, "top")),
|
||||
points: pathViaLane(
|
||||
endpointSidePoint(from, "top", "source"),
|
||||
lane("y", busY),
|
||||
endpointSidePoint(to, "top", "target"),
|
||||
),
|
||||
}),
|
||||
),
|
||||
...candidateBottomBusYs.map(
|
||||
(busY): FlowchartEdgeRoute => ({
|
||||
...route,
|
||||
labelAxis: route.labelAxis === undefined ? undefined : "x",
|
||||
points: pathViaLane(boundsSidePoint(from, "bottom"), lane("y", busY), boundsSidePoint(to, "bottom")),
|
||||
points: pathViaLane(
|
||||
endpointSidePoint(from, "bottom", "source"),
|
||||
lane("y", busY),
|
||||
endpointSidePoint(to, "bottom", "target"),
|
||||
),
|
||||
}),
|
||||
),
|
||||
]
|
||||
@@ -1120,7 +1211,7 @@ function avoidNodeObstacles(
|
||||
)
|
||||
const attachments = targetSides.flatMap((sourceSide) =>
|
||||
targetSides.flatMap((targetSide) =>
|
||||
buses.map((bus) => routeViaBus(boundsSidePoint(from, sourceSide), targetSide, bus)),
|
||||
buses.map((bus) => routeViaBus(endpointSidePoint(from, sourceSide, "source"), targetSide, bus)),
|
||||
),
|
||||
)
|
||||
if (subgraphBounds) {
|
||||
@@ -1149,6 +1240,7 @@ function avoidLabelOverlap(
|
||||
route: FlowchartEdgeRoute,
|
||||
otherRoutes: readonly FlowchartEdgeRoute[],
|
||||
bounds: ReadonlyMap<string, FlowchartNodeBounds>,
|
||||
endpoints: ReadonlyMap<string, FlowchartEndpointBounds>,
|
||||
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds> | undefined,
|
||||
targetWidth?: number,
|
||||
includeLabelWidth = true,
|
||||
@@ -1160,7 +1252,7 @@ function avoidLabelOverlap(
|
||||
other.edge.label ? [flowchartRouteLabelLayout(other, diagramTextWidth)] : [],
|
||||
)
|
||||
const otherConnectorBounds = otherRoutes.flatMap((other) => {
|
||||
const source = bounds.get(other.edge.from)
|
||||
const source = endpoints.get(other.edge.from)
|
||||
const sourcePoint = other.points[0]
|
||||
if (!source || !sourcePoint) return []
|
||||
const connector = flowchartSourceConnector(source, sourcePoint)
|
||||
@@ -1249,54 +1341,71 @@ export function routeFlowchartEdges(
|
||||
subgraphBounds?: ReadonlyMap<string, FlowchartSubgraphBounds>,
|
||||
targetWidth?: number,
|
||||
directionAligned = false,
|
||||
lockedRoutes?: ReadonlyMap<FlowchartEdge, FlowchartEdgeRoute>,
|
||||
): FlowchartEdgeRoute[] {
|
||||
const routedDiagram = { ...diagram, edges: diagram.edges.filter((edge) => !edge.orderOnly) }
|
||||
const endpoints = new Map<string, FlowchartEndpointBounds>([
|
||||
...(subgraphBounds ? [...subgraphBounds] : []),
|
||||
...bounds,
|
||||
])
|
||||
const handled = new Set<FlowchartEdge>()
|
||||
const routes: FlowchartEdgeRoute[] = []
|
||||
if (lockedRoutes) {
|
||||
for (const [edge, route] of lockedRoutes) {
|
||||
routes.push(route)
|
||||
handled.add(edge)
|
||||
}
|
||||
}
|
||||
const leftBoundary = subgraphBounds
|
||||
? Math.min(...[...bounds.values(), ...subgraphBounds.values()].map((bound) => bound.left))
|
||||
: undefined
|
||||
|
||||
routeParallelEdges(routedDiagram, bounds, directionForEdge, directionAligned, handled, routes)
|
||||
routeParallelEdges(routedDiagram, endpoints, directionForEdge, directionAligned, handled, routes)
|
||||
|
||||
for (const direction of ["LR", "RL"] satisfies FlowchartDirection[]) {
|
||||
const horizontalEdges = routedDiagram.edges.filter(
|
||||
(edge) => !handled.has(edge) && directionForEdge(edge) === direction,
|
||||
)
|
||||
if (horizontalEdges.length === 0) continue
|
||||
const records = horizontalForwardRecords(horizontalEdges, bounds, direction)
|
||||
routeHorizontalFanOut(records, bounds, direction, handled, routes)
|
||||
const records = horizontalForwardRecords(horizontalEdges, endpoints, direction)
|
||||
routeHorizontalFanOut(records, endpoints, direction, handled, routes)
|
||||
routeHorizontalFanIn(records, direction, handled, routes)
|
||||
}
|
||||
|
||||
routeHorizontalSubgraphExitFanIn(routedDiagram, bounds, subgraphBounds, handled, routes)
|
||||
routeHorizontalSubgraphEntries(routedDiagram, bounds, subgraphBounds, handled, routes)
|
||||
routeHorizontalSubgraphExitFanIn(routedDiagram, endpoints, subgraphBounds, handled, routes)
|
||||
routeHorizontalSubgraphEntries(routedDiagram, endpoints, subgraphBounds, handled, routes)
|
||||
|
||||
for (const direction of ["TD", "TB", "BT"] satisfies FlowchartDirection[]) {
|
||||
const verticalEdges = routedDiagram.edges.filter(
|
||||
(edge) => !handled.has(edge) && directionForEdge(edge) === direction,
|
||||
)
|
||||
if (verticalEdges.length === 0) continue
|
||||
const records = verticalForwardRecords(verticalEdges, bounds, direction)
|
||||
const records = verticalForwardRecords(verticalEdges, endpoints, direction)
|
||||
routeVerticalFanOut(records, direction, handled, routes)
|
||||
routeVerticalFanIn(records, direction, handled, routes)
|
||||
}
|
||||
|
||||
for (const edge of routedDiagram.edges) {
|
||||
if (handled.has(edge)) continue
|
||||
const from = bounds.get(edge.from)
|
||||
const to = bounds.get(edge.to)
|
||||
const from = endpoints.get(edge.from)
|
||||
const to = endpoints.get(edge.to)
|
||||
if (!from || !to) continue
|
||||
routes.push({ edge, points: edgePath(from, to, directionForEdge(edge), leftBoundary) })
|
||||
}
|
||||
for (let index = routes.length - 1; index >= 0; index--) {
|
||||
routes[index] = avoidNodeObstacles(routes[index]!, routes, bounds, subgraphBounds, index, routedDiagram)
|
||||
if (lockedRoutes?.has(routes[index]!.edge)) continue
|
||||
routes[index] = avoidNodeObstacles(routes[index]!, routes, bounds, endpoints, subgraphBounds, index, routedDiagram)
|
||||
}
|
||||
const subgraphs = diagram.subgraphs ?? []
|
||||
const subgraphById = new Map(subgraphs.map((subgraph) => [subgraph.id, subgraph]))
|
||||
const containers = (id: string) => {
|
||||
const ids = new Set<string>()
|
||||
let current = subgraphs.find((subgraph) => subgraph.nodeIds.includes(id))
|
||||
const directSubgraph = subgraphById.get(id)
|
||||
let current = directSubgraph
|
||||
? directSubgraph.parentId
|
||||
? subgraphById.get(directSubgraph.parentId)
|
||||
: undefined
|
||||
: subgraphs.find((subgraph) => subgraph.nodeIds.includes(id))
|
||||
while (current) {
|
||||
ids.add(current.id)
|
||||
current = current.parentId ? subgraphById.get(current.parentId) : undefined
|
||||
@@ -1319,6 +1428,7 @@ export function routeFlowchartEdges(
|
||||
return targets.size > 1 || sources.size > 1
|
||||
}
|
||||
return routes.reduce<FlowchartEdgeRoute[]>((resolved, route, index) => {
|
||||
if (lockedRoutes?.has(route.edge)) return [...resolved, route]
|
||||
const grouped = groupedLabelEdge(route.edge)
|
||||
return [
|
||||
...resolved,
|
||||
@@ -1326,6 +1436,7 @@ export function routeFlowchartEdges(
|
||||
route,
|
||||
[...resolved, ...routes.slice(index + 1)],
|
||||
bounds,
|
||||
endpoints,
|
||||
subgraphBounds,
|
||||
targetWidth !== undefined && subgraphs.length > 0 && grouped ? Math.max(1, targetWidth - 5) : targetWidth,
|
||||
subgraphs.length === 0 || grouped,
|
||||
@@ -1339,15 +1450,17 @@ export function avoidFlowchartFrameBorders(
|
||||
bounds: ReadonlyMap<string, FlowchartNodeBounds>,
|
||||
subgraphBounds: ReadonlyMap<string, FlowchartSubgraphBounds>,
|
||||
): void {
|
||||
const inside = (node: FlowchartNodeBounds, frame: FlowchartSubgraphBounds) =>
|
||||
const endpoints = new Map<string, FlowchartEndpointBounds>([...subgraphBounds, ...bounds])
|
||||
const inside = (node: FlowchartEndpointBounds, frame: FlowchartSubgraphBounds) =>
|
||||
node.id !== frame.id &&
|
||||
node.left >= frame.left &&
|
||||
node.top >= frame.top &&
|
||||
node.left + node.width <= frame.left + frame.width &&
|
||||
node.top + node.height <= frame.top + frame.height
|
||||
|
||||
for (const route of routes) {
|
||||
const source = bounds.get(route.edge.from)
|
||||
const target = bounds.get(route.edge.to)
|
||||
const source = endpoints.get(route.edge.from)
|
||||
const target = endpoints.get(route.edge.to)
|
||||
for (const frame of subgraphBounds.values()) {
|
||||
const inward = Boolean(source && target && inside(source, frame) && inside(target, frame))
|
||||
const right = frame.left + frame.width - 1
|
||||
@@ -1395,7 +1508,7 @@ export function avoidFlowchartFrameBorders(
|
||||
}
|
||||
}
|
||||
|
||||
function sideForOutsidePoint(bounds: FlowchartNodeBounds, sourcePoint: FlowchartPoint): DiagramSide {
|
||||
function sideForOutsidePoint(bounds: FlowchartEndpointBounds, sourcePoint: FlowchartPoint): DiagramSide {
|
||||
if (sourcePoint.x < bounds.left) return "left"
|
||||
if (sourcePoint.x >= bounds.left + bounds.width) return "right"
|
||||
if (sourcePoint.y < bounds.top) return "top"
|
||||
@@ -1416,7 +1529,7 @@ function connectorChar(side: DiagramSide): string {
|
||||
}
|
||||
|
||||
export function flowchartSourceConnector(
|
||||
from: FlowchartNodeBounds,
|
||||
from: FlowchartEndpointBounds,
|
||||
sourcePoint: FlowchartPoint,
|
||||
): { x: number; y: number; char: string } {
|
||||
const side = sideForOutsidePoint(from, sourcePoint)
|
||||
|
||||
@@ -51,6 +51,8 @@ export interface FlowchartSubgraphBounds extends DiagramBounds {
|
||||
labelSide: "top" | "bottom"
|
||||
}
|
||||
|
||||
export type FlowchartEndpointBounds = FlowchartNodeBounds | FlowchartSubgraphBounds
|
||||
|
||||
export type FlowchartPoint = DiagramPoint
|
||||
|
||||
export interface FlowchartEdgeRoute {
|
||||
|
||||
@@ -510,6 +510,29 @@ export function layoutFixtures(): readonly LayoutFixture[] {
|
||||
end`,
|
||||
curated: true,
|
||||
},
|
||||
{
|
||||
id: "flowchart/subgraph-pipeline/curated",
|
||||
kind: "flowchart" as const,
|
||||
family: "subgraph-pipeline",
|
||||
profile: "short" as const,
|
||||
source: `flowchart TD
|
||||
subgraph S1["1. provider.transform (seed)"]
|
||||
A1["Catalog model → copy catalog variants"]
|
||||
A2["Custom model → apply package/modelID/limit + seed default variants"]
|
||||
end
|
||||
subgraph S2["2. plugin model.transform"]
|
||||
B["Plugin can replace variants, clear to [], or ignore"]
|
||||
end
|
||||
subgraph S3["3. config model.transform (post)"]
|
||||
C1{"Config sets reasoning or variants?"}
|
||||
C1 -- yes --> C2["Apply explicit config"]
|
||||
C1 -- no --> C3{"Config changed package (or recreated removed model)?"}
|
||||
C3 -- yes --> C4["Respell / reseed variants"]
|
||||
C3 -- no --> C5["Keep current variants"]
|
||||
end
|
||||
S1 --> S2 --> S3`,
|
||||
curated: true,
|
||||
},
|
||||
...states,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -255,6 +255,7 @@ function auditFlowchart(fixture: LayoutFixture, viewport: (typeof auditViewports
|
||||
const size = grid.getTextSize({ trimTop: true, trimBottom: true })
|
||||
const routes = layout.routes.map((route) => ({ from: route.edge.from, to: route.edge.to, points: route.points }))
|
||||
const bodyIds = layout.diagram.nodes.map((node) => node.id)
|
||||
const endpointBounds = new Map<string, Bounds>([...layout.subgraphBounds, ...layout.bounds])
|
||||
|
||||
if (!Number.isFinite(size.width) || !Number.isFinite(size.height) || size.width <= 0 || size.height <= 0)
|
||||
violations.push("rendered grid has invalid dimensions")
|
||||
@@ -263,7 +264,7 @@ function auditFlowchart(fixture: LayoutFixture, viewport: (typeof auditViewports
|
||||
if (layout.routes.length !== layout.diagram.edges.filter((edge) => !edge.orderOnly).length)
|
||||
violations.push("rendered route count does not match visible edge count")
|
||||
validateBodies(layout.bounds, bodyIds, violations)
|
||||
validateRoutes(routes, layout.bounds, bodyIds, violations)
|
||||
validateRoutes(routes, endpointBounds, bodyIds, violations)
|
||||
|
||||
for (const node of layout.diagram.nodes)
|
||||
requireOutputLines(output, layout.bounds.get(node.id)?.lines ?? [], `node ${node.id}`, violations)
|
||||
|
||||
Reference in New Issue
Block a user