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/content manager: fix an issue
Adorian Doran008dcdf1863f
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex 46fd226fd6..537e9132bd 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -1,5 +1,6 @@import { afterEach, describe, expect, it, vi } from "vitest";+import FBranch from "../entities/fbranch";import { buildNote } from "../test/easy-froca";import attributeService from "./attributes";import {@@ -7,6 +8,7 @@ import {CONTENT_CATEGORIES,type ContentCategory,findCategoryNotes,+ getDisplayedBranchId,isCategoryEnabled,isUserContent,ownsCategoryTrigger,@@ -14,6 +16,7 @@ import {resolveProperties,setCategoryEnabled} from "./content_manager";+import froca from "./froca";import searchService from "./search";afterEach(() => vi.restoreAllMocks());@@ -392,6 +395,56 @@ describe("findCategoryNotes", () => {});});+describe("getDisplayedBranchId", () => {+ // A note path is only resolvable if it reaches the root, so anchor the fixtures there.+ function buildTree() {+ buildNote({+ id: "root",+ title: "root",+ children: [+ { id: "shallowParent", title: "Scripts", children: [ { id: "target", title: "Startup" } ] },+ { id: "deepParent", title: "Deep", children: [ { id: "deepChild", title: "Nested" } ] }+ ]+ });+ return froca.notes["target"];+ }++ it("resolves the branch of the placement the row shows", () => {+ expect(getDisplayedBranchId(buildTree())).toBe("shallowParent_target");+ });++ it("picks the placement matching the best path for a cloned note, not whichever branch was added first", () => {+ // `NoteLink` renders that same best path under the title, so the delete has to agree with it.+ const note = buildTree();+ const deepChild = froca.notes["deepChild"];+ const clonedBranchId = "deepChild_target";++ froca.branches[clonedBranchId] = new FBranch(froca, {+ branchId: clonedBranchId,+ noteId: note.noteId,+ parentNoteId: deepChild.noteId,+ notePosition: 0,+ fromSearchNote: false+ });+ note.addParent(deepChild.noteId, clonedBranchId, false);+ deepChild.addChild(note.noteId, clonedBranchId, false);++ const parentNoteId = note.getBestNotePath()?.at(-2);++ expect(getDisplayedBranchId(note)).toBe(note.parentToBranch[parentNoteId ?? ""]);+ // The shorter path wins, so it is the original placement rather than the deeper clone.+ expect(getDisplayedBranchId(note)).toBe("shallowParent_target");+ });++ it("ignores the virtual branches that back search results", () => {+ const note = buildTree();++ note.parentToBranch["shallowParent"] = "virt-something";++ expect(getDisplayedBranchId(note)).toBeUndefined();+ });+});+describe("ownsCategoryTrigger", () => {it("accepts an owned trigger in either spelling, and rejects an inherited one", async () => {const scripts = categoryById("backendScripts");diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex 21d797fba6..fb77da4cd8 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -326,6 +326,22 @@ function toArray(value: string | string[]) {return Array.isArray(value) ? value : [ value ];}+/**+ * The branch for the placement shown on the note's row, or `undefined` if there isn't a deletable one.+ *+ * A row identifies a note, but deleting needs a branch, and a cloned note has several. `NoteLink`+ * renders the location under each title from the note's "best" path, so resolving that same path+ * here makes the delete act on the placement the user can actually see, rather than on whichever+ * branch happens to come first in cache order.+ */+export function getDisplayedBranchId(note: FNote, hoistedNoteId = "root", activeNotePath: string | null = null) {+ const parentNoteId = note.getBestNotePath(hoistedNoteId, activeNotePath)?.at(-2);+ const branchId = parentNoteId ? note.parentToBranch[parentNoteId] : undefined;++ // Search results are backed by virtual branches, which cannot be deleted.+ return branchId && !branchId.startsWith("virt-") ? branchId : undefined;+}+/*** Whether the note itself carries one of the category's triggers.*diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsxindex 48b78def4e..0a2559d500 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx@@ -13,6 +13,7 @@ import {type ContentCategory,type ContentSortOrder,findCategoryNotes,+ getDisplayedBranchId,isCategoryEnabled,resolveProperties,setCategoryEnabled@@ -50,13 +51,16 @@ function ContentItemMenu({ note }: { note: FNote }) {hoistedNoteId: appContext.tabManager.getActiveContext()?.hoistedNoteId ?? null});} else if (command === "deleteNote") {- // Search results carry virtual branches, which cannot be deleted.- const [ branchId ] = note.getParentBranchIds().filter((id) => !id.startsWith("virt-"));+ const branchId = getDisplayedBranchId(+ note,+ appContext.tabManager.getActiveContext()?.hoistedNoteId ?? "root",+ appContext.tabManager.getActiveContextNotePath()+ );if (branchId) {- // Deliberately one branch, not all of them: the dialog counts a note's- // *other* placements to warn about clones and offer "delete all clones", so- // passing every branch would zero that count and delete them all silently.+ // One branch, not all of them: the dialog counts a note's *other* placements+ // to warn about clones and offer "delete all clones", so passing every branch+ // would zero that count and remove them all silently.// `moveToParent` is off — the user is on this page, not on the note.void branches.deleteNotes([ branchId ], false, false);}