feat(print): support progress report on electron

This commit is contained in:
Elian Doran 2025-11-21 19:57:13 +02:00
parent 586c707e51
commit 1a6e653600
No known key found for this signature in database
3 changed files with 23 additions and 8 deletions

View File

@ -3,6 +3,7 @@ import { render } from "preact";
import { CustomNoteList, useNoteViewType } from "./widgets/collections/NoteList";
import { useCallback, useLayoutEffect, useRef } from "preact/hooks";
import content_renderer, { applyInlineMermaid } from "./services/content_renderer";
import { dynamicRequire, isElectron } from "./services/utils";
interface RendererProps {
note: FNote;
@ -25,7 +26,12 @@ async function main() {
function App({ note, noteId }: { note: FNote | null | undefined, noteId: string }) {
const sentReadyEvent = useRef(false);
const onProgressChanged = useCallback((progress: number) => {
window.dispatchEvent(new CustomEvent("note-load-progress", { detail: { progress } }));
if (isElectron()) {
const { ipcRenderer } = dynamicRequire('electron');
ipcRenderer.send("print-progress", progress);
} else {
window.dispatchEvent(new CustomEvent("note-load-progress", { detail: { progress } }));
}
}, []);
const onReady = useCallback(() => {
if (sentReadyEvent.current) return;

View File

@ -113,11 +113,17 @@ export default function NoteDetail() {
useEffect(() => {
if (!isElectron()) return;
const { ipcRenderer } = dynamicRequire("electron");
const listener = () => {
toast.closePersistent("printing");
const onPrintProgress = (_e: any, progress: number) => {
console.log("Got print progress:", progress);
showToast("printing", progress);
};
const onPrintDone = () => toast.closePersistent("printing");
ipcRenderer.on("print-progress", onPrintProgress);
ipcRenderer.on("print-done", onPrintDone);
return () => {
ipcRenderer.off("print-progress", onPrintProgress);
ipcRenderer.off("print-done", onPrintDone);
};
ipcRenderer.on("print-done", listener);
return () => ipcRenderer.off("print-done", listener);
}, []);
useTriliumEvent("executeInActiveNoteDetailWidget", ({ callback }) => {

View File

@ -7,13 +7,11 @@ import log from "./log.js";
import sqlInit from "./sql_init.js";
import cls from "./cls.js";
import keyboardActionsService from "./keyboard_actions.js";
import electron from "electron";
import electron, { ipcMain } from "electron";
import type { App, BrowserWindowConstructorOptions, BrowserWindow, WebContents, IpcMainEvent } from "electron";
import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js";
import { t } from "i18next";
import { RESOURCE_DIR } from "./resource_dir.js";
import { PerformanceObserverEntryList } from "perf_hooks";
import options from "./options.js";
// Prevent the window being garbage collected
let mainWindow: BrowserWindow | null;
@ -155,6 +153,10 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
session: e.sender.session
},
});
const progressCallback = (_e, progress: number) => e.sender.send("print-progress", progress);
ipcMain.on("print-progress", progressCallback);
await browserWindow.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`);
await browserWindow.webContents.executeJavaScript(`
new Promise(resolve => {
@ -162,6 +164,7 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
window.addEventListener("note-ready", () => resolve());
});
`);
ipcMain.off("print-progress", progressCallback);
return browserWindow;
}