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): open a launcher added in the tree popup in the popup
Elian Doranab944318b4a6
diff --git a/apps/client/src/widgets/note_tree.spec.ts b/apps/client/src/widgets/note_tree.spec.tsindex adde8f056a..0faf2a6667 100644--- a/apps/client/src/widgets/note_tree.spec.ts+++ b/apps/client/src/widgets/note_tree.spec.ts@@ -1,9 +1,13 @@import $ from "jquery";import { afterEach, describe, expect, it, vi } from "vitest";+import appContext from "../components/app_context.js";+import type TabManager from "../components/tab_manager.js";import hoistedNoteService from "../services/hoisted_note.js";import noteCreateService from "../services/note_create.js";+import server from "../services/server.js";import treeService from "../services/tree.js";+import ws from "../services/ws.js";import NoteTreeWidget, { publishDropMarkerShift } from "./note_tree.js";vi.mock("../services/import.js", () => ({ uploadFiles: vi.fn() }));@@ -91,6 +95,26 @@ describe("NoteTreeWidget", () => {});});+ it("opens a new launcher in the tree's own note context, keeping the dialog that holds the tree open", async () => {+ const widget = await renderWidget();+ const treeContext = { ntxId: "_tree-popup", setNote: vi.fn() };+ const activeContext = { ntxId: "tab", setNote: vi.fn() };+ (widget as { noteContext?: unknown }).noteContext = treeContext;+ vi.spyOn(server, "post").mockResolvedValue({ success: true, note: { noteId: "newLauncher" } });+ vi.spyOn(ws, "waitForMaxKnownEntityChangeId").mockResolvedValue(undefined);++ const tabManager = appContext.tabManager;+ appContext.tabManager = { getActiveContext: () => activeContext } as unknown as TabManager;+ try {+ await widget.createLauncherNote({ data: { noteId: "_lbVisibleLaunchers" } } as unknown as Fancytree.FancytreeNode, "note");+ } finally {+ appContext.tabManager = tabManager;+ }++ expect(treeContext.setNote).toHaveBeenCalledWith("newLauncher", { keepActiveDialog: true });+ expect(activeContext.setNote).not.toHaveBeenCalled();+ });+it("refresh() consults hoisting with the tree's own hoistedNoteId for hidden-subtree paths", async () => {const widget = await renderWidget();(widget as { noteContext?: unknown }).noteContext = {diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex 6f37b17995..8b7f144664 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -492,20 +492,11 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {const notePath = treeService.getNotePath(data.node);- // Prefer the context this tree is bound to (e.g. a popup editor with its own- // hoisted context) over the globally active tab. For the main sidebar tree the- // bound context already is the active context, so behaviour is unchanged there.- const activeNoteContext = this.noteContext ?? appContext.tabManager.getActiveContext();- const opts: SetNoteOpts = {};- if (activeNoteContext?.viewScope?.viewMode === "contextual-help") {- opts.viewScope = activeNoteContext.viewScope;+ const { noteContext, opts } = this.getNavigationTarget();+ if (noteContext?.viewScope?.viewMode === "contextual-help") {+ opts.viewScope = noteContext.viewScope;}- // When this tree drives a context other than the active tab (e.g. one embedded in a- // popup editor), keep any open dialog so navigating the tree doesn't dismiss the popup.- if (activeNoteContext && activeNoteContext !== appContext.tabManager.getActiveContext()) {- opts.keepActiveDialog = true;- }- await activeNoteContext?.setNote(notePath, opts);+ await noteContext?.setNote(notePath, opts);},expand: (event, data) => this.setExpanded(data.node.data.branchId, true),collapse: (event, data) => this.setExpanded(data.node.data.branchId, false),@@ -1863,7 +1854,19 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {await ws.waitForMaxKnownEntityChangeId();- appContext.tabManager.getActiveContext()?.setNote(resp.note.noteId);+ const { noteContext, opts } = this.getNavigationTarget();+ await noteContext?.setNote(resp.note.noteId, opts);+ }++ /** Returns the tree's own note context (or the active one), with `keepActiveDialog` set when they differ. */+ private getNavigationTarget() {+ const activeContext = appContext.tabManager.getActiveContext();+ const noteContext = this.noteContext ?? activeContext;+ const opts: SetNoteOpts = {};+ if (noteContext && noteContext !== activeContext) {+ opts.keepActiveDialog = true;+ }+ return { noteContext, opts };}}