feat(print): report progress on export to PDF as well

This commit is contained in:
Elian Doran 2025-11-21 20:05:43 +02:00
parent 1a6e653600
commit 6023d53506
No known key found for this signature in database
2 changed files with 7 additions and 14 deletions

View File

@ -113,10 +113,7 @@ export default function NoteDetail() {
useEffect(() => { useEffect(() => {
if (!isElectron()) return; if (!isElectron()) return;
const { ipcRenderer } = dynamicRequire("electron"); const { ipcRenderer } = dynamicRequire("electron");
const onPrintProgress = (_e: any, progress: number) => { const onPrintProgress = (_e: any, { progress, action }: { progress: number, action: "printing" | "exporting_pdf" }) => showToast(action, progress);
console.log("Got print progress:", progress);
showToast("printing", progress);
};
const onPrintDone = () => toast.closePersistent("printing"); const onPrintDone = () => toast.closePersistent("printing");
ipcRenderer.on("print-progress", onPrintProgress); ipcRenderer.on("print-progress", onPrintProgress);
ipcRenderer.on("print-done", onPrintDone); ipcRenderer.on("print-done", onPrintDone);
@ -179,11 +176,7 @@ export default function NoteDetail() {
useTriliumEvent("exportAsPdf", () => { useTriliumEvent("exportAsPdf", () => {
if (!noteContext?.isActive() || !note) return; if (!noteContext?.isActive() || !note) return;
toast.showPersistent({ showToast("exporting_pdf");
icon: "bx bx-loader-circle bx-spin",
message: t("note_detail.printing_pdf"),
id: "printing"
});
const { ipcRenderer } = dynamicRequire("electron"); const { ipcRenderer } = dynamicRequire("electron");
ipcRenderer.send("export-as-pdf", { ipcRenderer.send("export-as-pdf", {
@ -332,7 +325,7 @@ function checkFullHeight(noteContext: NoteContext | undefined, type: ExtendedNot
function showToast(type: "printing" | "exporting_pdf", progress: number = 0) { function showToast(type: "printing" | "exporting_pdf", progress: number = 0) {
toast.showPersistent({ toast.showPersistent({
icon: "bx bx-loader-circle bx-spin", icon: "bx bx-loader-circle bx-spin",
message: t("note_detail.printing"), message: type === "printing" ? t("note_detail.printing") : t("note_detail.printing_pdf"),
id: "printing", id: "printing",
progress progress
}); });

View File

@ -80,7 +80,7 @@ interface ExportAsPdfOpts {
} }
electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => { electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
const browserWindow = await getBrowserWindowForPrinting(e, notePath); const browserWindow = await getBrowserWindowForPrinting(e, notePath, "printing");
browserWindow.webContents.print({}, (success, failureReason) => { browserWindow.webContents.print({}, (success, failureReason) => {
if (!success) { if (!success) {
electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason); electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason);
@ -91,7 +91,7 @@ electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
}); });
electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pageSize }: ExportAsPdfOpts) => { electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pageSize }: ExportAsPdfOpts) => {
const browserWindow = await getBrowserWindowForPrinting(e, notePath); const browserWindow = await getBrowserWindowForPrinting(e, notePath, "exporting_pdf");
async function print() { async function print() {
const filePath = electron.dialog.showSaveDialogSync(browserWindow, { const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
@ -143,7 +143,7 @@ electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pag
} }
}); });
async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) { async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string, action: "printing" | "exporting_pdf") {
const browserWindow = new electron.BrowserWindow({ const browserWindow = new electron.BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
@ -154,7 +154,7 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
}, },
}); });
const progressCallback = (_e, progress: number) => e.sender.send("print-progress", progress); const progressCallback = (_e, progress: number) => e.sender.send("print-progress", { progress, action });
ipcMain.on("print-progress", progressCallback); ipcMain.on("print-progress", progressCallback);
await browserWindow.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`); await browserWindow.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`);