Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): keep a right-click inside the context menu to itself

Elian Doran404c63458a43
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 17a3c9da91..6d812de9b2 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -512,6 +512,34 @@ describe("contextMenu", () => {
});
});
+ it("keeps a right-click or a long press inside it to itself, at every level", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ // Such as a host that would open a menu of its own, or Electron's for the editor.
+ const heard = vi.fn();
+ document.addEventListener("contextmenu", heard);
+
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [ { title: "Copy" }, { title: "Templates", items: [ { title: "Meeting" } ] } ]
+ });
+ const rows = [ ...menuElement()?.querySelectorAll<HTMLElement>("li.dropdown-item") ?? [] ];
+ rows[1]?.dispatchEvent(new PointerEvent("pointerenter"));
+ await vi.waitFor(() => expect(menuElement()?.querySelector("div.dropdown-submenu li")).not.toBeNull());
+ const targets = [ rows[0], menuElement(), menuElement()?.querySelector("div.dropdown-submenu li") ];
+ expect(targets.every(Boolean)).toBe(true);
+
+ for (const target of targets) {
+ const rightClick = new MouseEvent("contextmenu", { bubbles: true, cancelable: true, button: 2 });
+ target?.dispatchEvent(rightClick);
+ // The browser's own menu does not open over it.
+ expect(rightClick.defaultPrevented).toBe(true);
+ }
+ expect(heard).not.toHaveBeenCalled();
+ expect(contextMenu.isShown).toBe(true);
+ document.removeEventListener("contextmenu", heard);
+ });
+
describe("on a phone", () => {
afterEach(() => {
layout.onMobile = false;
diff --git a/apps/client/src/menus/context_menu_legacy.ts b/apps/client/src/menus/context_menu_legacy.ts
deleted file mode 100644
index f4cf45febf..0000000000
--- a/apps/client/src/menus/context_menu_legacy.ts
+++ /dev/null
@@ -1,153 +0,0 @@
-// Reference copy of the jQuery context menu from before the `Menu` rewrite. Nothing imports it.
-// Delete a piece once `Menu` covers it, and the file once `Menu` reaches parity.
-
-import { KeyboardActionNames } from "@triliumnext/commons";
-import { Tooltip } from "bootstrap";
-import { JSX } from "preact";
-
-import note_tooltip from "../services/note_tooltip.js";
-
-export interface ContextMenuOptions<T> {
- x: number;
- y: number;
- orientation?: "left";
- selectMenuItemHandler: MenuHandler<T>;
- items: MenuItem<T>[];
- /** On mobile, if set to `true` then the context menu is shown near the element. If `false` (default), then the context menu is shown at the bottom of the screen. */
- forcePositionOnMobile?: boolean;
- onHide?: () => void;
-}
-
-export interface CustomMenuItem {
- kind: "custom",
- componentFn: () => JSX.Element | null;
-}
-
-export interface MenuSeparatorItem {
- kind: "separator";
-}
-
-export interface MenuHeader {
- title: string;
- kind: "header";
-}
-
-export interface MenuItemBadge {
- title: string;
- className?: string;
-}
-
-export interface MenuCommandItem<T> {
- title: string;
- command?: T;
- type?: string;
- mime?: string;
- /**
- * The icon to display in the menu item.
- *
- * If not set, no icon is displayed and the item will appear shifted slightly to the left if there are other items with icons. To avoid this, use `bx bx-empty`.
- */
- uiIcon?: string;
- /**
- * Classes tinting {@link uiIcon} with the colour of whatever the item stands for, as
- * `cssClassManager.createClassForColor()` and `FNote#getColorClass()` return them. Only the
- * icon is tinted, the label staying readable against the highlight.
- */
- iconColorClass?: string;
- badges?: MenuItemBadge[];
- templateNoteId?: string;
- enabled?: boolean;
- handler?: MenuHandler<T>;
- items?: MenuItem<T>[] | null;
- shortcut?: string;
- keyboardShortcut?: KeyboardActionNames;
- spellingSuggestion?: string;
- checked?: boolean;
- /** Classes put on the item itself, for a menu styling one of its entries differently. */
- className?: string;
- /**
- * An icon shown at the trailing edge of the item, where a shortcut would go.
- *
- * Unlike {@link checked}, which takes the place of {@link uiIcon}, this leaves the item's own
- * icon standing — for a list where that icon is what tells one entry from another.
- */
- trailingIcon?: string;
- columns?: number;
-}
-
-export type MenuItem<T> = MenuCommandItem<T> | CustomMenuItem | MenuSeparatorItem | MenuHeader;
-export type MenuHandler<T> = (item: MenuCommandItem<T>, e: JQuery.MouseDownEvent<HTMLElement, undefined, HTMLElement, HTMLElement>) => void;
-export type ContextMenuEvent = PointerEvent | MouseEvent | JQuery.ContextMenuEvent;
-
-class ContextMenu {
- private $widget: JQuery<HTMLElement>;
- private options?: ContextMenuOptions<any>;
-
- constructor() {
- this.$widget = $("#context-menu-container");
- }
-
- async show<T>(options: ContextMenuOptions<T>) {
- this.options = options;
-
- note_tooltip.dismissAllTooltips();
- hideShownTooltips();
-
- if (this.$widget.hasClass("show")) {
- // The menu is already visible. Hide the menu then open it again
- // at the new location to re-trigger the opening animation.
- await this.hide();
- }
-
- $("body").addClass("context-menu-shown");
-
- this.$widget.empty();
-
- this.addItems(this.$widget, options.items);
- }
-
- addItems($parent: JQuery<HTMLElement>, items: MenuItem<any>[]) {
- for (const item of items) {
- if (!("kind" in item)) $parent.append(this.createMenuItem(item));
- }
- }
-
- private createMenuItem(item: MenuCommandItem<any>) {
- const $link = $("<span>");
-
- const $item = $("<li>")
- .addClass("dropdown-item")
- .append($link)
- .on("contextmenu", (e) => false);
-
- return $item;
- }
-
- /**
- * Whether a menu is up. For a host that answers a press itself and so keeps it from the
- * document, whose click is what would otherwise put the menu away (see the listener bound in
- * the constructor): such a host has to know a standing menu is what its press is really for.
- */
- isShown() {
- return this.$widget.hasClass("show");
- }
-
- async hide() {
- this.options?.onHide?.();
- this.$widget.removeClass("show");
- $("body").removeClass("context-menu-shown");
- this.$widget.hide();
- }
-}
-
-const contextMenu = new ContextMenu();
-
-export default contextMenu;
-
-// Bootstrap sets `aria-describedby` on a trigger while its tooltip is shown. `hide()` also clears
-// the hover and focus triggers that keep the tooltip up.
-function hideShownTooltips() {
- for (const trigger of document.querySelectorAll("[aria-describedby]")) {
- Tooltip.getInstance(trigger)?.hide();
- }
-}
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 1ce536e5ef..c926bb1934 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -92,6 +92,12 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
<div
ref={menuRef} id={id} role="menu"
className={clsx("dropdown-menu show tn-menu", bottomSheet && "mobile-bottom-menu", className)}
+ // Neither the browser's menu nor another of the app's opens over this one. Every level
+ // is inside this element, so one handler covers them all.
+ onContextMenu={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ }}
>
<MenuList level={0} items={items} state={state} />
{/* Inside the menu, so the rules scoped to it apply, but none inside another: a fixed