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
client/launchbar: make the context menu consistent across widgets
Adorian Doranb8c65b651f8e
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 130b4cfd52..c83359fcbc 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -161,6 +161,24 @@ class ContextMenu {.addClass("show");}+ private repositionSubmenu(submenuEl: HTMLElement) {+ const CONTEXT_MENU_PADDING = 5;++ // Reset so the natural (downward) placement is measured on every hover.+ submenuEl.classList.remove("submenu-flip-up");++ const rect = submenuEl.getBoundingClientRect();+ const clientHeight = document.documentElement.clientHeight;+ const overflowsBottom = rect.bottom > clientHeight - CONTEXT_MENU_PADDING;+ // Only flip up if there is actually more room above the parent than below, otherwise flipping+ // would just clip the other end.+ const fitsWhenFlippedUp = rect.top - rect.height >= CONTEXT_MENU_PADDING;++ if (overflowsBottom && fitsWhenFlippedUp) {+ submenuEl.classList.add("submenu-flip-up");+ }+ }+addItems($parent: JQuery<HTMLElement>, items: MenuItem<any>[], multicolumn = false) {let $group = $parent; // The current group or parent element to which items are being appendedlet shouldStartNewGroup = false; // If true, the next item will start a new group@@ -345,6 +363,12 @@ class ContextMenu {this.addItems($subMenu, item.items, hasColumns);$item.append($subMenu);++ // Submenus open downward by default (CSS `:hover`); flip them up when the parent item sits+ // near the bottom of the viewport, otherwise the submenu would be clipped off-screen.+ if (!this.isMobile) {+ $item.on("mouseenter", () => this.repositionSubmenu($subMenu[0]));+ }}return $item;}diff --git a/apps/client/src/menus/launcher_button_context_menu.ts b/apps/client/src/menus/launcher_button_context_menu.tsindex 1272deb859..a839dfb8fb 100644--- a/apps/client/src/menus/launcher_button_context_menu.ts+++ b/apps/client/src/menus/launcher_button_context_menu.ts@@ -1,10 +1,13 @@import type { ToggleInParentResponse } from "@triliumnext/commons";+import appContext from "../components/app_context.js";import type FNote from "../entities/fnote.js";import branchService from "../services/branches.js";import { t } from "../services/i18n.js";+import options from "../services/options.js";import server from "../services/server.js";import toast from "../services/toast.js";+import { isMobile, reloadFrontendApp } from "../services/utils.js";import contextMenu, { type ContextMenuEvent, type MenuItem } from "./context_menu.js";const VISIBLE_LAUNCHER_PARENTS = ["_lbVisibleLaunchers", "_lbMobileVisibleLaunchers"];@@ -69,20 +72,25 @@ export async function showLauncherContextMenu<T extends string>() {e.preventDefault();- const items = [...(options.extraItems ?? [])] as MenuItem<string>[];+ // Widget-specific items (e.g. "Open in new tab" for note launchers, history entries, "Remove from launch bar").+ const widgetItems = [...(options.extraItems ?? [])] as MenuItem<string>[];if (canRemoveFromLaunchBar(launcherNote)) {- if (items.length > 0) {- items.push({ kind: "separator" });+ if (widgetItems.length > 0) {+ widgetItems.push({ kind: "separator" });}- items.push({+ widgetItems.push({title: t("launcher_button_context_menu.remove_from_launch_bar"),command: REMOVE_COMMAND,uiIcon: "bx bx-x-circle"});}- if (items.length === 0) return;+ // Every launch-bar context menu starts with the shared launch-bar management segment.+ const items = buildLaunchBarManagementItems();+ if (widgetItems.length > 0) {+ items.push({ kind: "separator" }, ...widgetItems);+ }contextMenu.show<string>({x: e.pageX ?? 0,@@ -99,3 +107,70 @@ export async function showLauncherContextMenu<T extends string>(}});}++/**+ * Shows a context menu containing only the shared launch-bar management segment. Used by launch-bar chrome+ * that is not itself a launcher (e.g. the left-pane toggle), which has no launcher-specific items to add.+ */+export function showLaunchBarManagementContextMenu(e: ContextMenuEvent) {+ e.preventDefault();++ contextMenu.show<string>({+ x: e.pageX ?? 0,+ y: e.pageY ?? 0,+ items: buildLaunchBarManagementItems(),+ // The items are self-contained via their own handlers, so nothing needs to be routed here.+ selectMenuItemHandler: () => {}+ });+}++/**+ * Builds the shared segment shown at the top of every launch-bar context menu: the "Configure launch bar"+ * entry and (on desktop) the "Launch bar orientation" submenu. Each item is self-contained via its own+ * {@link MenuCommandItem.handler}, so it works regardless of the widget-specific `onCommand` router.+ */+function buildLaunchBarManagementItems(): MenuItem<string>[] {+ const items: MenuItem<string>[] = [{+ title: t("launcher_button_context_menu.configure_launch_bar"),+ uiIcon: "bx " + (isMobile() ? "bx-mobile" : "bx-sidebar"),+ handler: () => appContext.triggerCommand("showLaunchBarSubtree")+ }];++ // The layout orientation only applies to the desktop layout; the mobile layout is always horizontal.+ if (!isMobile()) {+ const orientation = options.get("layoutOrientation");+ items.push({+ title: t("launcher_button_context_menu.launch_bar_orientation"),+ uiIcon: "bx bx-layout",+ items: [+ buildOrientationItem("vertical", orientation),+ buildOrientationItem("horizontal", orientation)+ ]+ });+ }++ return items;+}++function buildOrientationItem(target: "vertical" | "horizontal", currentOrientation: string): MenuItem<string> {+ const isCurrent = currentOrientation === target;+ const label = target === "vertical" ? t("theme.layout-vertical-title") : t("theme.layout-horizontal-title");+ return {+ // The unchecked item triggers a reload when picked, so hint that after its label.+ title: isCurrent ? label : `${label} ${t("launcher_button_context_menu.will_reload_frontend")}`,+ // `bx-empty` reserves the icon slot so unchecked items stay aligned with the checked one.+ uiIcon: "bx bx-empty",+ checked: isCurrent,+ handler: () => setLayoutOrientation(target)+ };+}++async function setLayoutOrientation(orientation: "vertical" | "horizontal") {+ if (options.get("layoutOrientation") === orientation) {+ return;+ }++ await options.save("layoutOrientation", orientation);+ // The layout tree and body classes are computed once at boot, so a reload is required to apply the change.+ reloadFrontendApp(`layout orientation change: ${orientation}`);+}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 9bcc6d0185..8811f3eeb9 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2263,11 +2263,6 @@"search_not_executed": "Search has not been executed yet.","search_now": "Search now"},- "spacer": {- "configure_launchbar": "Configure launch bar",- "launcher_orientation": "Launch bar orientation",- "will_reload_frontend": "(will reload app)"- },"sql_result": {"not_executed": "The query has not been executed yet.","no_rows": "No rows have been returned for this query",@@ -2365,7 +2360,10 @@"duplicate-launcher": "Duplicate launcher <kbd data-command=\"duplicateSubtree\">"},"launcher_button_context_menu": {- "remove_from_launch_bar": "Remove from launch bar"+ "remove_from_launch_bar": "Remove from launch bar",+ "configure_launch_bar": "Configure launch bar",+ "launch_bar_orientation": "Launch bar orientation",+ "will_reload_frontend": "(will reload app)"},"highlighting": {"title": "Code Blocks",diff --git a/apps/client/src/widgets/launch_bar/SpacerWidget.tsx b/apps/client/src/widgets/launch_bar/SpacerWidget.tsxindex c0bfbdb51c..a01ed4cb83 100644--- a/apps/client/src/widgets/launch_bar/SpacerWidget.tsx+++ b/apps/client/src/widgets/launch_bar/SpacerWidget.tsx@@ -1,10 +1,5 @@-import appContext, { CommandNames } from "../../components/app_context";import FNote from "../../entities/fnote";-import { showLauncherContextMenu } from "../../menus/launcher_button_context_menu";-import type { MenuItem } from "../../menus/context_menu";-import { t } from "../../services/i18n";-import options from "../../services/options";-import { isMobile, reloadFrontendApp } from "../../services/utils";+import { launcherContextMenuHandler } from "./launch_bar_widgets";interface SpacerWidgetProps {launcherNote?: FNote;@@ -21,61 +16,7 @@ export default function SpacerWidget({ launcherNote, baseSize, growthFactor }: SflexGrow: growthFactor ?? 1000,flexShrink: 1000}}- onContextMenu={launcherNote ? (e) => showLauncherContextMenu<CommandNames>(launcherNote, e, {- extraItems: buildSpacerContextMenuItems(),- onCommand: (command) => {- if (command) appContext.triggerCommand(command);- }- }) : undefined}+ onContextMenu={launcherContextMenuHandler(launcherNote)}/>)}--function buildSpacerContextMenuItems(): MenuItem<CommandNames>[] {- const items: MenuItem<CommandNames>[] = [{- title: t("spacer.configure_launchbar"),- command: "showLaunchBarSubtree",- uiIcon: "bx " + (isMobile() ? "bx-mobile" : "bx-sidebar")- }];-- // The layout orientation only applies to the desktop layout; the mobile layout is always horizontal.- if (!isMobile()) {- const orientation = options.get("layoutOrientation");- items.push({- title: t("spacer.launcher_orientation"),- uiIcon: "bx bx-layout",- items: [- {- // The unchecked item triggers a reload when picked, so hint that after its label.- title: orientationItemTitle(t("theme.layout-vertical-title"), orientation === "vertical"),- // `bx-empty` reserves the icon slot so unchecked items stay aligned with the checked one.- uiIcon: "bx bx-empty",- checked: orientation === "vertical",- handler: () => setLayoutOrientation("vertical")- },- {- title: orientationItemTitle(t("theme.layout-horizontal-title"), orientation === "horizontal"),- uiIcon: "bx bx-empty",- checked: orientation === "horizontal",- handler: () => setLayoutOrientation("horizontal")- }- ]- });- }-- return items;-}--function orientationItemTitle(title: string, isCurrent: boolean) {- return isCurrent ? title : `${title} ${t("spacer.will_reload_frontend")}`;-}--async function setLayoutOrientation(orientation: "vertical" | "horizontal") {- if (options.get("layoutOrientation") === orientation) {- return;- }-- await options.save("layoutOrientation", orientation);- // The layout tree and body classes are computed once at boot, so a reload is required to apply the change.- reloadFrontendApp(`layout orientation change: ${orientation}`);-}