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(right pane): drop the collapse from a tab holding one widget
Elian Dorana7cdbfb65f1c
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.css b/apps/client/src/widgets/sidebar/RightPanelContainer.cssindex 4223933466..8fb53e62a0 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.css+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.css@@ -156,6 +156,13 @@ body.experimental-feature-new-layout #right-pane {&.collapsed .card-header > .icon-action {transform: rotate(-90deg);}++ /* The only widget of a tab has no chevron (see RightPanelWidget.tsx): its header is not+ something to press, and its title takes over where the chevron would have started. */+ &.not-collapsible .card-header {+ cursor: default;+ padding-inline-start: 8px;+ }}/* One per opened tab, all but the shown one display:none (see RightPanelContainer.tsx). Takes thediff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex d85a6f04f2..674ce5fd9f 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -26,7 +26,7 @@ import PdfAnnotations from "./pdf/PdfAnnotations";import PdfAttachments from "./pdf/PdfAttachments";import PdfLayers from "./pdf/PdfLayers";import PdfPages from "./pdf/PdfPages";-import RightPanelWidget from "./RightPanelWidget";+import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";import RightPanePeekButton from "./RightPanePeekButton";import RightPaneTabs, { RIGHT_PANE_TABS, RightPaneTabDefinition, RightPaneTabId } from "./RightPaneTabs";import TableOfContents from "./TableOfContents";@@ -132,7 +132,11 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByParerole="tabpanel"class={clsx("right-pane-tab-body", tab.id !== activeTab?.id && "hidden-ext")}>- {tab.items}+ {/* 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}+ </CollapsibleWidgets.Provider></div>))}</>diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsxnew file mode 100644index 0000000000..6d06950e4d--- /dev/null+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.spec.tsx@@ -0,0 +1,59 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import options from "../../services/options";+import server from "../../services/server";+import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";++describe("RightPanelWidget", () => {+ let container: HTMLElement;++ beforeEach(() => {+ // Both widgets below are remembered as collapsed, which only one of them is free to honour.+ options.set("rightPaneCollapsedItems", JSON.stringify([ "solitary", "shared" ]));+ server.put = vi.fn(async () => ({})) as typeof server.put;+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ });++ it("offers no collapsing when it is the only widget of its tab, remembered collapse and all", async () => {+ await act(async () => render(+ <CollapsibleWidgets.Provider value={false}>+ <RightPanelWidget id="solitary" title="Attributes">the body</RightPanelWidget>+ </CollapsibleWidgets.Provider>, container));++ expect(container.querySelector(".card-header > .icon-action")).toBeNull();+ expect(container.querySelector(".card")?.className).toContain("not-collapsible");+ // Expanded despite being remembered as collapsed: with no chevron there is no way back.+ expect(container.querySelector(".card-body")?.textContent).toBe("the body");++ // And its header is not something to press.+ await act(async () => container.querySelector<HTMLElement>(".card-header")?.click());+ expect(container.querySelector(".card-body")).not.toBeNull();+ });++ it("collapses, and expands again, on a press of its header when it shares its tab", async () => {+ await act(async () => render(+ <CollapsibleWidgets.Provider value={true}>+ <RightPanelWidget id="shared" title="Table of Contents">the body</RightPanelWidget>+ </CollapsibleWidgets.Provider>, container));++ // Starts collapsed, as it was remembered.+ expect(container.querySelector(".card")?.className).toContain("collapsed");+ expect(container.querySelector(".card-body")).toBeNull();+ expect(container.querySelector(".card-header > .icon-action")).not.toBeNull();++ await act(async () => container.querySelector<HTMLElement>(".card-header")?.click());+ expect(container.querySelector(".card")?.className).not.toContain("collapsed");+ expect(container.querySelector(".card-body")?.textContent).toBe("the body");++ await act(async () => container.querySelector<HTMLElement>(".card-header")?.click());+ expect(container.querySelector(".card-body")).toBeNull();+ });+});diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.tsxindex 37d03178ed..3949e03e14 100644--- a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx@@ -1,5 +1,5 @@import clsx from "clsx";-import { ComponentChildren, RefObject } from "preact";+import { ComponentChildren, createContext, RefObject } from "preact";import { useContext, useState } from "preact/hooks";import contextMenu, { MenuItem } from "../../menus/context_menu";@@ -7,6 +7,12 @@ import ActionButton from "../react/ActionButton";import { useSyncedRef, useTriliumOptionJson } from "../react/hooks";import { ParentComponent } from "../react/react_utils";+/**+ * Whether the widgets below can be collapsed, set by whoever lays them out: collapsing the only widget+ * of a right pane tab would leave the tab empty, so the tab says so and the widget drops its chevron.+ */+export const CollapsibleWidgets = createContext(true);+interface RightPanelWidgetProps {id: string;title: string;@@ -26,9 +32,13 @@ interface RightPanelWidgetProps {export default function RightPanelWidget({ id, title, buttons, children, containerRef: externalContainerRef, contextMenuItems, grow, keepMounted }: RightPanelWidgetProps) {const [ rightPaneCollapsedItems, setRightPaneCollapsedItems ] = useTriliumOptionJson<string[]>("rightPaneCollapsedItems");- const [ expanded, setExpanded ] = useState(!rightPaneCollapsedItems.includes(id));+ const [ collapsedByUser, setCollapsedByUser ] = useState(rightPaneCollapsedItems.includes(id));const containerRef = useSyncedRef<HTMLDivElement>(externalContainerRef, null);const parentComponent = useContext(ParentComponent);+ const collapsible = useContext(CollapsibleWidgets);+ // 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;if (parentComponent) {parentComponent.initialized = Promise.resolve();@@ -40,24 +50,24 @@ export default function RightPanelWidget({ id, title, buttons, children, containid={id}class={clsx("card widget", {collapsed: !expanded,+ "not-collapsible": !collapsible,grow})}><divclass="card-header"- onClick={() => {- const newExpanded = !expanded;- setExpanded(newExpanded);+ onClick={collapsible ? () => {+ setCollapsedByUser(expanded);const rightPaneCollapsedItemsSet = new Set(rightPaneCollapsedItems);- if (newExpanded) {- rightPaneCollapsedItemsSet.delete(id);- } else {+ if (expanded) {rightPaneCollapsedItemsSet.add(id);+ } else {+ rightPaneCollapsedItemsSet.delete(id);}setRightPaneCollapsedItems(Array.from(rightPaneCollapsedItemsSet));- }}+ } : undefined}>- <ActionButton icon="bx bx-chevron-down" text="" />+ {collapsible && <ActionButton icon="bx bx-chevron-down" text="" />}<div class="card-header-title">{title}</div><div class="card-header-buttons" onClick={e => e.stopPropagation()}>{buttons}