Skip to content

Build your personal knowledge base with Trilium Notes

feat(tree): offer the paste actions only while something is cut or copied

Elian Doran90e2efb12894
diff --git a/apps/client/src/menus/tree_context_menu.spec.ts b/apps/client/src/menus/tree_context_menu.spec.ts
index 08c733cb4d..d0c3f987ab 100644
--- a/apps/client/src/menus/tree_context_menu.spec.ts
+++ b/apps/client/src/menus/tree_context_menu.spec.ts
@@ -1,7 +1,43 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
+import type Component from "../components/component";
+import clipboard from "../services/clipboard";
+import froca from "../services/froca";
+import noteTypesService from "../services/note_types";
import { buildNote } from "../test/easy-froca";
-import { getDuplicateItems } from "./tree_context_menu";
+import type { MenuItem } from "./context_menu";
+import { buildTreeContextMenuItems, getDuplicateItems, type TreeCommandNames } from "./tree_context_menu";
+
+vi.mock("../services/clipboard", () => ({ default: { isClipboardEmpty: vi.fn(() => true) } }));
+vi.mock("../components/app_context", () => ({ default: { tabManager: { getActiveContext: () => undefined } } }));
+
+describe("buildTreeContextMenuItems", () => {
+ it("offers the paste actions only while something is cut or copied", async () => {
+ vi.spyOn(noteTypesService, "loadNoteTypeData")
+ .mockResolvedValue({ builtInTemplateNotes: [], userTemplateNotes: [], newTemplates: new Set() });
+ const parent = buildNote({ title: "Parent", children: [ { title: "Child" } ] });
+ const child = froca.notes[parent.children[0] ?? ""];
+ const [ branch ] = child?.getParentBranches() ?? [];
+ if (!child || !branch) throw new Error("expected the child and its branch");
+
+ async function actions() {
+ const items = await buildTreeContextMenuItems({
+ note: child, branch, notePath: `${parent.noteId}/${child.noteId}`, component: {} as Component
+ });
+ const row = items.find((item): item is Extract<MenuItem<TreeCommandNames>, { kind: "actions" }> =>
+ "kind" in item && item.kind === "actions");
+ return row?.items.map((item) => item.command);
+ }
+
+ expect(await actions()).toStrictEqual([ "cutNotesToClipboard", "copyNotesToClipboard", "deleteNotes" ]);
+
+ vi.mocked(clipboard.isClipboardEmpty).mockReturnValue(false);
+ expect(await actions()).toStrictEqual([
+ "cutNotesToClipboard", "copyNotesToClipboard", "pasteNotesFromClipboard", "pasteNotesAfterFromClipboard",
+ "deleteNotes"
+ ]);
+ });
+});
describe("getDuplicateItems", () => {
it("offers the note-only copy as soon as one of the notes has children", () => {
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index 9cc6e18282..ddb7054b2a 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -152,21 +152,24 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Pr
enabled: isNotRoot && !isHoisted && parentNotSearch
},
{ title: t("tree-context-menu.clone"), command: "copyNotesToClipboard", keyboardShortcut: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted },
- {
- title: t("tree-context-menu.paste-into"),
- command: "pasteNotesFromClipboard",
- keyboardShortcut: "pasteNotesFromClipboard",
- uiIcon: "bx bx-clipboard",
- uiIconBadge: "bx bx-subdirectory-right",
- enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes
- },
- {
- title: t("tree-context-menu.paste-after"),
- command: "pasteNotesAfterFromClipboard",
- uiIcon: "bx bx-clipboard",
- uiIconBadge: "bx bx-down-arrow-alt",
- enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes
- },
+ // Shown only while something is cut or copied, which is when they are looked for.
+ ...(clipboard.isClipboardEmpty() ? [] : [
+ {
+ title: t("tree-context-menu.paste-into"),
+ command: "pasteNotesFromClipboard",
+ keyboardShortcut: "pasteNotesFromClipboard",
+ uiIcon: "bx bx-clipboard",
+ uiIconBadge: "bx bx-subdirectory-right",
+ enabled: notSearch && noSelectedNotes
+ },
+ {
+ title: t("tree-context-menu.paste-after"),
+ command: "pasteNotesAfterFromClipboard",
+ uiIcon: "bx bx-clipboard",
+ uiIconBadge: "bx bx-down-arrow-alt",
+ enabled: isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes
+ }
+ ] satisfies MenuCommandItem<TreeCommandNames>[]),
{
title: t("tree-context-menu.delete"),
command: "deleteNotes",