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
feat(menu): show an action's shortcut, and what Clone does, in its tooltip
Elian Dorana0aae66ad37d
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex c737124f3b..52371bd4de 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -61,6 +61,11 @@ export interface MenuCommandItem<T> {* share one. Drawn in a row of actions only.*/uiIconBadge?: string;+ /**+ * What a row of actions shows in the item's tooltip in place of its title, for an action whose+ * short title needs a word of explanation.+ */+ tooltip?: string;/*** Classes tinting {@link uiIcon} with the colour of whatever the item stands for, as* `cssClassManager.createClassForColor()` and `FNote#getColorClass()` return them. Only thediff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.tsindex 97c5a417d7..3d1b4bada7 100644--- a/apps/client/src/menus/tree_context_menu.ts+++ b/apps/client/src/menus/tree_context_menu.ts@@ -154,7 +154,7 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): PruiIcon: "bx bx-cut",enabled: isNotRoot && !isHoisted && parentNotSearch},- { title: t("tree-context-menu.clone"), command: "copyNotesToClipboard", keyboardShortcut: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted },+ { title: t("tree-context-menu.clone"), tooltip: t("tree-context-menu.clone-tooltip"), command: "copyNotesToClipboard", keyboardShortcut: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted },// Shown only while something is cut or copied, which is when they are looked for....(clipboard.isClipboardEmpty() ? [] : [{diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 4d3403175d..6919796e54 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2540,7 +2540,8 @@"converted-to-attachments_other": "{{count}} notes have been converted to attachments.","convert-to-attachment-confirm": "Are you sure you want to convert the selected notes into attachments of their parent notes? This operation only applies to Image notes, other notes will be skipped.","open-in-popup": "Quick edit",- "clone": "Clone"+ "clone": "Clone",+ "clone-tooltip": "Clone, to have the same note in two places at once"},"shared_info": {"shared_publicly": "This note is shared publicly on {{- link}}.",diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsxindex c710dab9de..7ee95c446c 100644--- a/apps/client/src/widgets/react/Menu.spec.tsx+++ b/apps/client/src/widgets/react/Menu.spec.tsx@@ -1,11 +1,26 @@+import { Tooltip } from "bootstrap";import { render } from "preact";import { afterEach, describe, expect, it, vi } from "vitest";import type { MenuItem } from "../../menus/context_menu";+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display";import { FormDropdownDivider, FormDropdownSubmenu, FormListHeader, FormListItem } from "./FormList";import Menu, { type MenuProps } from "./Menu";import { shouldDropStart } from "./menu_context";+vi.mock("../../services/keyboard_actions", async (importOriginal) => {+ const original = await importOriginal<typeof import("../../services/keyboard_actions")>();+ return {+ ...original,+ default: {+ ...original.default,+ getAction: vi.fn(async (name: string) => (name === "cutNotesToClipboard"+ ? { effectiveShortcuts: [ "CommandOrControl+X" ] }+ : undefined))+ }+ };+});+describe("shouldDropStart", () => {/** A level 200px wide at `left`, in a viewport 1000px wide. */const at = (left: number) => ({ left, right: left + 200, width: 200 });@@ -276,9 +291,9 @@ describe("Menu with an action row", () => {{kind: "actions",items: [- { title: "Cut", command: "cut", uiIcon: "bx bx-cut" },+ { title: "Cut", command: "cut", uiIcon: "bx bx-cut", keyboardShortcut: "cutNotesToClipboard" },{ title: "Copy", command: "copy", uiIcon: "bx bx-copy", enabled: false },- { title: "Delete", command: "delete", uiIcon: "bx bx-trash", uiIconBadge: "bx bx-x" }+ { title: "Delete", command: "delete", uiIcon: "bx bx-trash", uiIconBadge: "bx bx-x", tooltip: "Delete, asking first" }]},{ kind: "separator" },@@ -327,6 +342,25 @@ describe("Menu with an action row", () => {]);});+ it("names each action, or describes it, with the shortcut a row would show in its tooltip", async () => {+ const { menu } = renderMenu();+ const [ cut, , remove ] = menu.querySelectorAll<HTMLElement>(".tn-menu-action");+ const tooltipText = (button: HTMLElement | undefined) => {+ const tooltip = button && Tooltip.getInstance(button);+ if (!tooltip) return undefined;+ tooltip.show();+ const text = document.querySelector(".tooltip:last-of-type .tooltip-inner")?.textContent;+ tooltip.hide();+ return text;+ };++ // Formatted for display, as a row's shortcut is, rather than the raw `CommandOrControl+X`.+ const shortcut = joinShortcut(formatShortcut("CommandOrControl+X"));+ await vi.waitFor(() => expect(tooltipText(cut)).toBe(`Cut (${shortcut})`));+ expect(shortcut).not.toContain("CommandOrControl");+ expect(tooltipText(remove)).toBe("Delete, asking first");+ });+it("keeps Right and Left for the submenus on the other rows, mirrored right to left", async () => {const { menu } = renderMenu();diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 12b6fec37e..e0f8e3e89f 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -10,7 +10,7 @@ import { t } from "../../services/i18n";import { isMobile } from "../../services/utils";import { FormDropdownDivider, FormDropdownSubmenu, FormListCustomItem, FormListHeader, FormListItem } from "./FormList";import FormTextBox from "./FormTextBox";-import { useUniqueName } from "./hooks";+import { useStaticTooltipWithKeyboardShortcut, useUniqueName } from "./hooks";import { type ActiveRow, isRightToLeft, MenuContext, type MenuContextValue, type MenuFilter, MenuFilterContext, MenuLevelContext, type OpenSubmenu, pointerMoved, type RowEntry, shouldDropStart, useMenu } from "./menu_context";import Popup, { type PopupProps } from "./Popup";@@ -70,6 +70,8 @@ const FILTER_INPUT = "input.tn-menu-filter-input";/** The filter of a menu's own top level, which no row opened. */const TOP_LEVEL = { rowId: "", level: -1 };+const ACTION_TOOLTIP = { placement: "top", animation: false } as const;+/** How long typed letters keep adding to the text a row is looked up by. */const TYPEAHEAD_TIMEOUT = 500;@@ -568,15 +570,19 @@ function MenuAction<T>({ item, onSelect }: { item: MenuCommandItem<T>, onSelect:const id = useUniqueName("menu-row");const disabled = item.enabled === false;const select = (e: MouseEvent | KeyboardEvent) => onSelect?.(item, e);+ const buttonRef = useRef<HTMLButtonElement | null>(null);+ // The full title, which a narrow menu can cut short, and the shortcut a row would show.+ useStaticTooltipWithKeyboardShortcut(buttonRef, item.tooltip ?? textOf(item.title), item.keyboardShortcut, ACTION_TOOLTIP);return (<buttonref={(element) => {+ buttonRef.current = element;menu?.registerRow(id, element? { level, element, custom: false, disabled, hasSubmenu: false, inline: true, select }: undefined);}}- id={menu ? id : undefined} type="button" role="menuitem" title={textOf(item.title)}+ id={menu ? id : undefined} type="button" role="menuitem"className={clsx("tn-menu-action", menu?.active?.id === id && "tn-menu-active")}disabled={disabled} tabIndex={menu ? -1 : undefined}// The keyboard goes on from the action the pointer last pointed at, as from a row.diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 1860cb8e47..0e3b8e6a45 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -16,6 +16,7 @@ import { expandAncestorDetails } from "../../services/collapsible";import froca from "../../services/froca";import { t } from "../../services/i18n";import keyboard_actions from "../../services/keyboard_actions";+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display";import { parseNavigationStateFromUrl, ViewScope } from "../../services/link";import { getNoteTypeOptions, type NoteTypeOption } from "../../services/note_types";import options, { type OptionValue } from "../../services/options";@@ -1421,7 +1422,9 @@ export function useStaticTooltipWithKeyboardShortcut() {const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();useStaticTooltip(elRef, {- title: keyboardShortcut?.length ? `${title} (${keyboardShortcut?.join(",")})` : title,+ title: keyboardShortcut?.length+ ? `${title} (${keyboardShortcut.map((shortcut) => joinShortcut(formatShortcut(shortcut))).join(", ")})`+ : title,...opts});