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): read the sidebar's note map type from an option
Elian Doranfcce7e9c6aac
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex b1d33cda18..da0d8995f3 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 same 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 note 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 603bf3eee9..c7161e4cc5 100644--- a/apps/client/src/widgets/note_map/NoteMap.tsx+++ b/apps/client/src/widgets/note_map/NoteMap.tsx@@ -11,14 +11,14 @@ import hoisted_note from "../../services/hoisted_note";import { t } from "../../services/i18n";import ActionButton from "../react/ActionButton";import Button from "../react/Button";-import { useColorScheme, useElementSize, useNoteLabel } from "../react/hooks";+import { useColorScheme, useElementSize, useNoteLabel, useTriliumOption } from "../react/hooks";import NoItems from "../react/NoItems";import Slider from "../react/Slider";import { loadNotesAndRelations, NoteMapLinkObject, NoteMapNodeObject, NotesAndRelationsData } from "./data";import { loadIconFont, resolveIconGlyphs } from "./icons";import MapTypeSwitcher from "./MapTypeSwitcher";import { CssData, setupRendering } from "./rendering";-import { isRootedAtCurrentNote, NoteMapWidgetMode, rgb2hex, toMapType } from "./utils";+import { isRootedAtCurrentNote, MapType, NOTE_MAP_TYPE_OPTION, NoteMapWidgetMode, rgb2hex, toMapType } from "./utils";/** Maximum number of notes to render in the note map before showing a warning. */const MAX_NOTES_THRESHOLD = 1_000;@@ -32,9 +32,8 @@ interface NoteMapProps {export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {const containerRef = useRef<HTMLDivElement>(null);const styleResolverRef = useRef<HTMLDivElement>(null);- const [ mapTypeRaw, setMapType ] = useNoteLabel(note, "mapType");+ const [ mapType, setMapType ] = useMapType(note, widgetMode);const [ mapRootIdLabel ] = useNoteLabel(note, "mapRootNoteId");- const mapType = toMapType(mapTypeRaw);const graphRef = useRef<ForceGraph<NoteMapNodeObject, NoteMapLinkObject>>();// Everything the map is drawn in is settled when it is built — the colour of a note of each type,@@ -236,6 +235,23 @@ 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+ * 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.+ */+function useMapType(note: FNote, widgetMode: NoteMapWidgetMode): [ MapType, (mapType: MapType) => void ] {+ const [ label, setLabel ] = useNoteLabel(note, "mapType");+ const [ option, setOption ] = useTriliumOption(NOTE_MAP_TYPE_OPTION);++ return widgetMode === "sidebar"+ ? [ toMapType(option), (mapType) => void setOption(mapType) ]+ : [ toMapType(label), setLabel ];+}+/*** What the map is drawn in, asked of the theme through elements wearing the colours it is after: a* canvas knows nothing of a stylesheet, and a theme is free to have its own idea of any of them.diff --git a/apps/client/src/widgets/note_map/utils.ts b/apps/client/src/widgets/note_map/utils.tsindex e855029ad6..1ba660f477 100644--- a/apps/client/src/widgets/note_map/utils.ts+++ b/apps/client/src/widgets/note_map/utils.ts@@ -1,6 +1,16 @@export type NoteMapWidgetMode = "ribbon" | "sidebar" | "hoisted" | "type";export type MapType = "tree" | "link";+/**+ * Where the connections tab's map is told which of the two to draw.+ *+ * A preference of the reader's rather than a property of the note being read: the tab is a lens on+ * whatever passes under it, and a map that changed kind as one navigated — because one note out of a+ * subtree had once been left as a tree map — would be an instrument that cannot be relied on. The+ * maps that are a note's own thing are told by that note instead, through its `mapType` label.+ */+export const NOTE_MAP_TYPE_OPTION = "rightPaneNoteMapType";+/** 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";diff --git a/apps/client/src/widgets/sidebar/NoteMap.tsx b/apps/client/src/widgets/sidebar/NoteMap.tsxindex 0786a48c82..b9edda2489 100644--- a/apps/client/src/widgets/sidebar/NoteMap.tsx+++ b/apps/client/src/widgets/sidebar/NoteMap.tsx@@ -5,9 +5,9 @@ import "./NoteMap.css";import appContext from "../../components/app_context";import { t } from "../../services/i18n";import MapTypeSwitcher from "../note_map/MapTypeSwitcher";-import { toMapType } from "../note_map/utils";+import { NOTE_MAP_TYPE_OPTION, toMapType } from "../note_map/utils";import ActionButton from "../react/ActionButton";-import { useActiveNoteContext, useNoteLabel } from "../react/hooks";+import { useActiveNoteContext, useTriliumOption } from "../react/hooks";import LazyComponent from "../react/LazyComponent";import RightPanelWidget from "./RightPanelWidget";import SidebarHelp from "./SidebarHelp";@@ -21,15 +21,16 @@ import SidebarHelp from "./SidebarHelp";** Which map to draw is asked here too, rather than in the map's own floating overlay: a card of the* pane keeps its controls in its header, and a map this small has little room to stand buttons over.- * The map reads the same label, so the two stay of one mind without being told.+ * The map reads the same option, so the two stay of one mind without being told.** It grows, sharing the room the tab's lists leave over with the backlinks below them and taking all* of it where they are collapsed — a map is worth whatever height it is given, and there is no height* at which it is finished being read. Where it stops is in NoteMap.css.*/export default function NoteMap() {- const { note, notePath } = useActiveNoteContext();- const [ mapTypeLabel, setMapType ] = useNoteLabel(note, "mapType");+ const { notePath } = useActiveNoteContext();+ // The reader's own preference rather than the note's — see NOTE_MAP_TYPE_OPTION.+ const [ mapType, setMapType ] = useTriliumOption(NOTE_MAP_TYPE_OPTION);return (<RightPanelWidget@@ -37,10 +38,14 @@ export default function NoteMap() {title={t("note_map.title")}buttons={<><SidebarHelp section="noteMap" />- <MapTypeSwitcher mapType={toMapType(mapTypeLabel)} setMapType={setMapType} />+ <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. The popup rather than a split, because a map that sits beside the note+ 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.++ 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 ithas sent the reader somewhere makes no such promise. */}{notePath && (diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.tsindex f709799646..f601bb9388 100644--- a/packages/commons/src/lib/options_interface.ts+++ b/packages/commons/src/lib/options_interface.ts@@ -122,6 +122,13 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActirightPaneWidth: number;rightPaneCollapsedItems: string;rightPaneSelectedTab: string;+ /**+ * Which map the connections tab draws, `link` or `tree`. A preference of the reader's rather than+ * a property of any one note: the tab is a lens on whatever note is being read, and a note map+ * that is a note's own thing — a note map note, a hoisted map — is told which to draw by that+ * note's own `mapType` label instead.+ */+ rightPaneNoteMapType: string;eraseEntitiesAfterTimeInSeconds: number;eraseEntitiesAfterTimeScale: number;autoReadonlySizeText: number;diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.tsindex 7949819618..0b104ff2b5 100644--- a/packages/trilium-core/src/routes/api/options.ts+++ b/packages/trilium-core/src/routes/api/options.ts@@ -72,6 +72,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>(["rightPaneWidth","rightPaneCollapsedItems","rightPaneSelectedTab",+ "rightPaneNoteMapType","rightPaneVisible","nativeTitleBarVisible","headingStyle",diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.tsindex 68733c420a..d716cbe874 100644--- a/packages/trilium-core/src/services/options_init.ts+++ b/packages/trilium-core/src/services/options_init.ts@@ -183,6 +183,9 @@ const defaultOptions: DefaultOption[] = [{ name: "rightPaneVisible", value: "true", isSynced: false },{ name: "rightPaneCollapsedItems", value: "[]", isSynced: false },{ name: "rightPaneSelectedTab", value: "outline", isSynced: false },+ // Synced, unlike the rest of the pane's state: which map to read connections as is a preference+ // rather than where a window happens to be left standing.+ { name: "rightPaneNoteMapType", value: "link", isSynced: true },{ name: "nativeTitleBarVisible", value: "false", isSynced: false },{ name: "eraseEntitiesAfterTimeInSeconds", value: "604800", isSynced: true }, // default is 7 days{ name: "eraseEntitiesAfterTimeScale", value: "86400", isSynced: true }, // default 86400 seconds = Day