Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): show the context menu items' badges and trailing icon

Elian Doran29a5d8000a66
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index f90d34489e..802ce0627d 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -247,6 +247,39 @@ describe("contextMenu", () => {
]);
});
+ it("shows badges after the title, and a trailing icon at the end of the row", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [
+ {
+ title: "Done", uiIcon: "bx bx-columns",
+ badges: [ { title: "<i>archived</i>", className: "archived" }, { title: "new" } ],
+ shortcut: "Ctrl+D",
+ trailingIcon: "bx bx-check"
+ },
+ { title: "Doing", uiIcon: "bx bx-columns", trailingIcon: undefined }
+ ]
+ });
+
+ const [ done, doing ] = [ ...menuElement()?.querySelectorAll("li.dropdown-item > span") ?? [] ]
+ .map(row => [ ...row.children ].map(child => `${child.tagName.toLowerCase()}.${child.className} ${child.textContent}`));
+ expect(done).toEqual([
+ "span.bx bx-columns tn-icon ",
+ "span.tn-menu-gap ",
+ "span. Done",
+ // A badge's title is text, as a column's status is a name the user wrote.
+ "span.badge archived <i>archived</i>",
+ "span.badge new",
+ "kbd. Ctrl+D",
+ // The item's own icon stands, unlike with `checked`.
+ "span.bx bx-check tn-icon menu-trailing-icon "
+ ]);
+ expect(doing).toHaveLength(3);
+ });
+
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 698a4f045d..34f662fa97 100644
--- a/apps/client/src/menus/context_menu_legacy.ts
+++ b/apps/client/src/menus/context_menu_legacy.ts
@@ -239,23 +239,6 @@ class ContextMenu {
private createMenuItem(item: MenuCommandItem<any>) {
const $link = $("<span>");
- if ("badges" in item && item.badges) {
- for (const badge of item.badges) {
- const badgeElement = $(`<span class="badge">`).text(badge.title);
-
- if (badge.className) {
- badgeElement.addClass(badge.className);
- }
-
- $link.append(badgeElement);
- }
- }
-
- if ("trailingIcon" in item && item.trailingIcon) {
- $link.append($("<span>")
- .addClass([ item.trailingIcon, "tn-icon", "menu-trailing-icon" ]));
- }
-
const $item = $("<li>")
.addClass("dropdown-item")
.append($link)
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index b55e1c9082..5d982e779b 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -82,7 +82,11 @@ function MenuRow<T>({ item, onSelect }: { item: MenuCommandItem<T>, onSelect: Me
<span className="tn-menu-gap" />
{/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}
<span dangerouslySetInnerHTML={{ __html: item.title }} />
+ {item.badges?.map((badge, index) => (
+ <span key={index} className={clsx("badge", badge.className)}>{badge.title}</span>
+ ))}
<MenuShortcut item={item} />
+ {item.trailingIcon && <span className={clsx(item.trailingIcon, "tn-icon", "menu-trailing-icon")} />}
</span>
</li>
);