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 {
}})); }}));
} }
if (noteIds.length === 1) {
exportMenu.append(new MenuItem({ label: 'PDF - ' + _('PDF File') , click: () => { exportMenu.append(new MenuItem({ label: 'PDF - ' + _('PDF File') , click: () => {
this.props.dispatch({ this.props.dispatch({
type: 'WINDOW_COMMAND', type: 'WINDOW_COMMAND',
name: 'exportPdf', name: 'exportPdf',
}); });
}})); }}));
}
const exportMenuItem = new MenuItem({label: _('Export'), submenu: exportMenu}); const exportMenuItem = new MenuItem({label: _('Export'), submenu: exportMenu});

View File

@ -909,15 +909,18 @@ 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') { }
if (this.state.note) {
if (command.name === 'textBold') {
fn = this.commandTextBold; fn = this.commandTextBold;
} else if (command.name === 'textItalic') { } else if (command.name === 'textItalic') {
fn = this.commandTextItalic; fn = this.commandTextItalic;
@ -928,6 +931,7 @@ class NoteTextComponent extends React.Component {
} else if (command.name === 'showLocalSearch') { } else if (command.name === 'showLocalSearch') {
fn = this.commandShowLocalSearch; 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() {
try {
if (!this.state.note) throw new Error(_('Only one note can be printed or exported to PDF at a time.'));
const path = bridge().showSaveDialog({ const path = bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf']}], filters: [{ name: _('PDF File'), extensions: ['pdf']}],
defaultPath: safeFilename(this.state.note.title), defaultPath: safeFilename(this.state.note.title),
}); });
if (path) { if (!path) return;
this.printTo_('pdf', { path: path }); this.printTo_('pdf', { path: path });
} catch (error) {
bridge().showErrorMessageBox(error.message);
} }
} }
commandPrint() { commandPrint() {
try {
this.printTo_('printer'); 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,