Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): act on the panel shortcuts only in the active note context

Elian Doran1c1f31d65d30
diff --git a/apps/client/src/widgets/layout/NoteActionsCustom.spec.tsx b/apps/client/src/widgets/layout/NoteActionsCustom.spec.tsx
index b155f82b50..5ae4c9e52a 100644
--- a/apps/client/src/widgets/layout/NoteActionsCustom.spec.tsx
+++ b/apps/client/src/widgets/layout/NoteActionsCustom.spec.tsx
@@ -32,6 +32,27 @@ describe("NoteActionsCustom", () => {
triggerCommand.mockRestore();
});
+ it("focuses its first button on the File and Image Properties shortcuts only for its own note context", async () => {
+ const note = buildNote({ title: "Diagram", type: "mermaid" });
+ const parent = new Component();
+ const container = renderInto(
+ <ParentComponent.Provider value={parent}>
+ <NoteActionsCustom note={note} ntxId="ntx-split-2" noteContext={{ viewScope: { viewMode: "default" } } as NoteContext} />
+ </ParentComponent.Provider>
+ );
+ const firstButton = container.querySelector(".note-actions-custom")?.firstElementChild;
+ expect(firstButton).toBeTruthy();
+
+ for (const eventName of [ "toggleRibbonTabFileProperties", "toggleRibbonTabImageProperties" ] as const) {
+ (document.activeElement as HTMLElement | null)?.blur();
+ await parent.handleEvent(eventName, { ntxId: "ntx-split-1" });
+ expect(document.activeElement).not.toBe(firstButton);
+
+ await parent.handleEvent(eventName, { ntxId: "ntx-split-2" });
+ expect(document.activeElement).toBe(firstButton);
+ }
+ });
+
function renderCopyButton({ isProtected = false }: { isProtected?: boolean }, viewScope: ViewScope = { viewMode: "default" }) {
const note = buildNote({ title: "Diagram", type: "mermaid" });
note.isProtected = isProtected;
diff --git a/apps/client/src/widgets/layout/NoteActionsCustom.tsx b/apps/client/src/widgets/layout/NoteActionsCustom.tsx
index b1c1c37ce7..891a72c6ad 100644
--- a/apps/client/src/widgets/layout/NoteActionsCustom.tsx
+++ b/apps/client/src/widgets/layout/NoteActionsCustom.tsx
@@ -65,7 +65,8 @@ export default function NoteActionsCustom(props: NoteActionsCustomProps) {
isReadOnly
};
- useTriliumEvents([ "toggleRibbonTabFileProperties", "toggleRibbonTabImageProperties" ], () => {
+ useTriliumEvents([ "toggleRibbonTabFileProperties", "toggleRibbonTabImageProperties" ], ({ ntxId }) => {
+ if (ntxId !== props.ntxId) return;
(containerRef.current?.firstElementChild as HTMLElement)?.focus();
});
diff --git a/apps/client/src/widgets/layout/NoteTitleActions.spec.tsx b/apps/client/src/widgets/layout/NoteTitleActions.spec.tsx
new file mode 100644
index 0000000000..55a215c469
--- /dev/null
+++ b/apps/client/src/widgets/layout/NoteTitleActions.spec.tsx
@@ -0,0 +1,48 @@
+import { act } from "preact/test-utils";
+import { describe, expect, it, vi } from "vitest";
+
+import Component from "../../components/component";
+import NoteContext from "../../components/note_context";
+import { buildNote } from "../../test/easy-froca";
+import { renderInto } from "../../test/render";
+import { NoteContextContext, ParentComponent } from "../react/react_utils";
+import NoteTitleActions from "./NoteTitleActions";
+
+// The attribute grid has a spec of its own; here one cell is enough for the section to show.
+vi.mock("../PromotedAttributes", () => ({
+ usePromotedAttributeData: () => [ [ { uniqueId: "cell" } ], () => {} ],
+ PromotedAttributesContent: () => <div className="promoted-attributes-stub" />
+}));
+vi.mock("../NoteDetail", () => ({
+ getExtendedWidgetType: async () => "text",
+ checkFullHeight: () => true
+}));
+vi.mock("./NoteTypeSwitcher", () => ({ default: () => null }));
+
+describe("NoteTitleActions", () => {
+ it("toggles the promoted attributes on their shortcut only for its own note context", async () => {
+ const note = buildNote({ title: "Text", type: "text" });
+ const noteContext = new NoteContext("ntx-split-2");
+ noteContext.noteId = note.noteId;
+ noteContext.viewScope = { viewMode: "default" };
+ const parent = new Component();
+ let container: HTMLElement | undefined;
+ await act(async () => {
+ container = renderInto(
+ <ParentComponent.Provider value={parent}>
+ <NoteContextContext.Provider value={noteContext}>
+ <NoteTitleActions />
+ </NoteContextContext.Provider>
+ </ParentComponent.Provider>
+ );
+ });
+ const expanded = () => container?.querySelector("[aria-expanded]")?.getAttribute("aria-expanded");
+ expect(expanded()).toBe("false");
+
+ await act(async () => { await parent.handleEvent("toggleRibbonTabPromotedAttributes", { ntxId: "ntx-split-1" }); });
+ expect(expanded()).toBe("false");
+
+ await act(async () => { await parent.handleEvent("toggleRibbonTabPromotedAttributes", { ntxId: "ntx-split-2" }); });
+ expect(expanded()).toBe("true");
+ });
+});
diff --git a/apps/client/src/widgets/layout/NoteTitleActions.tsx b/apps/client/src/widgets/layout/NoteTitleActions.tsx
index 34f49d0ed9..28221ffc2a 100644
--- a/apps/client/src/widgets/layout/NoteTitleActions.tsx
+++ b/apps/client/src/widgets/layout/NoteTitleActions.tsx
@@ -57,7 +57,10 @@ function PromotedAttributes({ note, componentId, noteContext }: {
}, [ note, noteContext ]);
// Keyboard shortcut.
- useTriliumEvent("toggleRibbonTabPromotedAttributes", () => setExpanded(!expanded));
+ useTriliumEvent("toggleRibbonTabPromotedAttributes", ({ ntxId }) => {
+ if (!noteContext || ntxId !== noteContext.ntxId) return;
+ setExpanded(!expanded);
+ });
if (!cells?.length) return false;
return (note && (
diff --git a/apps/client/src/widgets/note_bars/CollectionProperties.spec.tsx b/apps/client/src/widgets/note_bars/CollectionProperties.spec.tsx
new file mode 100644
index 0000000000..3e42dfa116
--- /dev/null
+++ b/apps/client/src/widgets/note_bars/CollectionProperties.spec.tsx
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest";
+
+import Component from "../../components/component";
+import NoteContext from "../../components/note_context";
+import { buildNote } from "../../test/easy-froca";
+import { renderInto } from "../../test/render";
+import { NoteContextContext, ParentComponent } from "../react/react_utils";
+import CollectionProperties from "./CollectionProperties";
+
+describe("CollectionProperties", () => {
+ it("focuses the view type switcher on its shortcut only for its own note context", async () => {
+ const note = buildNote({ title: "Collection", type: "book" });
+ const noteContext = new NoteContext("ntx-split-2");
+ noteContext.noteId = note.noteId;
+ const parent = new Component();
+ const container = renderInto(
+ <ParentComponent.Provider value={parent}>
+ <NoteContextContext.Provider value={noteContext}>
+ <CollectionProperties note={note} />
+ </NoteContextContext.Provider>
+ </ParentComponent.Provider>
+ );
+ const switcher = container.querySelector(".left-container button");
+ expect(switcher).toBeTruthy();
+
+ await parent.handleEvent("toggleRibbonTabBookProperties", { ntxId: "ntx-split-1" });
+ expect(document.activeElement).not.toBe(switcher);
+
+ await parent.handleEvent("toggleRibbonTabBookProperties", { ntxId: "ntx-split-2" });
+ expect(document.activeElement).toBe(switcher);
+ });
+});
diff --git a/apps/client/src/widgets/note_bars/CollectionProperties.tsx b/apps/client/src/widgets/note_bars/CollectionProperties.tsx
index 68ba8a0481..33d72022de 100644
--- a/apps/client/src/widgets/note_bars/CollectionProperties.tsx
+++ b/apps/client/src/widgets/note_bars/CollectionProperties.tsx
@@ -12,7 +12,7 @@ import { ViewTypeOptions } from "../collections/interface";
import ActionButton from "../react/ActionButton";
import Dropdown from "../react/Dropdown";
import { FormDropdownDivider, FormListItem } from "../react/FormList";
-import { useNoteLabel, useNoteProperty, useTriliumEvent } from "../react/hooks";
+import { useNoteContext, useNoteLabel, useNoteProperty, useTriliumEvent } from "../react/hooks";
import Icon from "../react/Icon";
import { CheckBoxProperty, ViewProperty } from "../react/NotePropertyMenu";
import { bookPropertiesConfig } from "./collection-properties-config";
@@ -131,7 +131,9 @@ export function useViewType(note: FNote | null | undefined) {
function ViewTypeSwitcher({ viewType, setViewType }: { viewType: ViewTypeOptions, setViewType: (newValue: ViewTypeOptions) => void }) {
// Keyboard shortcut
const dropdownContainerRef = useRef<HTMLDivElement>(null);
- useTriliumEvent("toggleRibbonTabBookProperties", () => {
+ const { ntxId: ownNtxId } = useNoteContext();
+ useTriliumEvent("toggleRibbonTabBookProperties", ({ ntxId }) => {
+ if (!ownNtxId || ntxId !== ownNtxId) return;
dropdownContainerRef.current?.querySelector("button")?.focus();
});