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/collections/board: reorganize the card context menu
Adorian Doranf104b6d51505
diff --git a/apps/client/src/menus/link_context_menu.spec.ts b/apps/client/src/menus/link_context_menu.spec.tsindex c11c1789b8..a0d3d2c2ac 100644--- a/apps/client/src/menus/link_context_menu.spec.ts+++ b/apps/client/src/menus/link_context_menu.spec.ts@@ -87,6 +87,19 @@ describe("getItems", () => {expect(linkContextMenu.getItems(contextMenuEvent())[1]).toMatchObject({ title: "link_context_menu.open_note_in_new_split" });});++ /** For a menu with entries of its own, which lists quick edit and folds the rest away. */+ it("folds the three places into one submenu, quick edit standing on its own", () => {+ const open = linkContextMenu.getOpenNoteItem(contextMenuEvent());++ expect(open).toMatchObject({ title: "link_context_menu.open_note" });+ expect("items" in open && open.items?.map((item) => "command" in item && item.command))+ .toEqual([ "openNoteInNewTab", "openNoteInNewSplit", "openNoteInNewWindow" ]);+ expect(linkContextMenu.getQuickEditItem()).toMatchObject({+ title: "link_context_menu.open_note_in_popup",+ command: "openNoteInPopup"+ });+ });});describe("handleLinkContextMenuItem", () => {diff --git a/apps/client/src/menus/link_context_menu.ts b/apps/client/src/menus/link_context_menu.tsindex 5974f770c3..e8c61320f6 100644--- a/apps/client/src/menus/link_context_menu.ts+++ b/apps/client/src/menus/link_context_menu.ts@@ -17,17 +17,40 @@ function openContextMenu(notePath: string, e: ContextMenuEvent, viewScope: ViewS}function getItems(e: ContextMenuEvent | GeoMouseEvent): MenuItem<CommandNames>[] {+ return [ ...getOpenItems(e), getQuickEditItem() ];+}++/** The places the note can be opened in, without the quick edit popup. */+function getOpenItems(e: ContextMenuEvent | GeoMouseEvent): MenuItem<CommandNames>[] {const ntxId = getNtxId(e);const isMobileSplitOpen = isMobile() && appContext.tabManager.getNoteContextById(ntxId).getMainContext().getSubContexts().length > 1;return [{ title: t("link_context_menu.open_note_in_new_tab"), command: "openNoteInNewTab", uiIcon: "bx bx-link-external" },{ title: !isMobileSplitOpen ? t("link_context_menu.open_note_in_new_split") : t("link_context_menu.open_note_in_other_split"), command: "openNoteInNewSplit", uiIcon: "bx bx-dock-right" },- { title: t("link_context_menu.open_note_in_new_window"), command: "openNoteInNewWindow", uiIcon: "bx bx-window-open" },- { title: t("link_context_menu.open_note_in_popup"), command: "openNoteInPopup", uiIcon: "bx bx-edit" }+ { title: t("link_context_menu.open_note_in_new_window"), command: "openNoteInNewWindow", uiIcon: "bx bx-window-open" }];}+/** Opens the note in a popup over the current one. */+function getQuickEditItem(): MenuItem<CommandNames> {+ return { title: t("link_context_menu.open_note_in_popup"), command: "openNoteInPopup", uiIcon: "bx bx-edit" };+}++/**+ * The same places, folded into one submenu, for a menu that lists entries of its own beside them.+ *+ * The items keep their commands, so `handleLinkContextMenuItem` handles them from a submenu as it+ * does from the top level.+ */+function getOpenNoteItem(e: ContextMenuEvent | GeoMouseEvent): MenuItem<CommandNames> {+ return {+ title: t("link_context_menu.open_note"),+ uiIcon: "bx bx-link-external",+ items: getOpenItems(e)+ };+}+function handleLinkContextMenuItem(command: string | undefined, e: ContextMenuEvent | GeoMouseEvent, notePath: string, viewScope = {}, hoistedNoteId: string | null = null) {if (!hoistedNoteId) {hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId ?? null;@@ -72,6 +95,8 @@ function getNtxId(e: ContextMenuEvent | GeoMouseEvent) {export default {getItems,+ getQuickEditItem,+ getOpenNoteItem,handleLinkContextMenuItem,openContextMenu};diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2ecfca6866..1b3fe71960 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2960,6 +2960,7 @@"reformat_action_items": "Action items"},"link_context_menu": {+ "open_note": "Open note","open_note_in_new_tab": "Open note in a new tab","open_note_in_new_split": "Open note in a new split","open_note_in_other_split": "Open note in the other split",diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 624233f9d7..e456ed3df1 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -24,7 +24,11 @@ import { OutsideFilterBadge } from "./card";// The card menu opens with the shared link items, which reach for the active note context.vi.mock("../../../menus/link_context_menu", () => ({- default: { getItems: () => [], handleLinkContextMenuItem: () => {} }+ default: {+ getQuickEditItem: () => ({ title: "Quick edit" }),+ getOpenNoteItem: () => ({ title: "Open note", items: [] }),+ handleLinkContextMenuItem: () => {}+ }}));// i18next is never initialised under test, so every label would read as undefined. The awaiteddiff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 3e5afce0f6..8f7afd46f7 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -15,7 +15,11 @@ import { openBoardContextMenu, openColumnContextMenu, openNoteContextMenu } from// The card menu opens with the shared link items, which reach for the active note context.vi.mock("../../../menus/link_context_menu", () => ({- default: { getItems: () => [], handleLinkContextMenuItem: () => {} }+ default: {+ getQuickEditItem: () => ({ title: "Quick edit" }),+ getOpenNoteItem: () => ({ title: "Open note", items: [] }),+ handleLinkContextMenuItem: () => {}+ }}));// i18next is never initialised under test, so `t` echoes the key it is given. The promise is what@@ -462,6 +466,26 @@ describe("Board column context menu", () => {describe("Board item context menu", () => {afterEach(() => vi.restoreAllMocks());+ /**+ * The two entries a card is worked on with come first; the other ways of opening it fold into+ * one submenu behind them.+ */+ it("leads with quick edit, the title editor and the open submenu", () => {+ const api = {+ columns: [],+ isColumnArchived: () => false,+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => ""+ } as unknown as BoardApi;++ const leading = openItemMenu(api).slice(0, 3);++ expect(leading.map(item => item && "title" in item ? item.title : "")).toEqual([+ "Quick edit", "board_view.edit-title", "Open note"+ ]);+ // The key that opens the same popup for the card the cursor stands on.+ expect(leading[0]).toMatchObject({ shortcut: "Space" });+ });/** The same editor F2 opens, for a reader who came to the card with the mouse. */it("opens the card's title editor", () => {@@ -525,8 +549,8 @@ describe("Board item context menu", () => {expect(focusCard).toHaveBeenCalled();});- /** The group still holds the two inserts, so the divider that heads it stays. */- it("keeps the divider while the column is arranged by hand", () => {+ /** The two inserts and the copy made below them, under the divider that heads the group. */+ it("lists the places a card is put, and the copy of it, in one group", () => {const items = openItemMenu({columns: [],isColumnArchived: () => false,@@ -535,10 +559,11 @@ describe("Board item context menu", () => {isFirstInColumn: () => true} as unknown as BoardApi);- const at = items.findIndex(item => item && "uiIcon" in item- && item.uiIcon === "bx bx-rename");+ const at = lastOfLeadingGroup(items);expect(items[at + 1]).toMatchObject({ kind: "separator" });- expect(items[at + 2]).toMatchObject({ uiIcon: "bx bx-list-plus" });+ expect(items.slice(at + 2, at + 5).map(item =>+ item && "uiIcon" in item ? item.uiIcon : undefined))+ .toEqual([ "bx bx-list-plus", "bx bx-empty", "bx bx-outline" ]);});it("says nothing about moving up the card already at the head", () => {@@ -577,12 +602,18 @@ describe("Board item context menu", () => {expect(icons).toContain("bx bx-rename");expect(icons).toContain("bx bx-outline");- // The divider goes with them: the heading below already breaks the menu there.- const at = items.findIndex(item => item && "uiIcon" in item- && item.uiIcon === "bx bx-rename");- expect(items[at + 1]).toMatchObject({ kind: "header" });+ // The copy is all the group has left, and the heading follows it.+ const at = lastOfLeadingGroup(items);+ expect(items[at + 1]).toMatchObject({ kind: "separator" });+ expect(items[at + 2]).toMatchObject({ uiIcon: "bx bx-outline" });+ expect(items[at + 3]).toMatchObject({ kind: "header" });});+ /** Where the entries a card is opened and named with end, which the next group follows. */+ function lastOfLeadingGroup(items: MenuItem<unknown>[]) {+ return items.findIndex(item => item && "title" in item && item.title === "Open note");+ }+it("copies a card into the board, after the one it was made from", async () => {const api = {columns: [ "To Do" ],diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 71b74d9a9f..351313359b 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -449,23 +449,32 @@ export function openNoteContextMenu(event.preventDefault();event.stopPropagation();- // Where a card goes among the others, which a sorted column decides for itself. Kept in a- // group of its own only while it holds something, or the menu shows a stray divider.- const placement: MenuItem<CommandNames>[] = api.isColumnSorted(column) ? [] : [- {- title: t("board_view.insert-above"),- uiIcon: "bx bx-list-plus",- shortcut: "Shift+Enter",- handler: () => onInsert(index)- },+ // A sorted column decides where its cards go, so it is offered no place to put one.+ const isSorted = api.isColumnSorted(column);++ // What the card is placed beside, and the copy made below it.+ const placement: MenuItem<CommandNames>[] = [+ ...(isSorted ? [] : [+ {+ title: t("board_view.insert-above"),+ uiIcon: "bx bx-list-plus",+ shortcut: "Shift+Enter",+ handler: () => onInsert(index)+ },+ {+ title: t("board_view.insert-below"),+ uiIcon: "bx bx-empty",+ shortcut: "Enter",+ handler: () => onInsert(index + 1)+ }+ ]),{- title: t("board_view.insert-below"),- uiIcon: "bx bx-empty",- shortcut: "Enter",- handler: () => onInsert(index + 1)+ title: t("board_view.duplicate-item"),+ uiIcon: "bx bx-outline",+ handler: () => api.duplicateItem(note.noteId, branchId)},// Left out for the card already at the head, which has nowhere to go.- ...(api.isFirstInColumn(branchId, column) ? [] : [ {+ ...(isSorted || api.isFirstInColumn(branchId, column) ? [] : [ {title: t("board_view.move-to-top"),uiIcon: "bx bx-vertical-top",shortcut: "Ctrl+Home",@@ -482,25 +491,20 @@ export function openNoteContextMenu(x: event.pageX,y: event.pageY,items: [- ...link_context_menu.getItems(event),+ // Space opens the same popup for the card the cursor stands on.+ { ...link_context_menu.getQuickEditItem(), shortcut: "Space" },{title: t("board_view.edit-title"),uiIcon: "bx bx-rename",shortcut: "F2",handler: () => api.startEditing(branchId)},- ...(placement.length- ? [ { kind: "separator" } as MenuItem<CommandNames>, ...placement ]- : []),+ link_context_menu.getOpenNoteItem(event),+ { kind: "separator" },+ ...placement,{ kind: "header", title: api.getStatusLabel() },...buildColumnItems(api, note, column, onFocusCard),{ kind: "separator" },- {- title: t("board_view.duplicate-item"),- uiIcon: "bx bx-outline",- handler: () => api.duplicateItem(note.noteId, branchId)- },- { kind: "separator" },getArchiveMenuItem(note),{title: t("board_view.remove-from-board"),diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex a7c5f07a73..e0fe97751e 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -33,7 +33,11 @@ vi.mock("../../../services/branches", () => ({// The card menu opens with the link items on top, which want a tab manager this spec has none of.vi.mock("../../../menus/link_context_menu", () => ({- default: { getItems: () => [], handleLinkContextMenuItem: vi.fn() }+ default: {+ getQuickEditItem: () => ({ title: "Quick edit" }),+ getOpenNoteItem: () => ({ title: "Open note", items: [] }),+ handleLinkContextMenuItem: vi.fn()+ }}));vi.mock("../../../services/i18n", () => ({