Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): run a menu row's command from the row that owns it

Elian Doran63ec6843d6e9
diff --git a/apps/client/src/widgets/buttons/global_menu.tsx b/apps/client/src/widgets/buttons/global_menu.tsx
index df5b769990..f2e4e05b30 100644
--- a/apps/client/src/widgets/buttons/global_menu.tsx
+++ b/apps/client/src/widgets/buttons/global_menu.tsx
@@ -146,11 +146,11 @@ function SwitchToOptions() {
}
function MenuItem({ icon, text, title, command, disabled, active }: MenuItemProps<KeyboardActionNames | CommandNames | (() => void)>) {
+ const parentComponent = useContext(ParentComponent);
return <FormListItem
icon={icon}
title={title}
- triggerCommand={typeof command === "string" ? command : undefined}
- onClick={typeof command === "function" ? command : undefined}
+ onClick={typeof command === "function" ? command : () => void parentComponent?.triggerCommand(command)}
disabled={disabled}
active={active}
>{text}</FormListItem>;
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index 87a3b8f4a1..db79decf93 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -131,7 +131,6 @@ export interface FormListItemOpts {
selected?: boolean;
container?: boolean;
onClick?: (e: MouseEvent) => void;
- triggerCommand?: CommandNames;
description?: string;
className?: string;
rtl?: boolean;
@@ -168,7 +167,7 @@ const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
animation: false
};
-export function FormListItem({ className, icon, iconClassName, value, title, active, disabled, checked, checkable, container, onClick, selected, rtl, triggerCommand, description, itemRef: externalItemRef, keyboardShortcut, shortcut, trailingIcon, closeOnSelect, ...contentProps }: FormListItemOpts) {
+export function FormListItem({ className, icon, iconClassName, value, title, active, disabled, checked, checkable, container, onClick, selected, rtl, description, itemRef: externalItemRef, keyboardShortcut, shortcut, trailingIcon, closeOnSelect, ...contentProps }: FormListItemOpts) {
const itemRef = useSyncedRef<HTMLLIElement>(externalItemRef, null);
// Inside a `Menu` the row is one of its items; elsewhere, as in a dropdown, it stands alone.
const menu = useContext(MenuContext);
@@ -245,7 +244,6 @@ export function FormListItem({ className, icon, iconClassName, value, title, act
onClick={(e) => {
if (!disabled) run(e);
}}
- data-trigger-command={triggerCommand}
dir={rtl ? "rtl" : undefined}
>
{/* One classless span holds the row, as the rows of a menu are laid out. */}
diff --git a/apps/client/src/widgets/ribbon/NoteActions.spec.tsx b/apps/client/src/widgets/ribbon/NoteActions.spec.tsx
new file mode 100644
index 0000000000..2e82815964
--- /dev/null
+++ b/apps/client/src/widgets/ribbon/NoteActions.spec.tsx
@@ -0,0 +1,47 @@
+import { render } from "preact";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import Component from "../../components/component";
+import Dropdown from "../react/Dropdown";
+import { ParentComponent } from "../react/react_utils";
+import { CommandItem } from "./NoteActions";
+
+// A dialog's focus trap would pull focus out of a menu portaled over it.
+vi.mock("../react/modal_focustrap", () => ({ suspendModalFocusTraps: () => () => {} }));
+
+describe("CommandItem", () => {
+ const host = document.createElement("div");
+ document.body.append(host);
+
+ afterEach(() => {
+ render(null, host);
+ });
+
+ it("runs its command on the component the menu stands in, on a click and on Enter", async () => {
+ const parent = new Component();
+ const triggerCommand = vi.spyOn(parent, "triggerCommand").mockReturnValue(undefined);
+ render((
+ <ParentComponent.Provider value={parent}>
+ <Dropdown text="Actions">
+ <CommandItem command="showRevisions" icon="bx bx-history" text="Revisions" />
+ </Dropdown>
+ </ParentComponent.Provider>
+ ), host);
+ const toggle = host.querySelector<HTMLButtonElement>("button");
+ if (!toggle) throw new Error("expected the toggle to render");
+ const popup = () => document.querySelector<HTMLElement>(".tn-popup");
+ const row = () => popup()?.querySelector<HTMLElement>("li.dropdown-item");
+
+ toggle.click();
+ await vi.waitFor(() => expect(row()).toBeTruthy());
+ row()?.click();
+ expect(triggerCommand).toHaveBeenCalledExactlyOnceWith("showRevisions");
+ await vi.waitFor(() => expect(popup()).toBeNull());
+
+ toggle.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));
+ await vi.waitFor(() => expect(row()?.classList.contains("tn-menu-active")).toBe(true));
+ popup()?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
+ expect(triggerCommand).toHaveBeenCalledTimes(2);
+ expect(triggerCommand).toHaveBeenLastCalledWith("showRevisions");
+ });
+});
diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsx
index 0c179c393c..83eba36170 100644
--- a/apps/client/src/widgets/ribbon/NoteActions.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx
@@ -361,11 +361,11 @@ function DevelopmentActions({ note, noteContext }: { note: FNote, noteContext?:
}
export function CommandItem({ icon, text, title, command, disabled }: { icon: string, text: string, title?: string, command: CommandNames | (() => void), disabled?: boolean, destructive?: boolean }) {
+ const parentComponent = useContext(ParentComponent);
return <FormListItem
icon={icon}
title={title}
- triggerCommand={typeof command === "string" ? command : undefined}
- onClick={typeof command === "function" ? command : undefined}
+ onClick={typeof command === "function" ? command : () => void parentComponent?.triggerCommand(command)}
disabled={disabled}
>{text}</FormListItem>;
}