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 a command in column menu to create a new item
Adorian Doran3e0b512ae19d
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 76554fb810..1fd7243ab8 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -56,6 +56,7 @@ export default function Column({isInRelationMode: boolean} & DragContext) {const [ isVisible, setVisible ] = useState(true);+ const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);const { setColumnNameToEdit } = useContext(BoardActionsContext);const { branchIdToEdit, columnNameToEdit, dropTarget, draggedCard, dropPosition } = useContext(BoardDragStateContext);const isEditing = (columnNameToEdit === column);@@ -69,7 +70,8 @@ export default function Column({value: column,color,archived,- onEditTitle: () => setColumnNameToEdit(column)+ onEditTitle: () => setColumnNameToEdit(column),+ onNewNote: () => setIsCreatingNewItem(true)});}, [ api, column, color, archived, setColumnNameToEdit ]);@@ -206,29 +208,42 @@ export default function Column({<div className="board-drop-placeholder show" />)}- <AddNewItem api={api} column={column} />+ <AddNewItem+ api={api}+ column={column}+ isCreating={isCreatingNewItem}+ setIsCreating={setIsCreatingNewItem}+ /></div></div>);}-function AddNewItem({ column, api }: { column: string, api: BoardApi }) {- const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);- const addItemCallback = useCallback(() => setIsCreatingNewItem(true), []);+/**+ * The editor a new card is named in, opened by the button below the column or by its menu. The+ * state is the column's rather than this component's, since the menu is raised from the header.+ */+function AddNewItem({ column, api, isCreating, setIsCreating }: {+ column: string,+ api: BoardApi,+ isCreating: boolean,+ setIsCreating: (isCreating: boolean) => void+}) {+ const addItemCallback = useCallback(() => setIsCreating(true), [ setIsCreating ]);const handleKeyDown = useCallback((e: KeyboardEvent) => {- if (!isCreatingNewItem && e.key === "Enter") {- setIsCreatingNewItem(true);+ if (!isCreating && e.key === "Enter") {+ setIsCreating(true);}- }, []);+ }, [ isCreating, setIsCreating ]);return (<div- className={`board-new-item ${isCreatingNewItem ? "editing" : ""}`}+ className={`board-new-item ${isCreating ? "editing" : ""}`}onClick={addItemCallback}onKeyDown={handleKeyDown}tabIndex={300}>- {!isCreatingNewItem ? (+ {!isCreating ? (<><Icon icon="bx bx-plus" />{" "}{t("board_view.new-item")}@@ -237,7 +252,7 @@ function AddNewItem({ column, api }: { column: string, api: BoardApi }) {<TitleEditorplaceholder={t("board_view.new-item-placeholder")}save={(title) => api.createNewItem(column, title)}- dismiss={() => setIsCreatingNewItem(false)}+ dismiss={() => setIsCreating(false)}mode="multiline" isNewItem/>)}diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex f2846b7b56..7cbd632669 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -21,7 +21,7 @@ describe("Board column context menu", () => {function openMenu(api: BoardApi,column: { color?: string, archived?: boolean } = {},- onEditTitle = () => {}+ callbacks: { onEditTitle?: () => void, onNewNote?: () => void } = {}) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});const event = {@@ -31,7 +31,12 @@ describe("Board column context menu", () => {pageY: 0} as ContextMenuEvent;- openColumnContextMenu(api, event, { value: "To Do", ...column, onEditTitle });+ openColumnContextMenu(api, event, {+ value: "To Do",+ ...column,+ onEditTitle: callbacks.onEditTitle ?? (() => {}),+ onNewNote: callbacks.onNewNote ?? (() => {})+ });// The spy outlives one call, so it is the menu just opened that is read back.return show.mock.calls.at(-1)?.[0].items ?? [];@@ -58,7 +63,7 @@ describe("Board column context menu", () => {it("offers the title editor, which the retired header button used to open", () => {const onEditTitle = vi.fn();- const items = openMenu({} as BoardApi, {}, onEditTitle);+ const items = openMenu({} as BoardApi, {}, { onEditTitle });// Found by its icon: i18next is never initialised under test, so every title is undefined.const entry = items.find(item =>@@ -85,10 +90,20 @@ describe("Board column context menu", () => {expect(api.setColumnArchived).toHaveBeenLastCalledWith("To Do", false);});- it("puts archiving above deleting, so the safe way out leads", () => {+ it("orders the entries, keeping the safe way out ahead of deleting", () => {const titled = openMenu({} as BoardApi).filter(item => item && "uiIcon" in item);expect(titled.map(item => "uiIcon" in item ? item.uiIcon : undefined))- .toEqual([ "bx bx-edit-alt", "bx bx-archive", "bx bx-trash" ]);+ .toEqual([ "bx bx-edit-alt", "bx bx-plus", "bx bx-archive", "bx bx-trash" ]);+ });++ it("opens the column's new-item editor, the same one its button opens", () => {+ const onNewNote = vi.fn();+ const entry = openMenu({} as BoardApi, {}, { onNewNote })+ .find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-plus");+ if (!entry || !("handler" in entry)) throw new Error("expected a new-item entry");++ entry.handler?.(entry, {} as never);+ expect(onNewNote).toHaveBeenCalled();});it("shows the column's own colour as the selected one", async () => {diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 0ef674528c..702285e2c2 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -17,6 +17,8 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:archived?: boolean;/** Puts the title into its inline editor, the menu being the only way there besides F2. */onEditTitle: () => void;+ /** Opens the column's own new-item editor, the same one its button opens. */+ onNewNote: () => void;}) {event.preventDefault();event.stopPropagation();@@ -30,6 +32,13 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:uiIcon: "bx bx-edit-alt",handler: column.onEditTitle},+ { kind: "separator" },+ {+ title: t("board_view.new-item"),+ uiIcon: "bx bx-plus",+ handler: column.onNewNote+ },+ { kind: "separator" },column.archived? {title: t("board_view.unarchive-column"),diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex e51d2cf3b3..dfe8c1d319 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -8,6 +8,7 @@ import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";import Component from "../../../components/component";+import contextMenu from "../../../menus/context_menu";import froca from "../../../services/froca";import { buildNote } from "../../../test/easy-froca";import { ParentComponent } from "../../react/react_utils";@@ -348,6 +349,32 @@ describe("Board column rename", () => {.toEqual([ false, true, false ]);});+ /**+ * The editor belongs to the column rather than to the button below it, so the header's menu+ * can raise the same one instead of a second way of making a card.+ */+ it("opens the new-item editor from the menu, as the button below the column does", async () => {+ const { container } = await setup();+ const column = container.querySelectorAll<HTMLElement>(".board-column")[1];+ expect(column.querySelector(".board-new-item textarea")).toBeNull();++ // Raised the way the header raises it, then the entry is invoked as the menu would.+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});+ column.querySelector("h3")?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));++ const entry = (show.mock.calls.at(-1)?.[0].items ?? [])+ .find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-plus");+ if (!entry || !("handler" in entry)) throw new Error("expected a new-item entry");++ await act(async () => {+ entry.handler?.(entry, {} as never);+ await flush();+ });+ expect(column.querySelector(".board-new-item.editing textarea")).toBeTruthy();++ show.mockRestore();+ });+it("keeps the cards of the renamed column under it", async () => {const { container } = await setup();