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: allow toggling card selection by keyboard
Adorian Dorancd0e5922c47f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 5b0303e305..2622932128 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3557,7 +3557,8 @@"selection": "Selection","select_column": "Select every card of the column","clear_selection": "Clear the selection",- "extend_selection": "Extend the selection down / up"+ "extend_selection": "Extend the selection down / up",+ "toggle_selection": "Pick the card out or put it back"},"status-alias": "Status","filter-placeholder": "Filter board...",diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 8eb17f0d5d..a64a50e9f1 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -1300,6 +1300,7 @@ describe("Board column rename", () => {keys: [ "Ctrl+Alt+Home", "Ctrl+Alt+End" ],labelKey: "board_view.hints.move_column_to_edge"},+ { keys: [ "Ctrl+Space" ], labelKey: "board_view.hints.toggle_selection" },{keys: [ "Shift+Down", "Shift+Up" ],labelKey: "board_view.hints.extend_selection"diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex a0b25cfea2..ea76a13a3e 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -341,6 +341,7 @@ const BOARD_HINTS: ShortcutHintDefinition = [{titleKey: "board_view.hints.selection",hints: [+ { keys: [ "Ctrl+Space" ], labelKey: "board_view.hints.toggle_selection" },{keys: [ "Shift+Down", "Shift+Up" ],labelKey: "board_view.hints.extend_selection"diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex 720e3ed79a..86d6e9ff99 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -529,7 +529,7 @@ describe("Board keyboard", () => {});});- describe("picking out a range with the arrows", () => {+ describe("picking cards out with the keyboard", () => {it("grows the range on its way down the column and shrinks it on the way back", async () => {const board = await renderBoard(undefined, undefined, [ "To Do" ]);focusCard(board, 0, 0);@@ -557,6 +557,25 @@ describe("Board keyboard", () => {expect(pickedNames(board)).toEqual([ "First" ]);});+ it("picks the focused card out with Ctrl and Space, and steps onto the next", async () => {+ const board = await renderBoard(undefined, undefined, [ "To Do" ]);+ focusCard(board, 0, 0);++ expect(press(board, " ", { ctrlKey: true })).toBe("Second");+ expect(pickedNames(board)).toEqual([ "First" ]);++ expect(press(board, " ", { ctrlKey: true })).toBe("Fourth");+ expect(pickedNames(board)).toEqual([ "First", "Second" ]);++ // The last card of the column keeps focus: the button under it holds no card.+ expect(press(board, " ", { ctrlKey: true })).toBe("Fourth");+ expect(pickedNames(board)).toEqual([ "First", "Second", "Fourth" ]);++ // A second press on the same card puts it back.+ expect(press(board, " ", { ctrlKey: true })).toBe("Fourth");+ expect(pickedNames(board)).toEqual([ "First", "Second" ]);+ });+/*** A range is measured against one column, so an anchor left in another names no place in* the list the press is counted against.diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex b4288d2286..0ccdb7bb88 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -222,6 +222,21 @@ export function useBoardKeyboard({return;}+ // The focused card, picked out or put back, with focus going on to the card below it+ // so a run of presses picks out several without an arrow between them.+ if (e.key === " " && !e.altKey && spot.kind === "item") {+ take(e);++ const noteId = itemAt(columns, byColumn, spot)?.note.noteId;+ if (noteId === undefined) return;++ selection.toggle(noteId);+ if (nextCard(container, spot, "ArrowDown")) {+ lastSpot.current = walk(container, spot, "ArrowDown") ?? spot;+ }+ return;+ }+// A column beside the one focus is in, wherever inside it focus sits. The button that// adds a column stands beside none, and is the plain way to add one at the end anyway.if (e.key === "Enter" && !e.altKey && spot.kind !== "add-column") {@@ -294,9 +309,11 @@ export function useBoardKeyboard({// Taken whether or not it leads anywhere: a card at the end of its column is no reason// to let the key scroll the board instead.take(e);- if (extendSelection(container, spot, e.key, { columns, api, selection })) {- lastSpot.current = walk(container, spot, e.key) ?? spot;- }+ const next = nextCard(container, spot, e.key);+ if (!next) return;++ extendSelection(spot, next, { columns, api, selection });+ lastSpot.current = walk(container, spot, e.key) ?? spot;return;}@@ -481,36 +498,37 @@ function spotOf(container: HTMLElement, element: Element | null): Spot | null {}/**- * Takes the selection one card along the column from the card focus is on, answering with whether- * it reached one. Focus follows only where it did, so the card at the end of a column keeps it- * rather than the press stepping onto the header or the button under the cards.+ * The card one step along the column, or null where the step reaches the header above the cards or+ * the button below them. Neither holds a card, so a selection key stops there rather than carrying+ * focus off the cards.+ */+function nextCard(container: HTMLElement, spot: Extract<Spot, { kind: "item" }>, key: string) {+ const next = destination(container, spot, key);+ return next?.kind === "item" ? next : null;+}++/**+ * Takes the selection from the card focus is on to the card a step along the column.** The anchor stays where the range began, so a press the other way shrinks the same range rather* than starting a new one. A range that is not already running through this column begins at the* focused card, which the press then takes along with the card it steps onto.*/function extendSelection(- container: HTMLElement,spot: Extract<Spot, { kind: "item" }>,- key: string,+ next: Extract<Spot, { kind: "item" }>,{ columns, api, selection }: Pick<BoardKeyboardOptions, "columns" | "api" | "selection">) {- const next = destination(container, spot, key);- // Up from the first card reaches the header and down from the last the button under them.- // Neither holds a card, so there is nothing to extend onto.- if (next?.kind !== "item") return false;-const ordered = api.getColumnNoteIds(columns[spot.column]);const from = ordered[spot.item];const to = ordered[next.item];- if (from === undefined || to === undefined) return false;+ if (from === undefined || to === undefined) return;if (selection.anchor === null || !ordered.includes(selection.anchor)) {selection.selectOnly(from);}selection.selectRange(ordered, to);- return true;}/**