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): approach architecture nodes from port direction

tianma-if940c4d8cdeb3
diff --git a/apps/web/src/components/DiagramEditorPane.tsx b/apps/web/src/components/DiagramEditorPane.tsx
index bee1b39c..daad8885 100644
--- a/apps/web/src/components/DiagramEditorPane.tsx
+++ b/apps/web/src/components/DiagramEditorPane.tsx
@@ -1388,7 +1388,7 @@ const applyOrthogonalEdgePorts = (graph: Graph, kind: DiagramDocument["kind"]) =
edge.setSource({ cell: source.id, port: ports.source });
edge.setTarget({ cell: target.id, port: ports.target });
edge.setRouter(kind === "architecture"
- ? architectureEdgeRouter(source.id, target.id, boundaryIds)
+ ? architectureEdgeRouter(source.id, target.id, boundaryIds, ports)
: flowchartEdgeIsStraight(sourceBox, targetBox) ? { name: "normal" } : FLOWCHART_EDGE_ROUTER);
}
};
diff --git a/packages/shared/src/diagram-architecture-style.test.mjs b/packages/shared/src/diagram-architecture-style.test.mjs
index 8d761fd6..53af8023 100644
--- a/packages/shared/src/diagram-architecture-style.test.mjs
+++ b/packages/shared/src/diagram-architecture-style.test.mjs
@@ -49,11 +49,19 @@ describe("architecture semantic paint", () => {
expect(architectureEdgePorts(source, target)).toEqual({ source: "left", target: "right" });
});
+ test("approaches a nearby column from below when the target is far above", () => {
+ const source = { x: 68, y: 1264, width: 156, height: 60 };
+ const target = { x: 246, y: 908, width: 156, height: 64 };
+ expect(architectureEdgePorts(source, target)).toEqual({ source: "right", target: "bottom" });
+ });
+
test("keeps architecture endpoints and group containers out of obstacle routing", () => {
- const router = architectureEdgeRouter("from", "to", ["group", "group"]);
+ const router = architectureEdgeRouter("from", "to", ["group", "group"], { source: "left", target: "right" });
expect(router.name).toBe("manhattan");
expect(router.args.excludeNodes).toEqual(["from", "to", "group"]);
expect(router.args.padding).toBeGreaterThan(0);
+ expect(router.args.startDirections).toEqual(["left"]);
+ expect(router.args.endDirections).toEqual(["right"]);
});
test("keeps every component fill distinct in light and dark", () => {
diff --git a/packages/shared/src/diagram-architecture-style.ts b/packages/shared/src/diagram-architecture-style.ts
index 5bb51919..c6f36ea0 100644
--- a/packages/shared/src/diagram-architecture-style.ts
+++ b/packages/shared/src/diagram-architecture-style.ts
@@ -22,20 +22,24 @@ 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 };
+const NARROW_COLUMN_GAP = 32;
+const CLEAR_VERTICAL_GAP = 64;
export const architectureEdgeRouter = (
sourceId: string,
targetId: string,
boundaryIds: string[],
+ ports?: { source: ArchitectureEdgePortName; target: ArchitectureEdgePortName },
) => ({
name: "manhattan" as const,
args: {
- padding: 12,
+ padding: 8,
step: 8,
maxLoopCount: 5000,
// Boundaries are visual containers; components and endpoints are the obstacles.
// Per-edge exclusions also keep X6's shared obstacle map specific to this edge.
excludeNodes: [...new Set([sourceId, targetId, ...boundaryIds])],
+ ...(ports ? { startDirections: [ports.source], endDirections: [ports.target] } : {}),
},
});
@@ -45,8 +49,20 @@ export const architectureEdgePorts = (
): { 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: "left", target: "right" };
+ const verticalGapAbove = source.y - (target.y + target.height);
+ const verticalGapBelow = target.y - (source.y + source.height);
+ if (target.x >= sourceRight) {
+ // A narrow side corridor cannot accommodate a horizontal approach when
+ // the target is several rows away; enter from its nearer vertical side.
+ if (target.x - sourceRight < NARROW_COLUMN_GAP && verticalGapAbove > CLEAR_VERTICAL_GAP) return { source: "right", target: "bottom" };
+ if (target.x - sourceRight < NARROW_COLUMN_GAP && verticalGapBelow > CLEAR_VERTICAL_GAP) return { source: "right", target: "top" };
+ return { source: "right", target: "left" };
+ }
+ if (targetRight <= source.x) {
+ if (source.x - targetRight < NARROW_COLUMN_GAP && verticalGapAbove > CLEAR_VERTICAL_GAP) return { source: "left", target: "bottom" };
+ if (source.x - targetRight < NARROW_COLUMN_GAP && verticalGapBelow > CLEAR_VERTICAL_GAP) return { source: "left", target: "top" };
+ return { source: "left", target: "right" };
+ }
const sourceCenterY = source.y + source.height / 2;
const targetCenterY = target.y + target.height / 2;
diff --git a/packages/shared/src/diagram-view.ts b/packages/shared/src/diagram-view.ts
index 39332012..d9c03e88 100644
--- a/packages/shared/src/diagram-view.ts
+++ b/packages/shared/src/diagram-view.ts
@@ -200,7 +200,7 @@ export const diagramDocumentToX6Cells = (
? { cell: edge.target, port: orthogonalPorts.target }
: { cell: edge.target },
router: document.kind === "architecture"
- ? architectureEdgeRouter(edge.source, edge.target, architectureBoundaryIds)
+ ? architectureEdgeRouter(edge.source, edge.target, architectureBoundaryIds, orthogonalPorts ?? undefined)
: document.kind === "flowchart"
? (orthogonalStraight ? { name: "normal" } : FLOWCHART_EDGE_ROUTER)
: undefined,
diff --git a/packages/shared/src/diagram.test.mjs b/packages/shared/src/diagram.test.mjs
index 49949ca8..8f1fab01 100644
--- a/packages/shared/src/diagram.test.mjs
+++ b/packages/shared/src/diagram.test.mjs
@@ -207,6 +207,8 @@ describe("diagram document", () => {
expect(edge.router.name).toBe("manhattan");
expect(edge.router.args.excludeNodes).toContain("system");
expect(edge.router.args.excludeNodes).not.toContain("database");
+ expect(edge.router.args.startDirections).toEqual([edge.source.port]);
+ expect(edge.router.args.endDirections).toEqual([edge.target.port]);
});
test("projects architecture edge labels below component typography", () => {