2020-11-07 17:59:37 +02:00
|
|
|
import InteropService from '@joplin/lib/services/interop/InteropService';
|
|
|
|
import CommandService from '@joplin/lib/services/CommandService';
|
|
|
|
import shim from '@joplin/lib/shim';
|
2023-12-15 20:18:11 +02:00
|
|
|
import { ExportModuleOutputFormat, ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types';
|
2023-07-12 11:30:38 +02:00
|
|
|
import { ExportModule } from '@joplin/lib/services/interop/Module';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2020-12-19 19:42:18 +02:00
|
|
|
import { PluginStates } from '@joplin/lib/services/plugins/reducer';
|
2024-02-02 20:19:01 +02:00
|
|
|
import bridge from './services/bridge';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-11-07 17:59:37 +02:00
|
|
|
const { friendlySafeFilename } = require('@joplin/lib/path-utils');
|
2021-01-22 19:41:11 +02:00
|
|
|
import time from '@joplin/lib/time';
|
2024-02-02 20:19:01 +02:00
|
|
|
import { BrowserWindow } from 'electron';
|
2019-12-17 11:44:48 +02:00
|
|
|
const md5 = require('md5');
|
|
|
|
const url = require('url');
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
interface ExportNoteOptions {
|
2020-11-12 21:29:22 +02:00
|
|
|
customCss?: string;
|
|
|
|
sourceNoteIds?: string[];
|
|
|
|
sourceFolderIds?: string[];
|
|
|
|
printBackground?: boolean;
|
|
|
|
pageSize?: string;
|
|
|
|
landscape?: boolean;
|
2020-11-17 13:50:46 +02:00
|
|
|
includeConflicts?: boolean;
|
2020-12-19 19:42:18 +02:00
|
|
|
plugins?: PluginStates;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class InteropServiceHelper {
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private static async exportNoteToHtmlFile(noteId: string, exportOptions: ExportNoteOptions) {
|
2019-12-17 11:44:48 +02:00
|
|
|
const tempFile = `${Setting.value('tempDir')}/${md5(Date.now() + Math.random())}.html`;
|
2020-01-24 23:46:48 +02:00
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const fullExportOptions: ExportOptions = { path: tempFile,
|
2023-12-15 20:18:11 +02:00
|
|
|
format: ExportModuleOutputFormat.Html,
|
2020-10-09 19:35:46 +02:00
|
|
|
target: FileSystemItem.File,
|
2020-01-24 23:46:48 +02:00
|
|
|
sourceNoteIds: [noteId],
|
2023-06-01 13:02:36 +02:00
|
|
|
customCss: '', ...exportOptions };
|
2019-12-17 11:44:48 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const service = InteropService.instance();
|
2019-12-17 11:44:48 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const result = await service.export(fullExportOptions);
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2019-12-17 11:44:48 +02:00
|
|
|
console.info('Export HTML result: ', result);
|
|
|
|
return tempFile;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private static async exportNoteTo_(target: string, noteId: string, options: ExportNoteOptions = {}) {
|
2024-02-02 20:19:01 +02:00
|
|
|
let win: BrowserWindow|null = null;
|
2020-11-12 21:13:28 +02:00
|
|
|
let htmlFile: string = null;
|
2019-12-17 11:44:48 +02:00
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
if (win) win.destroy();
|
2023-12-06 21:24:00 +02:00
|
|
|
if (htmlFile) void shim.fsDriver().remove(htmlFile);
|
2019-12-17 11:44:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-01-24 23:46:48 +02:00
|
|
|
const exportOptions = {
|
|
|
|
customCss: options.customCss ? options.customCss : '',
|
2020-12-19 19:42:18 +02:00
|
|
|
plugins: options.plugins,
|
2020-01-24 23:46:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
htmlFile = await this.exportNoteToHtmlFile(noteId, exportOptions);
|
2019-12-17 11:44:48 +02:00
|
|
|
|
|
|
|
const windowOptions = {
|
2019-12-30 21:44:15 +02:00
|
|
|
show: false,
|
2019-12-17 11:44:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
win = bridge().newBrowserWindow(windowOptions);
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-06 21:24:00 +02:00
|
|
|
return new Promise<any>((resolve, reject) => {
|
2020-04-15 00:58:23 +02:00
|
|
|
win.webContents.on('did-finish-load', () => {
|
|
|
|
|
|
|
|
// did-finish-load will trigger when most assets are done loading, probably
|
|
|
|
// images, JavaScript and CSS. However it seems it might trigger *before*
|
|
|
|
// all fonts are loaded, which will break for example Katex rendering.
|
|
|
|
// So we need to add an additional timer to make sure fonts are loaded
|
|
|
|
// as it doesn't seem there's any easy way to figure that out.
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.setTimeout(async () => {
|
2020-04-15 00:58:23 +02:00
|
|
|
if (target === 'pdf') {
|
|
|
|
try {
|
2022-12-28 18:12:36 +02:00
|
|
|
// The below line "opens" all <details> tags
|
|
|
|
// before printing. This assures that the
|
|
|
|
// contents of the tag are visible in printed
|
|
|
|
// pdfs.
|
|
|
|
// https://github.com/laurent22/joplin/issues/6254.
|
2024-02-02 20:19:01 +02:00
|
|
|
await win.webContents.executeJavaScript('document.querySelectorAll(\'details\').forEach(el=>el.setAttribute(\'open\',\'\'))');
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-02-02 20:19:01 +02:00
|
|
|
const data = await win.webContents.printToPDF(options as any);
|
2020-04-15 00:58:23 +02:00
|
|
|
resolve(data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-01 20:35:27 +02:00
|
|
|
// TODO: it is crashing at this point :( Appears to
|
|
|
|
// be a Chromium bug:
|
|
|
|
// https://github.com/electron/electron/issues/19946
|
|
|
|
// Maybe can be fixed by doing everything from main
|
|
|
|
// process? i.e. creating a function `print()` that
|
|
|
|
// takes the `htmlFile` variable as input.
|
|
|
|
//
|
|
|
|
// 2021-10-01: This old bug is fixed, and has been
|
|
|
|
// replaced by a brand new bug:
|
|
|
|
// https://github.com/electron/electron/issues/28192
|
|
|
|
// Still doesn't work but at least it doesn't crash
|
|
|
|
// the app.
|
2024-02-02 20:19:01 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
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 {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-02-02 20:19:01 +02:00
|
|
|
win.webContents.print(options as any, (success: boolean, reason: string) => {
|
|
|
|
cleanup();
|
|
|
|
if (!success && reason !== 'cancelled') reject(new Error(`Could not print: ${reason}`));
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
}
|
2019-12-17 13:08:55 +02:00
|
|
|
}
|
2020-04-15 00:58:23 +02:00
|
|
|
}, 2000);
|
|
|
|
|
2019-12-17 11:44:48 +02:00
|
|
|
});
|
|
|
|
|
2024-02-02 20:19:01 +02:00
|
|
|
void win.loadURL(url.format({
|
2019-12-17 11:44:48 +02:00
|
|
|
pathname: htmlFile,
|
|
|
|
protocol: 'file:',
|
|
|
|
slashes: true,
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
cleanup();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static async exportNoteToPdf(noteId: string, options: ExportNoteOptions = {}) {
|
2019-12-17 11:44:48 +02:00
|
|
|
return this.exportNoteTo_('pdf', noteId, options);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static async printNote(noteId: string, options: ExportNoteOptions = {}) {
|
2019-12-17 11:44:48 +02:00
|
|
|
return this.exportNoteTo_('printer', noteId, options);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static async defaultFilename(noteId: string, fileExtension: string) {
|
2020-06-17 19:35:13 +02:00
|
|
|
// Default filename is just the date
|
|
|
|
const date = time.formatMsToLocal(new Date().getTime(), time.dateFormat());
|
|
|
|
let filename = friendlySafeFilename(`${date}`, 100);
|
|
|
|
|
|
|
|
if (noteId) {
|
|
|
|
const note = await Note.load(noteId);
|
|
|
|
// In a rare case the passed note will be null, use the id for filename
|
|
|
|
filename = friendlySafeFilename(note ? note.title : noteId, 100);
|
|
|
|
}
|
|
|
|
|
2020-05-03 19:44:49 +02:00
|
|
|
return `${filename}.${fileExtension}`;
|
2020-04-03 01:24:33 +02:00
|
|
|
}
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-07-12 11:30:38 +02:00
|
|
|
public static async export(_dispatch: Function, module: ExportModule, options: ExportNoteOptions = null) {
|
2018-03-01 22:14:06 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
|
|
|
|
let path = null;
|
|
|
|
|
|
|
|
if (module.target === 'file') {
|
2020-05-03 19:44:49 +02:00
|
|
|
const noteId = options.sourceNoteIds && options.sourceNoteIds.length ? options.sourceNoteIds[0] : null;
|
2021-11-01 09:38:06 +02:00
|
|
|
path = await bridge().showSaveDialog({
|
2020-02-05 00:09:34 +02:00
|
|
|
filters: [{ name: module.description, extensions: module.fileExtensions }],
|
2020-05-03 19:44:49 +02:00
|
|
|
defaultPath: await this.defaultFilename(noteId, module.fileExtensions[0]),
|
2018-03-01 22:14:06 +02:00
|
|
|
});
|
|
|
|
} else {
|
2021-11-01 09:38:06 +02:00
|
|
|
path = await bridge().showOpenDialog({
|
2018-03-01 22:14:06 +02:00
|
|
|
properties: ['openDirectory', 'createDirectory'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path || (Array.isArray(path) && !path.length)) return;
|
|
|
|
|
|
|
|
if (Array.isArray(path)) path = path[0];
|
|
|
|
|
2020-11-25 16:40:25 +02:00
|
|
|
void CommandService.instance().execute('showModalMessage', _('Exporting to "%s" as "%s" format. Please wait...', path, module.format));
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const exportOptions: ExportOptions = {};
|
2018-03-01 22:14:06 +02:00
|
|
|
exportOptions.path = path;
|
|
|
|
exportOptions.format = module.format;
|
2020-11-17 16:50:28 +02:00
|
|
|
// exportOptions.modulePath = module.path;
|
2020-12-19 19:42:18 +02:00
|
|
|
if (options.plugins) exportOptions.plugins = options.plugins;
|
2021-05-19 15:00:16 +02:00
|
|
|
exportOptions.customCss = options.customCss;
|
2019-12-15 20:41:13 +02:00
|
|
|
exportOptions.target = module.target;
|
2020-11-17 13:50:46 +02:00
|
|
|
exportOptions.includeConflicts = !!options.includeConflicts;
|
2018-03-01 22:14:06 +02:00
|
|
|
if (options.sourceFolderIds) exportOptions.sourceFolderIds = options.sourceFolderIds;
|
|
|
|
if (options.sourceNoteIds) exportOptions.sourceNoteIds = options.sourceNoteIds;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const service = InteropService.instance();
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2019-10-11 20:20:12 +02:00
|
|
|
try {
|
|
|
|
const result = await service.export(exportOptions);
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2019-10-11 20:20:12 +02:00
|
|
|
console.info('Export result: ', result);
|
|
|
|
} catch (error) {
|
2019-12-17 02:40:25 +02:00
|
|
|
console.error(error);
|
2019-10-11 20:20:12 +02:00
|
|
|
bridge().showErrorMessageBox(_('Could not export notes: %s', error.message));
|
|
|
|
}
|
2018-03-01 22:14:06 +02:00
|
|
|
|
2020-11-25 16:40:25 +02:00
|
|
|
void CommandService.instance().execute('hideModalMessage');
|
2018-03-01 22:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|