2020-11-07 17:59:37 +02:00
|
|
|
import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
|
|
|
|
import shim from '@joplin/lib/shim';
|
2020-10-09 19:35:46 +02:00
|
|
|
import InteropServiceHelper from '../../../InteropServiceHelper';
|
2020-11-07 17:59:37 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Note from '@joplin/lib/models/Note';
|
2024-08-08 21:00:19 +02:00
|
|
|
import bridge from '../../../services/bridge';
|
2024-11-14 11:06:33 +02:00
|
|
|
import { WindowControl } from '../utils/useWindowControl';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'exportPdf',
|
|
|
|
label: () => `PDF - ${_('PDF File')}`,
|
|
|
|
};
|
|
|
|
|
2024-11-14 11:06:33 +02:00
|
|
|
export const runtime = (comp: WindowControl): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (context: CommandContext, noteIds: string[] = null) => {
|
2020-07-03 23:32:39 +02:00
|
|
|
try {
|
2020-10-18 22:52:10 +02:00
|
|
|
noteIds = noteIds || context.state.selectedNoteIds;
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
if (!noteIds.length) throw new Error('No notes selected for pdf export');
|
|
|
|
|
|
|
|
let path = null;
|
|
|
|
if (noteIds.length === 1) {
|
2021-11-01 09:38:06 +02:00
|
|
|
path = await bridge().showSaveDialog({
|
2020-07-03 23:32:39 +02:00
|
|
|
filters: [{ name: _('PDF File'), extensions: ['pdf'] }],
|
|
|
|
defaultPath: await InteropServiceHelper.defaultFilename(noteIds[0], 'pdf'),
|
|
|
|
});
|
|
|
|
} else {
|
2021-11-01 09:38:06 +02:00
|
|
|
path = await bridge().showOpenDialog({
|
2020-07-03 23:32:39 +02:00
|
|
|
properties: ['openDirectory', 'createDirectory'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-08 21:00:19 +02:00
|
|
|
if (Array.isArray(path)) {
|
|
|
|
if (path.length > 1) {
|
|
|
|
throw new Error('Only one output directory can be selected');
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path[0];
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
|
2024-11-14 11:06:33 +02:00
|
|
|
await comp.printTo('pdf', { path: pdfPath, noteId: note.id });
|
2020-07-03 23:32:39 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
|
|
|
},
|
2020-10-18 22:52:10 +02:00
|
|
|
|
|
|
|
enabledCondition: 'someNotesSelected',
|
2020-07-03 23:32:39 +02:00
|
|
|
};
|
|
|
|
};
|