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 shortcut to insert items, update the keyboard shortcuts
Adorian Doran90dbd789840f
diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsxindex 5eb6f6fbe6..85930c399a 100644--- a/apps/client/src/widgets/collections/board/card.spec.tsx+++ b/apps/client/src/widgets/collections/board/card.spec.tsx@@ -17,6 +17,7 @@ import server from "../../../services/server";import utils from "../../../services/utils";import { buildNote } from "../../../test/easy-froca";import { ParentComponent } from "../../react/react_utils";+import BoardApi from "./api";import BoardView from ".";// The card menu opens with the shared link items, which reach for the active note context.@@ -63,16 +64,31 @@ describe("Board card", () => {expect(cardIcon(first)).toContain("bx-bug");});- it("opens the note it stands for, by click and by Enter", async () => {+ it("opens the note it stands for when clicked", async () => {const { first } = await renderBoard();const openInPopup = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);await act(async () => { card(first).click(); });+expect(openInPopup).toHaveBeenCalledWith("openInPopup", { noteIdOrPath: first });+ });++ /** Enter adds a card where a spreadsheet would add a row, and Shift puts it above instead. */+ it("adds a card under the focused one for Enter, and over it for Shift and Enter", async () => {+ const insert = vi.spyOn(BoardApi.prototype, "insertRowAtPosition")+ .mockResolvedValue(undefined as never);+ const openInPopup = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);+ const { first } = await renderBoard();await act(async () => { press(card(first), "Enter"); });- expect(openInPopup).toHaveBeenCalledTimes(2);+ expect(insert).toHaveBeenLastCalledWith("To Do", expect.any(String), "after");++ await act(async () => { press(card(first), "Enter", { shiftKey: true }); });+ expect(insert).toHaveBeenLastCalledWith("To Do", expect.any(String), "before");++ // The card it was pressed on stays where it is; Space is what opens one.+ expect(openInPopup).not.toHaveBeenCalled();});it("opens the note once for a double click, not once per click of it", async () => {@@ -144,8 +160,8 @@ describe("Board card", () => {});/** Presses a key on a card, which listens for them to open and to rename. */- function press(target: HTMLElement, key: string) {- target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));+ function press(target: HTMLElement, key: string, init: KeyboardEventInit = {}) {+ target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, ...init }));}/**diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 7076c7efad..8b6b174389 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -88,11 +88,15 @@ function Card({const handleKeyDown = useCallback((e: KeyboardEvent) => {if (e.key === "Enter") {- api.openNote(note.noteId);+ // Enter makes a card rather than opening one, the way a row is made in a spreadsheet;+ // Space is what opens. Shift puts the new card above, the reversal Shift stands for+ // wherever else it pairs with a key.+ e.preventDefault();+ api.insertRowAtPosition(column, branch.branchId, e.shiftKey ? "before" : "after");} else if (e.key === "F2") {setBranchIdToEdit(branch.branchId);}- }, [ setBranchIdToEdit, note ]);+ }, [ api, column, branch, setBranchIdToEdit ]);useEffect(() => {editorRef.current?.focus();diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex c7cd701142..db5a16cce5 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -67,7 +67,7 @@ export interface BoardKeyboardOptions {** One handler on the board rather than one per element, because every key here is about the board* as a whole: where the next thing is, and where what is focused should go. The per-element- * handlers keep what is theirs (F2 to rename, Enter to open, typing to start a card).+ * handlers keep what is theirs (F2 to rename, Enter to add a card, typing to start one).** Focus follows what is under it rather than where it sits: a card moved to another column is* drawn as a new element, and columns are drawn unkeyed, so a header would otherwise be left