Merge branch 'master' of github.com:laurent22/joplin

pull/2497/head
Laurent Cozic 2020-02-11 22:29:02 +00:00
commit d02488f00c
4 changed files with 75 additions and 73 deletions

View File

@ -459,10 +459,11 @@ class Application extends BaseApplication {
label: `PDF - ${_('PDF File')}`, label: `PDF - ${_('PDF File')}`,
screens: ['Main'], screens: ['Main'],
click: async () => { click: async () => {
const selectedNoteIds = this.store().getState().selectedNoteIds;
this.dispatch({ this.dispatch({
type: 'WINDOW_COMMAND', type: 'WINDOW_COMMAND',
name: 'exportPdf', name: 'exportPdf',
noteId: null, noteIds: selectedNoteIds,
}); });
}, },
}); });

View File

@ -1121,7 +1121,7 @@ class NoteTextComponent extends React.Component {
if (command.name === 'exportPdf') { if (command.name === 'exportPdf') {
fn = this.commandSavePdf; fn = this.commandSavePdf;
args = { noteId: command.noteId }; args = { noteIds: command.noteIds };
} else if (command.name === 'print') { } else if (command.name === 'print') {
fn = this.commandPrint; fn = this.commandPrint;
} }
@ -1235,81 +1235,81 @@ class NoteTextComponent extends React.Component {
} }
async printTo_(target, options) { async printTo_(target, options) {
if (this.props.selectedNoteIds.length !== 1 || !this.webviewRef_.current) {
throw new Error(_('Only one note can be printed or exported to PDF at a time.'));
}
// Concurrent print calls are disallowed to avoid incorrect settings being restored upon completion // Concurrent print calls are disallowed to avoid incorrect settings being restored upon completion
if (this.isPrinting_) { if (this.isPrinting_) {
console.log(`Printing ${options.path} to ${target} disallowed, already printing.`);
return; return;
} }
this.isPrinting_ = true; this.isPrinting_ = true;
// const previousBody = this.state.note.body;
// const tempBody = `${this.state.note.title}\n\n${previousBody}`;
// const previousTheme = Setting.value('theme');
// Setting.setValue('theme', Setting.THEME_LIGHT);
// this.lastSetHtml_ = '';
// await this.updateHtml(this.state.note.markup_language, tempBody, { useCustomCss: true });
// this.forceUpdate();
// const restoreSettings = async () => {
// Setting.setValue('theme', previousTheme);
// this.lastSetHtml_ = '';
// await this.updateHtml(this.state.note.markup_language, previousBody);
// this.forceUpdate();
// };
// Need to save because the interop service reloads the note from the database // Need to save because the interop service reloads the note from the database
await this.saveIfNeeded(); await this.saveIfNeeded();
setTimeout(async () => { if (target === 'pdf') {
if (target === 'pdf') { try {
try { const pdfData = await InteropServiceHelper.exportNoteToPdf(options.noteId, {
const pdfData = await InteropServiceHelper.exportNoteToPdf(options.noteId, { printBackground: true,
printBackground: true, pageSize: Setting.value('export.pdfPageSize'),
pageSize: Setting.value('export.pdfPageSize'), landscape: Setting.value('export.pdfPageOrientation') === 'landscape',
landscape: Setting.value('export.pdfPageOrientation') === 'landscape', customCss: this.props.customCss,
customCss: this.props.customCss, });
}); await shim.fsDriver().writeFile(options.path, pdfData, 'buffer');
await shim.fsDriver().writeFile(options.path, pdfData, 'buffer'); } catch (error) {
} catch (error) { console.error(error);
console.error(error); bridge().showErrorMessageBox(error.message);
bridge().showErrorMessageBox(error.message);
}
} else if (target === 'printer') {
try {
await InteropServiceHelper.printNote(options.noteId, {
printBackground: true,
customCss: this.props.customCss,
});
} catch (error) {
console.error(error);
bridge().showErrorMessageBox(error.message);
}
// restoreSettings();
} }
this.isPrinting_ = false; } else if (target === 'printer') {
}, 100); try {
await InteropServiceHelper.printNote(options.noteId, {
printBackground: true,
customCss: this.props.customCss,
});
} catch (error) {
console.error(error);
bridge().showErrorMessageBox(error.message);
}
}
this.isPrinting_ = false;
}
pdfFileName_(note, folder) {
return safeFilename(`${note.title} - ${folder.title}.pdf`, 255, true);
} }
async commandSavePdf(args) { async commandSavePdf(args) {
try { try {
if (!this.state.note && !args.noteId) throw new Error(_('Only one note can be exported to PDF at a time.')); if (!this.state.note && !args.noteIds) throw new Error('No notes selected for pdf export');
const note = (!args.noteId ? this.state.note : await Note.load(args.noteId)); let noteIds = args.noteIds ? args.noteIds : [this.state.note.id];
const path = bridge().showSaveDialog({ let path = null;
filters: [{ name: _('PDF File'), extensions: ['pdf'] }], if (noteIds.length === 1) {
defaultPath: safeFilename(note.title), const note = await Note.load(noteIds[0]);
}); const folder = Folder.byId(this.props.folders, note.parent_id);
path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf'] }],
defaultPath: this.pdfFileName_(note, folder),
});
} else {
path = bridge().showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
});
}
if (!path) return; if (!path) return;
await this.printTo_('pdf', { path: path, noteId: note.id }); for (let i = 0; i < noteIds.length; i++) {
const note = await Note.load(noteIds[i]);
const folder = Folder.byId(this.props.folders, note.parent_id);
const pdfPath = (noteIds.length === 1) ? path :
await shim.fsDriver().findUniqueFilename(`${path}/${this.pdfFileName_(note, folder)}`);
await this.printTo_('pdf', { path: pdfPath, noteId: note.id });
}
} catch (error) { } catch (error) {
bridge().showErrorMessageBox(error.message); bridge().showErrorMessageBox(error.message);
} }

View File

@ -162,20 +162,18 @@ class NoteListUtils {
); );
} }
if (noteIds.length === 1) { exportMenu.append(
exportMenu.append( new MenuItem({
new MenuItem({ label: `PDF - ${_('PDF File')}`,
label: `PDF - ${_('PDF File')}`, click: () => {
click: () => { props.dispatch({
props.dispatch({ type: 'WINDOW_COMMAND',
type: 'WINDOW_COMMAND', name: 'exportPdf',
name: 'exportPdf', noteIds: noteIds,
noteId: noteIds[0], });
}); },
}, })
}) );
);
}
const exportMenuItem = new MenuItem({ label: _('Export'), submenu: exportMenu }); const exportMenuItem = new MenuItem({ label: _('Export'), submenu: exportMenu });

View File

@ -37,8 +37,11 @@ class FsDriverBase {
if (!exists) return nameToTry; if (!exists) return nameToTry;
nameToTry = `${nameNoExt} (${counter})${extension}`; nameToTry = `${nameNoExt} (${counter})${extension}`;
counter++; counter++;
if (counter >= 1000) nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`; if (counter >= 1000) {
if (counter >= 10000) throw new Error('Cannot find unique title'); nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`;
await time.msleep(10);
}
if (counter >= 1100) throw new Error('Cannot find unique filename');
} }
} }