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 selecting a range of cards by keyboard
Adorian Doran86ea483f9ab5
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex bd0e03b452..5b0303e305 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3556,7 +3556,8 @@"move_column_to_edge": "Move the column to the far left / right","selection": "Selection","select_column": "Select every card of the column",- "clear_selection": "Clear the selection"+ "clear_selection": "Clear the selection",+ "extend_selection": "Extend the selection down / up"},"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 fb322392d3..8eb17f0d5d 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -1300,6 +1300,10 @@ describe("Board column rename", () => {keys: [ "Ctrl+Alt+Home", "Ctrl+Alt+End" ],labelKey: "board_view.hints.move_column_to_edge"},+ {+ keys: [ "Shift+Down", "Shift+Up" ],+ labelKey: "board_view.hints.extend_selection"+ },{ keys: [ "Ctrl+A" ], labelKey: "board_view.hints.select_column" },{ keys: [ "Escape" ], labelKey: "board_view.hints.clear_selection" }]);diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 64c466cbee..a0b25cfea2 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -341,6 +341,10 @@ const BOARD_HINTS: ShortcutHintDefinition = [{titleKey: "board_view.hints.selection",hints: [+ {+ keys: [ "Shift+Down", "Shift+Up" ],+ labelKey: "board_view.hints.extend_selection"+ },{ keys: [ "Ctrl+A" ], labelKey: "board_view.hints.select_column" },{ keys: [ "Escape" ], labelKey: "board_view.hints.clear_selection" }]diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex 15b12ff8f8..720e3ed79a 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -529,6 +529,48 @@ describe("Board keyboard", () => {});});+ describe("picking out a range with the arrows", () => {+ 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);++ expect(press(board, "ArrowDown", { shiftKey: true })).toBe("Second");+ expect(pickedNames(board)).toEqual([ "First", "Second" ]);++ expect(press(board, "ArrowDown", { shiftKey: true })).toBe("Fourth");+ expect(pickedNames(board)).toEqual([ "First", "Second", "Fourth" ]);++ // The button under the cards holds none, so the press stops on the last of them.+ expect(press(board, "ArrowDown", { shiftKey: true })).toBe("Fourth");+ expect(pickedNames(board)).toEqual([ "First", "Second", "Fourth" ]);++ // The anchor stays on the card the range began at, so the way back shrinks that same+ // range rather than opening a new one downwards.+ expect(press(board, "ArrowUp", { shiftKey: true })).toBe("Second");+ expect(pickedNames(board)).toEqual([ "First", "Second" ]);++ expect(press(board, "ArrowUp", { shiftKey: true })).toBe("First");+ expect(pickedNames(board)).toEqual([ "First" ]);++ // As does the header above them.+ expect(press(board, "ArrowUp", { shiftKey: true })).toBe("First");+ expect(pickedNames(board)).toEqual([ "First" ]);+ });++ /**+ * A range is measured against one column, so an anchor left in another names no place in+ * the list the press is counted against.+ */+ it("begins a range at the focused card when the one picked out stands elsewhere", async () => {+ const board = await renderBoard();+ pick(board, 1, 0);+ focusCard(board, 0, 0);++ expect(press(board, "ArrowDown", { shiftKey: true })).toBe("Second");+ expect(pickedNames(board)).toEqual([ "First", "Second" ]);+ });+ });+describe("moving what is focused", () => {/*** A collapsed column draws no cards, so the card carried into it would have nothing to bediff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex 705d4af153..b4288d2286 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -21,6 +21,9 @@ const ITEM_MOVE_KEYS = [ "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Hom/** With Ctrl and Alt, these carry the column focus is in, from wherever inside it focus sits. */const COLUMN_MOVE_KEYS = [ "ArrowLeft", "ArrowRight", "Home", "End" ];+/** With Shift, these take a range of cards along a column. */+const RANGE_KEYS = [ "ArrowUp", "ArrowDown" ];+/*** A place on the board that can hold focus.*@@ -281,6 +284,22 @@ export function useBoardKeyboard({// focus: it would otherwise pull it back out of whatever the key opened.pendingFocus.current = null;+ // Shift with the arrows takes the selection one card further along the column, or one card+ // back towards where the range began, and carries focus with it.+ if (e.shiftKey && RANGE_KEYS.includes(e.key)) {+ // A header and the button under a column hold no card, so a range starts at neither.+ // The press is left to the browser there, as it was before the board answered for it.+ if (spot.kind !== "item") return;++ // 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;+ }+ return;+ }+if (NAVIGATION_KEYS.includes(e.key)) {// The plain arrows would otherwise scroll the page past the end of a column.take(e);@@ -429,6 +448,11 @@ function shiftIsOurs(e: KeyboardEvent) {return true;}+ // Shift with the up and down arrows takes a range of cards along a column.+ if (!e.ctrlKey && !e.altKey && RANGE_KEYS.includes(e.key)) {+ return true;+ }+return e.ctrlKey && CARD_END_KEYS.includes(e.key);}@@ -456,6 +480,39 @@ function spotOf(container: HTMLElement, element: Element | null): Spot | null {return { kind: "item", column, item: indexOfCard(card as HTMLElement, columnElement) };}+/**+ * 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 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,+ { 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 (selection.anchor === null || !ordered.includes(selection.anchor)) {+ selection.selectOnly(from);+ }++ selection.selectRange(ordered, to);+ return true;+}+/*** Moves focus, answering with whether the key led anywhere.*