mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	fix(desktop/print): proper reporting when it finishes
This commit is contained in:
		
							parent
							
								
									d1854d85ce
								
							
						
					
					
						commit
						3cf7e709fc
					
				@ -1723,7 +1723,8 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "note_detail": {
 | 
					  "note_detail": {
 | 
				
			||||||
    "could_not_find_typewidget": "Could not find typeWidget for type '{{type}}'",
 | 
					    "could_not_find_typewidget": "Could not find typeWidget for type '{{type}}'",
 | 
				
			||||||
    "printing": "Printing in progress..."
 | 
					    "printing": "Printing in progress...",
 | 
				
			||||||
 | 
					    "printing_pdf": "Exporting to PDF in progress..."
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "note_title": {
 | 
					  "note_title": {
 | 
				
			||||||
    "placeholder": "type note's title here..."
 | 
					    "placeholder": "type note's title here..."
 | 
				
			||||||
 | 
				
			|||||||
@ -141,6 +141,13 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
 | 
				
			|||||||
    doRender() {
 | 
					    doRender() {
 | 
				
			||||||
        this.$widget = $(TPL);
 | 
					        this.$widget = $(TPL);
 | 
				
			||||||
        this.contentSized();
 | 
					        this.contentSized();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (utils.isElectron()) {
 | 
				
			||||||
 | 
					            const { ipcRenderer } = utils.dynamicRequire("electron");
 | 
				
			||||||
 | 
					            ipcRenderer.on("print-done", () => {
 | 
				
			||||||
 | 
					                toast.closePersistent("printing");
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async refresh() {
 | 
					    async refresh() {
 | 
				
			||||||
@ -330,6 +337,12 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
 | 
				
			|||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        toast.showPersistent({
 | 
				
			||||||
 | 
					            icon: "bx bx-loader-circle bx-spin",
 | 
				
			||||||
 | 
					            message: t("note_detail.printing_pdf"),
 | 
				
			||||||
 | 
					            id: "printing"
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const { ipcRenderer } = utils.dynamicRequire("electron");
 | 
					        const { ipcRenderer } = utils.dynamicRequire("electron");
 | 
				
			||||||
        ipcRenderer.send("export-as-pdf", {
 | 
					        ipcRenderer.send("export-as-pdf", {
 | 
				
			||||||
            title: this.note.title,
 | 
					            title: this.note.title,
 | 
				
			||||||
 | 
				
			|||||||
@ -89,30 +89,12 @@ electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
 | 
				
			|||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason);
 | 
					            electron.dialog.showErrorBox(t("pdf.unable-to-print"), failureReason);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        e.sender.send("print-done");
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
 | 
					 | 
				
			||||||
    const browserWindow = new electron.BrowserWindow({
 | 
					 | 
				
			||||||
        show: false,
 | 
					 | 
				
			||||||
        webPreferences: {
 | 
					 | 
				
			||||||
            nodeIntegration: true,
 | 
					 | 
				
			||||||
            contextIsolation: false,
 | 
					 | 
				
			||||||
            offscreen: true,
 | 
					 | 
				
			||||||
            session: e.sender.session
 | 
					 | 
				
			||||||
        },
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
    await browserWindow.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`);
 | 
					 | 
				
			||||||
    await browserWindow.webContents.executeJavaScript(`
 | 
					 | 
				
			||||||
        new Promise(resolve => {
 | 
					 | 
				
			||||||
            if (window._noteReady) return resolve();
 | 
					 | 
				
			||||||
            window.addEventListener("note-ready", () => resolve());
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
    `);
 | 
					 | 
				
			||||||
    return browserWindow;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
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) => {
 | 
				
			||||||
 | 
					    async function print() {
 | 
				
			||||||
        const browserWindow = await getBrowserWindowForPrinting(e, notePath);
 | 
					        const browserWindow = await getBrowserWindowForPrinting(e, notePath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
 | 
					        const filePath = electron.dialog.showSaveDialogSync(browserWindow, {
 | 
				
			||||||
@ -124,9 +106,7 @@ electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pag
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    if (!filePath) {
 | 
					        if (!filePath) return;
 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let buffer: Buffer;
 | 
					        let buffer: Buffer;
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
@ -156,8 +136,32 @@ electron.ipcMain.on("export-as-pdf", async (e, { title, notePath, landscape, pag
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        electron.shell.openPath(filePath);
 | 
					        electron.shell.openPath(filePath);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await print();
 | 
				
			||||||
 | 
					    e.sender.send("print-done");
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
 | 
				
			||||||
 | 
					    const browserWindow = new electron.BrowserWindow({
 | 
				
			||||||
 | 
					        show: false,
 | 
				
			||||||
 | 
					        webPreferences: {
 | 
				
			||||||
 | 
					            nodeIntegration: true,
 | 
				
			||||||
 | 
					            contextIsolation: false,
 | 
				
			||||||
 | 
					            offscreen: true,
 | 
				
			||||||
 | 
					            session: e.sender.session
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    await browserWindow.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`);
 | 
				
			||||||
 | 
					    await browserWindow.webContents.executeJavaScript(`
 | 
				
			||||||
 | 
					        new Promise(resolve => {
 | 
				
			||||||
 | 
					            if (window._noteReady) return resolve();
 | 
				
			||||||
 | 
					            window.addEventListener("note-ready", () => resolve());
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    `);
 | 
				
			||||||
 | 
					    return browserWindow;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function createMainWindow(app: App) {
 | 
					async function createMainWindow(app: App) {
 | 
				
			||||||
    if ("setUserTasks" in app) {
 | 
					    if ("setUserTasks" in app) {
 | 
				
			||||||
        app.setUserTasks([
 | 
					        app.setUserTasks([
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user