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): note paths badge opens the sidebar instead of a popup
Elian Doranb37898aad753
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.tsindex def968541b..ec018e8ff3 100644--- a/apps/client/src/components/app_context.ts+++ b/apps/client/src/components/app_context.ts@@ -359,7 +359,19 @@ export type CommandMappings = {toggleRightPane: CommandData;peekRightPane: CommandData;/** Shows the given tab of the right pane, opening the pane if it is closed. */- selectRightPaneTab: CommandData & { tabId: RightPaneTabId };+ selectRightPaneTab: CommandData & {+ tabId: RightPaneTabId;+ /**+ * Peek the pane rather than dock it when it is closed, for an entry point that is only a glance+ * at the tab and shouldn't reflow the content around it. An already docked pane stays docked.+ */+ peek?: boolean;+ /**+ * The id of a widget of that tab (see `RightPanelWidget`) to expand, so that an entry point+ * aimed at one widget doesn't land on it collapsed.+ */+ expandWidgetId?: string;+ };printActiveNote: CommandData;exportAsPdf: CommandData;showPrintPreview: PrintPreviewData;diff --git a/apps/client/src/widgets/layout/StatusBar.css b/apps/client/src/widgets/layout/StatusBar.cssindex 90267bf1f7..e1e42c764c 100644--- a/apps/client/src/widgets/layout/StatusBar.css+++ b/apps/client/src/widgets/layout/StatusBar.css@@ -67,12 +67,6 @@padding: 1em !important;}- .dropdown-note-paths {- .note-paths-widget {- padding: 0.5em;- }- }-.backlinks-widget > .dropdown-menu {--menu-padding-size: .9em;diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsxindex 8c83124e3f..83cfd0f0ad 100644--- a/apps/client/src/widgets/layout/StatusBar.tsx+++ b/apps/client/src/widgets/layout/StatusBar.tsx@@ -7,7 +7,7 @@ import { type ComponentChildren, RefObject } from "preact";import { createPortal } from "preact/compat";import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";-import { CommandNames } from "../../components/app_context";+import appContext, { CommandNames } from "../../components/app_context";import NoteContext from "../../components/note_context";import FNote from "../../entities/fnote";import attributes from "../../services/attributes";@@ -30,7 +30,7 @@ import AttributeEditor, { AttributeEditorImperativeHandlers } from "../ribbon/coimport AttributeHelp, { ATTRIBUTE_HELP_PAGE } from "../ribbon/components/AttributeHelp";import InheritedAttributesTab from "../ribbon/InheritedAttributesTab";import { NoteSizeWidget, useNoteMetadata } from "../ribbon/NoteInfoTab";-import { NotePathsWidget, useSortedNotePaths } from "../ribbon/NotePathsTab";+import { useSortedNotePaths } from "../ribbon/NotePathsTab";import SimilarNotesTab from "../ribbon/SimilarNotesTab";import { useAttachments } from "../type_widgets/Attachment";import { useProcessedLocales } from "../type_widgets/options/components/LocaleSelector";@@ -428,28 +428,35 @@ function AttributesPane({ note, noteContext, attributesShown, setAttributesShown//#endregion//#region Note paths-function NotePaths({ note, hoistedNoteId, notePath }: StatusBarContext) {- const dropdownRef = useRef<BootstrapDropdown>(null);+/**+ * The paths themselves live in the sidebar's connections tab, so the badge is only the way in (and, on+ * a second press, out) rather than a second copy of the list in a popup of its own.+ */+function NotePaths({ note, hoistedNoteId }: StatusBarContext) {const sortedNotePaths = useSortedNotePaths(note, hoistedNoteId);const count = sortedNotePaths?.length ?? 0;- const enabled = true;++ const toggleNotePaths = useCallback(() => {+ void appContext.triggerEvent("selectRightPaneTab", {+ tabId: "connections",+ // A glance at the paths from the status bar shouldn't reflow the content around a pane the+ // user has closed; one they keep docked stays docked.+ peek: true,+ expandWidgetId: "notePaths"+ });+ }, []);// Keyboard shortcut.- useTriliumEvent("toggleRibbonTabNotePaths", () => enabled && dropdownRef.current?.show());+ useTriliumEvent("toggleRibbonTabNotePaths", toggleNotePaths);- return (enabled &&- <StatusBarDropdown- title={t("status_bar.note_paths_title")}- dropdownRef={dropdownRef}- dropdownContainerClassName="dropdown-note-paths"+ return (+ <StatusBarButton+ className="note-paths-button"icon="bx bx-directions"+ title={t("status_bar.note_paths_title")}text={t("status_bar.note_paths", { count })}- >- <NotePathsWidget- sortedNotePaths={sortedNotePaths}- currentNotePath={notePath}- />- </StatusBarDropdown>+ onClick={toggleNotePaths}+ />);}//#endregiondiff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex 97d8ad8a70..a6e69a8479 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -4,7 +4,7 @@ import "./RightPanelContainer.css";import Split from "@triliumnext/split.js";import clsx from "clsx";import { VNode } from "preact";-import { useLayoutEffect, useRef } from "preact/hooks";+import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";import { WidgetsByParent } from "../../services/bundle";@@ -28,7 +28,7 @@ import PdfAnnotations from "./pdf/PdfAnnotations";import PdfAttachments from "./pdf/PdfAttachments";import PdfLayers from "./pdf/PdfLayers";import PdfPages from "./pdf/PdfPages";-import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";+import RightPanelWidget, { CollapsibleWidgets, ExpandWidgetRequest, ExpandWidgetRequests } from "./RightPanelWidget";import RightPanePeekButton from "./RightPanePeekButton";import RightPaneTabs, { RIGHT_PANE_TABS, RightPaneTabDefinition, RightPaneTabId } from "./RightPaneTabs";import SimilarNotes from "./SimilarNotes";@@ -54,6 +54,7 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPareconst { mode, visible, mounted, togglePeek, toggleDocked, dock, close, dismiss } = usePaneMode("rightPaneVisible");const tabs = useItems(mounted, widgetsByParent);const [ selectedTabId, setSelectedTabId ] = useTriliumOption("rightPaneSelectedTab");+ const [ expandRequest, setExpandRequest ] = useState<ExpandWidgetRequest | null>(null);useSplit(mode);// The chosen tab may have nothing to show for this note (or may have gone away entirely, e.g. the@@ -74,25 +75,43 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPareuseTriliumEvent("toggleRightPane", toggleDocked);useTriliumEvent("peekRightPane", togglePeek);- // An entry point aimed at one tab (the chat launcher): it opens the pane on that tab, brings it to- // the front if the pane is already open on another one, and closes it if it is the tab on show —- // so the button it comes from keeps behaving like a toggle for its own tab.- useTriliumEvent("selectRightPaneTab", ({ tabId }) => {+ // An entry point aimed at one tab (the chat launcher, the status bar's note paths badge): it opens+ // the pane on that tab, brings it to the front if the pane is already open on another one, and+ // closes it if it is the tab on show — so the button it comes from keeps behaving like a toggle for+ // its own tab. `peek` opens it as a glance instead of a dock; `expandWidgetId` opens the one widget+ // the entry point is really about, in case the user has it collapsed.+ useTriliumEvent("selectRightPaneTab", ({ tabId, peek, expandWidgetId }) => {if (visible && activeTab?.id === tabId) {close();return;}void setSelectedTabId(tabId);+ // Raised before the pane opens, so a widget mounted for the first time already reads it.+ if (expandWidgetId) {+ setExpandRequest((prev) => ({ id: expandWidgetId, counter: (prev?.counter ?? 0) + 1 }));+ }if (!visible) {- toggleDocked();+ if (peek) {+ togglePeek();+ } else {+ toggleDocked();+ }}});+ // A request doesn't outlive the pane's content: it would otherwise be honoured all over again by+ // the widget that remounts on the next opening, long after the press that asked for it.+ useEffect(() => {+ if (!mounted) setExpandRequest(null);+ }, [ mounted ]);+// Outside-press / Esc *soft*-dismisses the peek: it hides but stays mounted, so re-peeking is// instant and preserves widget state. The × button and the docked toggle hard-close (unmount).+ // The status bar's note paths badge peeks the pane itself (see selectRightPaneTab above), so+ // dismissing on its press would only close the pane for the click that follows to reopen it.usePeekDismiss(mode === "peek", dismiss, {- keepOpenSelector: "#right-pane, .right-pane-peek-button",+ keepOpenSelector: "#right-pane, .right-pane-peek-button, .status-bar .note-paths-button",focusSelector: ".right-pane-peek-button"});@@ -138,11 +157,13 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare{/* Collapsing the only widget of a tab would leave the tab empty,so a tab of one offers no collapsing at all. */}<CollapsibleWidgets.Provider value={tab.items.length > 1}>- {tab.items.length > 0 ? tab.items : (- // A tab that stays for a note it has nothing to show- // (see RightPaneTabDefinition.alwaysShown) says so.- <NoItems icon={tab.icon} text={t("right_pane.empty_message")} />- )}+ <ExpandWidgetRequests.Provider value={expandRequest}>+ {tab.items.length > 0 ? tab.items : (+ // A tab that stays for a note it has nothing to show+ // (see RightPaneTabDefinition.alwaysShown) says so.+ <NoItems icon={tab.icon} text={t("right_pane.empty_message")} />+ )}+ </ExpandWidgetRequests.Provider></CollapsibleWidgets.Provider></div>))}diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsxindex 6d06950e4d..9615b7f484 100644--- a/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx@@ -4,7 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import options from "../../services/options";import server from "../../services/server";-import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";+import RightPanelWidget, { CollapsibleWidgets, ExpandWidgetRequest, ExpandWidgetRequests } from "./RightPanelWidget";describe("RightPanelWidget", () => {let container: HTMLElement;@@ -56,4 +56,26 @@ describe("RightPanelWidget", () => {await act(async () => container.querySelector<HTMLElement>(".card-header")?.click());expect(container.querySelector(".card-body")).toBeNull();});++ it("expands on a request for it by name, and remembers it, but leaves the other widgets alone", async () => {+ const renderWith = async (request: ExpandWidgetRequest | null) => act(async () => render(+ <ExpandWidgetRequests.Provider value={request}>+ <RightPanelWidget id="shared" title="Note Paths">the body</RightPanelWidget>+ <RightPanelWidget id="solitary" title="Backlinks">the other body</RightPanelWidget>+ </ExpandWidgetRequests.Provider>, container));+ const card = (id: string) => container.querySelector(`#${id}`);++ // Asked for on the very first render, i.e. mounted along with the tab it was asked from.+ await renderWith({ id: "shared", counter: 1 });+ expect(card("shared")?.className).not.toContain("collapsed");+ expect(card("solitary")?.className).toContain("collapsed");+ // Remembered, so the widget is open again the next time its tab is built.+ expect(server.put).toHaveBeenCalledWith("options", { rightPaneCollapsedItems: JSON.stringify([ "solitary" ]) });++ // Collapsed again by the user, then asked for a second time: the fresh request opens it again.+ await act(async () => container.querySelector<HTMLElement>("#shared .card-header")?.click());+ expect(card("shared")?.className).toContain("collapsed");+ await renderWith({ id: "shared", counter: 2 });+ expect(card("shared")?.className).not.toContain("collapsed");+ });});diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.tsxindex c3af8dac85..7ef101cb95 100644--- a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx@@ -1,6 +1,6 @@import clsx from "clsx";import { ComponentChildren, createContext, RefObject } from "preact";-import { useContext, useState } from "preact/hooks";+import { useContext, useEffect, useState } from "preact/hooks";import contextMenu, { MenuItem } from "../../menus/context_menu";import ActionButton from "../react/ActionButton";@@ -13,6 +13,19 @@ import { ParentComponent } from "../react/react_utils";*/export const CollapsibleWidgets = createContext(true);+/**+ * A request to expand one widget of the pane, raised by an entry point aimed at it (the status bar's+ * note paths badge) so that it doesn't land on a widget the user has collapsed. The counter tells+ * repeated requests apart, so the widget opens again after having been collapsed in between.+ */+export interface ExpandWidgetRequest {+ id: string;+ counter: number;+}++/** The pending {@link ExpandWidgetRequest}, set by whoever lays the widgets out. */+export const ExpandWidgetRequests = createContext<ExpandWidgetRequest | null>(null);+interface RightPanelWidgetProps {id: string;title: string;@@ -44,10 +57,23 @@ export default function RightPanelWidget({ id, title, buttons, children, containconst containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);const parentComponent = useContext(ParentComponent);const collapsible = useContext(CollapsibleWidgets);+ const expandRequest = useContext(ExpandWidgetRequests);// Whatever an earlier layout collapsed stays remembered but is disregarded here: the widget is all// its tab has, so a collapsed one would leave nothing behind, chevron included, to expand it again.const expanded = collapsible ? !collapsedByUser : true;+ // Being asked for by name (see ExpandWidgetRequests) opens the widget, and is remembered as if the+ // user had opened it themselves — so it is still open the next time its tab is built.+ useEffect(() => {+ if (expandRequest?.id !== id) return;+ setCollapsedByUser(false);+ if (rightPaneCollapsedItems.includes(id)) {+ void setRightPaneCollapsedItems(rightPaneCollapsedItems.filter((item) => item !== id));+ }+ // The request alone triggers this; the remembered list is read as it stands at that moment.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [ expandRequest, id ]);+if (parentComponent) {parentComponent.initialized = Promise.resolve();}