Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: show keyboard shortcuts on the context menus

Adorian Dorane0edfd763551
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 75d856710e..00b30edb57 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3392,6 +3392,7 @@
"move_item": "Move item up / down",
"move_within": "Move to the top / bottom of the column",
"move_across": "Move to the previous / next column",
+ "move_to_end_column": "Move to the first / last column",
"move_column": "Move the column left / right",
"move_column_to_edge": "Move the column to the far left / right"
},
diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.ts
index 1bd7ee06cc..de5de1dc7d 100644
--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts
@@ -161,6 +161,25 @@ describe("Board column context menu", () => {
expect(api.addExistingItem).not.toHaveBeenCalled();
});
+ /** The keys are the board's own, so they are written out rather than looked up as actions. */
+ it("names the key beside each column entry that has one", () => {
+ const items = openMenu({} as unknown as BoardApi);
+ const byIcon = (icon: string) =>
+ items.find(item => item && "uiIcon" in item && item.uiIcon === icon);
+
+ expect(byIcon("bx bx-edit-alt")).toMatchObject({ shortcut: "F2" });
+ expect(byIcon("bx bx-trash")).toMatchObject({ shortcut: "Delete" });
+ // Nothing claims a key it does not answer for.
+ expect(byIcon("bx bx-link")).not.toHaveProperty("shortcut");
+
+ const submenu = byIcon("bx bx-columns");
+ const children = submenu && "items" in submenu ? submenu.items ?? [] : [];
+
+ // Before then after, the order the menu offers them in.
+ expect(children[0]).toMatchObject({ shortcut: "Ctrl+Shift+Enter" });
+ expect(children[1]).toMatchObject({ shortcut: "Ctrl+Enter" });
+ });
+
/** 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;
@@ -260,7 +279,7 @@ describe("Board item context menu", () => {
});
/** Opens the menu a card offers, and hands back what it was given to show. */
- function openItemMenu(api: BoardApi) {
+ function openItemMenu(api: BoardApi, column = "To Do") {
const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
const event = {
preventDefault: () => {},
@@ -269,11 +288,58 @@ describe("Board item context menu", () => {
pageY: 0
} as ContextMenuEvent;
- openNoteContextMenu(api, event, buildNote({ title: "Card" }) as FNote, "branchId", "To Do");
+ openNoteContextMenu(api, event, buildNote({ title: "Card" }) as FNote, "branchId", column);
return show.mock.calls.at(-1)?.[0].items ?? [];
}
+ /**
+ * The whole-way keys land on the same two entries wherever the card stands, so the menu names
+ * them. The one-column keys land somewhere different for every card and are left unnamed.
+ */
+ it("names the whole-way keys on the ends, and nothing on the columns beside", () => {
+ const api = {
+ columns: [ "To Do", "Doing", "Done", "Shipped" ],
+ isColumnArchived: () => false
+ } as unknown as BoardApi;
+
+ const moveTo = openItemMenu(api, "Doing").find(item =>
+ item && "uiIcon" in item && item.uiIcon === "bx bx-transfer");
+ if (!moveTo || !("items" in moveTo)) throw new Error("expected a move-to entry");
+
+ expect((moveTo.items ?? []).map(item => item && "shortcut" in item ? item.shortcut : null))
+ .toEqual([ "Ctrl+Shift+Left", undefined, undefined, "Ctrl+Shift+Right" ]);
+ });
+
+ it("leaves the end a card already stands in bare", () => {
+ const api = {
+ columns: [ "To Do", "Doing", "Done" ],
+ isColumnArchived: () => false
+ } as unknown as BoardApi;
+
+ const moveTo = openItemMenu(api, "To Do").find(item =>
+ item && "uiIcon" in item && item.uiIcon === "bx bx-transfer");
+ if (!moveTo || !("items" in moveTo)) throw new Error("expected a move-to entry");
+
+ expect((moveTo.items ?? []).map(item => item && "shortcut" in item ? item.shortcut : null))
+ .toEqual([ undefined, undefined, "Ctrl+Shift+Right" ]);
+ });
+
+ /** A hidden column is not one the keys reach, so the end is the last column drawn. */
+ it("counts the end over an archived column rather than onto it", () => {
+ const api = {
+ columns: [ "To Do", "Doing", "Parked" ],
+ isColumnArchived: (column: string) => column === "Parked"
+ } as unknown as BoardApi;
+
+ const moveTo = openItemMenu(api, "To Do").find(item =>
+ item && "uiIcon" in item && item.uiIcon === "bx bx-transfer");
+ if (!moveTo || !("items" in moveTo)) throw new Error("expected a move-to entry");
+
+ expect((moveTo.items ?? []).map(item => item && "shortcut" in item ? item.shortcut : null))
+ .toEqual([ undefined, "Ctrl+Shift+Right", undefined ]);
+ });
+
it("marks the archived columns it offers to move a card to", () => {
const api = {
columns: [ "To Do", "Done" ],
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index 16e9a7efdb..21e7aff9e1 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -32,6 +32,7 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
{
title: t("board_view.rename-column"),
uiIcon: "bx bx-edit-alt",
+ shortcut: "F2",
handler: column.onEditTitle
},
{ kind: "separator" },
@@ -59,10 +60,12 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
items: [
{
title: t("board_view.add-column-before"),
+ shortcut: "Ctrl+Shift+Enter",
handler: () => column.onAddColumn("before")
},
{
title: t("board_view.add-column-after"),
+ shortcut: "Ctrl+Enter",
handler: () => column.onAddColumn("after")
}
]
@@ -82,6 +85,7 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
{
title: t("board_view.delete-column"),
uiIcon: "bx bx-trash",
+ shortcut: "Delete",
handler: () => api.confirmAndRemoveColumn(column.value)
},
{ kind: "separator" },
@@ -114,6 +118,27 @@ function ColumnColorPicker({ api, value, color }: { api: Api, value: string, col
});
}
+/**
+ * The key that reaches `target` from `from`, for the two ends of the board and no others: `Ctrl`
+ * and `Shift` with an arrow sends a card the whole way, and the entry that answers for it is the
+ * same one wherever the card stands. `Ctrl` with an arrow moves a card one column and is left
+ * unnamed here, the entry it would land on being a different one for every card.
+ *
+ * Counted over the columns the board draws. An archived column is listed in the menu but stands off
+ * screen unless the board is showing archived notes, and a key that walks the board steps over it.
+ */
+function endColumnKey(api: Api, from: string, target: string) {
+ const drawn = api.columns.filter((name) => !api.isColumnArchived(name));
+ const at = drawn.indexOf(from);
+ if (at < 0) {
+ return undefined;
+ }
+
+ if (target === drawn[0] && at > 0) return "Ctrl+Shift+Left";
+ if (target === drawn[drawn.length - 1] && at < drawn.length - 1) return "Ctrl+Shift+Right";
+ return undefined;
+}
+
export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNote, branchId: string, column: string) {
event.preventDefault();
event.stopPropagation();
@@ -127,11 +152,13 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo
{
title: t("board_view.insert-above"),
uiIcon: "bx bx-list-plus",
+ shortcut: "Shift+Enter",
handler: () => api.insertRowAtPosition(column, branchId, "before")
},
{
title: t("board_view.insert-below"),
uiIcon: "bx bx-empty",
+ shortcut: "Enter",
handler: () => api.insertRowAtPosition(column, branchId, "after")
},
{ kind: "separator" },
@@ -143,6 +170,7 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo
items: api.columns.map(columnToMoveTo => ({
title: columnToMoveTo,
enabled: columnToMoveTo !== column,
+ shortcut: endColumnKey(api, column, columnToMoveTo),
badges: api.isColumnArchived(columnToMoveTo)
? [ { title: t("board_view.archived-badge") } ]
: undefined,
@@ -154,11 +182,13 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo
{
title: t("board_view.remove-from-board"),
uiIcon: "bx bx-task-x",
+ shortcut: "Delete",
handler: () => api.removeFromBoard(note.noteId)
},
{
title: t("board_view.delete-note"),
uiIcon: "bx bx-trash",
+ shortcut: "Shift+Delete",
handler: () => branches.deleteNotes([ branchId ], false, false)
},
{ kind: "separator" },
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 76b9a34b05..cd79f6039d 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -329,6 +329,10 @@ describe("Board column rename", () => {
{ 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" },
+ {
+ keys: [ "Ctrl+Shift+Left", "Ctrl+Shift+Right" ],
+ labelKey: "board_view.hints.move_to_end_column"
+ },
{
keys: [ "Ctrl+Alt+Left", "Ctrl+Alt+Right" ],
labelKey: "board_view.hints.move_column"
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 7b0a570789..fdce2cbdfe 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -144,6 +144,10 @@ const BOARD_HINTS: ShortcutHintDefinition = [
{ 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" },
+ {
+ keys: [ "Ctrl+Shift+Left", "Ctrl+Shift+Right" ],
+ labelKey: "board_view.hints.move_to_end_column"
+ },
{
keys: [ "Ctrl+Alt+Left", "Ctrl+Alt+Right" ],
labelKey: "board_view.hints.move_column"
diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
index 57ee481bc4..ea72394457 100644
--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx
+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
@@ -462,6 +462,32 @@ describe("Board keyboard", () => {
expect(branches.moveAfterBranch).toHaveBeenCalled();
});
+ it("sends a card the whole way with Ctrl, Shift and a sideways arrow", async () => {
+ const board = await renderBoard();
+ focusCard(board, 1, 0);
+ const moved = noteOf(board, "Second");
+
+ press(board, "ArrowRight", { ctrlKey: true, shiftKey: true });
+ setStatus(moved, "Done");
+ await redraw();
+ expect(columnOf(board, "Second")).toBe(2);
+
+ press(board, "ArrowLeft", { ctrlKey: true, shiftKey: true });
+ setStatus(moved, "To Do");
+ await redraw();
+ expect(columnOf(board, "Second")).toBe(0);
+ });
+
+ it("moves no card already standing at the end it is sent to", async () => {
+ const board = await renderBoard();
+ focusCard(board, 0, 0);
+
+ press(board, "ArrowLeft", { ctrlKey: true, shiftKey: true });
+
+ expect(branches.moveAfterBranch).not.toHaveBeenCalled();
+ expect(columnOf(board, "First")).toBe(0);
+ });
+
it("does nothing at the edges it cannot move past", 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 ebe9e21dac..89db8e0b40 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -5,8 +5,8 @@ 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" ];
+/** Sideways, and with Ctrl, these carry the focused card the whole way rather than one column. */
+const CARD_END_KEYS = [ "ArrowLeft", "ArrowRight" ];
/** Plain, these walk the board. */
const NAVIGATION_KEYS = [ "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End" ];
@@ -120,8 +120,8 @@ export function useBoardKeyboard({
if (!container || e.metaKey) 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;
+ // direction, the harder form, or the further reach of something the board already does.
+ if (e.shiftKey && !shiftIsOurs(e)) return;
// An editor is open on the thing that is focused, and every key belongs to it.
if (target?.closest("input, textarea")) return;
@@ -146,6 +146,9 @@ export function useBoardKeyboard({
// anywhere within a column, for a reader who is standing on one of its cards.
const carriesColumn = e.altKey || spot.kind === "header";
+ // Shift says how far a card goes, and a column has no such reach of its own.
+ if (e.shiftKey && carriesColumn) return;
+
// Taken whether or not it leads anywhere: a card already at the end of its column is no
// reason to let the key mean something else instead.
if (!(carriesColumn ? COLUMN_MOVE_KEYS : ITEM_MOVE_KEYS).includes(e.key)) return;
@@ -159,7 +162,7 @@ export function useBoardKeyboard({
return;
}
- const moved = move(spot, e.key, { columns, byColumn, api });
+ const moved = move(spot, e.key, { columns, byColumn, api }, e.shiftKey);
if (moved) {
const pending: PendingFocus = { intent: moved.intent };
pendingFocus.current = pending;
@@ -270,6 +273,15 @@ function askForMenu(element: HTMLElement) {
}));
}
+/** Whether Shift on this press is the board's to answer for, rather than the application's. */
+function shiftIsOurs(e: KeyboardEvent) {
+ if (e.key === "Enter" || e.key === "Delete") {
+ return true;
+ }
+
+ return e.ctrlKey && CARD_END_KEYS.includes(e.key);
+}
+
/** Keeps a key the board has answered from also reaching whatever else is bound to it. */
function take(e: KeyboardEvent) {
e.preventDefault();
@@ -358,7 +370,9 @@ function entryOf(container: HTMLElement, column: number): Spot {
function move(
spot: Spot,
key: string,
- { columns, byColumn, api }: Pick<BoardKeyboardOptions, "columns" | "byColumn" | "api">
+ { columns, byColumn, api }: Pick<BoardKeyboardOptions, "columns" | "byColumn" | "api">,
+ /** Whether the card goes the whole way rather than one place. */
+ toEnd = false
): { intent: FocusIntent; done: Promise<unknown> } | false {
const item = itemAt(columns, byColumn, spot);
if (!item || spot.kind !== "item") return false;
@@ -369,8 +383,11 @@ function move(
const { branchId } = item.branch;
if (key === "ArrowLeft" || key === "ArrowRight") {
- const target = columns[spot.column + (key === "ArrowRight" ? 1 : -1)];
- if (!target) return false;
+ const target = toEnd
+ ? columns[key === "ArrowRight" ? columns.length - 1 : 0]
+ : columns[spot.column + (key === "ArrowRight" ? 1 : -1)];
+ // Only the whole way can ask for the column a card already stands in.
+ if (!target || target === column) return false;
return { intent: { noteId }, done: api.moveToColumnEnd(noteId, branchId, target) };
}