Skip to content

Build your personal knowledge base with Trilium Notes

fix(relation_map): keep relations between nearby boxes of different sizes

Elian Doran7379eb081d5b
diff --git a/apps/client/src/widgets/type_widgets/relation_map/Connections.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/Connections.spec.tsx
index 081a1cbc72..029b960a2d 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/Connections.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/Connections.spec.tsx
@@ -105,7 +105,7 @@ describe("relation map Connections", () => {
it("draws a relation being created toward the pointer", () => {
mount({ sourceNoteId: "a", pointer: { x: 250, y: 20 } });
- expect(connection("pending")?.querySelector("path")?.getAttribute("d")).toBe("M 103 20 Q 150 20 250 20");
+ expect(connection("pending")?.querySelector("path")?.getAttribute("d")).toBe("M 103 20 Q 176.5 20 250 20");
});
});
diff --git a/apps/client/src/widgets/type_widgets/relation_map/geometry.spec.ts b/apps/client/src/widgets/type_widgets/relation_map/geometry.spec.ts
index 5c279846aa..89270e1cee 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/geometry.spec.ts
+++ b/apps/client/src/widgets/type_widgets/relation_map/geometry.spec.ts
@@ -18,6 +18,19 @@ describe("relation map geometry", () => {
expect(both?.arrows.map(arrowTip)).toEqual([ { x: 297, y: 20 }, { x: 103, y: 20 } ]);
});
+ it("runs a line between nearby boxes of different sizes, across the gap between them", () => {
+ const narrow: Box = { x: 0, y: 0, width: 150, height: 40 };
+ const wide: Box = { x: 200, y: 0, width: 300, height: 40 };
+ const tall: Box = { x: 200, y: -80, width: 300, height: 200 };
+
+ for (const target of [ wide, tall ]) {
+ const layout = layoutLine(narrow, target);
+ expect(layout?.path).toBe("M 153 20 Q 175 20 197 20");
+ expect(arrowTip(layout?.arrows[0])).toEqual({ x: 197, y: 20 });
+ }
+ expect(layoutLine(wide, narrow, { bend: 15 })).not.toBeNull();
+ });
+
it("turns a label along its line and keeps it upright", () => {
const upward = layoutLine(boxB, { ...boxA, y: 300 }, { labelAt: [ 0.5 ] })?.labels[0];
expect(upward?.angle).toBe(-45);
@@ -25,7 +38,7 @@ describe("relation map geometry", () => {
});
it("draws a relation being created up to the pointer, and nothing from inside its own box", () => {
- expect(layoutLine(boxA, { x: 250, y: 20 })?.path).toBe("M 103 20 Q 150 20 250 20");
+ expect(layoutLine(boxA, { x: 250, y: 20 })?.path).toBe("M 103 20 Q 176.5 20 250 20");
expect(layoutLine(boxA, { x: 60, y: 30 })).toBeNull();
});
diff --git a/apps/client/src/widgets/type_widgets/relation_map/geometry.ts b/apps/client/src/widgets/type_widgets/relation_map/geometry.ts
index 92c00dd1ab..0db3395f3b 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/geometry.ts
+++ b/apps/client/src/widgets/type_widgets/relation_map/geometry.ts
@@ -98,14 +98,19 @@ export function layoutLine(source: Box, target: Box | Point, { bend = 0, arrowAt
const distance = Math.hypot(dx, dy);
if (!distance) return null;
+ // The ends of the straight line, which the control point is measured from, so that it falls
+ // between the boxes even when one box is much larger than the other.
+ const straightStart = exitPoint(source, to);
+ const straightEnd = "width" in target ? exitPoint(target, from) : target;
+ if (!straightStart || !straightEnd) return null;
+
// The curve passes halfway between the middle of the straight line and its control point.
const control = {
- x: (from.x + to.x) / 2 + (dy / distance) * bend * 2,
- y: (from.y + to.y) / 2 - (dx / distance) * bend * 2
+ x: (straightStart.x + straightEnd.x) / 2 + (dy / distance) * bend * 2,
+ y: (straightStart.y + straightEnd.y) / 2 - (dx / distance) * bend * 2
};
- const start = exitPoint(source, control);
- const end = "width" in target ? exitPoint(target, control) : target;
- if (!start || !end) return null;
+ const start = exitPoint(source, control) ?? straightStart;
+ const end = "width" in target ? exitPoint(target, control) ?? straightEnd : target;
return {
path: `M ${round(start.x)} ${round(start.y)} Q ${round(control.x)} ${round(control.y)} ${round(end.x)} ${round(end.y)}`,