Skip to content

Build your personal knowledge base with Trilium Notes

fix(print): live-print a render note only from its render view

Elian Doran3ba53ab7d07a
diff --git a/apps/client/src/services/live_print.spec.ts b/apps/client/src/services/live_print.spec.ts
index fe642deddd..ca50a160b1 100644
--- a/apps/client/src/services/live_print.spec.ts
+++ b/apps/client/src/services/live_print.spec.ts
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { buildNote } from "../test/easy-froca";
-import { endLivePrint, printLiveInBrowser, startLivePrint } from "./live_print";
+import { endLivePrint, getLivePrintTarget, printLiveInBrowser, startLivePrint } from "./live_print";
function addThemeLink(href: string, media?: string) {
const link = document.createElement("link");
@@ -107,3 +107,17 @@ describe("live print", () => {
expect(target.classList.contains("print-live-target")).toBe(false);
});
});
+
+describe("getLivePrintTarget", () => {
+ it("prints the render output only while the render widget is the one shown", () => {
+ const noteDetail = document.createElement("div");
+ noteDetail.innerHTML = `<div class="note-detail-render"><div class="note-detail-render-content">Output</div></div>`;
+ const content = noteDetail.querySelector(".note-detail-render-content");
+
+ expect(getLivePrintTarget(noteDetail, "render")).toBe(content);
+ // Another view of the note (attachments, source) keeps the render widget mounted but hidden.
+ expect(getLivePrintTarget(noteDetail, "attachmentList")).toBeUndefined();
+ expect(getLivePrintTarget(noteDetail, undefined)).toBeUndefined();
+ expect(getLivePrintTarget(null, "render")).toBeUndefined();
+ });
+});
diff --git a/apps/client/src/services/live_print.ts b/apps/client/src/services/live_print.ts
index 18ef9591b9..8140127b25 100644
--- a/apps/client/src/services/live_print.ts
+++ b/apps/client/src/services/live_print.ts
@@ -18,6 +18,16 @@ const UNSCROLL_CLASS = "print-live-unscroll";
let activeTarget: HTMLElement | undefined;
+/**
+ * Returns the element of `noteDetail` that a live print prints when it shows the type widget
+ * `shownType`, or `undefined` when the note prints on the `?print` page instead.
+ */
+export function getLivePrintTarget(noteDetail: HTMLElement | null, shownType: string | undefined) {
+ // Another view of a render note (source, attachments) keeps the render widget mounted but hidden.
+ if (shownType !== "render") return undefined;
+ return noteDetail?.querySelector<HTMLElement>(".note-detail-render-content") ?? undefined;
+}
+
/** Prepares the window to print `target`, ending any live print still active. */
export async function startLivePrint(note: FNote, target: HTMLElement) {
endLivePrint();
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index bf5f2ffeeb..afa2355484 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -13,7 +13,7 @@ import attributes from "../services/attributes";
import dialog from "../services/dialog";
import froca from "../services/froca";
import { t } from "../services/i18n";
-import { endLivePrint, printLiveInBrowser } from "../services/live_print";
+import { endLivePrint, getLivePrintTarget, printLiveInBrowser } from "../services/live_print";
import { stopBackgroundMedia } from "../services/media_playback";
import protected_session_holder from "../services/protected_session_holder";
import toast from "../services/toast.js";
@@ -185,9 +185,7 @@ export default function NoteDetail() {
if (note.type === "file" && note.mime === "application/pdf") return;
// A render note prints from the screen, keeping its script's state.
- const liveTarget = note.type === "render"
- ? containerRef.current?.querySelector<HTMLElement>(".note-detail-render-content") ?? undefined
- : undefined;
+ const liveTarget = getLivePrintTarget(containerRef.current, type);
if (isElectron() && noteContext.notePath) {
appContext.triggerCommand("showPrintPreview", { note, notePath: noteContext.notePath, liveTarget });