mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-27 08:21:03 +02:00
Merge branch 'master' of github.com:laurent22/joplin
This commit is contained in:
commit
d02488f00c
@ -459,10 +459,11 @@ class Application extends BaseApplication {
|
||||
label: `PDF - ${_('PDF File')}`,
|
||||
screens: ['Main'],
|
||||
click: async () => {
|
||||
const selectedNoteIds = this.store().getState().selectedNoteIds;
|
||||
this.dispatch({
|
||||
type: 'WINDOW_COMMAND',
|
||||
name: 'exportPdf',
|
||||
noteId: null,
|
||||
noteIds: selectedNoteIds,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
@ -1121,7 +1121,7 @@ class NoteTextComponent extends React.Component {
|
||||
|
||||
if (command.name === 'exportPdf') {
|
||||
fn = this.commandSavePdf;
|
||||
args = { noteId: command.noteId };
|
||||
args = { noteIds: command.noteIds };
|
||||
} else if (command.name === 'print') {
|
||||
fn = this.commandPrint;
|
||||
}
|
||||
@ -1235,37 +1235,17 @@ class NoteTextComponent extends React.Component {
|
||||
}
|
||||
|
||||
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
|
||||
if (this.isPrinting_) {
|
||||
console.log(`Printing ${options.path} to ${target} disallowed, already printing.`);
|
||||
return;
|
||||
}
|
||||
|
||||
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
|
||||
await this.saveIfNeeded();
|
||||
|
||||
setTimeout(async () => {
|
||||
if (target === 'pdf') {
|
||||
try {
|
||||
const pdfData = await InteropServiceHelper.exportNoteToPdf(options.noteId, {
|
||||
@ -1289,27 +1269,47 @@ class NoteTextComponent extends React.Component {
|
||||
console.error(error);
|
||||
bridge().showErrorMessageBox(error.message);
|
||||
}
|
||||
|
||||
// restoreSettings();
|
||||
}
|
||||
this.isPrinting_ = false;
|
||||
}, 100);
|
||||
}
|
||||
|
||||
pdfFileName_(note, folder) {
|
||||
return safeFilename(`${note.title} - ${folder.title}.pdf`, 255, true);
|
||||
}
|
||||
|
||||
async commandSavePdf(args) {
|
||||
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;
|
||||
if (noteIds.length === 1) {
|
||||
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: safeFilename(note.title),
|
||||
defaultPath: this.pdfFileName_(note, folder),
|
||||
});
|
||||
|
||||
} else {
|
||||
path = bridge().showOpenDialog({
|
||||
properties: ['openDirectory', 'createDirectory'],
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
bridge().showErrorMessageBox(error.message);
|
||||
}
|
||||
|
@ -162,7 +162,6 @@ class NoteListUtils {
|
||||
);
|
||||
}
|
||||
|
||||
if (noteIds.length === 1) {
|
||||
exportMenu.append(
|
||||
new MenuItem({
|
||||
label: `PDF - ${_('PDF File')}`,
|
||||
@ -170,12 +169,11 @@ class NoteListUtils {
|
||||
props.dispatch({
|
||||
type: 'WINDOW_COMMAND',
|
||||
name: 'exportPdf',
|
||||
noteId: noteIds[0],
|
||||
noteIds: noteIds,
|
||||
});
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
const exportMenuItem = new MenuItem({ label: _('Export'), submenu: exportMenu });
|
||||
|
||||
|
@ -37,8 +37,11 @@ class FsDriverBase {
|
||||
if (!exists) return nameToTry;
|
||||
nameToTry = `${nameNoExt} (${counter})${extension}`;
|
||||
counter++;
|
||||
if (counter >= 1000) nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`;
|
||||
if (counter >= 10000) throw new Error('Cannot find unique title');
|
||||
if (counter >= 1000) {
|
||||
nameToTry = `${nameNoExt} (${new Date().getTime()})${extension}`;
|
||||
await time.msleep(10);
|
||||
}
|
||||
if (counter >= 1100) throw new Error('Cannot find unique filename');
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user