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 remove columns
Adorian Doran64f1a933199a
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1a8b0558d4..75d856710e 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3387,6 +3387,7 @@"rename": "Rename item or column","remove_item": "Remove item from the board","delete_item": "Delete the note",+ "remove_column": "Remove column (on a selected column header)","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/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 03bc972deb..e050a4dbf8 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -5,6 +5,7 @@ import branches from "../../../services/branches";import attributes from "../../../services/attributes";import { executeBulkActions } from "../../../services/bulk_action";import dialog from "../../../services/dialog";+import toast from "../../../services/toast";import FNote from "../../../entities/fnote";import froca from "../../../services/froca";import note_create from "../../../services/note_create";@@ -888,3 +889,28 @@ describe("pending writes shared between the views of a board", () => {expect(getPendingWrites("board3|status")).not.toBe(writes);});});++describe("removing a column with the question put first", () => {+ it("drops it only once agreed, and says so when the write is refused", async () => {+ const { api, saved } = createApi(+ { columns: [ { value: "To Do" }, { value: "Done" } ] },+ [ "To Do", "Done" ]+ );+ const confirm = vi.spyOn(dialog, "confirm").mockResolvedValue(false);+ const error = vi.spyOn(toast, "showError").mockReturnValue(undefined);++ expect(await api.confirmAndRemoveColumn("Done")).toBe(false);+ expect(confirm).toHaveBeenCalled();+ expect(saved).toEqual([]);++ confirm.mockResolvedValue(true);+ expect(await api.confirmAndRemoveColumn("Done")).toBe(true);+ expect(saved.at(-1)?.columns?.map(column => column.value)).toEqual([ "To Do" ]);+ expect(error).not.toHaveBeenCalled();++ // A refusal from the server is reported rather than passing for a deletion.+ vi.mocked(executeBulkActions).mockRejectedValueOnce(new Error("offline"));+ expect(await api.confirmAndRemoveColumn("To Do")).toBe(false);+ expect(error).toHaveBeenCalledWith("board_view.save-error");+ });+});diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 9ac82cd57b..7b838fdfe0 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -220,6 +220,28 @@ export default class BoardApi {return name;}+ /**+ * Asks before taking a column off the board, the grouping label going from every card in it.+ * Both the menu and the Delete key come through here, so the question is put once and the same+ * way, and a refusal from the server is reported rather than passing for a deletion.+ *+ * @returns whether the column went, for a caller with something to do afterwards.+ */+ async confirmAndRemoveColumn(column: string) {+ if (!await dialog.confirm(t("board_view.delete-column-confirmation"))) {+ return false;+ }++ try {+ await this.removeColumn(column);+ return true;+ } catch (e) {+ console.error("Failed to delete the board column:", e);+ toast.showError(t("board_view.save-error"));+ return false;+ }+ }+async removeColumn(column: string) {// Remove the value from the notes.const noteIds = this.byColumn?.get(column)?.map(item => item.note.noteId) || [];diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 6d2c512cec..1bd7ee06cc 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -161,21 +161,17 @@ describe("Board column context menu", () => {expect(api.addExistingItem).not.toHaveBeenCalled();});- it("asks before deleting a column, and drops it only once agreed", async () => {- const api = { removeColumn: vi.fn(async () => {}) } as unknown as BoardApi;- const confirm = vi.spyOn(dialog, "confirm").mockResolvedValue(false);+ /** The asking lives on the api, so the Delete key puts the same question the same way. */+ it("hands a column deletion to the api, which is what asks", async () => {+ const api = { confirmAndRemoveColumn: vi.fn(async () => true) } as unknown as BoardApi;const entry = openMenu(api).find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-trash");if (!entry || !("handler" in entry)) throw new Error("expected a delete entry");await entry.handler?.(entry, {} as never);- expect(confirm).toHaveBeenCalled();- expect(api.removeColumn).not.toHaveBeenCalled();- confirm.mockResolvedValue(true);- await entry.handler?.(entry, {} as never);- expect(api.removeColumn).toHaveBeenCalledWith("To Do");+ expect(api.confirmAndRemoveColumn).toHaveBeenCalledWith("To Do");});it("shows the column's own colour as the selected one", async () => {diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 113dcd64c8..16e9a7efdb 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -8,7 +8,6 @@ import branches from "../../../services/branches";import dialog from "../../../services/dialog";import { getArchiveMenuItem } from "../../../menus/context_menu_utils";import { t } from "../../../services/i18n";-import toast from "../../../services/toast";import ColorPicker from "../../react/ColorPicker";import Api from "./api";@@ -83,19 +82,7 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:{title: t("board_view.delete-column"),uiIcon: "bx bx-trash",- async handler() {- const confirmed = await dialog.confirm(t("board_view.delete-column-confirmation"));- if (!confirmed) {- return;- }-- try {- await api.removeColumn(column.value);- } catch (e) {- console.error("Failed to delete the board column:", e);- toast.showError(t("board_view.save-error"));- }- }+ handler: () => api.confirmAndRemoveColumn(column.value)},{ kind: "separator" },{diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex b4cb342d2d..76b9a34b05 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -325,6 +325,7 @@ describe("Board column 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" },+ { keys: [ "Delete" ], labelKey: "board_view.hints.remove_column" },{ 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.tsxindex 83264badb9..7b0a570789 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -134,7 +134,8 @@ const BOARD_HINTS: ShortcutHintDefinition = [{ 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: [ "Shift+Delete" ], labelKey: "board_view.hints.delete_item" },+ { keys: [ "Delete" ], labelKey: "board_view.hints.remove_column" }]},{diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex cf8d599eae..d98c2ca991 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -16,6 +16,7 @@ import froca from "../../../services/froca";import server from "../../../services/server";import { buildNote } from "../../../test/easy-froca";import { ParentComponent } from "../../react/react_utils";+import BoardApi from "./api";import BoardView, { BoardViewData } from ".";vi.mock("../../../services/branches", () => ({@@ -218,16 +219,33 @@ describe("Board keyboard", () => {expect(focusedName(board)).toBe("Second");});- it("does nothing on a header, which stands for no card", async () => {+ it("takes the whole column off the board from its header, asking first", async () => {const board = await renderBoard();const strip = vi.spyOn(attributes, "removeOwnedLabelByName").mockReturnValue(true);+ const remove = vi.spyOn(BoardApi.prototype, "confirmAndRemoveColumn")+ .mockResolvedValue(true);focusHeader(board, 0);press(board, "Delete");+ expect(remove).toHaveBeenCalledWith("To Do");+ // The cards in it are the column'"'"'s to take, not this key'"'"'s.expect(strip).not.toHaveBeenCalled();expect(branches.deleteNotes).not.toHaveBeenCalled();});++ /** Escalating a column would take every note in it, which nothing else here offers. */+ it("leaves Shift and Delete on a header unanswered", async () => {+ const board = await renderBoard();+ const remove = vi.spyOn(BoardApi.prototype, "confirmAndRemoveColumn")+ .mockResolvedValue(true);+ focusHeader(board, 0);++ press(board, "Delete", { shiftKey: true });++ expect(remove).not.toHaveBeenCalled();+ expect(branches.deleteNotes).not.toHaveBeenCalled();+ });});describe("moving what is focused", () => {diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex 6558968d07..bbbd95a9ca 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -194,6 +194,21 @@ export function useBoardKeyboard({return;}+ if (e.key === "Delete" && spot.kind === "header" && !e.shiftKey) {+ take(e);++ // Where focus goes once the column is gone, taken up only if it does go. Shift is left+ // unanswered here: escalating a column would take every note in it, which nothing else+ // on the board offers.+ const neighbour = columns[spot.column + 1] ?? columns[spot.column - 1];+ api.confirmAndRemoveColumn(columns[spot.column]).then((removed) => {+ if (removed && neighbour) {+ pendingFocus.current = { intent: { column: neighbour, part: "header" } };+ }+ });+ return;+ }+if (e.key === "Delete" && spot.kind === "item") {const item = itemAt(columns, byColumn, spot);if (!item) return;