Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(diagram): isolate architecture edge routing

tianma-if554c5d12edb0
diff --git a/apps/web/src/components/DiagramEditorPane.test.mjs b/apps/web/src/components/DiagramEditorPane.test.mjs
index 8f846c1c..b48e5be7 100644
--- a/apps/web/src/components/DiagramEditorPane.test.mjs
+++ b/apps/web/src/components/DiagramEditorPane.test.mjs
@@ -187,7 +187,7 @@ describe("diagram editor canvas surface", () => {
expect(source).toContain('{ fill: "none" }');
expect(source).toContain('if (kind !== "mind-map") edge.attr("line/fill", "none")');
expect(source).toContain("FLOWCHART_EDGE_ROUTER");
- expect(source).toContain("applyFlowchartEdgePorts(graph)");
+ expect(source).toContain("applyOrthogonalEdgePorts(graph, document.kind)");
expect(source).toContain("flowchartEdgeIsStraight");
expect(source).toContain('showTheme={document.kind !== "architecture"}');
expect(source).toContain('themeCatalog={document.kind === "flowchart" ? "flowchart" : "mind-map"}');
diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsx
index 4f0c6eb8..a102a24b 100644
--- a/apps/web/src/components/DiagramEditorPane.tsx
+++ b/apps/web/src/components/DiagramEditorPane.tsx
@@ -111,6 +111,7 @@ import {
ARCHITECTURE_NODE_FONT_WEIGHT,
ARCHITECTURE_NODE_LINE_HEIGHT,
ARCHITECTURE_SHAPE_RESOURCE,
+ architectureEdgePorts,
architectureEdgeVisual,
architectureIconOffset,
architectureNodeVisual,
@@ -1343,14 +1344,16 @@ const edgeMetadata = (
};
};
-const applyFlowchartEdgePorts = (graph: Graph) => {
+const applyOrthogonalEdgePorts = (graph: Graph, kind: DiagramDocument["kind"]) => {
for (const edge of graph.getEdges()) {
const source = edge.getSourceNode();
const target = edge.getTargetNode();
if (!source || !target) continue;
const sourceBox = { ...source.getPosition(), ...source.getSize() };
const targetBox = { ...target.getPosition(), ...target.getSize() };
- const ports = flowchartEdgePorts(sourceBox, targetBox);
+ const ports = kind === "architecture"
+ ? architectureEdgePorts(sourceBox, targetBox)
+ : flowchartEdgePorts(sourceBox, targetBox);
edge.setSource({ cell: source.id, port: ports.source });
edge.setTarget({ cell: target.id, port: ports.target });
edge.setRouter(flowchartEdgeIsStraight(sourceBox, targetBox) ? { name: "normal" } : FLOWCHART_EDGE_ROUTER);
@@ -1965,7 +1968,7 @@ export const DiagramEditorPane = ({
}
}
graph.addEdges(document.edges.map((edge) => edgeMetadata(edge, document.kind, documentTheme, appearance, documentStructure)));
- if (usesOrthogonalDiagramEdges(document.kind)) applyFlowchartEdgePorts(graph);
+ if (usesOrthogonalDiagramEdges(document.kind)) applyOrthogonalEdgePorts(graph, document.kind);
applyGraphPalette(graph, documentTheme, document.kind, appearance, documentStructure);
graph.on("scale", () => setZoomPercent(Math.round(graph.scale().sx * 100)));
graph.cleanHistory();
@@ -2157,7 +2160,7 @@ export const DiagramEditorPane = ({
target: { cell: currentCell.id, ...(currentPort ? { port: currentPort } : {}) },
});
graph.stopBatch("connect");
- if (usesOrthogonalDiagramEdges(document.kind)) applyFlowchartEdgePorts(graph);
+ if (usesOrthogonalDiagramEdges(document.kind)) applyOrthogonalEdgePorts(graph, document.kind);
return;
}
if (!currentPoint || !containerRef.current) {
@@ -2698,7 +2701,7 @@ export const DiagramEditorPane = ({
target: { cell: id, ...(oppositeFlowPort(pending.sourcePort) ? { port: oppositeFlowPort(pending.sourcePort) } : {}) },
});
graph.stopBatch("quick-create");
- applyFlowchartEdgePorts(graph);
+ applyOrthogonalEdgePorts(graph, document.kind);
settleScroller();
graph.cleanSelection();
graph.select(node);
@@ -2777,7 +2780,7 @@ export const DiagramEditorPane = ({
node.resize(geometry.width, geometry.height);
}
}
- if (usesOrthogonalDiagramEdges(document.kind)) applyFlowchartEdgePorts(graph);
+ if (usesOrthogonalDiagramEdges(document.kind)) applyOrthogonalEdgePorts(graph, document.kind);
if (document.kind === "mind-map") applyMindMapHierarchy(graph, themeRef.current, appearanceRef.current, structureRef.current);
graph.stopBatch("layout");
ensureDiagramPaperContainsNodes(graph);
diff --git a/packages/shared/src/diagram-architecture-style.test.mjs b/packages/shared/src/diagram-architecture-style.test.mjs
index a49cd927..9da66d60 100644
--- a/packages/shared/src/diagram-architecture-style.test.mjs
+++ b/packages/shared/src/diagram-architecture-style.test.mjs
@@ -4,6 +4,7 @@ import { ARCHITECTURE_RESOURCE_ICON_ELEMENTS } from "./diagram-architecture-icon
import {
ARCHITECTURE_COMPONENT_SHAPES,
ARCHITECTURE_SURFACES,
+ architectureEdgePorts,
architectureEdgeVisual,
architectureIconElements,
architectureMermaidClassDefs,
@@ -28,6 +29,12 @@ const contrast = (foreground, background) => {
};
describe("architecture semantic paint", () => {
+ test("routes forward diagonal edges between horizontal ports", () => {
+ const lowerLeft = { x: 194, y: 711, width: 170, height: 64 };
+ const upperRight = { x: 446, y: 658, width: 170, height: 68 };
+ expect(architectureEdgePorts(lowerLeft, upperRight)).toEqual({ source: "right", target: "left" });
+ });
+
test("keeps every component fill distinct in light and dark", () => {
for (const appearance of ["light", "dark"]) {
const fills = ARCHITECTURE_COMPONENT_SHAPES.map((shape) => resolveArchitectureSurface(appearance).nodes[shape].fill);
diff --git a/packages/shared/src/diagram-architecture-style.ts b/packages/shared/src/diagram-architecture-style.ts
index 3dbecfd0..98a04040 100644
--- a/packages/shared/src/diagram-architecture-style.ts
+++ b/packages/shared/src/diagram-architecture-style.ts
@@ -17,6 +17,24 @@ export const ARCHITECTURE_ICON_FRAME = 34;
export const ARCHITECTURE_ICON_INSET = 10;
export type ArchitectureAppearance = "light" | "dark";
+export type ArchitectureEdgePortName = "top" | "right" | "bottom" | "left";
+export type ArchitectureEdgeBox = { x: number; y: number; width: number; height: number };
+
+export const architectureEdgePorts = (
+ source: ArchitectureEdgeBox,
+ target: ArchitectureEdgeBox,
+): { source: ArchitectureEdgePortName; target: ArchitectureEdgePortName } => {
+ const sourceRight = source.x + source.width;
+ const targetRight = target.x + target.width;
+ if (target.x >= sourceRight) return { source: "right", target: "left" };
+ if (targetRight <= source.x) return { source: "top", target: "top" };
+
+ const sourceCenterY = source.y + source.height / 2;
+ const targetCenterY = target.y + target.height / 2;
+ return targetCenterY >= sourceCenterY
+ ? { source: "bottom", target: "top" }
+ : { source: "top", target: "bottom" };
+};
export type ArchitectureComponentShape = Exclude<
DiagramNodeShape,
diff --git a/packages/shared/src/diagram-flowchart-style.test.mjs b/packages/shared/src/diagram-flowchart-style.test.mjs
index a9972e74..0742931d 100644
--- a/packages/shared/src/diagram-flowchart-style.test.mjs
+++ b/packages/shared/src/diagram-flowchart-style.test.mjs
@@ -123,12 +123,6 @@ describe("flowchart edge geometry", () => {
expect(flowchartEdgePorts(loop, above)).toEqual({ source: "left", target: "left" });
expect(flowchartEdgeIsStraight(loop, above)).toBe(false);
});
-
- test("keeps a predominantly forward diagonal edge out of its source node", () => {
- const lowerLeft = { x: 80, y: 280, width: 176, height: 64 };
- const upperRight = { x: 420, y: 120, width: 176, height: 64 };
- expect(flowchartEdgePorts(lowerLeft, upperRight)).toEqual({ source: "right", target: "left" });
- });
});
describe("flowchart readable viewport", () => {
@@ -146,4 +140,3 @@ describe("flowchart readable viewport", () => {
expect(DIAGRAM_READABLE_MIN_SCALE).toBe(0.85);
});
});
-
diff --git a/packages/shared/src/diagram-flowchart-style.ts b/packages/shared/src/diagram-flowchart-style.ts
index 26657128..37ba1dba 100644
--- a/packages/shared/src/diagram-flowchart-style.ts
+++ b/packages/shared/src/diagram-flowchart-style.ts
@@ -20,16 +20,8 @@ export const flowchartEdgePorts = (
const targetCenter = { x: target.x + target.width / 2, y: target.y + target.height / 2 };
const dx = targetCenter.x - sourceCenter.x;
const dy = targetCenter.y - sourceCenter.y;
- // Treat an edge as a return path only when the backwards displacement is
- // also its dominant axis. A left-to-right architecture edge may legitimately
- // point a little upward; routing that edge through two left ports makes the
- // path double back through its source node.
- if (dy < -source.height && Math.abs(dy) >= Math.abs(dx)) {
- return { source: "left", target: "left" };
- }
- if (dx < -source.width && Math.abs(dx) >= Math.abs(dy)) {
- return { source: "top", target: "top" };
- }
+ if (dy < -source.height) return { source: "left", target: "left" };
+ if (dx < -source.width) return { source: "top", target: "top" };
if (Math.abs(dy) >= Math.abs(dx)) {
return dy >= 0
? { source: "bottom", target: "top" }
diff --git a/packages/shared/src/diagram-view.ts b/packages/shared/src/diagram-view.ts
index a9da047d..a92ad8ef 100644
--- a/packages/shared/src/diagram-view.ts
+++ b/packages/shared/src/diagram-view.ts
@@ -1,6 +1,7 @@
import { flowchartNodePresentation } from "./diagram-node-presentation";
import {
ARCHITECTURE_LABEL_FONT,
+ architectureEdgePorts,
architectureEdgeVisual,
architectureNodeVisual,
resolveArchitectureSurface,
@@ -166,8 +167,12 @@ export const diagramDocumentToX6Cells = (
?? mindEdge?.stroke
?? flowchartSurface?.edge
?? palette.flowEdge;
- const orthogonalPorts = (document.kind === "flowchart" || document.kind === "architecture") && sourceNode && targetNode
- ? flowchartEdgePorts(sourceNode, targetNode)
+ const orthogonalPorts = sourceNode && targetNode
+ ? document.kind === "architecture"
+ ? architectureEdgePorts(sourceNode, targetNode)
+ : document.kind === "flowchart"
+ ? flowchartEdgePorts(sourceNode, targetNode)
+ : null
: null;
const orthogonalStraight = Boolean(orthogonalPorts && sourceNode && targetNode && flowchartEdgeIsStraight(sourceNode, targetNode));
return {
diff --git a/packages/shared/src/diagram.test.mjs b/packages/shared/src/diagram.test.mjs
index e337a87c..cc3ff1fb 100644
--- a/packages/shared/src/diagram.test.mjs
+++ b/packages/shared/src/diagram.test.mjs
@@ -164,6 +164,19 @@ describe("diagram document", () => {
expect(fallback).toContain("classDef archDatabase");
});
+ test("projects forward diagonal architecture edges through horizontal ports", () => {
+ const document = createDefaultDiagramDocument("architecture");
+ const edge = document.edges[0];
+ const source = document.nodes.find((node) => node.id === edge.source);
+ const target = document.nodes.find((node) => node.id === edge.target);
+ Object.assign(source, { x: 194, y: 711, width: 170, height: 64 });
+ Object.assign(target, { x: 446, y: 658, width: 170, height: 68 });
+
+ const projectedEdge = diagramDocumentToX6Cells(document, "light").edges[0];
+ expect(projectedEdge.source.port).toBe("right");
+ expect(projectedEdge.target.port).toBe("left");
+ });
+
test("keeps legacy architecture nodes valid and projects resource-specific icons", () => {
const legacy = createDefaultDiagramDocument("architecture");
expect(legacy.nodes.every((node) => node.resourceIcon === undefined)).toBe(true);