Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): run the quick edit popup's note, not the active tab's

Elian Dorana57979dadc95
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index edbdcbe2c7..2150073807 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -313,7 +313,10 @@ export type CommandMappings = {
zoomFactor: string;
};
- runActiveNote: CommandData;
+ runActiveNote: CommandData & {
+ /** The note to run, for a context that is not a tab, such as the quick edit popup; the active tab's note otherwise. */
+ noteId?: string;
+ };
scrollContainerTo: CommandData & {
position: number;
};
diff --git a/apps/client/src/components/entrypoints.spec.ts b/apps/client/src/components/entrypoints.spec.ts
index 276a26a2ce..1537d6b2b2 100644
--- a/apps/client/src/components/entrypoints.spec.ts
+++ b/apps/client/src/components/entrypoints.spec.ts
@@ -1,7 +1,14 @@
import type { ElectronApi } from "@triliumnext/commons";
import { beforeEach, describe, expect, it, vi } from "vitest";
+import froca from "../services/froca.js";
+import server from "../services/server.js";
+import toastService from "../services/toast.js";
+import { buildNote } from "../test/easy-froca.js";
+import appContext from "./app_context.js";
import Entrypoints from "./entrypoints.js";
+import type NoteContext from "./note_context.js";
+import type TabManager from "./tab_manager.js";
describe("openInWindowCommand", () => {
const entrypoints = new Entrypoints();
@@ -70,3 +77,30 @@ describe("logoutCommand", () => {
window.history.replaceState({}, "", "/");
});
});
+
+describe("runActiveNoteCommand", () => {
+ const entrypoints = new Entrypoints();
+
+ beforeEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it("runs the note the command names, in its own context, rather than the active tab's", async () => {
+ buildNote({ id: "tabNote", title: "Tab", type: "code", mime: "text/x-sqlite;schema=trilium" });
+ buildNote({ id: "popupQuery", title: "Popup", type: "code", mime: "text/x-sqlite;schema=trilium" });
+ const tabContext = { ntxId: "tab", note: froca.getNoteFromCache("tabNote") } as NoteContext;
+ appContext.tabManager = { getActiveContext: () => tabContext } as TabManager;
+ const post = vi.spyOn(server, "post").mockResolvedValue({ success: true, results: [] });
+ const triggerEvent = vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined);
+ vi.spyOn(toastService, "showMessage").mockImplementation(() => {});
+
+ await entrypoints.runActiveNoteCommand({ ntxId: "_popup-editor", noteId: "popupQuery" });
+ expect(post).toHaveBeenCalledWith("sql/execute/popupQuery");
+ expect(triggerEvent).toHaveBeenCalledWith("sqlQueryResults", expect.objectContaining({ ntxId: "_popup-editor" }));
+
+ // Without a note named, as the badges trigger it, the active tab's note runs.
+ post.mockClear();
+ await entrypoints.runActiveNoteCommand();
+ expect(post).toHaveBeenCalledWith("sql/execute/tabNote");
+ });
+});
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 366a40c71a..8e33e7870a 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -13,7 +13,7 @@ import server from "../services/server.js";
import toastService from "../services/toast.js";
import utils from "../services/utils.js";
import ws from "../services/ws.js";
-import appContext, { type NoteCommandData } from "./app_context.js";
+import appContext, { type CommandListenerData, type NoteCommandData } from "./app_context.js";
import Component from "./component.js";
export default class Entrypoints extends Component {
@@ -171,12 +171,10 @@ export default class Entrypoints extends Component {
await appContext.tabManager.openInSameTab(todayNote.noteId);
}
- async runActiveNoteCommand() {
- const noteContext = appContext.tabManager.getActiveContext();
- if (!noteContext) {
- return;
- }
- const { ntxId, note } = noteContext;
+ async runActiveNoteCommand({ ntxId: requestedNtxId, noteId }: CommandListenerData<"runActiveNote"> = {}) {
+ const activeContext = appContext.tabManager.getActiveContext();
+ const note = noteId ? await froca.getNote(noteId) : activeContext?.note;
+ const ntxId = noteId ? requestedNtxId : activeContext?.ntxId;
// ctrl+enter is also used elsewhere, so make sure we're running only when appropriate
if (!note || note.type !== "code") {
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index b457b06c4b..d48d2f5e69 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -138,7 +138,7 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC
await spacedUpdate.updateNowIfNecessary();
}
- return await parentComponent?.parent?.triggerCommand("runActiveNote", params);
+ return await parentComponent?.parent?.triggerCommand("runActiveNote", { ...params, noteId: note.noteId });
}
});