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): expand the connections map into the map it was showing
Elian Doran076ab47f5ea9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex da0d8995f3..b1d33cda18 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1398,7 +1398,7 @@"highlights_what": "The passages this note picks out by their formatting. That is a wider net than it sounds: an ordinary bold or italic sentence counts, not only text you have highlighted.","highlights_configure": "Which formats count is yours to set: bold, italic, underlined, coloured text and text on a background colour can each be listed or left out, from the gear in this card's header. Pressing an entry scrolls the note to it.","note_map_what": "A map of what this note connects to. The link map draws the internal links and relations going in and out of it; the tree map draws where it sits in the note tree. The switcher in this header chooses between the two.",- "note_map_interaction": "Hovering a note brings its own connections forward and fades the rest. The expand button opens the note map at the size of the window.",+ "note_map_interaction": "Hovering a note brings its own connections forward and fades the rest. The expand button opens the same map at the size of the window.","note_paths_what": "A note can sit in more than one place in the note tree at once, and each of those places is listed here as a path.","note_paths_cloning": "Adding a place is called cloning: nothing is copied, so an edit made under one path shows under all of them, and removing one path leaves the note under the others.","backlinks_what": "The notes pointing at this one, either through a link in their text or through a relation naming it.",diff --git a/apps/client/src/widgets/note_map/NoteMap.tsx b/apps/client/src/widgets/note_map/NoteMap.tsxindex c7161e4cc5..3416beaf72 100644--- a/apps/client/src/widgets/note_map/NoteMap.tsx+++ b/apps/client/src/widgets/note_map/NoteMap.tsx@@ -18,7 +18,7 @@ import { loadNotesAndRelations, NoteMapLinkObject, NoteMapNodeObject, NotesAndReimport { loadIconFont, resolveIconGlyphs } from "./icons";import MapTypeSwitcher from "./MapTypeSwitcher";import { CssData, setupRendering } from "./rendering";-import { isRootedAtCurrentNote, MapType, NOTE_MAP_TYPE_OPTION, NoteMapWidgetMode, rgb2hex, toMapType } from "./utils";+import { isRootedAtCurrentNote, MapType, NOTE_MAP_TYPE_OPTION, NoteMapWidgetMode, rgb2hex, toMapType, usesReaderPreference } from "./utils";/** Maximum number of notes to render in the note map before showing a warning. */const MAX_NOTES_THRESHOLD = 1_000;@@ -239,7 +239,7 @@ export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {* Which of the two maps to draw, and how to ask for the other one.** The connections tab's map is a lens on whatever note is being read, so which map it draws is the- * reader's own preference and is kept as an option (see {@link NOTE_MAP_TYPE_OPTION}). Everywhere+ * reader's own preference and is kept as an option (see {@link usesReaderPreference}). Everywhere* else the map is a note's own thing — a note map note, a hoisted map, the ribbon's tab — and the* note it belongs to says which to draw through its `mapType` label.*/@@ -247,7 +247,7 @@ function useMapType(note: FNote, widgetMode: NoteMapWidgetMode): [ MapType, (mapconst [ label, setLabel ] = useNoteLabel(note, "mapType");const [ option, setOption ] = useTriliumOption(NOTE_MAP_TYPE_OPTION);- return widgetMode === "sidebar"+ return usesReaderPreference(widgetMode)? [ toMapType(option), (mapType) => void setOption(mapType) ]: [ toMapType(label), setLabel ];}diff --git a/apps/client/src/widgets/note_map/utils.spec.ts b/apps/client/src/widgets/note_map/utils.spec.tsindex ec1811e737..83614dd6c2 100644--- a/apps/client/src/widgets/note_map/utils.spec.ts+++ b/apps/client/src/widgets/note_map/utils.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { getFitPadding, getHopDistances, mixColors, rgb2hex, toMapType, withAlpha } from "./utils";+import { getFitPadding, getHopDistances, mixColors, rgb2hex, toMapType, usesReaderPreference, withAlpha } from "./utils";describe("getHopDistances", () => {const link = (source: string, target: string) => ({ source, target });@@ -88,6 +88,19 @@ describe("toMapType", () => {});});+describe("usesReaderPreference", () => {+ it("is the connections tab's map and the same map expanded, and no other", () => {+ // Which of the two maps those draw is the reader's own preference; everywhere else it is the+ // note's, asked for through its `mapType` label.+ expect(usesReaderPreference("sidebar")).toBe(true);+ expect(usesReaderPreference("expanded")).toBe(true);++ expect(usesReaderPreference("ribbon")).toBe(false);+ expect(usesReaderPreference("type")).toBe(false);+ expect(usesReaderPreference("hoisted")).toBe(false);+ });+});+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.@@ -101,9 +114,11 @@ describe("getFitPadding", () => {// 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.+ // The maps with room of their own keep their fixed margins whatever is in them — the same map+ // expanded to the window included, which has the room the card was short of.expect(getFitPadding("ribbon", 1)).toBe(50);expect(getFitPadding("ribbon", 500)).toBe(50);+ expect(getFitPadding("expanded", 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 1ba660f477..e029bebd0c 100644--- a/apps/client/src/widgets/note_map/utils.ts+++ b/apps/client/src/widgets/note_map/utils.ts@@ -1,4 +1,9 @@-export type NoteMapWidgetMode = "ribbon" | "sidebar" | "hoisted" | "type";+/**+ * Where a map is being drawn. `expanded` is the connections tab's own map at the size of the window,+ * which the card's expand button opens — the only thing that opens it — so it is that card's map+ * rather than a surface of its own, and reads what the card reads.+ */+export type NoteMapWidgetMode = "ribbon" | "sidebar" | "expanded" | "hoisted" | "type";export type MapType = "tree" | "link";/**@@ -11,9 +16,18 @@ export type MapType = "tree" | "link";*/export const NOTE_MAP_TYPE_OPTION = "rightPaneNoteMapType";+/**+ * Whether the map takes the reader's own preference of the two ({@link NOTE_MAP_TYPE_OPTION}) rather+ * than what the note it is drawn for asks for. The connections tab and what its expand button opens,+ * which is the same map at the size of the window and is left showing the same one.+ */+export function usesReaderPreference(widgetMode: NoteMapWidgetMode) {+ return widgetMode === "sidebar" || widgetMode === "expanded";+}+/** Whether the map is rooted at the note being read, rather than at a configured or hoisted note. */export function isRootedAtCurrentNote(widgetMode: NoteMapWidgetMode) {- return widgetMode === "ribbon" || widgetMode === "sidebar";+ return widgetMode === "ribbon" || widgetMode === "sidebar" || widgetMode === "expanded";}/** The map a note asks to be drawn as through its `mapType` label, the link map standing for anything else. */@@ -26,6 +40,9 @@ const FIT_PADDING: Record<NoteMapWidgetMode, number> = {// The sidebar's is not a fixed one — see getFitPadding.sidebar: 0,ribbon: 50,+ // Not the sidebar's own, small enough that a graph fitted with room to spare is a thumbnail: what+ // the card is short of, a map given the greater part of the window has plenty of.+ expanded: 50,hoisted: 30,type: 30};diff --git a/apps/client/src/widgets/sidebar/NoteMap.tsx b/apps/client/src/widgets/sidebar/NoteMap.tsxindex b9edda2489..4616a01266 100644--- a/apps/client/src/widgets/sidebar/NoteMap.tsx+++ b/apps/client/src/widgets/sidebar/NoteMap.tsx@@ -41,9 +41,9 @@ export default function NoteMap() {<MapTypeSwitcher mapType={toMapType(mapType)} setMapType={(type) => void setMapType(type)} />{/* One rung of the same ladder the quick-edit popup already offers: a press here takes themap from a card to the greater part of the window, and the popup's own expand takes it- on to a tab. What it opens is the note's own map, drawn as that note's `mapType` label- asks for — which is not this card's option, and so need not be the map being expanded.- The popup is reached from the ribbon's tab as well, and belongs to neither of them.+ on to a tab. What it opens is this card's own map — nothing else opens that view, so it+ is drawn in `expanded` mode and left showing the map being expanded, rather than+ whichever one the note itself asks for.The popup rather than a split, because a map that sits beside the notebeing read looks as though it follows it and doesn't; a glance that goes away when itdiff --git a/apps/client/src/widgets/type_widgets/NoteMap.tsx b/apps/client/src/widgets/type_widgets/NoteMap.tsxindex 3f33fe586d..ac8653618a 100644--- a/apps/client/src/widgets/type_widgets/NoteMap.tsx+++ b/apps/client/src/widgets/type_widgets/NoteMap.tsx@@ -8,10 +8,13 @@ export default function NoteMap({ note, noteContext }: TypeWidgetProps) {return (<div ref={containerRef}>+ {/* The `note-map` view of an ordinary note is what the connections tab's expand button+ opens, and nothing else opens it — so it is drawn as that card's map rather than as the+ map of a note that is one, which is what this widget otherwise draws. */}<NoteMapElparentRef={containerRef}note={note}- widgetMode={noteContext?.viewScope?.viewMode === "note-map" ? "ribbon" : "type"} />+ widgetMode={noteContext?.viewScope?.viewMode === "note-map" ? "expanded" : "type"} /></div>);}