feat: add multi-file export support using files-menu event
- Add files-menu event handler for multi-file selection in file explorer - Create exportFiles() method for batch export with default settings - Create showExportModalForFiles() method for batch export with configuration - Add exportFilesWithConfig() helper for batch processing - Update ExportConfigModalSettings to support files array - Update modal header to show file count for multi-file exports - Users can now select multiple files and export them all at once - Maintains backward compatibility with single file exports 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
3 files changed
+177
-1
No files matched your search
@@ -11,6 +11,7 @@ import {
|
||||
Setting,
|
||||
TFile,
|
||||
TFolder,
|
||||
TAbstractFile,
|
||||
Notice,
|
||||
Menu,
|
||||
MarkdownView,
|
||||
@@ -228,6 +229,36 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
// Add multi-file menu item (for multiple selected files in file explorer)
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('files-menu', (menu: Menu, files: TAbstractFile[]) => {
|
||||
// Filter for markdown files only
|
||||
const markdownFiles = files.filter(file =>
|
||||
file instanceof TFile && file.extension === 'md'
|
||||
) as TFile[];
|
||||
|
||||
if (markdownFiles.length > 0) {
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle(`Export to PDF (Typst)`)
|
||||
.setIcon('file-output')
|
||||
.onClick(() => {
|
||||
this.exportFiles(markdownFiles);
|
||||
});
|
||||
});
|
||||
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle(`Export with configuration...`)
|
||||
.setIcon('settings')
|
||||
.onClick(() => {
|
||||
this.showExportModalForFiles(markdownFiles);
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -338,6 +369,69 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
|
||||
modal.open();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the export configuration modal for multiple files
|
||||
*/
|
||||
private async showExportModalForFiles(files: TFile[]): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
new Notice('No files to export');
|
||||
return;
|
||||
}
|
||||
|
||||
// Get available templates first
|
||||
const availableTemplates = await this.templateManager.getAvailableTemplates();
|
||||
|
||||
// Use the first file for modal settings but will export all files
|
||||
const firstFile = files[0];
|
||||
|
||||
// Prepare modal settings with hierarchy: plugin defaults as base
|
||||
const modalSettings: Partial<ExportConfigModalSettings> = {
|
||||
notePath: firstFile.path,
|
||||
noteTitle: files.length === 1 ? firstFile.basename : `${files.length} files`,
|
||||
files: files,
|
||||
// Plugin defaults from settings tab
|
||||
template: this.settings.exportDefaults.template,
|
||||
format: this.settings.exportDefaults.format,
|
||||
outputFolder: this.settings.outputFolder,
|
||||
openAfterExport: this.settings.behavior.openAfterExport,
|
||||
preserveFolderStructure: this.settings.behavior.preserveFolderStructure,
|
||||
availableTemplates: availableTemplates,
|
||||
// Template variables from settings tab values
|
||||
templateVariables: {
|
||||
pageSize: this.settings.pageSetup.size,
|
||||
orientation: this.settings.pageSetup.orientation,
|
||||
flipped: this.settings.pageSetup.orientation === 'landscape',
|
||||
marginTop: this.settings.pageSetup.margins.top.toString(),
|
||||
marginBottom: this.settings.pageSetup.margins.bottom.toString(),
|
||||
marginLeft: this.settings.pageSetup.margins.left.toString(),
|
||||
marginRight: this.settings.pageSetup.margins.right.toString(),
|
||||
bodyFont: this.settings.typography.fonts.body,
|
||||
headingFont: this.settings.typography.fonts.heading,
|
||||
monospaceFont: this.settings.typography.fonts.monospace,
|
||||
bodyFontSize: this.settings.typography.fontSizes.body,
|
||||
// Auto-adjust width for single-page landscape mode
|
||||
...(this.settings.pageSetup.orientation === 'landscape' && this.settings.exportDefaults.format === 'single-page'
|
||||
? { width: 'auto' }
|
||||
: {})
|
||||
}
|
||||
};
|
||||
|
||||
// Show modal - ModalState will handle localStorage hierarchy automatically
|
||||
const modal = new ExportConfigModal(
|
||||
this.app,
|
||||
this,
|
||||
modalSettings,
|
||||
async (config: ExportConfig) => {
|
||||
await this.exportFilesWithConfig(files, config);
|
||||
},
|
||||
() => {
|
||||
this.cancelExport();
|
||||
}
|
||||
);
|
||||
|
||||
modal.open();
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a file with default configuration
|
||||
@@ -368,6 +462,43 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
|
||||
await this.exportFileWithConfig(file, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export multiple files with default configuration
|
||||
*/
|
||||
private async exportFiles(files: TFile[]): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
new Notice('No files to export');
|
||||
return;
|
||||
}
|
||||
|
||||
new Notice(`Exporting ${files.length} files to PDF...`);
|
||||
|
||||
let successful = 0;
|
||||
let failed = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
await this.exportFile(file);
|
||||
successful++;
|
||||
} catch (error) {
|
||||
console.error(`Failed to export ${file.name}:`, error);
|
||||
failed++;
|
||||
errors.push(`${file.name}: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Show completion notice
|
||||
if (failed === 0) {
|
||||
new Notice(`✓ Successfully exported ${successful} files`);
|
||||
} else {
|
||||
new Notice(`Export complete: ${successful} successful, ${failed} failed`, 5000);
|
||||
if (errors.length > 0) {
|
||||
console.error('Export errors:', errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export a file with specific configuration
|
||||
@@ -504,6 +635,43 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export multiple files with specific configuration
|
||||
*/
|
||||
private async exportFilesWithConfig(files: TFile[], config: ExportConfig): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
new Notice('No files to export');
|
||||
return;
|
||||
}
|
||||
|
||||
new Notice(`Exporting ${files.length} files with custom configuration...`);
|
||||
|
||||
let successful = 0;
|
||||
let failed = 0;
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
await this.exportFileWithConfig(file, config);
|
||||
successful++;
|
||||
} catch (error) {
|
||||
console.error(`Failed to export ${file.name}:`, error);
|
||||
failed++;
|
||||
errors.push(`${file.name}: ${error.message || error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Show completion notice
|
||||
if (failed === 0) {
|
||||
new Notice(`✓ Successfully exported ${successful} files`);
|
||||
} else {
|
||||
new Notice(`Export complete: ${successful} successful, ${failed} failed`, 5000);
|
||||
if (errors.length > 0) {
|
||||
console.error('Export errors:', errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the current export
|
||||
|
||||
@@ -100,8 +100,13 @@ export class ExportConfigModal extends Modal {
|
||||
header.createEl('h2', { text: 'Export to PDF' });
|
||||
|
||||
if (this.state.settings.noteTitle) {
|
||||
const isMultiFile = this.state.settings.files && this.state.settings.files.length > 1;
|
||||
const displayText = isMultiFile
|
||||
? `Exporting ${this.state.settings.files!.length} files`
|
||||
: `Exporting: ${this.state.settings.noteTitle}`;
|
||||
|
||||
header.createEl('p', {
|
||||
text: `Exporting: ${this.state.settings.noteTitle}`,
|
||||
text: displayText,
|
||||
cls: 'export-note-title'
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
* Type definitions for the Export Configuration Modal
|
||||
*/
|
||||
|
||||
import { TFile } from 'obsidian';
|
||||
import { ExportFormat } from '../core/settings';
|
||||
|
||||
export interface ExportConfig {
|
||||
@@ -24,6 +25,8 @@ export interface ExportConfigModalSettings extends ExportConfig {
|
||||
notePath: string;
|
||||
/** Note title for display */
|
||||
noteTitle: string;
|
||||
/** Files being exported (for multi-file export) */
|
||||
files?: TFile[];
|
||||
/** Available templates from TemplateManager */
|
||||
availableTemplates: string[];
|
||||
/** Current export in progress */
|
||||
|
||||
Reference in new issue
Block a user