Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): note map staying blank for a note with nothing linked to it
Elian Dorane62d705f3543
diff --git a/apps/client/src/widgets/note_map/data.spec.ts b/apps/client/src/widgets/note_map/data.spec.tsindex a2b6664102..2d5c693d6e 100644--- a/apps/client/src/widgets/note_map/data.spec.ts+++ b/apps/client/src/widgets/note_map/data.spec.ts@@ -12,7 +12,9 @@ describe("dropUnlinkedNotes", () => {// Nothing linked at all: the root still stands on its own rather than leaving an empty map.expect(dropUnlinkedNotes(nodes, [], "root")).toEqual([ { id: "root" } ]);- // A root that isn't among the nodes (a search note removes itself) doesn't conjure one up.- expect(dropUnlinkedNotes(nodes, [], "missing")).toEqual([]);+ // A root that isn't among the nodes (a search note is not part of its own results) would+ // leave nothing to keep, so the map is left as it was rather than emptied.+ expect(dropUnlinkedNotes(nodes, [], "missing")).toEqual(nodes);+ expect(dropUnlinkedNotes(nodes, links, "missing")).toEqual([ { id: "source" }, { id: "target" } ]);});});diff --git a/apps/client/src/widgets/note_map/data.ts b/apps/client/src/widgets/note_map/data.tsindex 2b3367a544..3fd27d3565 100644--- a/apps/client/src/widgets/note_map/data.ts+++ b/apps/client/src/widgets/note_map/data.ts@@ -78,11 +78,15 @@ export async function loadNotesAndRelations(mapRootNoteId: string, excludeRelati/*** Keeps only the notes some relation touches, plus the map root itself — a note with no relations at* all is still worth showing as itself, rather than as an empty map.+ *+ * Filtering never empties the map: a search note is not part of its own results, so a search whose+ * results link to nothing would otherwise be left with no node to keep at all.*/export function dropUnlinkedNotes<T extends { id: string }>(nodes: T[], links: GroupedLink[], mapRootNoteId: string) {const linkedNoteIds = new Set(links.flatMap((link) => [ link.sourceNoteId, link.targetNoteId ]));+ const kept = nodes.filter((node) => node.id === mapRootNoteId || linkedNoteIds.has(node.id));- return nodes.filter((node) => node.id === mapRootNoteId || linkedNoteIds.has(node.id));+ return kept.length > 0 ? kept : nodes;}function calculateNodeSizes(resp: NoteMapPostResponse, mapType: MapType) {diff --git a/apps/client/src/widgets/note_map/rendering.ts b/apps/client/src/widgets/note_map/rendering.tsindex 6ce68f524d..ffdd47ce5c 100644--- a/apps/client/src/widgets/note_map/rendering.ts+++ b/apps/client/src/widgets/note_map/rendering.ts@@ -47,7 +47,10 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkOfunction paintNode(node: NoteMapNodeObject, color: string, ctx: CanvasRenderingContext2D) {const { x, y } = node;- if (!x || !y) {+ // A coordinate of exactly 0 is a position like any other, and a common one: d3 lays the+ // first node out at an angle of 0, so its y is 0 — and in a one-node map nothing ever moves+ // it off that axis, which used to leave the map permanently blank.+ if (x === undefined || y === undefined) {return;}const size = noteIdToSizeMap[node.id];@@ -93,7 +96,7 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkOreturn;}- if (source.x && source.y && target.x && target.y) {+ if (source.x !== undefined && source.y !== undefined && target.x !== undefined && target.y !== undefined) {const x = (source.x + target.x) / 2;const y = (source.y + target.y) / 2;ctx.save();@@ -160,7 +163,7 @@ export function setupRendering(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkOctx.fillStyle = color;ctx.beginPath();- if (node.x && node.y) {+ if (node.x !== undefined && node.y !== undefined) {ctx.arc(node.x, node.y, noteIdToSizeMap[node.id], 0, 2 * Math.PI, false);}ctx.fill();