文件
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

74 行
2.3 KiB
TypeScript

import { CommandRuntime, CommandDeclaration, CommandContext } from '@joplin/lib/services/CommandService';
import shim from '@joplin/lib/shim';
import InteropServiceHelper from '../../../InteropServiceHelper';
import { _ } from '@joplin/lib/locale';
import Note from '@joplin/lib/models/Note';
import bridge from '../../../services/bridge';
import { WindowControl } from '../utils/useWindowControl';
export const declaration: CommandDeclaration = {
name: 'exportPdf',
label: () => `PDF - ${_('PDF File')}`,
};
export const runtime = (comp: WindowControl): CommandRuntime => {
return {
execute: async (context: CommandContext, noteIds: string[] = null) => {
try {
noteIds = noteIds || context.state.selectedNoteIds;
if (!noteIds.length) throw new Error('No notes selected for pdf export');
const lockedNoteChoice = await InteropServiceHelper.confirmLockedNoteExport(context.dispatch, noteIds);
if (lockedNoteChoice === 'cancel') return;
if (lockedNoteChoice === 'skipLocked') {
noteIds = (await Note.byIds(noteIds, { fields: ['id', 'is_locked'] })).filter(note => !note.is_locked).map(note => note.id);
if (!noteIds.length) return;
}
let path = null;
if (noteIds.length === 1) {
path = await bridge().showSaveDialog({
filters: [{ name: _('PDF File'), extensions: ['pdf'] }],
defaultPath: await InteropServiceHelper.defaultFilename(noteIds[0], 'pdf'),
});
} else {
path = await bridge().showOpenDialog({
properties: ['openDirectory', 'createDirectory'],
});
}
if (Array.isArray(path)) {
if (path.length > 1) {
throw new Error('Only one output directory can be selected');
}
path = path[0];
}
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}`);
}
await comp.printTo('pdf', { path: pdfPath, noteId: note.id });
}
} catch (error) {
console.error(error);
bridge().showErrorMessageBox(error.message);
}
},
enabledCondition: 'someNotesSelected && (!noteLockContentUnavailable || !noteLockSessionUnlocked)',
};
};