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 keyboard shortcut to insert columns
Adorian Doraneb378fd8f6ea
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 60813677ac..16001b35a1 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3382,6 +3382,7 @@"first_last_item": "First / last item of the column","editing": "Editing","insert_item": "Insert item below / above",+ "insert_column": "Insert column after / before","open_item": "Open item","rename": "Rename item or column","moving": "Moving",diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 8b6b174389..ed81e10f3c 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -87,7 +87,7 @@ function Card({}, [ setBranchIdToEdit, branch ]);const handleKeyDown = useCallback((e: KeyboardEvent) => {- if (e.key === "Enter") {+ if (e.key === "Enter" && !e.ctrlKey) {// 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.diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 310b86203e..f809635904 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -267,7 +267,7 @@ function AddNewItem({ column, api, itemCount, isCreating, setIsCreating }: {const handleKeyDown = useCallback((e: KeyboardEvent) => {if (isCreating) return;- if (e.key === "Enter") {+ if (e.key === "Enter" && !e.ctrlKey) {open("");return;}diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex d5c5e15b09..36c5b06607 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -317,6 +317,10 @@ describe("Board column rename", () => {{ keys: [ "Left", "Right" ], labelKey: "board_view.hints.navigate_columns" },{ keys: [ "Home", "End" ], labelKey: "board_view.hints.first_last_item" },{ keys: [ "Enter", "Shift+Enter" ], labelKey: "board_view.hints.insert_item" },+ {+ keys: [ "Ctrl+Enter", "Ctrl+Shift+Enter" ],+ labelKey: "board_view.hints.insert_column"+ },{ keys: [ "Space" ], labelKey: "board_view.hints.open_item" },{ keys: [ "F2" ], labelKey: "board_view.hints.rename" },{ keys: [ "Ctrl+Up", "Ctrl+Down" ], labelKey: "board_view.hints.move_item" },diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 35d227002c..3653036eb0 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -127,6 +127,10 @@ const BOARD_HINTS: ShortcutHintDefinition = [titleKey: "board_view.hints.editing",hints: [{ keys: [ "Enter", "Shift+Enter" ], labelKey: "board_view.hints.insert_item" },+ {+ keys: [ "Ctrl+Enter", "Ctrl+Shift+Enter" ],+ labelKey: "board_view.hints.insert_column"+ },{ keys: [ "Space" ], labelKey: "board_view.hints.open_item" },{ keys: [ "F2" ], labelKey: "board_view.hints.rename" }]@@ -303,7 +307,10 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveCcolumns: shownColumns,byColumn,api,- moveColumn: handleColumnDrop+ moveColumn: handleColumnDrop,+ insertColumn: useCallback(async (relativeTo: string, direction: "before" | "after") => {+ setColumnNameToEdit(await api.insertColumn(relativeTo, direction));+ }, [ api ])});useTriliumEvent("entitiesReloaded", ({ loadResults }) => {diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex 2789927362..e7a0804cf6 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -404,6 +404,24 @@ describe("Board keyboard", () => {expect(focusedName(board)).toBe("To Do");});+ it("puts a new column after the one focus is in, and before it with Shift", async () => {+ const board = await renderBoard();++ focusCard(board, 1, 0);+ press(board, "Enter", { ctrlKey: true });+ await act(async () => { await flush(); });+ expect(saved.at(-1)?.columns?.map(column => column.value))+ .toEqual([ "To Do", "Doing", "board_view.new-column", "Done" ]);++ focusHeader(board, 0);+ press(board, "Enter", { ctrlKey: true, shiftKey: true });+ await act(async () => { await flush(); });+ expect(saved.at(-1)?.columns?.map(column => column.value))+ .toEqual([+ "board_view.new-column 2", "To Do", "Doing", "board_view.new-column", "Done"+ ]);+ });+it("sends a column to either end with Ctrl, Alt and Home or End", async () => {const board = await renderBoard();@@ -476,13 +494,14 @@ describe("Board keyboard", () => {function press(board: HTMLElement,key: string,- options: { altKey?: boolean, ctrlKey?: boolean } = {}+ options: { altKey?: boolean, ctrlKey?: boolean, shiftKey?: boolean } = {}) {act(() => {document.activeElement?.dispatchEvent(new KeyboardEvent("keydown", {key,altKey: options.altKey ?? false,ctrlKey: options.ctrlKey ?? false,+ shiftKey: options.shiftKey ?? false,bubbles: true,cancelable: true}));diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex 5839ef20a1..66d322f221 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -56,6 +56,8 @@ export interface BoardKeyboardOptions {api: BoardApi;/** Moves a column to sit before the given position, as a drag onto that position would. */moveColumn: (fromIndex: number, toIndex: number) => void;+ /** Puts a new column beside the given one and opens its title editor, as the menu does. */+ insertColumn: (relativeTo: string, direction: "before" | "after") => void;}/**@@ -74,7 +76,7 @@ export interface BoardKeyboardOptions {* focused on whichever column took its place.*/export function useBoardKeyboard({- containerRef, columns, byColumn, api, moveColumn+ containerRef, columns, byColumn, api, moveColumn, insertColumn}: BoardKeyboardOptions) {const pendingFocus = useRef<PendingFocus | null>(null);@@ -111,7 +113,11 @@ export function useBoardKeyboard({return useCallback((e: KeyboardEvent) => {const container = containerRef.current;const target = e.target as HTMLElement | null;- if (!container || e.metaKey || e.shiftKey) return;+ if (!container || e.metaKey) return;++ // Shift is left to whatever else answers for it, bar the one key where it names the other+ // direction of something the board already does.+ if (e.shiftKey && !(e.ctrlKey && e.key === "Enter")) return;// An editor is open on the thing that is focused, and every key belongs to it.if (target?.closest("input, textarea")) return;@@ -124,6 +130,14 @@ export function useBoardKeyboard({if (!spot) return;if (e.ctrlKey) {+ // 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") {+ take(e);+ insertColumn(columns[spot.column], e.shiftKey ? "before" : "after");+ return;+ }+// A header holds no card, so the key carries the column it heads. Alt says so from// anywhere within a column, for a reader who is standing on one of its cards.const carriesColumn = e.altKey || spot.kind === "header";@@ -174,7 +188,7 @@ export function useBoardKeyboard({api.openNote(item.note.noteId);}}- }, [ containerRef, columns, byColumn, api, moveColumn ]);+ }, [ containerRef, columns, byColumn, api, moveColumn, insertColumn ]);}/** Keeps a key the board has answered from also reaching whatever else is bound to it. */