refactor: extract EventHandlers from main.ts

Extract event handling logic into focused EventHandlers module:
- Create src/plugin/EventHandlers.ts for menu and ribbon handlers
- Move registerEventHandlers() and handleRibbonClick() methods
- Make exportFile(), exportFiles(), showExportModalForFiles(), isMarkdownFile() public
- Reduce main.ts by ~100 lines, improving maintainability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jarvis committed 2025-09-08 19:27:38 +02:00
1 parent 651671da2d
commit d1d322fa17
2 files changed
+120 -103

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
/**
* Event Handler Registration
* Handles registration of all plugin event handlers and menu items
*/
import { Menu, Editor, MarkdownView, TFile, TAbstractFile, Notice } from 'obsidian';
import type { obsidianTypstPDFExport } from '../../main';
export class EventHandlers {
constructor(private plugin: obsidianTypstPDFExport) {}
/**
* Register all plugin event handlers
*/
registerEventHandlers(): void {
// Add context menu item
this.plugin.registerEvent(
this.plugin.app.workspace.on('file-menu', (menu: Menu, file: TFile) => {
if (file.extension === 'md') {
menu.addItem((item) => {
item
.setTitle('Export to PDF (Typst)')
.setIcon('file-output')
.onClick(() => {
this.plugin.exportFile(file);
});
});
}
})
);
// Add editor menu item
this.plugin.registerEvent(
this.plugin.app.workspace.on('editor-menu', (menu: Menu, editor: Editor, view: MarkdownView) => {
menu.addItem((item) => {
item
.setTitle('Export to PDF (Typst)')
.setIcon('file-output')
.onClick(() => {
this.plugin.exportCurrentNote(view);
});
});
})
);
// Add multi-file menu item (for multiple selected files in file explorer)
this.plugin.registerEvent(
this.plugin.app.workspace.on('files-menu', (menu: Menu, files: TAbstractFile[]) => {
// Filter for markdown files only
const markdownFiles = files.filter(this.plugin.isMarkdownFile);
if (markdownFiles.length > 0) {
menu.addItem((item) => {
item
.setTitle(`Export to PDF (Typst)`)
.setIcon('file-output')
.onClick(() => {
this.plugin.exportFiles(markdownFiles);
});
});
menu.addItem((item) => {
item
.setTitle(`Export with configuration...`)
.setIcon('settings')
.onClick(() => {
this.plugin.showExportModalForFiles(markdownFiles);
});
});
}
})
);
}
/**
* Handle ribbon icon click
*/
handleRibbonClick(event: MouseEvent): void {
const activeView = this.plugin.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView || !activeView.file) {
new Notice('Please open a markdown file to export');
return;
}
// Create context menu for ribbon click
const menu = new Menu();
menu.addItem((item) =>
item
.setTitle('Export current note(s)')
.setIcon('file-output')
.onClick(() => {
this.plugin.exportFile(activeView.file!);
})
);
menu.addItem((item) =>
item
.setTitle('Export with configuration…')
.setIcon('settings')
.onClick(() => {
this.plugin.showExportModal(activeView);
})
);
menu.showAtMouseEvent(event);
}
}