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 c63c6370b5 Desktop: Refactored command system
The goal is to make the command system more modular, so each command can
be defined as a single object that includes a declaration (name, label,
etc.) and a runtime (to execute the command, test if it should be
enabled, etc.)

Utility methods are provided to convert a command to a menu item or a
toolbar button, thus reducing duplicated and boiler plate code across the
codebase (often the menu item logic was duplicated in the toolbar
button logic and vice versa).

The goal is to make it easier to add new commands (and associated menu
item and toolbar buttons) and to call them from
anywhere. This is also useful for plugins, which can also easily define
new commands.

Could also allow creating a command palette.
2020-07-03 22:32:39 +01:00

63 lines
1.7 KiB
TypeScript

import { CommandRuntime, CommandDeclaration } from '../../../lib/services/CommandService';
const Note = require('lib/models/Note');
const { _ } = require('lib/locale');
const { shim } = require('lib/shim');
const { bridge } = require('electron').remote.require('./bridge');
const InteropServiceHelper = require('../../../InteropServiceHelper.js');
export const declaration:CommandDeclaration = {
name: 'exportPdf',
label: () => `PDF - ${_('PDF File')}`,
};
export const runtime = (comp:any):CommandRuntime => {
return {
execute: async ({ noteIds }:any) => {
try {
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);
}
},
isEnabled: (props:any):boolean => {
return !!props.noteIds.length;
},
mapStateToProps: (state:any):any => {
return {
noteIds: state.selectedNoteIds,
};
},
};
};