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): activate notes created from tree popups inside the popup
Elian Doranac1bb1e83eb8
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.tsindex b664e7e54f..f188dea234 100644--- a/apps/client/src/menus/tree_context_menu.ts+++ b/apps/client/src/menus/tree_context_menu.ts@@ -1,5 +1,6 @@import appContext, { type ContextMenuCommandData, type FilteredCommandNames } from "../components/app_context.js";import type Component from "../components/component.js";+import type NoteContext from "../components/note_context.js";import type { SelectMenuItemEventListener } from "../components/events.js";import type FAttachment from "../entities/fattachment.js";import type FBranch from "../entities/fbranch.js";@@ -49,6 +50,12 @@ export interface TreeContextMenuContext {* own parent component.*/component: Component;+ /**+ * Note context to activate newly created notes in. Trees hosted in popup dialogs (e.g. the+ * task states tree popup) pass their own context, which lives outside the tab manager —+ * without it, creation would activate the note in the background tab.+ */+ noteContext?: NoteContext;/** Branches to operate on in bulk-capable commands (defaults to [branch.branchId]). */selectedOrActiveBranchIds?: string[];/** Notes to operate on in bulk-capable commands (defaults to [note.noteId]). */@@ -352,14 +359,16 @@ export async function handleTreeContextMenuSelect(type,mime,isProtected: parentNote?.isProtected ?? false,- templateNoteId+ templateNoteId,+ noteContext: resolved.noteContext});} else if (command === "insertChildNote") {noteCreateService.createNote(notePath, {type,mime,isProtected: note.isProtected,- templateNoteId+ templateNoteId,+ noteContext: resolved.noteContext});} else if (command === "openNoteInSplit") {const subContexts = appContext.tabManager.getActiveContext()?.getSubContexts();@@ -462,6 +471,7 @@ export default class TreeContextMenu implements SelectMenuItemEventListener<Treebranch,notePath: treeService.getNotePath(this.node),component: this.treeWidget,+ noteContext: this.treeWidget.noteContext,selectedOrActiveBranchIds: this.treeWidget.getSelectedOrActiveBranchIds(this.node),selectedOrActiveNoteIds: this.treeWidget.getSelectedOrActiveNoteIds(this.node),selectedNotes,diff --git a/apps/client/src/services/note_create.spec.ts b/apps/client/src/services/note_create.spec.tsindex 61d1a1a674..49cb9006f6 100644--- a/apps/client/src/services/note_create.spec.ts+++ b/apps/client/src/services/note_create.spec.ts@@ -99,12 +99,25 @@ describe("createNote", () => {expect.objectContaining({ title: "Hello", content: "", isProtected: false }),"comp-1");- expect(setNote).toHaveBeenCalledWith(`root/${NOTE_ID}`);- expect(triggerEvent).toHaveBeenCalledWith("focusAndSelectTitle", { isNewNote: true });+ expect(setNote).toHaveBeenCalledWith(`root/${NOTE_ID}`, { keepActiveDialog: false });+ expect(triggerEvent).toHaveBeenCalledWith("focusAndSelectTitle", { isNewNote: true, ntxId: "ntx-1" });expect(result.note).toBe(childNote);expect(result.branch).toBe(froca.getBranch(BRANCH_ID));});+ it("activates in the supplied noteContext (popup) and keeps the dialog open", async () => {+ // The tab manager's active context must remain untouched (background tab)+ const activeSetNote = setActiveContext(true);+ const popupSetNote = vi.fn(async () => {});+ const popupContext = { ntxId: "ntx-popup", setNote: popupSetNote } as any;++ await noteCreateService.createNote("root", { noteContext: popupContext });++ expect(popupSetNote).toHaveBeenCalledWith(`root/${NOTE_ID}`, { keepActiveDialog: true });+ expect(activeSetNote).not.toHaveBeenCalled();+ expect(triggerEvent).toHaveBeenCalledWith("focusAndSelectTitle", { isNewNote: true, ntxId: "ntx-popup" });+ });+it("focuses content when focus=content", async () => {setActiveContext(true);await noteCreateService.createNote("root", { focus: "content" });@@ -116,7 +129,7 @@ describe("createNote", () => {const setNote = setActiveContext(true);// an out-of-range focus value still activates the note but triggers no focus eventawait noteCreateService.createNote("root", { focus: undefined as any });- expect(setNote).toHaveBeenCalledWith(`root/${NOTE_ID}`);+ expect(setNote).toHaveBeenCalledWith(`root/${NOTE_ID}`, { keepActiveDialog: false });expect(triggerEvent).not.toHaveBeenCalled();});diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.tsindex 9c4f37f4c5..f1ae13f32a 100644--- a/apps/client/src/services/note_create.ts+++ b/apps/client/src/services/note_create.ts@@ -2,6 +2,7 @@ import type { CKTextEditor } from "@triliumnext/ckeditor5";import { AttributeRow } from "@triliumnext/commons";import appContext from "../components/app_context.js";+import type NoteContext from "../components/note_context.js";import type FBranch from "../entities/fbranch.js";import type FNote from "../entities/fnote.js";import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js";@@ -28,6 +29,13 @@ export interface CreateNoteOpts {textEditor?: CKTextEditor;/** Attributes to be set on the note. These are set atomically on note creation, so entity changes are not sent for attributes defined here. */attributes?: Omit<AttributeRow, "noteId" | "attributeId">[];+ /**+ * Note context to activate the new note in. Popup dialogs (quick edit, tree popup) pass their+ * own context here — it lives outside the tab manager, so defaulting to the active tab would+ * activate the note in the background and close the dialog. When set, the surrounding dialog+ * is kept open.+ */+ noteContext?: NoteContext;}interface Response {@@ -84,12 +92,14 @@ async function createNote(parentNotePath: string | undefined, options: CreateNotawait ws.waitForMaxKnownEntityChangeId();- const activeNoteContext = appContext.tabManager.getActiveContext();+ const activeNoteContext = options.noteContext ?? appContext.tabManager.getActiveContext();if (activeNoteContext && options.activate) {- await activeNoteContext.setNote(`${parentNotePath}/${note.noteId}`);+ await activeNoteContext.setNote(`${parentNotePath}/${note.noteId}`, {+ keepActiveDialog: !!options.noteContext+ });if (options.focus === "title") {- appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });+ appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true, ntxId: activeNoteContext.ntxId });} else if (options.focus === "content") {appContext.triggerEvent("focusOnDetail", { ntxId: activeNoteContext.ntxId });}diff --git a/apps/client/src/widgets/note_title.tsx b/apps/client/src/widgets/note_title.tsxindex ae4b5ac357..3b77d913eb 100644--- a/apps/client/src/widgets/note_title.tsx+++ b/apps/client/src/widgets/note_title.tsx@@ -71,7 +71,10 @@ export default function NoteTitleWidget(props: {className?: string}) {}, [title]);useTriliumEvents([ "focusOnTitle", "focusAndSelectTitle" ], (e, eventName) => {- if (noteContext?.isActive() && textBoxRef.current) {+ // When the event targets a specific context (e.g. a popup's own context, which is never+ // "active" in the tab manager), match on it; otherwise fall back to the active context.+ const isTargeted = e.ntxId ? e.ntxId === noteContext?.ntxId : noteContext?.isActive();+ if (isTargeted && textBoxRef.current) {// In the new layout, there are two NoteTitleWidget instances. Only handle if visible.if (!textBoxRef.current.checkVisibility({ checkOpacity: true })) {return;diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex b9f82209e3..7b5fde0b92 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -234,7 +234,12 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {} else if (target.classList.contains("add-note-button")) {const node = $.ui.fancytree.getNode(e as unknown as Event);const parentNotePath = treeService.getNotePath(node);- noteCreateService.createNote(parentNotePath, { isProtected: node.data.isProtected });+ noteCreateService.createNote(parentNotePath, {+ isProtected: node.data.isProtected,+ // Activate in this tree's own context — in popup dialogs (e.g. the task states+ // tree popup) it is not the tab manager's active context.+ noteContext: this.noteContext+ });} else if (target.classList.contains("enter-workspace-button")) {const node = $.ui.fancytree.getNode(e as unknown as Event);this.triggerCommand("hoistNote", { noteId: node.data.noteId });