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): offer the sidebar's map type in the card header
Elian Doranf160faa658ae
diff --git a/apps/client/src/widgets/note_map/MapTypeSwitcher.tsx b/apps/client/src/widgets/note_map/MapTypeSwitcher.tsxnew file mode 100644index 0000000000..4c4f7eddeb--- /dev/null+++ b/apps/client/src/widgets/note_map/MapTypeSwitcher.tsx@@ -0,0 +1,57 @@+import clsx from "clsx";++import { t } from "../../services/i18n";+import ActionButton from "../react/ActionButton";+import { MapType } from "./utils";++interface MapTypeSwitcherProps {+ mapType: MapType;+ setMapType: (mapType: MapType) => void;+ /**+ * Raised buttons, for the map's own floating overlay where they stand over the graph. The pane's+ * header wants them flat, as the tab strip above it is.+ */+ frame?: boolean;+ className?: string;+}++/**+ * The choice between the two maps a note can be drawn as, drawn the way Trilium draws a choice+ * between several things: a button group, a recessed track with the one on show raised out of it —+ * the same the right pane's tab strip is built from.+ *+ * Kept in a module of its own so that the pane can offer the choice in a card's header without+ * pulling in the map (and force-graph with it) to do so.+ */+export default function MapTypeSwitcher({ mapType, setMapType, frame, className }: MapTypeSwitcherProps) {+ return (+ <div class={clsx("btn-group map-type-switcher", className)} role="group">+ <MapTypeButton+ type="link" icon="bx bx-network-chart" text={t("note-map.button-link-map")}+ currentMapType={mapType} setMapType={setMapType} frame={frame}+ />+ <MapTypeButton+ type="tree" icon="bx bx-sitemap" text={t("note-map.button-tree-map")}+ currentMapType={mapType} setMapType={setMapType} frame={frame}+ />+ </div>+ );+}++function MapTypeButton({ icon, text, type, currentMapType, setMapType, frame }: {+ icon: string;+ text: string;+ type: MapType;+ currentMapType: MapType;+ setMapType: (mapType: MapType) => void;+ frame?: boolean;+}) {+ return (+ <ActionButton+ icon={icon} text={text}+ active={currentMapType === type}+ onClick={() => setMapType(type)}+ frame={frame}+ />+ );+}diff --git a/apps/client/src/widgets/note_map/NoteMap.tsx b/apps/client/src/widgets/note_map/NoteMap.tsxindex 5162516fcc..7a9f1ad39e 100644--- a/apps/client/src/widgets/note_map/NoteMap.tsx+++ b/apps/client/src/widgets/note_map/NoteMap.tsx@@ -16,8 +16,9 @@ import { useElementSize, useNoteLabel } from "../react/hooks";import NoItems from "../react/NoItems";import Slider from "../react/Slider";import { loadNotesAndRelations, NoteMapLinkObject, NoteMapNodeObject, NotesAndRelationsData } from "./data";+import MapTypeSwitcher from "./MapTypeSwitcher";import { CssData, setupRendering } from "./rendering";-import { isRootedAtCurrentNote, MapType, NoteMapWidgetMode, rgb2hex } from "./utils";+import { isRootedAtCurrentNote, 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;@@ -33,7 +34,7 @@ export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {const styleResolverRef = useRef<HTMLDivElement>(null);const [ mapTypeRaw, setMapType ] = useNoteLabel(note, "mapType");const [ mapRootIdLabel ] = useNoteLabel(note, "mapRootNoteId");- const mapType: MapType = mapTypeRaw === "tree" ? "tree" : "link";+ const mapType = toMapType(mapTypeRaw);const graphRef = useRef<ForceGraph<NoteMapNodeObject, NoteMapLinkObject>>();const containerSize = useElementSize(parentRef);@@ -176,10 +177,14 @@ export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {return (<div className="note-map-widget">- <div className="btn-group btn-group-sm map-type-switcher content-floating-buttons top-left" role="group">- <MapTypeSwitcher type="link" icon="bx bx-network-chart" text={t("note-map.button-link-map")} currentMapType={mapType} setMapType={setMapType} />- <MapTypeSwitcher type="tree" icon="bx bx-sitemap" text={t("note-map.button-tree-map")} currentMapType={mapType} setMapType={setMapType} />- </div>+ {/* The sidebar offers the choice in its card's header instead, where the pane keeps the+ controls of a widget — see sidebar/NoteMap.tsx. */}+ {widgetMode !== "sidebar" && (+ <MapTypeSwitcher+ mapType={mapType} setMapType={setMapType}+ className="btn-group-sm content-floating-buttons top-left" frame+ />+ )}{/* Not in the sidebar, where neither has anything to hold on to: a map that small is notone to arrange by hand, and it is rebuilt from scratch on every note it is read for,@@ -209,23 +214,6 @@ export default function NoteMap({ note, widgetMode, parentRef }: NoteMapProps) {);}-function MapTypeSwitcher({ icon, text, type, currentMapType, setMapType }: {- icon: string;- text: string;- type: MapType;- currentMapType: MapType;- setMapType: (type: MapType) => void;-}) {- return (- <ActionButton- icon={icon} text={text}- active={currentMapType === type}- onClick={() => setMapType(type)}- frame- />- );-}-function getCssData(container: HTMLElement, styleResolver: HTMLElement): CssData {const containerStyle = window.getComputedStyle(container);const styleResolverStyle = window.getComputedStyle(styleResolver);diff --git a/apps/client/src/widgets/note_map/utils.ts b/apps/client/src/widgets/note_map/utils.tsindex 25996e9fcd..a5f6ce5de9 100644--- a/apps/client/src/widgets/note_map/utils.ts+++ b/apps/client/src/widgets/note_map/utils.ts@@ -6,6 +6,11 @@ export function isRootedAtCurrentNote(widgetMode: NoteMapWidgetMode) {return widgetMode === "ribbon" || widgetMode === "sidebar";}+/** The map a note asks to be drawn as through its `mapType` label, the link map standing for anything else. */+export function toMapType(labelValue: string | null | undefined): MapType {+ return labelValue === "tree" ? "tree" : "link";+}+export function rgb2hex(rgb: string) {return `#${(rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/) || []).slice(1)diff --git a/apps/client/src/widgets/sidebar/NoteMap.css b/apps/client/src/widgets/sidebar/NoteMap.cssindex 7cee83aed2..3a8dca4b9c 100644--- a/apps/client/src/widgets/sidebar/NoteMap.css+++ b/apps/client/src/widgets/sidebar/NoteMap.css@@ -1,3 +1,20 @@+/*+ * The choice of map, offered in the card's header rather than over the map itself. The group is the+ * theme's (recessed track, raised segment on show), as the pane's tab strip is; only its size is this+ * card's own business, the theme building it for a toolbar, where it stands taller than a header row.+ */+#noteMap .card-header-buttons .map-type-switcher {+ gap: 2px;+ padding: 2px;++ .icon-action {+ --icon-button-size: 22px;+ /* Kept well inside the segment: the theme's own ratio is meant for a segment half again as+ tall, where the same glyph has room to breathe. */+ --icon-button-icon-ratio: 0.62;+ }+}+.sidebar-note-map {/* Anchors the map's floating buttons (map type, fix nodes), same as the ribbon wrapper. */position: relative;diff --git a/apps/client/src/widgets/sidebar/NoteMap.tsx b/apps/client/src/widgets/sidebar/NoteMap.tsxindex 0b2a5c903f..26576165c9 100644--- a/apps/client/src/widgets/sidebar/NoteMap.tsx+++ b/apps/client/src/widgets/sidebar/NoteMap.tsx@@ -1,4 +1,11 @@+// Also imported by the map itself, which the styles of its container belong to: the header's are+// wanted before it has finished loading, so they cannot wait for that module to arrive.+import "./NoteMap.css";+import { t } from "../../services/i18n";+import MapTypeSwitcher from "../note_map/MapTypeSwitcher";+import { toMapType } from "../note_map/utils";+import { useActiveNoteContext, useNoteLabel } from "../react/hooks";import LazyComponent from "../react/LazyComponent";import RightPanelWidget from "./RightPanelWidget";@@ -8,12 +15,20 @@ import RightPanelWidget from "./RightPanelWidget";* leaves no box but is still an element as far as selectors are concerned, so a card behind it is no* longer a child of the tab body and misses everything the pane styles by that relation — its dividing* border, the first card's header padding, the sharing out of the tab's height.+ *+ * 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.*/export default function NoteMap() {+ const { note } = useActiveNoteContext();+ const [ mapTypeLabel, setMapType ] = useNoteLabel(note, "mapType");+return (<RightPanelWidgetid="noteMap"title={t("note_map.title")}+ buttons={<MapTypeSwitcher mapType={toMapType(mapTypeLabel)} setMapType={setMapType} />}noPadding><LazyComponent loader={() => import("./NoteMapGraph.jsx")} />