Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): show the context menu's headers

Elian Doran5327c1f5c666
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 02d263e7f1..f90d34489e 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -220,6 +220,33 @@ describe("contextMenu", () => {
expect(contextMenu.isShown()).toBe(true);
});
+ it("shows a header as text above the items it introduces", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [
+ // A name the user wrote, such as a board column's status.
+ { kind: "header", title: "<b>Doing</b>" },
+ { title: "Move left" },
+ { kind: "separator" },
+ { kind: "header", title: "Colour" },
+ { title: "Red" }
+ ]
+ });
+
+ const rows = [ ...menuElement()?.children ?? [] ]
+ .map(row => `${row.tagName.toLowerCase()}.${row.className} ${row.innerHTML.includes("<b>") ? "(markup)" : row.textContent}`);
+ expect(rows).toEqual([
+ "h6.dropdown-header <b>Doing</b>",
+ "li.dropdown-item Move left",
+ "div.dropdown-divider ",
+ "h6.dropdown-header Colour",
+ "li.dropdown-item Red"
+ ]);
+ });
+
it("runs an item pressed with the primary button, then hides", async () => {
buildPage();
const contextMenu = await buildContextMenu();
diff --git a/apps/client/src/menus/context_menu_legacy.ts b/apps/client/src/menus/context_menu_legacy.ts
index 4954d93833..698a4f045d 100644
--- a/apps/client/src/menus/context_menu_legacy.ts
+++ b/apps/client/src/menus/context_menu_legacy.ts
@@ -219,7 +219,6 @@ class ContextMenu {
$group.append($("<div>").addClass("dropdown-divider"));
shouldResetGroup = true; // End the group after the next item
} else if (itemKind === "header") {
- $group.append($("<h6>").addClass("dropdown-header").text((item as MenuHeader).title));
shouldResetGroup = true;
} else {
$group.append(this.createMenuItem(item as MenuCommandItem<any>));
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 1b2f667bae..b55e1c9082 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -4,7 +4,7 @@ import { autoUpdate, computePosition, flip, type Placement, shift, size, type Vi
import clsx from "clsx";
import { useLayoutEffect, useRef } from "preact/hooks";
-import type { CustomMenuItem, MenuCommandItem, MenuItem, MenuSeparatorItem } from "../../menus/context_menu";
+import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
import { getActionSync } from "../../services/keyboard_actions";
import { joinElements } from "./react_utils";
import { renderShortcutKbds } from "./shortcut_kbd";
@@ -50,6 +50,7 @@ export default function Menu<T>({ id, className, x, y, orientation, items, onSel
{menuRows(items).map((row, index) => {
if (!("kind" in row)) return <MenuRow key={index} item={row} onSelect={onSelect} />;
if (row.kind === "separator") return <div key={index} className="dropdown-divider" role="separator" />;
+ if (row.kind === "header") return <h6 key={index} className="dropdown-header">{row.title}</h6>;
return (
<li key={index} className="dropdown-custom-item" onClick={onClose}>
<row.componentFn />
@@ -118,22 +119,21 @@ function MenuIconSlot<T>({ item }: { item: MenuCommandItem<T> }) {
: <span>{"\u00a0"}</span>;
}
-/** The items `Menu` renders so far, with a run of separators reduced to one. */
+/** The items with a run of separators reduced to one. */
function menuRows<T>(items: MenuItem<T>[]) {
- const rows: (MenuCommandItem<T> | MenuSeparatorItem | CustomMenuItem)[] = [];
+ const rows: MenuItem<T>[] = [];
for (const item of items) {
- const kind = "kind" in item ? item.kind : undefined;
- if (kind === "separator") {
- const previous = rows.at(-1);
- if (previous && "kind" in previous && previous.kind === "separator") continue;
- rows.push(item as MenuSeparatorItem);
- } else if (kind !== "header") {
- rows.push(item as MenuCommandItem<T> | CustomMenuItem);
- }
+ const previous = rows.at(-1);
+ if (isSeparator(item) && previous && isSeparator(previous)) continue;
+ rows.push(item);
}
return rows;
}
+function isSeparator<T>(item: MenuItem<T>) {
+ return "kind" in item && item.kind === "separator";
+}
+
/**
* Positions `menu` beside `anchor`, preferring `placement` and then the placements that mirror it,
* the way a native menu opens above or to the left of a pointer that is near the viewport's edge.