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 "Insert new" in card context menu for automatically sorted columns
Adorian Doranf3af6aa8efd2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1b3fe71960..26e7d23f91 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3415,6 +3415,7 @@"status-header": "Status","duplicate-item": "Duplicate","edit-title": "Edit title & icon",+ "insert-new": "Insert new","insert-above": "Insert new above","insert-below": "Insert new below","move-to-top": "Move to top",diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex c77f320c21..8326f0d08c 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -35,7 +35,8 @@ function Card({isDragging,isEditing,onFocusCard,- onInsert+ onInsert,+ onNewItem}: {api: BoardApi,note: FNote,@@ -63,7 +64,9 @@ function Card({* Opens the field a new card is named in at a place in the column, which is what makes the* card. The index is where the field stands among the cards.*/- onInsert: (index: number) => void+ onInsert: (index: number) => void,+ /** Opens the field at the foot of the column, for a card whose column sorts itself. */+ onNewItem: () => void}) {const { setBranchIdToEdit } = useContext(BoardActionsContext);const shownAttributes = useContext(BoardPromotedAttributesContext);@@ -92,8 +95,9 @@ function Card({});const handleContextMenu = useCallback((e: ContextMenuEvent) => {- openNoteContextMenu(api, e, note, branch.branchId, column, index, onFocusCard, onInsert);- }, [ api, note, branch, column, index, onFocusCard, onInsert ]);+ openNoteContextMenu(+ api, e, note, branch.branchId, column, index, onFocusCard, onInsert, onNewItem);+ }, [ api, note, branch, column, index, onFocusCard, onInsert, onNewItem ]);const handleOpen = useCallback((e: MouseEvent) => {// A double click is one gesture, and its second click would open the note over itself: thediff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 4df6ae4ed7..5060f7ae83 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -168,6 +168,8 @@ export default function Column({setInsertBefore({ branchId: itemsRef.current?.[index]?.branch.branchId });}, []);+ /** Opens the field at the foot of the column, which its button and its menu also open. */+ const beginNewItem = useCallback(() => setIsCreatingNewItem(true), []);/** The card the footer just made, which is revealed and scrolled to as it is drawn. */const [ createdNoteId, setCreatedNoteId ] = useState<string>();/**@@ -425,7 +427,7 @@ export default function Column({keepCollapsed,nested,onEditTitle: () => setColumnNameToEdit(column),- onNewItem: () => setIsCreatingNewItem(true),+ onNewItem: beginNewItem,onAddColumn: async (direction) => {setColumnNameToEdit(await api.insertColumn(column, direction));},@@ -674,6 +676,7 @@ export default function Column({isEditing={branch.branchId === branchIdToEdit}onFocusCard={onFocusCard}onInsert={beginInsert}+ onNewItem={beginNewItem}/></Fragment>))}diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 8f7afd46f7..d377da62fa 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -602,11 +602,31 @@ describe("Board item context menu", () => {expect(icons).toContain("bx bx-rename");expect(icons).toContain("bx bx-outline");- // The copy is all the group has left, and the heading follows it.+ // The field at the foot of the column and the copy are what the group has left.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" });+ expect(items[at + 2]).toMatchObject({ title: "board_view.insert-new" });+ expect(items[at + 3]).toMatchObject({ uiIcon: "bx bx-outline" });+ expect(items[at + 4]).toMatchObject({ kind: "header" });+ });++ /** The column names its own cards at the foot, so that is where the menu sends the reader. */+ it("opens the field at the foot of a sorted column", () => {+ const api = {+ columns: [],+ isColumnArchived: () => false,+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => "",+ isColumnSorted: () => true+ } as unknown as BoardApi;+ const newItem = vi.fn();++ const entry = openItemMenu(api, "To Do", vi.fn(), vi.fn(), 2, newItem)+ .find(item => item && "title" in item && item.title === "board_view.insert-new");+ if (!entry || !("handler" in entry)) throw new Error("expected an insert-new entry");++ entry.handler?.(entry, {} as never);+ expect(newItem).toHaveBeenCalled();});/** Where the entries a card is opened and named with end, which the next group follows. */@@ -704,7 +724,8 @@ describe("Board item context menu", () => {/** 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(), insert = vi.fn(), index = 2) {+ api: BoardApi, column = "To Do", focusCard = vi.fn(), insert = vi.fn(), index = 2,+ newItem = vi.fn()) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});const event = {preventDefault: () => {},@@ -725,7 +746,7 @@ describe("Board item context menu", () => {api);openNoteContextMenu(withDefaults, event, buildNote({ title: "Card" }) as FNote, "branchId", column, index,- focusCard, insert);+ focusCard, insert, newItem);return show.mock.calls.at(-1)?.[0].items ?? [];}diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 351313359b..2ed12d49d6 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -444,17 +444,24 @@ export function openNoteContextMenu(/** Refocuses the card after a column change has redrawn it elsewhere. */onFocusCard: (noteId: string) => void,/** Opens the new-card editor at an index in the column, above or below this card. */- onInsert: (index: number) => void+ onInsert: (index: number) => void,+ /** Opens the editor at the foot of the column, which a sorted column offers instead. */+ onNewItem: () => void) {event.preventDefault();event.stopPropagation();- // A sorted column decides where its cards go, so it is offered no place to put one.+ // A sorted column decides where its cards go, so the entries naming a place are left out.const isSorted = api.isColumnSorted(column);// What the card is placed beside, and the copy made below it.const placement: MenuItem<CommandNames>[] = [- ...(isSorted ? [] : [+ // A sorted column takes its new cards at the foot, where its own button makes them.+ ...(isSorted ? [ {+ title: t("board_view.insert-new"),+ uiIcon: "bx bx-plus",+ handler: onNewItem+ } ] : [{title: t("board_view.insert-above"),uiIcon: "bx bx-list-plus",