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';
|
|
|
|
import { ExportOptions, FileSystemItem, Module } from '@joplin/lib/services/interop/types';
|
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';
|
2020-10-09 19:35:46 +02:00
|
|
|
const bridge = require('electron').remote.require('./bridge').default;
|
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';
|
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
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
const fullExportOptions: ExportOptions = Object.assign({}, {
|
2020-01-24 23:46:48 +02:00
|
|
|
path: tempFile,
|
|
|
|
format: 'html',
|
2020-10-09 19:35:46 +02:00
|
|
|
target: FileSystemItem.File,
|
2020-01-24 23:46:48 +02:00
|
|
|
sourceNoteIds: [noteId],
|
|
|
|
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);
|
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 = {}) {
|
|
|
|
let win: any = null;
|
|
|
|
let htmlFile: string = null;
|
2019-12-17 11:44:48 +02:00
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
if (win) win.destroy();
|
|
|
|
if (htmlFile) shim.fsDriver().remove(htmlFile);
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return new Promise((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 {
|
|
|
|
const data = await win.webContents.printToPDF(options);
|
|
|
|
resolve(data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-03 19:44:49 +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.
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
win.webContents.print(options, (success: boolean, reason: string) => {
|
2020-04-15 00:58:23 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
if (!success && reason !== 'cancelled') reject(new Error(`Could not print: ${reason}`));
|
|
|
|
resolve();
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
|
|
|
|
win.loadURL(url.format({
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static async export(_dispatch: Function, module: Module, 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;
|
2018-03-01 22:14:06 +02:00
|
|
|
path = 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 {
|
|
|
|
path = bridge().showOpenDialog({
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|