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 the context menu items' icons, and titles as HTML
Elian Doran170a3da28e68
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex d2b5a8192b..50af1bc298 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -69,6 +69,46 @@ describe("contextMenu", () => {expect(container?.querySelector("[role=menuitem]")?.classList.contains("tn-menu-item")).toBe(true);});+ it("renders a title as HTML, as callers escape and mark it up", async () => {+ const container = buildPage();+ const contextMenu = await buildContextMenu();++ await contextMenu.show({+ x: 10, y: 10, selectMenuItemHandler: () => {},+ items: [ { title: `Tolkien & <sons> <span class="tn-menu-name">Not set</span>` } ]+ });++ const row = container?.querySelector("[role=menuitem]");+ expect(row?.textContent).toBe("Tolkien & <sons> Not set");+ expect(row?.querySelector(".tn-menu-name")).not.toBeNull();+ });++ it("shows an item's icon, a check mark in its place when checked, and a slot for none", async () => {+ const container = buildPage();+ const contextMenu = await buildContextMenu();++ await contextMenu.show({+ x: 10, y: 10, selectMenuItemHandler: () => {},+ items: [+ { title: "To Do", uiIcon: "bx bx-list-ul", iconColorClass: "use-note-color color-e64d4d" },+ { title: "Done", uiIcon: "bx bx-list-ul", checked: true },+ { title: "Aligned", uiIcon: undefined },+ { title: "Plain" }+ ]+ });++ const icons = [ ...container?.querySelectorAll("[role=menuitem]") ?? [] ]+ .map(row => row.querySelector(".tn-icon")?.className ?? null);+ expect(icons).toEqual([+ "bx bx-list-ul use-note-color color-e64d4d tn-icon",+ "bx bx-check tn-icon",+ "bx bx-empty tn-icon",+ null+ ]);+ // Only the icon is tinted, so the title keeps the menu's own colour.+ expect(container?.querySelectorAll(".use-note-color")).toHaveLength(1);+ });+it("runs an item pressed with the primary button, then hides", async () => {const container = buildPage();const contextMenu = await buildContextMenu();diff --git a/apps/client/src/menus/context_menu_legacy.ts b/apps/client/src/menus/context_menu_legacy.tsindex 9a51adda12..f2cf041af6 100644--- a/apps/client/src/menus/context_menu_legacy.ts+++ b/apps/client/src/menus/context_menu_legacy.ts@@ -257,26 +257,7 @@ class ContextMenu {}private createMenuItem(item: MenuCommandItem<any>) {- const $icon = $("<span>");-- if ("uiIcon" in item || "checked" in item) {- const icon = (item.checked ? "bx bx-check" : item.uiIcon);- if (icon) {- $icon.addClass([icon, "tn-icon"]);- if (item.iconColorClass) {- $icon.addClass(item.iconColorClass);- }- } else {- $icon.append(" ");- }- }-- const $link = $("<span>")- .append($icon)- // An element, not spaces: in a flex row, text merges with a plain title but is- // trimmed before one boxed by `menuName()`, indenting the two kinds of row apart.- .append($("<span>").addClass("tn-menu-gap"))- .append(item.title);+ const $link = $("<span>");if ("badges" in item && item.badges) {for (const badge of item.badges) {diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.cssindex 65ffcf5df7..4ca0b729e9 100644--- a/apps/client/src/widgets/react/Menu.css+++ b/apps/client/src/widgets/react/Menu.css@@ -28,6 +28,9 @@}.tn-menu-item {+ display: flex;+ align-items: center;+ gap: 0.5em;padding-block: var(--menu-item-vertical-padding);padding-inline: var(--menu-item-start-padding) var(--menu-item-end-padding);cursor: pointer;@@ -38,6 +41,18 @@color: var(--hover-item-text-color);background-color: var(--hover-item-background-color);}++ .tn-icon {+ flex: none;+ translate: 0 var(--menu-item-icon-vert-offset);+ color: var(--menu-item-icon-color);+ font-size: 1.1em;+ }++ /* `iconColorClass` tints the icon with the color of what the item stands for. */+ .tn-icon.use-note-color {+ color: var(--custom-color, var(--menu-item-icon-color));+ }}.tn-menu-separator {diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 7605e5c52c..ee13668772 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -5,6 +5,7 @@ import { useCallback, useLayoutEffect, useRef } from "preact/hooks";import type { MenuCommandItem, MenuItem, MenuSeparatorItem } from "../../menus/context_menu";import { useResizeObserver } from "./hooks";+import Icon from "./Icon";export interface MenuProps<T> {/** Where the menu opens, in viewport coordinates. */@@ -54,7 +55,11 @@ function MenuRow<T>({ item, onSelect }: { item: MenuCommandItem<T>, onSelect: MeonSelect(item, e);}}>- {item.title}+ {("uiIcon" in item || "checked" in item) && (+ <Icon icon={item.checked ? "bx bx-check" : item.uiIcon} className={item.iconColorClass} />+ )}+ {/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}+ <span dangerouslySetInnerHTML={{ __html: item.title }} /></div>);}