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/card context menu: group excess states in a submenu
Adorian Doran422af6e3c6a3
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex e91f536bcb..40f7225a14 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3376,6 +3376,7 @@"insert-above": "Insert new above","insert-below": "Insert new below","move-to-top": "Move to top",+ "more-columns": "More states…","delete-column": "Delete column...","archive-column": "Archive column","unarchive-column": "Unarchive column",diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 4c8f82f54e..f954748eb3 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -2,7 +2,8 @@ import { h, render } from "preact";import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";-import contextMenu, { ContextMenuEvent } from "../../../menus/context_menu";+import type { CommandNames } from "../../../components/app_context";+import contextMenu, { ContextMenuEvent, MenuItem } from "../../../menus/context_menu";import branches from "../../../services/branches";import dialog from "../../../services/dialog";import FNote from "../../../entities/fnote";@@ -506,6 +507,50 @@ describe("Board item context menu", () => {expect(deleteNotes).toHaveBeenCalledWith([ "branchId" ], false, false);});+ /**+ * A board can hold more columns than a menu has screen to stand in, so what does not fit goes+ * behind one entry. The card's own column is listed whatever its place, since the check beside+ * it is what says where the card stands.+ */+ it("lists seven columns and puts the rest behind an entry of their own", () => {+ const columns = [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" ];+ const api = {+ columns,+ isColumnArchived: () => false,+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => "",+ changeColumn: vi.fn()+ } as unknown as BoardApi;++ const listed = statusItems(api, "One");+ expect(names(listed.slice(0, -1))).toEqual(columns.slice(0, 7));++ // Named by its icon: `t()` answers with nothing here, i18next never being initialised.+ const more = listed.at(-1);+ if (!more || !("items" in more) || !more.items) throw new Error("expected a more entry");+ expect("uiIcon" in more && more.uiIcon).toBe("bx bx-dots-horizontal-rounded");+ expect(names(more.items)).toEqual([ "Eight", "Nine" ]);++ // The card is in the ninth column, which stands in the menu itself along with the first+ // seven, and is gone from what the entry holds.+ const withNinth = statusItems(api, "Nine");+ expect(names(withNinth.slice(0, -1))).toEqual([ ...columns.slice(0, 7), "Nine" ]);+ const rest = withNinth.at(-1);+ if (!rest || !("items" in rest) || !rest.items) throw new Error("expected a more entry");+ expect(names(rest.items)).toEqual([ "Eight" ]);++ // Seven of them fit as they are.+ expect(names(statusItems({ ...api, columns: columns.slice(0, 7) } as BoardApi, "One")))+ .toEqual(columns.slice(0, 7));+ });++ /** The names the menu shows, each boxed in a span the stylesheet sizes. */+ function names(items: MenuItem<unknown>[]) {+ return items.flatMap(item => item && "title" in item && item.title+ ? [ item.title.replace(/<[^>]*>/g, "") ]+ : []);+ }+/** Opens the menu a card offers, and hands back what it was given to show. */function openItemMenu(api: BoardApi, column = "To Do", focusCard = vi.fn()) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 74a5d98ed8..8081022467 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -165,6 +165,9 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:});}+/** How many columns a card's menu lists before the rest go behind an entry of their own. */+const LISTED_COLUMNS = 7;+/** What the board itself offers, for a press on the ground the columns stand on. */export function openBoardContextMenu(event: ContextMenuEvent,@@ -308,6 +311,8 @@ function ColumnColorPicker({ api, value, color }: { api: Api, value: string, col/*** The columns a card can be filed under, standing in the menu itself rather than behind a submenu:* moving a card is what a board is for, and a submenu puts every column a step further away.+ * Past `LISTED_COLUMNS` of them the menu would run off the screen, so what does not fit goes behind+ * one entry, and the column the card is in is listed whether or not it is among the first.** Archived columns are offered like any other, since filing a card under one is a fair thing to* want; the badge is there so it is not a surprise when the card goes out of sight.@@ -315,7 +320,7 @@ function ColumnColorPicker({ api, value, color }: { api: Api, value: string, colfunction buildColumnItems(api: Api, note: FNote, column: string, onFocusCard: (noteId: string) => void): MenuItem<CommandNames>[] {- return api.columns.map((name) => ({+ const items: MenuItem<CommandNames>[] = api.columns.map((name) => ({// The menu reads a title as markup, which is what puts the name in a box of its own: a// bare run of text inside the item's flex row is an anonymous box, and nothing can be said// about its width. What a crafted name would plant there is escaped into the text it is@@ -338,6 +343,29 @@ function buildColumnItems(api.changeColumn(note.noteId, name);}}));++ if (items.length <= LISTED_COLUMNS) {+ return items;+ }++ const listed = items.slice(0, LISTED_COLUMNS);+ const rest = items.slice(LISTED_COLUMNS);++ // The card's own column is listed even where it falls outside: the check beside it is what+ // says which column the card is in, and behind an entry it says nothing.+ const current = api.columns.indexOf(column);+ if (current >= LISTED_COLUMNS) {+ listed.push(...rest.splice(current - LISTED_COLUMNS, 1));+ }++ return [+ ...listed,+ {+ title: t("board_view.more-columns"),+ uiIcon: "bx bx-dots-horizontal-rounded",+ items: rest+ }+ ];}export function openNoteContextMenu(