1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ElectronClient/gui/MainScreen/commands/exportPdf.ts
Laurent Cozic 3a57cfea02 Desktop: Simplified and improve command service, and added command palette
- Commands "enabled" state is now expressed using a "when-clause" like in VSCode
- A command palette has been added to the Tools menu
2020-10-18 21:52:10 +01:00

58 lines
1.7 KiB
TypeScript

import { CommandRuntime, CommandDeclaration, CommandContext } from 'lib/services/CommandService';
import shim from 'lib/shim';
import InteropServiceHelper from '../../../InteropServiceHelper';
import { _ } from 'lib/locale';
const Note = require('lib/models/Note');
const bridge = require('electron').remote.require('./bridge').default;
export const declaration:CommandDeclaration = {
name: 'exportPdf',
label: () => `PDF - ${_('PDF File')}`,
};
export const runtime = (comp:any):CommandRuntime => {
return {
execute: async (context:CommandContext, noteIds:string[] = null) => {
try {
noteIds = noteIds || context.state.selectedNoteIds;
if (!noteIds.length) throw new Error('No notes selected for pdf export');
let path = null;
if (noteIds.length === 1) {
path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf'] }],
defaultPath: await InteropServiceHelper.defaultFilename(noteIds[0], 'pdf'),
});
} else {
path = bridge().showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
});
}
if (!path) return;
for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
let pdfPath = '';
if (noteIds.length === 1) {
pdfPath = path;
} else {
const n = await InteropServiceHelper.defaultFilename(note.id, 'pdf');
pdfPath = await shim.fsDriver().findUniqueFilename(`${path}/${n}`);
}
await comp.printTo_('pdf', { path: pdfPath, noteId: note.id });
}
} catch (error) {
console.error(error);
bridge().showErrorMessageBox(error.message);
}
},
enabledCondition: 'someNotesSelected',
};
};