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: add "Move to top" to the card context menu
Adorian Dorance6e2d808376
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 98ca1fec6e..ac63e01abe 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3375,6 +3375,7 @@"edit-title": "Edit title","insert-above": "Insert above","insert-below": "Insert below",+ "move-to-top": "Move to top","delete-column": "Delete column...","archive-column": "Archive column","unarchive-column": "Unarchive column",diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 9ab3bb654c..85a4ddfd8e 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -720,6 +720,20 @@ describe("BoardApi card operations", () => {expect.not.objectContaining({ target: "before" }));});+ it("sends a card to the head of its column, and leaves one already there alone", async () => {+ const { api, items } = createBoardWithCards();+ const moveBefore = vi.spyOn(branches, "moveBeforeBranch").mockResolvedValue(undefined);++ const last = items[items.length - 1];+ await api.moveToColumnStart(last.note.noteId, last.branch.branchId, "Done");+ expect(moveBefore).toHaveBeenCalledWith(+ [ last.branch.branchId ], items[0].branch.branchId);++ moveBefore.mockClear();+ await api.moveToColumnStart(items[0].note.noteId, items[0].branch.branchId, "Done");+ expect(moveBefore).not.toHaveBeenCalled();+ });+it("hands the editing state straight through to the board", () => {const { api, editing } = createBoardWithCards();diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 2072ed0ecb..9f3818c57a 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -891,6 +891,27 @@ export default class BoardApi {this.sentToColumnEnd.set(targetColumn, branchId);}+ /** Whether a card stands at the head of its column, with nowhere left to be moved up to. */+ isFirstInColumn(branchId: string, column: string) {+ return this.byColumn?.get(column)?.[0]?.branch.branchId === branchId;+ }++ /**+ * Moves a card to the head of the column it stands in, where Ctrl+Home also sends it.+ *+ * The card's own place is looked up here rather than asked of the caller: the menu is opened on+ * a card, which knows the column it is in but not where it stands among the others.+ */+ async moveToColumnStart(noteId: string, branchId: string, column: string) {+ const items = this.byColumn?.get(column) ?? [];+ const at = items.findIndex(item => item.branch.branchId === branchId);+ if (at <= 0) {+ return;+ }++ await this.moveWithinBoard(noteId, branchId, at, 0, column, column);+ }+async moveWithinBoard(noteId: string, sourceBranchId: string, sourceIndex: number, targetIndex: number, sourceColumn: string, targetColumn: string) {const targetItems = this.byColumn?.get(targetColumn) ?? [];diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 4b08802dbc..5ab8aff0d6 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -416,6 +416,41 @@ describe("Board item context menu", () => {.toEqual([ "before", "after" ]);});+ /** Where Ctrl+Home sends it, for a reader who reached the card with the mouse. */+ it("sends a card to the head of its column, and keeps the focus on it", () => {+ const api = {+ columns: [],+ isColumnArchived: () => false,+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => "",+ moveToColumnStart: vi.fn(async () => {})+ } as unknown as BoardApi;+ const focusCard = vi.fn();++ const entry = openItemMenu(api, "To Do", focusCard).find(item =>+ item && "uiIcon" in item && item.uiIcon === "bx bx-vertical-top");+ if (!entry || !("handler" in entry)) throw new Error("expected a move-to-top entry");+ expect("shortcut" in entry && entry.shortcut).toBe("Ctrl+Home");++ entry.handler?.(entry, {} as never);+ expect(api.moveToColumnStart).toHaveBeenCalledWith(+ expect.any(String), "branchId", "To Do");+ expect(focusCard).toHaveBeenCalled();+ });++ it("says nothing about moving up the card already at the head", () => {+ const api = {+ columns: [],+ isColumnArchived: () => false,+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => "",+ isFirstInColumn: () => true+ } as unknown as BoardApi;++ expect(openItemMenu(api).some(item =>+ item && "uiIcon" in item && item.uiIcon === "bx bx-vertical-top")).toBe(false);+ });+it("copies a card into the board, after the one it was made from", async () => {const api = {columns: [ "To Do" ],@@ -468,7 +503,12 @@ describe("Board item context menu", () => {// Every item menu asks what the board calls its grouping field; a test says so only when// that is what it is about.const withDefaults = Object.assign(- { getStatusLabel: () => "Status", getColumnTitle: (name: string) => name }, api);+ {+ getStatusLabel: () => "Status",+ getColumnTitle: (name: string) => name,+ isFirstInColumn: () => false+ },+ api);openNoteContextMenu(withDefaults, event, buildNote({ title: "Card" }) as FNote, "branchId", column,focusCard, () => {});diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 5dec082754..6098072588 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -336,6 +336,18 @@ export function openNoteContextMenu(handler: () => api.insertRowAtPosition(column, branchId, "after").then(created => onCreated(created?.noteId))},+ // Left out for the card already at the head, which has nowhere to go.+ ...(api.isFirstInColumn(branchId, column) ? [] : [ {+ title: t("board_view.move-to-top"),+ uiIcon: "bx bx-vertical-top",+ shortcut: "Ctrl+Home",+ handler: () => {+ // Asked for before the write: the card is blurred as it is moved in the page,+ // and the reveal that follows the focus is what shows where it went.+ onFocusCard(note.noteId);+ api.moveToColumnStart(note.noteId, branchId, column);+ }+ } ]),{ kind: "header", title: api.getStatusLabel() },...buildColumnItems(api, note, column, onFocusCard),{ kind: "separator" },