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(core): support clones in virtual subtrees; don't force frontend reload on locale change
Claude58ebf6f9bbf7
diff --git a/packages/trilium-core/src/becca/becca_loader.ts b/packages/trilium-core/src/becca/becca_loader.tsindex 374c34f922..47c8ad8256 100644--- a/packages/trilium-core/src/becca/becca_loader.ts+++ b/packages/trilium-core/src/becca/becca_loader.ts@@ -117,18 +117,15 @@ function injectVirtualSubtrees() {}}-function validateVirtualSubtree(provider: VirtualNoteProvider, items: VirtualSubtreeItem[], seenIds = new Set<string>()) {+function validateVirtualSubtree(provider: VirtualNoteProvider, items: VirtualSubtreeItem[]) {+ // Repeated IDs are deliberately allowed: an ID appearing in several places is a clone+ // (multiple placements of one note), which e.g. the User Guide meta makes use of.for (const item of items) {if (!item.id.startsWith(provider.namespace)) {throw new Error(`Item '${item.id}' is outside the provider's namespace '${provider.namespace}'.`);}- if (seenIds.has(item.id)) {- throw new Error(`Duplicate item ID '${item.id}'.`);- }-- seenIds.add(item.id);- validateVirtualSubtree(provider, item.children ?? [], seenIds);+ validateVirtualSubtree(provider, item.children ?? []);}}@@ -143,23 +140,31 @@ function injectVirtualItem(provider: VirtualNoteProvider, parentNoteId: string,return;}- const noteRow: Partial<NoteRow> = {- noteId: item.id,- title: item.title,- type: item.type,- mime: item.mime ?? "",- isProtected: false- };+ // An already-virtual note means this is a repeated placement (a clone within the virtual+ // subtree): the first occurrence defined the note and its attributes, this one only+ // contributes an additional branch.+ const isCloneOccurrence = !!note?.isVirtual;- if (note) {- // Skeleton created by a forward reference (e.g. a persisted branch or relation loaded- // before this virtual note existed) — fill it in place so those links survive.- note.updateFromRow(noteRow);- } else {- note = new BNote(noteRow);- }+ if (!isCloneOccurrence) {+ const noteRow: Partial<NoteRow> = {+ noteId: item.id,+ title: item.title,+ type: item.type,+ mime: item.mime ?? "",+ isProtected: false+ };++ if (note) {+ // Skeleton created by a forward reference (e.g. a persisted branch or relation+ // loaded before this virtual note existed) — fill it in place so those links+ // survive.+ note.updateFromRow(noteRow);+ } else {+ note = new BNote(noteRow);+ }- note.isVirtual = true;+ note.isVirtual = true;+ }const branchId = `${parentNoteId}_${item.id}`;@@ -175,9 +180,9 @@ function injectVirtualItem(provider: VirtualNoteProvider, parentNoteId: string,branch.isVirtual = true;}- const attributeDefs = [...(item.attributes ?? [])];+ const attributeDefs = isCloneOccurrence ? [] : [...(item.attributes ?? [])];- if (item.icon) {+ if (!isCloneOccurrence && item.icon) {attributeDefs.push({ type: "label", name: "iconClass", value: `bx ${item.icon}` });}diff --git a/packages/trilium-core/src/becca/virtual_notes_injection.spec.ts b/packages/trilium-core/src/becca/virtual_notes_injection.spec.tsindex 20d75b599d..9292f68f06 100644--- a/packages/trilium-core/src/becca/virtual_notes_injection.spec.ts+++ b/packages/trilium-core/src/becca/virtual_notes_injection.spec.ts@@ -31,7 +31,14 @@ const SUBTREE: VirtualSubtreeItem[] = [// backfill of forward references within a virtual subtreeattributes: [{ type: "relation", name: "seeAlso", value: "_vtestLast" }]},- { id: "_vtestLast", title: "Last page", type: "text" }+ {+ id: "_vtestLast",+ title: "Last page",+ type: "text",+ // repeated placement of _vtestPage — a clone within the virtual subtree,+ // as the real User Guide meta contains (same ID, several locations)+ children: [{ id: "_vtestPage", title: "Clone placement", type: "text" }]+ }]}];@@ -88,6 +95,18 @@ describe("virtual note injection", () => {expect(page.getOwnedAttributes().every((attribute) => attribute.isVirtual)).toBe(true);});+ it("treats repeated item IDs as clones: one note, several branches, attributes once", () => {+ const page = becca.getNoteOrThrow("_vtestPage");++ // the first occurrence defines the note; the second only places it again+ expect(page.title).toBe("Virtual page");+ expect(page.getParentNotes().map((note) => note.noteId).sort()).toEqual(["_vtest", "_vtestLast"]);+ expect(becca.getBranchFromChildAndParent("_vtestPage", "_vtestLast")?.isVirtual).toBe(true);++ // attributes come from the first occurrence only, without duplication+ expect(page.getOwnedAttributes().filter((attribute) => attribute.name === "docName")).toHaveLength(1);+ });+it("persists nothing to the database", () => {expect(countRows("notes", "noteId")).toBe(0);expect(countRows("branches", "noteId")).toBe(0);diff --git a/packages/trilium-core/src/services/app_info.spec.ts b/packages/trilium-core/src/services/app_info.spec.tsindex 96c581a619..82714e6a6f 100644--- a/packages/trilium-core/src/services/app_info.spec.ts+++ b/packages/trilium-core/src/services/app_info.spec.ts@@ -19,7 +19,7 @@ describe("app_info", () => {it("exposes the fixed sync and clipper protocol versions", () => {// These are protocol constants; bumping them is a deliberate,// breaking change, so they are locked here as a regression guard.- expect(appInfo.syncVersion).toBe(39);+ expect(appInfo.syncVersion).toBe(40); // 40: in-app help notes became virtual (v240 migration)expect(appInfo.clipperProtocolVersion).toBe("1.0");});diff --git a/packages/trilium-core/src/services/i18n.ts b/packages/trilium-core/src/services/i18n.tsindex 735b2c4e4e..1321021b4b 100644--- a/packages/trilium-core/src/services/i18n.ts+++ b/packages/trilium-core/src/services/i18n.ts@@ -3,6 +3,7 @@ import sql_init from "./sql_init";import options from "./options";import i18next from "i18next";import hidden_subtree from "./hidden_subtree";+import { getContext } from "./context";export type TranslationProvider = (i18nextInstance: typeof i18next, locale: LOCALE_IDS) => Promise<void>;@@ -37,9 +38,12 @@ export async function changeLanguage(locale: string) {hidden_subtree.checkHiddenSubtree(true, { restoreNames: true });// Virtual subtrees (e.g. the in-app help) build their titles with t() at injection time,- // so a becca reload is needed to re-render them in the new language.- const { reload } = await import("../becca/becca_loader.js");- reload("locale changed");+ // so a becca reload is needed to re-render them in the new language. Deliberately load()+ // and not reload(): the latter would push a reload-frontend message mid-request, aborting+ // the very options call that triggered the language change — and the client performs its+ // own frontend reload after a locale switch anyway.+ const { load } = await import("../becca/becca_loader.js");+ getContext().init(() => load());}/**