mirror of
https://github.com/zadam/trilium.git
synced 2025-11-26 10:34:25 +01:00
feat(print): support progress report on electron
This commit is contained in:
parent
586c707e51
commit
1a6e653600
@ -3,6 +3,7 @@ import { render } from "preact";
|
|||||||
import { CustomNoteList, useNoteViewType } from "./widgets/collections/NoteList";
|
import { CustomNoteList, useNoteViewType } from "./widgets/collections/NoteList";
|
||||||
import { useCallback, useLayoutEffect, useRef } from "preact/hooks";
|
import { useCallback, useLayoutEffect, useRef } from "preact/hooks";
|
||||||
import content_renderer, { applyInlineMermaid } from "./services/content_renderer";
|
import content_renderer, { applyInlineMermaid } from "./services/content_renderer";
|
||||||
|
import { dynamicRequire, isElectron } from "./services/utils";
|
||||||
|
|
||||||
interface RendererProps {
|
interface RendererProps {
|
||||||
note: FNote;
|
note: FNote;
|
||||||
@ -25,7 +26,12 @@ async function main() {
|
|||||||
function App({ note, noteId }: { note: FNote | null | undefined, noteId: string }) {
|
function App({ note, noteId }: { note: FNote | null | undefined, noteId: string }) {
|
||||||
const sentReadyEvent = useRef(false);
|
const sentReadyEvent = useRef(false);
|
||||||
const onProgressChanged = useCallback((progress: number) => {
|
const onProgressChanged = useCallback((progress: number) => {
|
||||||
|
if (isElectron()) {
|
||||||
|
const { ipcRenderer } = dynamicRequire('electron');
|
||||||
|
ipcRenderer.send("print-progress", progress);
|
||||||
|
} else {
|
||||||
window.dispatchEvent(new CustomEvent("note-load-progress", { detail: { progress } }));
|
window.dispatchEvent(new CustomEvent("note-load-progress", { detail: { progress } }));
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
const onReady = useCallback(() => {
|
const onReady = useCallback(() => {
|
||||||
if (sentReadyEvent.current) return;
|
if (sentReadyEvent.current) return;
|
||||||
|
|||||||
@ -113,11 +113,17 @@ export default function NoteDetail() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isElectron()) return;
|
if (!isElectron()) return;
|
||||||
const { ipcRenderer } = dynamicRequire("electron");
|
const { ipcRenderer } = dynamicRequire("electron");
|
||||||
const listener = () => {
|
const onPrintProgress = (_e: any, progress: number) => {
|
||||||
toast.closePersistent("printing");
|
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 }) => {
|
useTriliumEvent("executeInActiveNoteDetailWidget", ({ callback }) => {
|
||||||
|
|||||||
@ -7,13 +7,11 @@ import log from "./log.js";
|
|||||||
import sqlInit from "./sql_init.js";
|
import sqlInit from "./sql_init.js";
|
||||||
import cls from "./cls.js";
|
import cls from "./cls.js";
|
||||||
import keyboardActionsService from "./keyboard_actions.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 type { App, BrowserWindowConstructorOptions, BrowserWindow, WebContents, IpcMainEvent } from "electron";
|
||||||
import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js";
|
import { formatDownloadTitle, isDev, isMac, isWindows } from "./utils.js";
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
import { RESOURCE_DIR } from "./resource_dir.js";
|
import { RESOURCE_DIR } from "./resource_dir.js";
|
||||||
import { PerformanceObserverEntryList } from "perf_hooks";
|
|
||||||
import options from "./options.js";
|
|
||||||
|
|
||||||
// Prevent the window being garbage collected
|
// Prevent the window being garbage collected
|
||||||
let mainWindow: BrowserWindow | null;
|
let mainWindow: BrowserWindow | null;
|
||||||
@ -155,6 +153,10 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
|
|||||||
session: e.sender.session
|
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.loadURL(`http://127.0.0.1:${port}/?print#${notePath}`);
|
||||||
await browserWindow.webContents.executeJavaScript(`
|
await browserWindow.webContents.executeJavaScript(`
|
||||||
new Promise(resolve => {
|
new Promise(resolve => {
|
||||||
@ -162,6 +164,7 @@ async function getBrowserWindowForPrinting(e: IpcMainEvent, notePath: string) {
|
|||||||
window.addEventListener("note-ready", () => resolve());
|
window.addEventListener("note-ready", () => resolve());
|
||||||
});
|
});
|
||||||
`);
|
`);
|
||||||
|
ipcMain.off("print-progress", progressCallback);
|
||||||
return browserWindow;
|
return browserWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user