From 04f67776279dc3a80680da9549a153a87945d1d5 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 20 Oct 2025 14:04:04 +0300 Subject: [PATCH] chore(client/print): address requested changes --- apps/client/src/print.tsx | 1 + apps/client/src/types.d.ts | 3 +++ apps/client/src/widgets/note_detail.ts | 7 ++++++- apps/server/src/services/window.ts | 17 ++++++++++------- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsx index 4aff8729b..e762f5781 100644 --- a/apps/client/src/print.tsx +++ b/apps/client/src/print.tsx @@ -26,6 +26,7 @@ function App({ note, noteId }: { note: FNote | null | undefined, noteId: string const onReady = useCallback(() => { if (sentReadyEvent.current) return; window.dispatchEvent(new Event("note-ready")); + window._noteReady = true; sentReadyEvent.current = true; }, []); const props: RendererProps | undefined | null = note && { note, onReady }; diff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.ts index 2546d2ffa..c5a93bd0a 100644 --- a/apps/client/src/types.d.ts +++ b/apps/client/src/types.d.ts @@ -59,6 +59,9 @@ declare global { process?: ElectronProcess; glob?: CustomGlobals; + /** On the printing endpoint, set to true when the note has fully loaded and is ready to be printed/exported as PDF. */ + _noteReady?: boolean; + EXCALIDRAW_ASSET_PATH?: string; } diff --git a/apps/client/src/widgets/note_detail.ts b/apps/client/src/widgets/note_detail.ts index a701d442b..a976b97ce 100644 --- a/apps/client/src/widgets/note_detail.ts +++ b/apps/client/src/widgets/note_detail.ts @@ -322,7 +322,12 @@ export default class NoteDetailWidget extends NoteContextAwareWidget { iframe.className = "print-iframe"; document.body.appendChild(iframe); iframe.onload = () => { - if (!iframe.contentWindow) return; + if (!iframe.contentWindow) { + toast.closePersistent("printing"); + document.body.removeChild(iframe); + return; + } + iframe.contentWindow.addEventListener("note-ready", () => { toast.closePersistent("printing"); iframe.contentWindow?.print(); diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts index 69d273158..459ebdf59 100644 --- a/apps/server/src/services/window.ts +++ b/apps/server/src/services/window.ts @@ -84,19 +84,18 @@ interface ExportAsPdfOpts { electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => { const browserWindow = await getBrowserWindowForPrinting(e, notePath); browserWindow.webContents.print({}, (success, failureReason) => { - if (success) { - browserWindow.destroy(); - } else { + if (!success) { electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason); } e.sender.send("print-done"); + browserWindow.destroy(); }); }); electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pageSize }: ExportAsPdfOpts) => { - async function print() { - const browserWindow = await getBrowserWindowForPrinting(e, notePath); + const browserWindow = await getBrowserWindowForPrinting(e, notePath); + async function print() { const filePath = electron.dialog.showSaveDialogSync(browserWindow, { defaultPath: formatDownloadTitle(title, "file", "application/pdf"), filters: [ @@ -138,8 +137,12 @@ electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pag electron.shell.openPath(filePath); } - await print(); - e.sender.send("print-done"); + try { + await print(); + } finally { + e.sender.send("print-done"); + browserWindow.destroy(); + } }); async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {