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): route forward diagonal edges correctly

tianma-ife2b691c56c65
diff --git a/packages/shared/src/diagram-flowchart-style.test.mjs b/packages/shared/src/diagram-flowchart-style.test.mjs
index 7c6ca81e..a9972e74 100644
--- a/packages/shared/src/diagram-flowchart-style.test.mjs
+++ b/packages/shared/src/diagram-flowchart-style.test.mjs
@@ -123,6 +123,12 @@ 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", () => {
@@ -141,4 +147,3 @@ describe("flowchart readable viewport", () => {
});
});
-
diff --git a/packages/shared/src/diagram-flowchart-style.ts b/packages/shared/src/diagram-flowchart-style.ts
index 37ba1dba..26657128 100644
--- a/packages/shared/src/diagram-flowchart-style.ts
+++ b/packages/shared/src/diagram-flowchart-style.ts
@@ -20,8 +20,16 @@ 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;
- if (dy < -source.height) return { source: "left", target: "left" };
- if (dx < -source.width) return { source: "top", target: "top" };
+ // 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 (Math.abs(dy) >= Math.abs(dx)) {
return dy >= 0
? { source: "bottom", target: "top" }