Skip to content

Build your personal knowledge base with Trilium Notes

feat(right pane): divide the sidebar into tabs

Elian Doran087d2e5f47f0
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index ee6e2913d4..1061b7cd0c 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -27,6 +27,7 @@ import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx
import type { PrintPreviewData } from "../widgets/dialogs/print_preview.jsx";
import type { PromptDialogOptions } from "../widgets/dialogs/prompt.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
+import type { RightPaneTabId } from "../widgets/sidebar/RightPaneTabs.jsx";
import Component from "./component.js";
import Entrypoints from "./entrypoints.js";
import MainTreeExecutors from "./main_tree_executors.js";
@@ -355,6 +356,8 @@ export type CommandMappings = {
toggleRibbonTabSimilarNotes: CommandData;
toggleRightPane: CommandData;
peekRightPane: CommandData;
+ /** Shows the given tab of the right pane, opening the pane if it is closed. */
+ selectRightPaneTab: CommandData & { tabId: RightPaneTabId };
printActiveNote: CommandData;
exportAsPdf: CommandData;
showPrintPreview: PrintPreviewData;
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 87b7aef131..aa9302743f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2998,7 +2998,11 @@
"hide": "Hide right pane",
"dock": "Dock panel",
"close": "Close panel",
- "custom_widget_go_to_source": "Go to source code"
+ "custom_widget_go_to_source": "Go to source code",
+ "tab_outline": "Outline",
+ "tab_attributes": "Attributes",
+ "tab_chat": "AI chat",
+ "tab_widgets": "Widgets"
},
"print_preview": {
"title": "Print preview",
diff --git a/apps/client/src/widgets/launch_bar/SidebarChatButton.tsx b/apps/client/src/widgets/launch_bar/SidebarChatButton.tsx
index b4c70de863..784d1afddc 100644
--- a/apps/client/src/widgets/launch_bar/SidebarChatButton.tsx
+++ b/apps/client/src/widgets/launch_bar/SidebarChatButton.tsx
@@ -5,13 +5,13 @@ import { t } from "../../services/i18n";
import { LaunchBarActionButton, LauncherNoteProps } from "./launch_bar_widgets";
/**
- * Launcher button to open the sidebar (which contains the chat).
- * The chat widget is always visible in the sidebar for non-chat notes.
+ * Launcher button to open the sidebar on the chat.
+ * The chat is one of the sidebar's tabs, available for non-chat notes.
*/
export default function SidebarChatButton({ launcherNote }: LauncherNoteProps) {
const handleClick = useCallback(() => {
- // Open right pane if hidden, or toggle it if visible
- appContext.triggerEvent("toggleRightPane", {});
+ // Opens the right pane on the chat tab, or closes it if the chat is what it is already showing.
+ appContext.triggerEvent("selectRightPaneTab", { tabId: "chat" });
}, []);
return (
diff --git a/apps/client/src/widgets/sidebar/RightPaneTabs.css b/apps/client/src/widgets/sidebar/RightPaneTabs.css
new file mode 100644
index 0000000000..83131ff040
--- /dev/null
+++ b/apps/client/src/widgets/sidebar/RightPaneTabs.css
@@ -0,0 +1,60 @@
+/*
+ * The tab strip is also the pane's header row: the pin/close actions are absolutely positioned over
+ * its end (see RightPanelContainer.css), so it keeps that much room free of tabs.
+ */
+.right-pane-tabs {
+ --right-pane-tab-size: 28px;
+
+ display: flex;
+ align-items: stretch;
+ gap: 2px;
+ flex-shrink: 0;
+ height: var(--right-pane-header-height);
+ padding-inline: 4px;
+ padding-inline-end: 84px;
+ border-bottom: 1px solid var(--main-border-color);
+ /* More tabs than the pane is wide for is possible at the smallest widths; they scroll rather than
+ wrap, which would break the fixed header height. */
+ overflow-x: auto;
+ overflow-y: hidden;
+ scrollbar-width: none;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+
+ /* Every tab is the same square: nothing but the icon is in it, centred on both axes. */
+ .right-pane-tab {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex: 0 0 auto;
+ width: var(--right-pane-tab-size);
+ padding: 0;
+ border: none;
+ border-bottom: 2px solid transparent;
+ /* The bottom border is the selected tab's indicator, so the corners it runs between are kept
+ square: rounding them curls both of its ends up, away from the strip's own border. */
+ border-end-start-radius: 0;
+ border-end-end-radius: 0;
+ background: none;
+ color: var(--muted-text-color);
+ cursor: pointer;
+ }
+
+ .right-pane-tab:hover {
+ color: var(--main-text-color);
+ }
+
+ .right-pane-tab.active {
+ border-bottom-color: currentcolor;
+ color: var(--main-text-color);
+ }
+
+ .right-pane-tab-icon {
+ font-size: 1.1em;
+ /* The glyph is what is being centred: themes centre icons with `text-align`, which leaves it on
+ the text baseline of its span rather than in the middle of the square. */
+ line-height: 1;
+ }
+}
diff --git a/apps/client/src/widgets/sidebar/RightPaneTabs.tsx b/apps/client/src/widgets/sidebar/RightPaneTabs.tsx
new file mode 100644
index 0000000000..c7cd1bf12e
--- /dev/null
+++ b/apps/client/src/widgets/sidebar/RightPaneTabs.tsx
@@ -0,0 +1,79 @@
+import "./RightPaneTabs.css";
+
+import clsx from "clsx";
+import { useRef } from "preact/hooks";
+
+import { t } from "../../services/i18n";
+import { useStaticTooltip } from "../react/hooks";
+
+export type RightPaneTabId = "outline" | "attributes" | "chat" | "widgets";
+
+export interface RightPaneTabDefinition {
+ id: RightPaneTabId;
+ title: string;
+ icon: string;
+}
+
+/**
+ * The groups the right pane's widgets are divided into, in the order they are offered. A tab with
+ * nothing to show for the current note is left out entirely, so most notes see fewer than all four.
+ */
+export const RIGHT_PANE_TABS: RightPaneTabDefinition[] = [
+ { id: "outline", title: t("right_pane.tab_outline"), icon: "bx bx-list-ul" },
+ { id: "attributes", title: t("right_pane.tab_attributes"), icon: "bx bx-hash" },
+ { id: "chat", title: t("right_pane.tab_chat"), icon: "bx bx-bot" },
+ // Widgets contributed by the user's own scripts, which belong to none of the groups above.
+ { id: "widgets", title: t("right_pane.tab_widgets"), icon: "bx bx-extension" }
+];
+
+interface RightPaneTabsProps {
+ tabs: RightPaneTabDefinition[];
+ activeTabId?: RightPaneTabId;
+ onSelect: (tabId: RightPaneTabId) => void;
+}
+
+/**
+ * The right pane's header: which group of widgets is on show. It doubles as the row the pane's own
+ * pin/close actions sit in, so it is rendered whenever the pane is open.
+ */
+export default function RightPaneTabs({ tabs, activeTabId, onSelect }: RightPaneTabsProps) {
+ return (
+ <div class="right-pane-tabs" role="tablist">
+ {tabs.map((tab) => (
+ <RightPaneTab
+ key={tab.id}
+ tab={tab}
+ active={tab.id === activeTabId}
+ onSelect={() => onSelect(tab.id)}
+ />
+ ))}
+ </div>
+ );
+}
+
+function RightPaneTab({ tab, active, onSelect }: { tab: RightPaneTabDefinition; active: boolean; onSelect: () => void }) {
+ const buttonRef = useRef<HTMLButtonElement>(null);
+ // No tab is named in the strip, the selected one included: every tab is the same icon-sized square,
+ // which is what keeps the whole strip inside a pane narrow enough to only fit the icons. The name is
+ // the tooltip, and it is the only place it appears.
+ useStaticTooltip(buttonRef, {
+ title: tab.title,
+ placement: "bottom",
+ fallbackPlacements: [ "bottom" ],
+ animation: false
+ });
+
+ return (
+ <button
+ ref={buttonRef}
+ type="button"
+ role="tab"
+ aria-selected={active}
+ aria-label={tab.title}
+ class={clsx("right-pane-tab", active && "active")}
+ onClick={onSelect}
+ >
+ <span class={clsx("right-pane-tab-icon tn-icon", tab.icon)} />
+ </button>
+ );
+}
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.css b/apps/client/src/widgets/sidebar/RightPanelContainer.css
index 3c357ffa1c..4223933466 100644
--- a/apps/client/src/widgets/sidebar/RightPanelContainer.css
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.css
@@ -109,6 +109,9 @@ body.experimental-feature-new-layout #right-pane {
position: relative;
display: flex;
flex-direction: column;
+ /* The scrolling belongs to the shown tab's body rather than to the pane, which would carry the tab
+ strip (and the actions overlaying it) off the top with it. */
+ overflow: hidden;
&.right-pane-mode-peek {
background: var(--right-pane-peek-background);
@@ -155,11 +158,20 @@ body.experimental-feature-new-layout #right-pane {
}
}
- .right-pane-actions + * {
- /* TODO: Find a more elegant way to avoid the right-pane-actions overlapping the card-header. */
- .card-header {
- padding-inline-end: 80px;
- }
+ /* One per opened tab, all but the shown one display:none (see RightPanelContainer.tsx). Takes the
+ pane's leftover height so a `grow` card inside it can still claim it. */
+ .right-pane-tab-body {
+ flex: 1;
+ min-height: 0;
+ display: flex;
+ flex-direction: column;
+ overflow-y: auto;
+ }
+
+ /* The strip is what the pane's actions overlay, so the cards no longer make room for them (the
+ first card's header used to be what they sat over); it sits right under the strip's border. */
+ .right-pane-tab-body > .card:first-child .card-header {
+ padding-top: 0;
}
.card.grow:not(.collapsed) {
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx
new file mode 100644
index 0000000000..d4953bccee
--- /dev/null
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+
+import { groupIntoTabs, RightPanelWidgetDefinition } from "./RightPanelContainer";
+
+describe("groupIntoTabs", () => {
+ it("keeps the enabled widgets only, in position order, and drops the tabs left empty", () => {
+ const toc = widget("outline");
+ const highlights = widget("outline");
+ const attributes = widget("attributes");
+ const chat = widget("chat", { position: 1000 });
+
+ const tabs = groupIntoTabs([
+ // Declared out of order, and with a widget of its own in a tab that ends up empty.
+ highlights,
+ widget("widgets", { enabled: false }),
+ chat,
+ attributes,
+ toc
+ ]);
+
+ expect(tabs.map((tab) => tab.id)).toEqual([ "outline", "attributes", "chat" ]);
+ // Positions are handed out in declaration order, so `highlights` precedes `toc`.
+ expect(tabs[0].items).toEqual([ highlights.el, toc.el ]);
+ expect(tabs[1].items).toEqual([ attributes.el ]);
+ expect(tabs[2].items).toEqual([ chat.el ]);
+ });
+
+ it("has nothing to show when no widget is enabled", () => {
+ expect(groupIntoTabs([ widget("outline", { enabled: false }) ])).toEqual([]);
+ });
+});
+
+function widget(tab: RightPanelWidgetDefinition["tab"], overrides: Partial<RightPanelWidgetDefinition> = {}) {
+ return { el: <div class={tab} />, enabled: true, tab, ...overrides };
+}
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
index c3d933a6e9..d85a6f04f2 100644
--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx
@@ -14,7 +14,7 @@ import { DEFAULT_GUTTER_SIZE } from "../../services/resizer";
import { isStandalone } from "../../services/utils";
import ActionButton from "../react/ActionButton";
import Button from "../react/Button";
-import { useActiveNoteContext, useGetContextData, useLegacyWidget, useNoteProperty, useTriliumEvent, useTriliumOptionBool, useTriliumOptionJson } from "../react/hooks";
+import { useActiveNoteContext, useGetContextData, useLegacyWidget, useNoteProperty, useTriliumEvent, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../react/hooks";
import LazyComponent from "../react/LazyComponent";
import NoItems from "../react/NoItems";
import { PaneMode, usePaneMode, usePeekDismiss } from "../react/peek_pane";
@@ -28,27 +28,64 @@ import PdfLayers from "./pdf/PdfLayers";
import PdfPages from "./pdf/PdfPages";
import RightPanelWidget from "./RightPanelWidget";
import RightPanePeekButton from "./RightPanePeekButton";
+import RightPaneTabs, { RIGHT_PANE_TABS, RightPaneTabDefinition, RightPaneTabId } from "./RightPaneTabs";
import TableOfContents from "./TableOfContents";
const MIN_WIDTH_PERCENT = 5;
const MAX_WIDTH_PERCENT = 90;
-interface RightPanelWidgetDefinition {
+export interface RightPanelWidgetDefinition {
el: VNode;
enabled: boolean;
position?: number;
+ /** Which tab of the pane the widget belongs in. */
+ tab: RightPaneTabId;
+}
+
+export interface RightPaneTabContents extends RightPaneTabDefinition {
+ /** The enabled widgets of this tab, in the order they are shown. Never empty. */
+ items: VNode[];
}
export default function RightPanelContainer({ widgetsByParent }: { widgetsByParent: WidgetsByParent }) {
const { mode, visible, mounted, togglePeek, toggleDocked, dock, close, dismiss } = usePaneMode("rightPaneVisible");
- const items = useItems(mounted, widgetsByParent);
+ const tabs = useItems(mounted, widgetsByParent);
+ const [ selectedTabId, setSelectedTabId ] = useTriliumOption("rightPaneSelectedTab");
useSplit(mode);
+ // The chosen tab may have nothing to show for this note (or may have gone away entirely, e.g. the
+ // chat once AI is switched off), in which case the first one that does stands in — without
+ // overwriting the choice, so it is back as soon as the note has that tab again.
+ const activeTab = tabs.find((tab) => tab.id === selectedTabId) ?? tabs[0];
+
+ // A tab's widgets are mounted when it is first opened and kept mounted afterwards: switching back
+ // is instant and stateful (a chat in progress survives it), while a tab never opened costs
+ // nothing — which is what keeps the LLM chat's bundle out of the pane until it is asked for.
+ const openedTabs = useRef(new Set<RightPaneTabId>());
+ if (activeTab) {
+ openedTabs.current.add(activeTab.id);
+ }
+
// Legacy entry points (tab-row toggle, empty-state button) open/close the *docked* pane;
// the keyboard shortcut peeks it (same as clicking the peek button).
useTriliumEvent("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 }) => {
+ if (visible && activeTab?.id === tabId) {
+ close();
+ return;
+ }
+
+ void setSelectedTabId(tabId);
+ if (!visible) {
+ toggleDocked();
+ }
+ });
+
// 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).
usePeekDismiss(mode === "peek", dismiss, {
@@ -81,8 +118,24 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare
</div>
)}
{mounted && (
- items.length > 0 ? (
- items
+ tabs.length > 0 ? (
+ <>
+ <RightPaneTabs
+ tabs={tabs}
+ activeTabId={activeTab?.id}
+ onSelect={(tabId) => void setSelectedTabId(tabId)}
+ />
+
+ {tabs.filter((tab) => openedTabs.current.has(tab.id)).map((tab) => (
+ <div
+ key={tab.id}
+ role="tabpanel"
+ class={clsx("right-pane-tab-body", tab.id !== activeTab?.id && "hidden-ext")}
+ >
+ {tab.items}
+ </div>
+ ))}
+ </>
) : (
<NoItems
icon="bx bx-sidebar"
@@ -104,7 +157,7 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare
);
}
-function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent) {
+function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent): RightPaneTabContents[] {
const { note } = useActiveNoteContext();
const noteType = useNoteProperty(note, "type");
const noteMime = useNoteProperty(note, "mime");
@@ -120,58 +173,78 @@ function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent) {
const definitions: RightPanelWidgetDefinition[] = [
{
el: <AttributeList />,
- enabled: !!note
+ enabled: !!note,
+ tab: "attributes"
},
{
el: <TableOfContents />,
enabled: (noteType === "text" || noteType === "doc" || isPdf || noteType === "llmChat" || !!note?.isMarkdown()),
+ tab: "outline"
},
{
el: <PdfPages />,
enabled: isPdf,
+ tab: "outline"
},
{
el: <PdfAttachments />,
enabled: isPdf,
+ tab: "outline"
},
{
el: <PdfLayers />,
enabled: isPdf,
+ tab: "outline"
},
{
el: <PdfAnnotations />,
enabled: isPdf,
+ tab: "outline"
},
{
el: <HighlightsList />,
enabled: noteType === "text" && highlightsList.length > 0,
+ tab: "outline"
},
{
el: <ChatHighlightsList />,
enabled: noteType === "llmChat" && (chatHighlights?.highlights.length ?? 0) > 0,
+ tab: "outline"
},
{
// Loaded lazily because the chat pulls in the whole LLM + CKEditor graph,
// which users without the LLM experimental feature should never download.
el: <LazyComponent loader={() => import("./SidebarChat.jsx")} />,
enabled: noteType !== "llmChat" && !isStandalone && aiEnabled,
- position: 1000
+ position: 1000,
+ tab: "chat"
},
...widgetsByParent.getLegacyWidgets("right-pane").map((widget) => ({
el: <CustomLegacyWidget key={widget._noteId} originalWidget={widget as LegacyRightPanelWidget} />,
enabled: true,
- position: widget.position
+ position: widget.position,
+ tab: "widgets" as const
})),
...widgetsByParent.getPreactWidgets("right-pane").map((widget) => {
const El = widget.render;
return {
el: <El />,
enabled: true,
- position: widget.position
+ position: widget.position,
+ tab: "widgets" as const
};
})
];
+ return groupIntoTabs(definitions);
+}
+
+/**
+ * Splits the enabled widgets into the pane's tabs, each in position order. A tab with no enabled
+ * widget is dropped rather than shown empty, which is what makes the strip follow the note: a plain
+ * image note offers attributes alone, a PDF the whole outline.
+ */
+export function groupIntoTabs(definitions: RightPanelWidgetDefinition[]): RightPaneTabContents[] {
// Assign a position to items that don't have one yet.
let pos = 10;
for (const definition of definitions) {
@@ -181,10 +254,13 @@ function useItems(rightPaneVisible: boolean, widgetsByParent: WidgetsByParent) {
}
}
- return definitions
+ const enabled = definitions
.filter(e => e.enabled)
- .toSorted((a, b) => (a.position ?? 10) - (b.position ?? 10))
- .map(e => e.el);
+ .toSorted((a, b) => (a.position ?? 10) - (b.position ?? 10));
+
+ return RIGHT_PANE_TABS
+ .map((tab) => ({ ...tab, items: enabled.filter((e) => e.tab === tab.id).map((e) => e.el) }))
+ .filter((tab) => tab.items.length > 0);
}
function useSplit(mode: PaneMode) {
diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.ts
index 043e9702a4..3d76666416 100644
--- a/packages/commons/src/lib/options_interface.ts
+++ b/packages/commons/src/lib/options_interface.ts
@@ -119,6 +119,7 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActi
leftPaneWidth: number;
rightPaneWidth: number;
rightPaneCollapsedItems: string;
+ rightPaneSelectedTab: 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.ts
index ecb575e1e8..d7a26e0821 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -71,6 +71,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>([
"leftPaneVisible",
"rightPaneWidth",
"rightPaneCollapsedItems",
+ "rightPaneSelectedTab",
"rightPaneVisible",
"nativeTitleBarVisible",
"headingStyle",
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index 0634c957e7..039aeba57b 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -182,6 +182,7 @@ const defaultOptions: DefaultOption[] = [
{ name: "rightPaneWidth", value: "25", isSynced: false },
{ name: "rightPaneVisible", value: "true", isSynced: false },
{ name: "rightPaneCollapsedItems", value: "[]", isSynced: false },
+ { name: "rightPaneSelectedTab", value: "outline", isSynced: false },
{ 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