Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: add a keyboard shortcut to remove/delete notes

Adorian Doran149d693981e9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 16001b35a1..1a8b0558d4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3385,6 +3385,8 @@
"insert_column": "Insert column after / before",
"open_item": "Open item",
"rename": "Rename item or column",
+ "remove_item": "Remove item from the board",
+ "delete_item": "Delete the note",
"moving": "Moving",
"move_item": "Move item up / down",
"move_within": "Move to the top / bottom of the column",
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 36c5b06607..b4cb342d2d 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -323,6 +323,8 @@ describe("Board column rename", () => {
},
{ keys: [ "Space" ], labelKey: "board_view.hints.open_item" },
{ keys: [ "F2" ], labelKey: "board_view.hints.rename" },
+ { keys: [ "Delete" ], labelKey: "board_view.hints.remove_item" },
+ { keys: [ "Shift+Delete" ], labelKey: "board_view.hints.delete_item" },
{ keys: [ "Ctrl+Up", "Ctrl+Down" ], labelKey: "board_view.hints.move_item" },
{ keys: [ "Ctrl+Home", "Ctrl+End" ], labelKey: "board_view.hints.move_within" },
{ keys: [ "Ctrl+Left", "Ctrl+Right" ], labelKey: "board_view.hints.move_across" },
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 3653036eb0..83264badb9 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -132,7 +132,9 @@ const BOARD_HINTS: ShortcutHintDefinition = [
labelKey: "board_view.hints.insert_column"
},
{ keys: [ "Space" ], labelKey: "board_view.hints.open_item" },
- { keys: [ "F2" ], labelKey: "board_view.hints.rename" }
+ { keys: [ "F2" ], labelKey: "board_view.hints.rename" },
+ { keys: [ "Delete" ], labelKey: "board_view.hints.remove_item" },
+ { keys: [ "Shift+Delete" ], labelKey: "board_view.hints.delete_item" }
]
},
{
diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
index e7a0804cf6..cf8d599eae 100644
--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx
+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
@@ -10,6 +10,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../../../components/app_context";
import Component from "../../../components/component";
+import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
import froca from "../../../services/froca";
import server from "../../../services/server";
@@ -22,7 +23,8 @@ vi.mock("../../../services/branches", () => ({
moveBeforeBranch: vi.fn(async () => {}),
moveAfterBranch: vi.fn(async () => {}),
cloneNoteToParentNote: vi.fn(async () => {}),
- cloneNoteAfter: vi.fn(async () => {})
+ cloneNoteAfter: vi.fn(async () => {}),
+ deleteNotes: vi.fn(async () => {})
}
}));
@@ -39,6 +41,7 @@ describe("Board keyboard", () => {
saved.length = 0;
vi.restoreAllMocks();
// A mocked module is not a spy, so restoring leaves its calls where the last test left.
+ vi.mocked(branches.deleteNotes).mockClear();
vi.mocked(branches.moveBeforeBranch).mockClear();
vi.mocked(branches.moveAfterBranch).mockClear();
vi.spyOn(server, "put").mockResolvedValue(undefined);
@@ -170,6 +173,63 @@ describe("Board keyboard", () => {
});
});
+ describe("taking a card off the board", () => {
+ it("strips the grouping label with Delete, leaving the note where it is", async () => {
+ const board = await renderBoard();
+ const removed = noteOf(board, "First");
+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);
+ focusCard(board, 0, 0);
+
+ press(board, "Delete");
+
+ expect(strip)
+ .toHaveBeenCalledWith(expect.objectContaining({ noteId: removed }), "status");
+ expect(branches.deleteNotes).not.toHaveBeenCalled();
+ });
+
+ it("deletes the note itself with Shift and Delete", async () => {
+ const board = await renderBoard();
+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);
+ focusCard(board, 0, 0);
+
+ press(board, "Delete", { shiftKey: true });
+
+ expect(branches.deleteNotes).toHaveBeenCalledWith(
+ [ branchOf(board, "First") ], false, false);
+ expect(strip).not.toHaveBeenCalled();
+ });
+
+ /**
+ * The card is about to go, so focus is handed on as the key is pressed rather than left to
+ * the redraw: until the card goes it still holds focus, and a redraw reaching the board
+ * first would take that for the reader having chosen where to be.
+ */
+ it("hands focus to the card below the one that goes, before it goes", async () => {
+ const board = await renderBoard();
+ vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);
+ const removed = noteOf(board, "First");
+ focusCard(board, 0, 0);
+
+ press(board, "Delete");
+ expect(focusedName(board)).toBe("Second");
+
+ setStatus(removed, "");
+ await redraw();
+ expect(focusedName(board)).toBe("Second");
+ });
+
+ it("does nothing on a header, which stands for no card", async () => {
+ const board = await renderBoard();
+ const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);
+ focusHeader(board, 0);
+
+ press(board, "Delete");
+
+ expect(strip).not.toHaveBeenCalled();
+ expect(branches.deleteNotes).not.toHaveBeenCalled();
+ });
+ });
+
describe("moving what is focused", () => {
it("moves a card up and down its own column", async () => {
const board = await renderBoard();
diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.ts
index 66d322f221..6558968d07 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -1,9 +1,13 @@
import { RefObject } from "preact";
import { useCallback, useLayoutEffect, useRef } from "preact/hooks";
+import branches from "../../../services/branches";
import BoardApi from "./api";
import { ColumnMap } from "./data";
+/** The keys the board answers for with Shift held down; every other Shift press is left alone. */
+const SHIFT_KEYS = [ "Enter", "Delete" ];
+
/** Plain, these walk the board. */
const NAVIGATION_KEYS = [ "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End" ];
@@ -115,9 +119,9 @@ export function useBoardKeyboard({
const target = e.target as HTMLElement | null;
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;
+ // Shift is left to whatever else answers for it, bar the keys where it names the other
+ // direction, or the harder form, of something the board already does.
+ if (e.shiftKey && !SHIFT_KEYS.includes(e.key)) return;
// An editor is open on the thing that is focused, and every key belongs to it.
if (target?.closest("input, textarea")) return;
@@ -187,10 +191,45 @@ export function useBoardKeyboard({
take(e);
api.openNote(item.note.noteId);
}
+ return;
+ }
+
+ if (e.key === "Delete" && spot.kind === "item") {
+ const item = itemAt(columns, byColumn, spot);
+ if (!item) return;
+ take(e);
+
+ // Straight away rather than through `pendingFocus`, which waits for a redraw: until the
+ // card goes, focus is still on it, and a redraw arriving first would read that as the
+ // reader having chosen where to be and let the intent go.
+ neighbourOf(container, columns, byColumn, spot)?.focus();
+
+ if (e.shiftKey) {
+ // The note itself, with the confirmation deleting one anywhere else asks for.
+ branches.deleteNotes([ item.branch.branchId ], false, false);
+ } else {
+ api.removeFromBoard(item.note.noteId);
+ }
}
}, [ containerRef, columns, byColumn, api, moveColumn, insertColumn ]);
}
+/** What to put focus on once a card goes: the one under it, the one over it, or the column. */
+function neighbourOf(
+ container: HTMLElement,
+ columns: string[],
+ byColumn: ColumnMap | undefined,
+ spot: Extract<Spot, { kind: "item" }>
+) {
+ const column = columns[spot.column];
+ const items = byColumn?.get(column) ?? [];
+ const next = items[spot.item + 1] ?? items[spot.item - 1];
+
+ return next
+ ? findCard(container, next.note.noteId)
+ : findInColumn(container, column, "add-item");
+}
+
/** Keeps a key the board has answered from also reaching whatever else is bound to it. */
function take(e: KeyboardEvent) {
e.preventDefault();