1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Desktop: Resolves #1059: Fixed behaviour of export to PDF and print

This commit is contained in:
Laurent Cozic 2019-01-10 18:58:58 +00:00
parent a8cc8763b0
commit 8268c3edba
2 changed files with 46 additions and 25 deletions

View File

@ -155,12 +155,14 @@ class NoteListComponent extends React.Component {
}})); }}));
} }
exportMenu.append(new MenuItem({ label: 'PDF - ' + _('PDF File') , click: () => { if (noteIds.length === 1) {
this.props.dispatch({ exportMenu.append(new MenuItem({ label: 'PDF - ' + _('PDF File') , click: () => {
type: 'WINDOW_COMMAND', this.props.dispatch({
name: 'exportPdf', type: 'WINDOW_COMMAND',
}); name: 'exportPdf',
}})); });
}}));
}
const exportMenuItem = new MenuItem({label: _('Export'), submenu: exportMenu}); const exportMenuItem = new MenuItem({label: _('Export'), submenu: exportMenu});

View File

@ -909,24 +909,28 @@ class NoteTextComponent extends React.Component {
} }
async doCommand(command) { async doCommand(command) {
if (!command || !this.state.note) return; if (!command) return;
let fn = null; let fn = null;
if (command.name === 'exportPdf' && this.webview_) { if (command.name === 'exportPdf') {
fn = this.commandSavePdf; fn = this.commandSavePdf;
} else if (command.name === 'print' && this.webview_) { } else if (command.name === 'print') {
fn = this.commandPrint; fn = this.commandPrint;
} else if (command.name === 'textBold') { }
fn = this.commandTextBold;
} else if (command.name === 'textItalic') { if (this.state.note) {
fn = this.commandTextItalic; if (command.name === 'textBold') {
} else if (command.name === 'insertDateTime' ) { fn = this.commandTextBold;
fn = this.commandDateTime; } else if (command.name === 'textItalic') {
} else if (command.name === 'commandStartExternalEditing') { fn = this.commandTextItalic;
fn = this.commandStartExternalEditing; } else if (command.name === 'insertDateTime' ) {
} else if (command.name === 'showLocalSearch') { fn = this.commandDateTime;
fn = this.commandShowLocalSearch; } else if (command.name === 'commandStartExternalEditing') {
fn = this.commandStartExternalEditing;
} else if (command.name === 'showLocalSearch') {
fn = this.commandShowLocalSearch;
}
} }
if (!fn) return; if (!fn) return;
@ -998,6 +1002,10 @@ class NoteTextComponent extends React.Component {
} }
printTo_(target, options) { printTo_(target, options) {
if (this.props.selectedNoteIds.length !== 1 || !this.webview_) {
throw new Error(_('Only one note can be printed or exported to PDF at a time.'));
}
const previousBody = this.state.note.body; const previousBody = this.state.note.body;
const tempBody = "# " + this.state.note.title + "\n\n" + previousBody; const tempBody = "# " + this.state.note.title + "\n\n" + previousBody;
@ -1033,18 +1041,28 @@ class NoteTextComponent extends React.Component {
} }
commandSavePdf() { commandSavePdf() {
const path = bridge().showSaveDialog({ try {
filters: [{ name: _('PDF File'), extensions: ['pdf']}], if (!this.state.note) throw new Error(_('Only one note can be printed or exported to PDF at a time.'));
defaultPath: safeFilename(this.state.note.title),
}); const path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf']}],
defaultPath: safeFilename(this.state.note.title),
});
if (!path) return;
if (path) {
this.printTo_('pdf', { path: path }); this.printTo_('pdf', { path: path });
} catch (error) {
bridge().showErrorMessageBox(error.message);
} }
} }
commandPrint() { commandPrint() {
this.printTo_('printer'); try {
this.printTo_('printer');
} catch (error) {
bridge().showErrorMessageBox(error.message);
}
} }
async commandStartExternalEditing() { async commandStartExternalEditing() {
@ -1705,6 +1723,7 @@ class NoteTextComponent extends React.Component {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
noteId: state.selectedNoteIds.length === 1 ? state.selectedNoteIds[0] : null, noteId: state.selectedNoteIds.length === 1 ? state.selectedNoteIds[0] : null,
selectedNoteIds: state.selectedNoteIds,
noteTags: state.selectedNoteTags, noteTags: state.selectedNoteTags,
folderId: state.selectedFolderId, folderId: state.selectedFolderId,
itemType: state.selectedItemType, itemType: state.selectedItemType,