Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: add a keyboard shortcut to open the context menu

Adorian Doran0f76b57cddb3
diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
index d98c2ca991..57ee481bc4 100644
--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx
+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx
@@ -12,6 +12,7 @@ import appContext from "../../../components/app_context";
import Component from "../../../components/component";
import attributes from "../../../services/attributes";
import branches from "../../../services/branches";
+import contextMenu from "../../../menus/context_menu";
import froca from "../../../services/froca";
import server from "../../../services/server";
import { buildNote } from "../../../test/easy-froca";
@@ -29,6 +30,11 @@ vi.mock("../../../services/branches", () => ({
}
}));
+// The card menu opens with the link items on top, which want a tab manager this spec has none of.
+vi.mock("../../../menus/link_context_menu", () => ({
+ default: { getItems: () => [], handleLinkContextMenuItem: vi.fn() }
+}));
+
vi.mock("../../../services/i18n", () => ({
t: (key: string) => key,
translationsInitializedPromise: $.Deferred().resolve()
@@ -174,6 +180,37 @@ describe("Board keyboard", () => {
});
});
+ describe("the menu key", () => {
+ it("asks the focused card and header for their own menus", async () => {
+ const board = await renderBoard();
+ const show = vi.spyOn(contextMenu, "show").mockResolvedValue(undefined);
+
+ focusCard(board, 0, 0);
+ press(board, "ContextMenu");
+ expect(show).toHaveBeenCalledTimes(1);
+ // The card menu, which is the only one offering to take a note off the board.
+ expect(show.mock.calls[0][0].items.some(item =>
+ item && "title" in item && item.title === "board_view.remove-from-board"
+ )).toBe(true);
+
+ focusHeader(board, 0);
+ press(board, "ContextMenu");
+ expect(show).toHaveBeenCalledTimes(2);
+ expect(show.mock.calls[1][0].items.some(item =>
+ item && "title" in item && item.title === "board_view.rename-column")).toBe(true);
+ });
+
+ it("leaves the key alone where nothing focused has a menu", async () => {
+ const board = await renderBoard();
+ const show = vi.spyOn(contextMenu, "show").mockResolvedValue(undefined);
+
+ focusButton(board, 0);
+ press(board, "ContextMenu");
+
+ expect(show).not.toHaveBeenCalled();
+ });
+ });
+
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();
diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.ts
index bbbd95a9ca..ebe9e21dac 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -194,6 +194,15 @@ export function useBoardKeyboard({
return;
}
+ if (e.key === "ContextMenu" && (spot.kind === "item" || spot.kind === "header")) {
+ const element = document.activeElement;
+ if (!(element instanceof HTMLElement)) return;
+
+ take(e);
+ askForMenu(element);
+ return;
+ }
+
if (e.key === "Delete" && spot.kind === "header" && !e.shiftKey) {
take(e);
@@ -245,6 +254,22 @@ function neighbourOf(
: findInColumn(container, column, "add-item");
}
+/**
+ * Asks the focused card or header for its menu the way a right click does, rather than by opening
+ * one here: both already answer for `contextmenu`, and what each menu offers is theirs to say.
+ *
+ * The press is taken first, so the browser sends no `contextmenu` of its own and the menu is not
+ * opened twice. Where it opens is named here, a key press carrying no position of its own.
+ */
+function askForMenu(element: HTMLElement) {
+ const { left, bottom } = element.getBoundingClientRect();
+ element.dispatchEvent(new MouseEvent("contextmenu", {
+ bubbles: true,
+ clientX: left,
+ clientY: bottom
+ }));
+}
+
/** Keeps a key the board has answered from also reaching whatever else is bound to it. */
function take(e: KeyboardEvent) {
e.preventDefault();