1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Linux: Fixes #8240: Fix print on Linux (#9810)

This commit is contained in:
Henry Heino 2024-02-02 10:19:01 -08:00 committed by GitHub
parent dba218a681
commit bc8392ad52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,11 +6,12 @@ import { ExportModule } from '@joplin/lib/services/interop/Module';
import { _ } from '@joplin/lib/locale';
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
const bridge = require('@electron/remote').require('./bridge').default;
import bridge from './services/bridge';
import Setting from '@joplin/lib/models/Setting';
import Note from '@joplin/lib/models/Note';
const { friendlySafeFilename } = require('@joplin/lib/path-utils');
import time from '@joplin/lib/time';
import { BrowserWindow } from 'electron';
const md5 = require('md5');
const url = require('url');
@ -45,7 +46,7 @@ export default class InteropServiceHelper {
}
private static async exportNoteTo_(target: string, noteId: string, options: ExportNoteOptions = {}) {
let win: any = null;
let win: BrowserWindow|null = null;
let htmlFile: string = null;
const cleanup = () => {
@ -83,8 +84,8 @@ export default class InteropServiceHelper {
// contents of the tag are visible in printed
// pdfs.
// https://github.com/laurent22/joplin/issues/6254.
win.webContents.executeJavaScript('document.querySelectorAll(\'details\').forEach(el=>el.setAttribute(\'open\',\'\'))');
const data = await win.webContents.printToPDF(options);
await win.webContents.executeJavaScript('document.querySelectorAll(\'details\').forEach(el=>el.setAttribute(\'open\',\'\'))');
const data = await win.webContents.printToPDF(options as any);
resolve(data);
} catch (error) {
reject(error);
@ -104,21 +105,39 @@ export default class InteropServiceHelper {
// https://github.com/electron/electron/issues/28192
// Still doesn't work but at least it doesn't crash
// the app.
//
// 2024-01-31: Printing with webContents.print still
// fails on Linux (even if run in the main process).
// As such, we use window.print(), which seems to work.
win.webContents.print(options, (success: boolean, reason: string) => {
// TODO: This is correct but broken in Electron 4. Need to upgrade to 5+
// It calls the callback right away with "false" even if the document hasn't be print yet.
if (shim.isLinux()) {
await win.webContents.executeJavaScript(`
// Blocks while the print dialog is open
window.print();
`);
shim.setTimeout(() => {
// To prevent a crash, the window can only be closed after a timeout.
// This timeout can't be too small, or else it may still crash (e.g. 100ms
// is too short).
//
// See https://github.com/electron/electron/issues/31635 for details
cleanup();
resolve(null);
}, 1000);
} else {
win.webContents.print(options as any, (success: boolean, reason: string) => {
cleanup();
if (!success && reason !== 'cancelled') reject(new Error(`Could not print: ${reason}`));
resolve(null);
});
}
}
}, 2000);
});
win.loadURL(url.format({
void win.loadURL(url.format({
pathname: htmlFile,
protocol: 'file:',
slashes: true,