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
refactor(menu): mount the context menu into a host it creates
Elian Doran501eaced7891
diff --git a/apps/client/index.html b/apps/client/index.htmlindex dee8ca1a59..88a3ab0088 100644--- a/apps/client/index.html+++ b/apps/client/index.html@@ -144,7 +144,6 @@</div><div id="context-menu-cover"></div>- <div id="context-menu-host"></div><!-- Required to match the PWA's top bar color with the theme --><!-- This works even when the user directly changes --root-background in CSS -->diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex 8545961824..d0fb404f73 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -2,10 +2,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";vi.mock("../services/note_tooltip", () => ({ default: { dismissAllTooltips: vi.fn() } }));-/** The page as the shell leaves it: the element the menu mounts into, at the end of the body. */+/** The page as the shell leaves it. */function buildPage() {- document.body.innerHTML = `<div id="app"></div><div id="context-menu-host"></div>`;- return document.getElementById("context-menu-host");+ document.body.innerHTML = `<div id="app"></div>`;}/** The open menu, under the id the stylesheets and the app's floating layers know it by. */@@ -31,15 +30,15 @@ beforeEach(() => {});describe("contextMenu", () => {- it("renders the menu into its host and removes it on hide", async () => {- const host = buildPage();+ it("mounts the menu while it is up, and leaves nothing behind once hidden", async () => {+ buildPage();const contextMenu = await buildContextMenu();const onHide = vi.fn();await contextMenu.show({ x: 10, y: 20, items, selectMenuItemHandler: () => {}, onHide });const menu = menuElement();- expect(menu?.parentElement).toBe(host);+ expect(menu?.parentElement?.parentElement).toBe(document.body);expect(menu?.getAttribute("role")).toBe("menu");// The classes the old menu carried, which the stylesheets and themes style it by.expect([ ...menu?.classList ?? [] ]).toEqual(expect.arrayContaining([@@ -49,6 +48,7 @@ describe("contextMenu", () => {await contextMenu.hide();expect(menuElement()).toBeNull();+ expect(document.body.innerHTML).toBe(`<div id="app"></div>`);expect(document.body.classList.contains("context-menu-shown")).toBe(false);expect(onHide).toHaveBeenCalledTimes(1);@@ -214,29 +214,22 @@ describe("contextMenu", () => {expect(contextMenu.isShown()).toBe(false);});- it("stands at the end of the page while nothing has the screen", async () => {- const menu = buildPage();- const contextMenu = await buildContextMenu();-- await contextMenu.show({ x: 10, y: 10, items, selectMenuItemHandler: () => {} });-- expect(menu?.parentElement).toBe(document.body);- });-- it("moves inside whatever has the screen, and back once nothing does", async () => {- const menu = buildPage();+ it("opens inside whatever has the screen, and in the body once nothing does", async () => {+ buildPage();const contextMenu = await buildContextMenu();const map = document.getElementById("app");+ const show = () => contextMenu.show({ x: 10, y: 10, items, selectMenuItemHandler: () => {} });setFullscreenElement(map);- await contextMenu.show({ x: 10, y: 10, items, selectMenuItemHandler: () => {} });+ await show();// A browser paints only the fullscreen element, so a menu outside it never shows.- expect(menu?.parentElement).toBe(map);+ expect(menuElement()?.parentElement?.parentElement).toBe(map);setFullscreenElement(null);- await contextMenu.show({ x: 10, y: 10, items, selectMenuItemHandler: () => {} });-- expect(menu?.parentElement).toBe(document.body);+ await show();+ expect(menuElement()?.parentElement?.parentElement).toBe(document.body);+ // The host the fullscreen menu was mounted into went with it.+ expect(map?.childElementCount).toBe(0);});it("hides a Bootstrap tooltip that is up when the menu opens", async () => {diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex e9ed980d53..7afe8f2e43 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -79,15 +79,12 @@ export type MenuHandler<T> = (item: MenuCommandItem<T>, e: MouseEvent) => void;export type ContextMenuEvent = PointerEvent | MouseEvent | JQuery.ContextMenuEvent;class ContextMenu {- private readonly container: HTMLElement | null;private readonly cover: HTMLElement | null;- /** Where the menu stands while nothing has the screen to itself. See {@link hostInWhateverHasTheScreen}. */- private readonly home: HTMLElement | null;+ /** What `Menu` renders into while a menu is up. */+ private host?: HTMLElement;private options?: ContextMenuOptions<any>;constructor() {- this.container = document.getElementById("context-menu-host");- this.home = this.container?.parentElement ?? null;this.cover = utils.isMobile() ? document.getElementById("context-menu-cover") : null;if (this.cover) {@@ -106,10 +103,11 @@ class ContextMenu {await this.hide();}- if (!this.container) return;-this.options = options;- this.hostInWhateverHasTheScreen(this.container);+ // A browser showing an element fullscreen paints only that element, so the menu goes+ // inside it.+ this.host = document.createElement("div");+ (document.fullscreenElement ?? document.body).append(this.host);this.cover?.classList.add("show");document.body.classList.add("context-menu-shown");@@ -126,19 +124,7 @@ class ContextMenu {item.handler?.(item, e);options.selectMenuItemHandler(item, e);}- }), this.container);- }-- /**- * Puts the menu inside the element that currently has the screen to itself, and back where it- * belongs once nothing does. A browser showing an element fullscreen paints only that element,- * so a menu outside it would never appear.- */- private hostInWhateverHasTheScreen(container: HTMLElement) {- const host = document.fullscreenElement ?? this.home;- if (host && container.parentElement !== host) {- host.appendChild(container);- }+ }), this.host);}/**@@ -155,10 +141,12 @@ class ContextMenu {this.cover?.classList.remove("show");document.body.classList.remove("context-menu-shown");- if (options && this.container) {- render(null, this.container);- options.onHide?.();+ if (this.host) {+ render(null, this.host);+ this.host.remove();+ this.host = undefined;}+ options?.onHide?.();}}diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 76474f81ac..9fb090b003 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1667,11 +1667,6 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tbackground: rgba(0, 0, 0, 0.1);}-/* Only mounts the context menu, so it takes no part in the page's layout. */-#context-menu-host {- display: contents;-}-#context-menu-container {max-height: 100vh;}diff --git a/apps/standalone/src/index.html b/apps/standalone/src/index.htmlindex 3061b73624..78e669e7e2 100644--- a/apps/standalone/src/index.html+++ b/apps/standalone/src/index.html@@ -144,7 +144,6 @@</div><div id="context-menu-cover"></div>- <div id="context-menu-host"></div><!-- Required for match the PWA's top bar color with the theme --><!-- This works even when the user directly changes --root-background in CSS -->