mirror of
https://github.com/zadam/trilium.git
synced 2025-10-28 16:56:34 +01:00
feat(desktop/print): integrate with offscreen rendering
This commit is contained in:
@@ -28,7 +28,7 @@ import ContentWidgetTypeWidget from "./type_widgets/content_widget.js";
|
|||||||
import AttachmentListTypeWidget from "./type_widgets/attachment_list.js";
|
import AttachmentListTypeWidget from "./type_widgets/attachment_list.js";
|
||||||
import AttachmentDetailTypeWidget from "./type_widgets/attachment_detail.js";
|
import AttachmentDetailTypeWidget from "./type_widgets/attachment_detail.js";
|
||||||
import MindMapWidget from "./type_widgets/mind_map.js";
|
import MindMapWidget from "./type_widgets/mind_map.js";
|
||||||
import utils from "../services/utils.js";
|
import utils, { isElectron } from "../services/utils.js";
|
||||||
import type { NoteType } from "../entities/fnote.js";
|
import type { NoteType } from "../entities/fnote.js";
|
||||||
import type TypeWidget from "./type_widgets/type_widget.js";
|
import type TypeWidget from "./type_widgets/type_widget.js";
|
||||||
import { MermaidTypeWidget } from "./type_widgets/mermaid.js";
|
import { MermaidTypeWidget } from "./type_widgets/mermaid.js";
|
||||||
@@ -303,6 +303,13 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
|
|||||||
message: t("note_detail.printing"),
|
message: t("note_detail.printing"),
|
||||||
id: "printing"
|
id: "printing"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isElectron()) {
|
||||||
|
const { ipcRenderer } = utils.dynamicRequire("electron");
|
||||||
|
ipcRenderer.send("print-note", {
|
||||||
|
notePath: this.notePath
|
||||||
|
});
|
||||||
|
} else {
|
||||||
const iframe = document.createElement('iframe');
|
const iframe = document.createElement('iframe');
|
||||||
iframe.src = `?print#${this.notePath}`;
|
iframe.src = `?print#${this.notePath}`;
|
||||||
iframe.className = "print-iframe";
|
iframe.className = "print-iframe";
|
||||||
@@ -316,6 +323,7 @@ export default class NoteDetailWidget extends NoteContextAwareWidget {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async exportAsPdfEvent() {
|
async exportAsPdfEvent() {
|
||||||
if (!this.noteContext?.isActive() || !this.note) {
|
if (!this.noteContext?.isActive() || !this.note) {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import type { App, BrowserWindowConstructorOptions, BrowserWindow, WebContents }
|
|||||||
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;
|
||||||
@@ -67,12 +69,38 @@ electron.ipcMain.on("create-extra-window", (event, arg) => {
|
|||||||
createExtraWindow(arg.extraWindowHash);
|
createExtraWindow(arg.extraWindowHash);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
interface PrintOpts {
|
||||||
|
notePath: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface ExportAsPdfOpts {
|
interface ExportAsPdfOpts {
|
||||||
title: string;
|
title: string;
|
||||||
landscape: boolean;
|
landscape: boolean;
|
||||||
pageSize: "A0" | "A1" | "A2" | "A3" | "A4" | "A5" | "A6" | "Legal" | "Letter" | "Tabloid" | "Ledger";
|
pageSize: "A0" | "A1" | "A2" | "A3" | "A4" | "A5" | "A6" | "Legal" | "Letter" | "Tabloid" | "Ledger";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
electron.ipcMain.on("print-note", async (e, { notePath }: PrintOpts) => {
|
||||||
|
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());
|
||||||
|
});
|
||||||
|
`);
|
||||||
|
browserWindow.webContents.print({}, () => {
|
||||||
|
browserWindow.destroy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
electron.ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => {
|
electron.ipcMain.on("export-as-pdf", async (e, opts: ExportAsPdfOpts) => {
|
||||||
const browserWindow = electron.BrowserWindow.fromWebContents(e.sender);
|
const browserWindow = electron.BrowserWindow.fromWebContents(e.sender);
|
||||||
if (!browserWindow) {
|
if (!browserWindow) {
|
||||||
|
|||||||
Reference in New Issue
Block a user