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
fix(client): no clone, move or paste on help pages
Elian Dorand0361007792a
diff --git a/apps/client/src/menus/tree_context_menu.spec.ts b/apps/client/src/menus/tree_context_menu.spec.tsindex c1ee118aa4..b1d8ed629b 100644--- a/apps/client/src/menus/tree_context_menu.spec.ts+++ b/apps/client/src/menus/tree_context_menu.spec.ts@@ -97,18 +97,30 @@ describe("buildTreeContextMenuItems on system notes", () => {const items = await buildTreeContextMenuItems({note: child, branch, notePath: `${parentId}/${childId}`, component: {} as Component});- const find = (command: TreeCommandNames) => items.find((item) => "command" in item && item.command === command);+ // The clipboard rows sit in an `actions` group of their own.+ const rows = items.flatMap((item) => ("kind" in item && item.kind === "actions" ? item.items : [ item ]));const enabled = (command: TreeCommandNames) => {- const item = find(command);+ const item = rows.find((row) => "command" in row && row.command === command);if (!item || "kind" in item) throw new Error(`expected a ${command} row`);return item.enabled;};- return { exportNote: enabled("exportNote"), importIntoNote: enabled("importIntoNote") };+ return {+ exportNote: enabled("exportNote"),+ importIntoNote: enabled("importIntoNote"),+ branchCommands: (+ [ "cutNotesToClipboard", "copyNotesToClipboard", "moveNotesTo", "cloneNotesTo" ] as const+ ).map(enabled)+ };}it("exports a help page, which cannot be imported into, and neither for an options page", async () => {- expect(await enabledState("_helpParent", "_helpPage")).toStrictEqual({ exportNote: true, importIntoNote: false });- expect(await enabledState("_optionsParent", "_optionsPage")).toStrictEqual({ exportNote: false, importIntoNote: false });+ expect(await enabledState("_helpParent", "_helpPage")).toMatchObject({ exportNote: true, importIntoNote: false });+ expect(await enabledState("_optionsParent", "_optionsPage")).toMatchObject({ exportNote: false, importIntoNote: false });+ });++ it("does not cut, clone or move a help page, which is virtual, but does an options page", async () => {+ expect((await enabledState("_helpParent", "_helpPage")).branchCommands).toStrictEqual([ false, false, false, false ]);+ expect((await enabledState("_optionsParent", "_optionsPage")).branchCommands).toStrictEqual([ true, true, true, true ]);});});diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.tsindex ef335520f3..d261eee428 100644--- a/apps/client/src/menus/tree_context_menu.ts+++ b/apps/client/src/menus/tree_context_menu.ts@@ -126,7 +126,9 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Prconst notSearch = note.type !== "search";const hasSubtreeHidden = note.isLabelTruthy("subtreeHidden") ?? false;const notOptions = !note.noteId.startsWith("_options");- const notOptionsOrHelp = notOptions && !note.noteId.startsWith("_help");+ // Help pages are virtual notes, which the server never clones, moves or gives children.+ const notHelp = !note.noteId.startsWith("_help");+ const notOptionsOrHelp = notOptions && notHelp;const parentNotSearch = !parentNote || parentNote.type !== "search";const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;@@ -153,9 +155,9 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Prcommand: "cutNotesToClipboard",keyboardShortcut: "cutNotesToClipboard",uiIcon: "bx bx-cut",- enabled: isNotRoot && !isHoisted && parentNotSearch+ enabled: isNotRoot && !isHoisted && parentNotSearch && notHelp},- { title: t("tree-context-menu.clone"), tooltip: t("tree-context-menu.clone-tooltip"), command: "copyNotesToClipboard", keyboardShortcut: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted },+ { title: t("tree-context-menu.clone"), tooltip: t("tree-context-menu.clone-tooltip"), command: "copyNotesToClipboard", keyboardShortcut: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted && notHelp },// Shown only while something is cut or copied, which is when they are looked for....(clipboard.isClipboardEmpty() ? [] : [{@@ -164,14 +166,14 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): PrkeyboardShortcut: "pasteNotesFromClipboard",uiIcon: "bx bx-clipboard",uiIconBadge: "bx bx-subdirectory-right",- enabled: notSearch && noSelectedNotes+ enabled: notSearch && noSelectedNotes && notHelp},{title: t("tree-context-menu.paste-after"),command: "pasteNotesAfterFromClipboard",uiIcon: "bx bx-clipboard",uiIconBadge: "bx bx-down-arrow-alt",- enabled: isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes+ enabled: isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes && notHelp}] satisfies MenuCommandItem<TreeCommandNames>[]),{@@ -242,10 +244,10 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Prcommand: "moveNotesTo",keyboardShortcut: "moveNotesTo",uiIcon: "bx bx-transfer",- enabled: isNotRoot && !isHoisted && parentNotSearch+ enabled: isNotRoot && !isHoisted && parentNotSearch && notHelp},- { title: t("tree-context-menu.clone-to"), command: "cloneNotesTo", keyboardShortcut: "cloneNotesTo", uiIcon: "bx bx-duplicate", enabled: isNotRoot && !isHoisted },+ { title: t("tree-context-menu.clone-to"), command: "cloneNotesTo", keyboardShortcut: "cloneNotesTo", uiIcon: "bx bx-duplicate", enabled: isNotRoot && !isHoisted && notHelp },{title: t("tree-context-menu.duplicate"),diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex db3754d96f..c376e7d5fc 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -528,7 +528,7 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {return false;} else if (node.data.noteId === "_lbRoot") {return false;- } else if (node.data.noteId.startsWith("_options")) {+ } else if (node.data.noteId.startsWith("_options") || node.data.noteId.startsWith("_help")) {return false;} else if (node.data.noteId === hoistedNoteService.getHoistedNoteId()) {// The hoisted note is the tree root, so it has no visible siblings to drop before/after.@@ -581,6 +581,12 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {return;}+ // Help pages are virtual notes, which the server never moves.+ if (notes.some((note) => note.noteId.startsWith("_help"))) {+ toastService.showError(t("note_tree.dropping-not-allowed"));+ return;+ }+// This function MUST be defined to enable dropping of items on the tree.// data.hitMode is 'before', 'after', or 'over'.