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
feat(client): fit the sidebar's map to the box it is drawn in
Elian Doranb6dc587cb1e7
diff --git a/apps/client/src/widgets/note_map/rendering.ts b/apps/client/src/widgets/note_map/rendering.tsindex b9e93901f2..aa5bd25539 100644--- a/apps/client/src/widgets/note_map/rendering.ts+++ b/apps/client/src/widgets/note_map/rendering.ts@@ -1,7 +1,7 @@import type ForceGraph from "force-graph";import { NoteMapLinkObject, NoteMapNodeObject, NotesAndRelationsData } from "./data";import { LinkObject, NodeObject } from "force-graph";-import { generateColorFromString, isRootedAtCurrentNote, MapType, NoteMapWidgetMode } from "./utils";+import { generateColorFromString, getFitPadding, isRootedAtCurrentNote, MapType, NoteMapWidgetMode } from "./utils";import { escapeHtml } from "../../services/utils";import FNote from "../../entities/fnote";@@ -223,7 +223,7 @@ function setupFraming(graph: ForceGraph<NoteMapNodeObject, NoteMapLinkObject>, cconst nodeFilter = framedNoteIds.size > 1? (node: NoteMapNodeObject) => framedNoteIds.has(node.id): undefined;- const padding = isRootedAtCurrentNote(widgetMode) ? 50 : 30;+ const padding = getFitPadding(widgetMode, framedNoteIds.size);// Panning or zooming hands the view over to the user for good — re-fitting under their fingers// would make the map impossible to explore while it is still settling.diff --git a/apps/client/src/widgets/note_map/utils.spec.ts b/apps/client/src/widgets/note_map/utils.spec.tsnew file mode 100644index 0000000000..bd3ae50dd7--- /dev/null+++ b/apps/client/src/widgets/note_map/utils.spec.ts@@ -0,0 +1,34 @@+import { describe, expect, it } from "vitest";++import { getFitPadding, toMapType } from "./utils";++describe("toMapType", () => {+ it("only reads the tree map out of the label, the link map standing for anything else", () => {+ expect(toMapType("tree")).toBe("tree");+ expect(toMapType("link")).toBe("link");+ expect(toMapType("nonsense")).toBe("link");+ expect(toMapType(null)).toBe("link");+ expect(toMapType(undefined)).toBe("link");+ });+});++describe("getFitPadding", () => {+ it("crops the sidebar's map the more of it there is to crop, and leaves the others alone", () => {+ // A handful of notes is mostly its labels, which the fit does not measure: given a margin.+ expect(getFitPadding("sidebar", 1)).toBe(20);+ expect(getFitPadding("sidebar", 5)).toBe(20);++ // A crowd is fitted past the edges of its box, the outermost notes cropped.+ expect(getFitPadding("sidebar", 25)).toBe(-25);+ expect(getFitPadding("sidebar", 500)).toBe(-25);++ // Between the two, evenly.+ expect(getFitPadding("sidebar", 15)).toBe(-2.5);++ // The maps with room of their own keep their fixed margins whatever is in them.+ expect(getFitPadding("ribbon", 1)).toBe(50);+ expect(getFitPadding("ribbon", 500)).toBe(50);+ expect(getFitPadding("type", 500)).toBe(30);+ expect(getFitPadding("hoisted", 500)).toBe(30);+ });+});diff --git a/apps/client/src/widgets/note_map/utils.ts b/apps/client/src/widgets/note_map/utils.tsindex a5f6ce5de9..a624b5f1fe 100644--- a/apps/client/src/widgets/note_map/utils.ts+++ b/apps/client/src/widgets/note_map/utils.ts@@ -11,6 +11,44 @@ export function toMapType(labelValue: string | null | undefined): MapType {return labelValue === "tree" ? "tree" : "link";}+/** How much of the map's box is kept clear around the graph when the view is fitted to it. */+const FIT_PADDING: Record<NoteMapWidgetMode, number> = {+ // The sidebar's is not a fixed one — see getFitPadding.+ sidebar: 0,+ ribbon: 50,+ hoisted: 30,+ type: 30+};++/** What the sidebar's map is fitted with, between a map of a handful of notes and one of a crowd. */+const SIDEBAR_FIT_PADDING = { sparse: 20, dense: -25 };++/** The counts of framed notes those two are reached at, everything between being shared out evenly. */+const SIDEBAR_FIT_NOTES = { sparse: 5, dense: 25 };++/**+ * How much of the box to keep clear around the graph, given how many notes are being framed in it.+ *+ * Fixed everywhere but the sidebar, whose box is small enough that a graph fitted with room to spare+ * is a thumbnail of itself. A map of a crowd is fitted past the edges there (a negative padding): the+ * outermost notes are cropped rather than everything being shrunk to clear them, which is what the+ * room is better spent on when there is plenty left in the middle to look at.+ *+ * A map of a handful of notes has nothing to spare, and is mostly its labels — which the fit knows+ * nothing about, being measured on the notes alone — so those are given a margin instead, and the+ * maps between the two are shared out evenly.+ */+export function getFitPadding(widgetMode: NoteMapWidgetMode, framedNoteCount: number) {+ if (widgetMode !== "sidebar") {+ return FIT_PADDING[widgetMode];+ }++ const { sparse, dense } = SIDEBAR_FIT_NOTES;+ const crowdedness = Math.max(0, Math.min(1, (framedNoteCount - sparse) / (dense - sparse)));++ return SIDEBAR_FIT_PADDING.sparse + (SIDEBAR_FIT_PADDING.dense - SIDEBAR_FIT_PADDING.sparse) * crowdedness;+}+export function rgb2hex(rgb: string) {return `#${(rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/) || []).slice(1)