Skip to content

Build your personal knowledge base with Trilium Notes

feat(relation_map): set relation names along the line like the note map

Elian Doran20355a26e2e3
diff --git a/apps/client/src/widgets/type_widgets/relation_map/Connections.tsx b/apps/client/src/widgets/type_widgets/relation_map/Connections.tsx
index 73136b9c3b..9b0159de51 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/Connections.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/Connections.tsx
@@ -19,7 +19,7 @@ export const PENDING_CONNECTION_ID = "pending";
/**
* Draws the relations between the boxes of a relation map: an SVG layer under the boxes for the
- * lines and arrowheads, and HTML labels over the lines.
+ * lines and arrowheads, and HTML labels along the lines.
*
* While a box is hovered, its relations take the note's `color` label (or
* `--relation-map-highlight-color`) and the other relations fade, as on the note map.
@@ -92,8 +92,13 @@ export default function Connections({ relations, inverseRelations, boxes, hovere
return labelsOf(relation, inverseRelations).map((text, index) => text ? (
<div
key={`${relation.attributeId}-${index}`}
- className={clsx("connection-label", className)}
- style={{ ...style, left: layout.labels[index].x, top: layout.labels[index].y }}
+ className={clsx("connection-label", layout.labels[index].side, className)}
+ style={{
+ ...style,
+ left: layout.labels[index].x,
+ top: layout.labels[index].y,
+ "--relation-map-label-angle": `${layout.labels[index].angle}deg`
+ }}
{...hoverProps(relation)}
>
{text}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
index 520ee4e423..7c929c79a4 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.css
@@ -62,19 +62,28 @@
.note-detail-relation-map .connection-label {
position: absolute;
z-index: 2;
- translate: -50% -50%;
+ transform-origin: 0 0;
+ transform: rotate(var(--relation-map-label-angle, 0deg)) translate(-50%, 0.1em);
white-space: nowrap;
- background-color: var(--accented-background-color);
- color: var(--main-text-color);
- opacity: 0.8;
- padding: 0.3em;
- border-radius: 0.5em;
- border: 1px solid #666;
+ font-size: 0.8em;
+ line-height: 1.2;
+ color: var(--muted-text-color);
+ text-shadow:
+ 0 0 2px var(--main-background-color),
+ 0 0 4px var(--main-background-color);
cursor: pointer;
- transition: opacity 0.2s ease;
+ transition: opacity 0.2s ease, color 0.2s ease;
+
+ &.above {
+ transform: rotate(var(--relation-map-label-angle, 0deg)) translate(-50%, calc(-100% - 0.1em));
+ }
+
+ &.end {
+ transform: rotate(var(--relation-map-label-angle, 0deg)) translate(0.4em, -50%);
+ }
&.hovered {
- background-color: var(--more-accented-background-color);
+ color: var(--main-text-color);
}
}
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 1aa155b401..5c279846aa 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
@@ -12,12 +12,18 @@ describe("relation map geometry", () => {
expect(layout?.path).toBe("M 103 20 Q 200 20 297 20");
expect(layout?.arrows).toHaveLength(1);
expect(arrowTip(layout?.arrows[0])).toEqual({ x: 297, y: 20 });
- expect(layout?.labels).toEqual([ { x: 200, y: 20 } ]);
+ expect(layout?.labels).toEqual([ { x: 200, y: 20, angle: 0, side: "below" } ]);
const both = layoutLine(boxA, boxB, { arrowAtSource: true });
expect(both?.arrows.map(arrowTip)).toEqual([ { x: 297, y: 20 }, { x: 103, y: 20 } ]);
});
+ 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);
+ expect(upward?.side).toBe("below");
+ });
+
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: 60, y: 30 })).toBeNull();
@@ -33,6 +39,8 @@ describe("relation map geometry", () => {
const offset = (id: string) => (layouts.get(id)?.labels[0].y ?? 20) - 20;
expect(offset("ab") * offset("ba")).toBeLessThan(0);
expect(Math.abs(offset("ab") - offset("ba"))).toBeGreaterThan(20);
+ expect([ layouts.get("ab")?.labels[0].side, layouts.get("ba")?.labels[0].side ].sort()).toEqual([ "above", "below" ]);
+ expect(layouts.get("ab")?.labels[0].side).toBe(offset("ab") < 0 ? "above" : "below");
expect(layouts.get("single")?.path).toMatch(/^M [\d.]+ [\d.]+ Q 200 170 /);
});
@@ -46,6 +54,7 @@ describe("relation map geometry", () => {
expect(layouts.get("second")?.path).toBe("M 72 0 A 28 28 0 1 1 100 28");
expect(arrowTip(layouts.get("first")?.arrows[0])).toEqual({ x: 100, y: 18 });
const label = layouts.get("first")?.labels[0];
+ expect([ label?.angle, label?.side ]).toEqual([ 0, "end" ]);
expect([ label?.x, label?.y ].map((value) => Math.round(value ?? 0))).toEqual([ 113, -13 ]);
});
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 18a2a37643..92c00dd1ab 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/geometry.ts
+++ b/apps/client/src/widgets/type_widgets/relation_map/geometry.ts
@@ -22,13 +22,23 @@ export interface ConnectionShape {
labelAt: number[];
}
+/**
+ * Where a label goes: at `x`, `y` on the line, turned by `angle` degrees to run along it and kept
+ * upright, as on the note map. `side` places the text below or above the line, or after the point
+ * (`end`), all in the turned frame.
+ */
+export interface LabelPlacement extends Point {
+ angle: number;
+ side: "below" | "above" | "end";
+}
+
export interface ConnectionLayout {
/** SVG path data of the line. */
path: string;
/** Outlines of the arrowheads, as SVG `points`. */
arrows: string[];
- /** Centers of the labels, in the order of `ConnectionShape.labelAt`. */
- labels: Point[];
+ /** The labels, in the order of `ConnectionShape.labelAt`. */
+ labels: LabelPlacement[];
}
/** Length of an arrowhead. The rounded stroke of `.relation-map-arrow` adds about half a pixel on every side. */
@@ -100,13 +110,14 @@ export function layoutLine(source: Box, target: Box | Point, { bend = 0, arrowAt
return {
path: `M ${round(start.x)} ${round(start.y)} Q ${round(control.x)} ${round(control.y)} ${round(end.x)} ${round(end.y)}`,
arrows: arrowsAt([ [ control, end ], ...(arrowAtSource ? [ [ control, start ] as const ] : []) ]),
- labels: labelAt.map((t) => quadraticAt(start, control, end, t))
+ labels: labelAt.map((t) => placeLabel(start, control, end, t, bend))
};
}
/**
* Lays out a relation from a box to itself: an arc around the box's top-right corner, from its top
* edge to its right edge. `index` makes each further loop of the box wider than the one before.
+ * Its labels are level and start at the arc, outside it.
*/
function layoutLoop(box: Box, index: number, { arrowAtSource, labelAt }: ConnectionShape): ConnectionLayout {
const radius = LOOP_RADIUS + index * LOOP_SPACING;
@@ -114,9 +125,9 @@ function layoutLoop(box: Box, index: number, { arrowAtSource, labelAt }: Connect
const start = { x: corner.x - radius, y: corner.y };
const end = { x: corner.x, y: corner.y + radius };
// The arc runs three quarters of a circle clockwise, from the left of the corner to below it.
- const pointAt = (t: number) => {
+ const pointAt = (t: number): LabelPlacement => {
const angle = Math.PI + t * 1.5 * Math.PI;
- return { x: corner.x + radius * Math.cos(angle), y: corner.y + radius * Math.sin(angle) };
+ return { x: corner.x + radius * Math.cos(angle), y: corner.y + radius * Math.sin(angle), angle: 0, side: "end" };
};
return {
@@ -168,6 +179,32 @@ function arrowsAt(directions: (readonly [ Point, Point ])[]) {
.map((outline) => outline.map(([ x, y ]) => `${round(x)},${round(y)}`).join(" "));
}
+/**
+ * Places a label at `t` along the curve, turned along it and kept upright. On a bowed line the text
+ * goes on the outer side of the bow, so that the labels of relations between the same boxes move
+ * apart; on a straight line it goes below.
+ */
+function placeLabel(start: Point, control: Point, end: Point, t: number, bend: number): LabelPlacement {
+ const point = quadraticAt(start, control, end, t);
+ const tangentX = (1 - t) * (control.x - start.x) + t * (end.x - control.x);
+ const tangentY = (1 - t) * (control.y - start.y) + t * (end.y - control.y);
+ let angle = Math.atan2(tangentY, tangentX);
+ if (angle > Math.PI / 2) angle -= Math.PI;
+ if (angle < -Math.PI / 2) angle += Math.PI;
+
+ let side: LabelPlacement["side"] = "below";
+ if (bend) {
+ // "Below" in the turned frame, against the direction the middle of the curve is pushed to.
+ const belowX = -Math.sin(angle);
+ const belowY = Math.cos(angle);
+ const pushX = control.x - (start.x + end.x) / 2;
+ const pushY = control.y - (start.y + end.y) / 2;
+ side = belowX * pushX + belowY * pushY >= 0 ? "below" : "above";
+ }
+
+ return { ...point, angle: round(angle * 180 / Math.PI), side };
+}
+
function quadraticAt(start: Point, control: Point, end: Point, t: number): Point {
const u = 1 - t;
return {