From d1d322fa177eb17beb66d6a8704504295cb29e54 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Mon, 8 Sep 2025 19:27:38 +0200 Subject: [PATCH] refactor: extract EventHandlers from main.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- main.ts | 114 ++++-------------------------------- src/plugin/EventHandlers.ts | 109 ++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 103 deletions(-) create mode 100644 src/plugin/EventHandlers.ts diff --git a/main.ts b/main.ts index bbeadef..824eaea 100644 --- a/main.ts +++ b/main.ts @@ -37,6 +37,7 @@ import { FolderSuggest } from './src/ui/components/FolderSuggest'; import { SUPPORTED_PAPER_SIZES } from './src/utils/paperSizeMapper'; import { PluginLifecycle } from './src/plugin/PluginLifecycle'; import { CommandRegistry } from './src/plugin/CommandRegistry'; +import { EventHandlers } from './src/plugin/EventHandlers'; import * as path from 'path'; import * as fs from 'fs'; @@ -48,9 +49,10 @@ export class obsidianTypstPDFExport extends Plugin { currentExportController: AbortController | null = null; private lifecycle: PluginLifecycle; private commandRegistry: CommandRegistry; + private eventHandlers: EventHandlers; // Type predicate to filter for markdown TFiles - private isMarkdownFile(file: TAbstractFile): file is TFile { + isMarkdownFile(file: TAbstractFile): file is TFile { return file instanceof TFile && file.extension === 'md'; } @@ -64,6 +66,9 @@ export class obsidianTypstPDFExport extends Plugin { // Initialize command registry this.commandRegistry = new CommandRegistry(this); + // Initialize event handlers + this.eventHandlers = new EventHandlers(this); + // Register custom icon addIcon('typst-pdf-export', ` @@ -72,14 +77,14 @@ export class obsidianTypstPDFExport extends Plugin { // Add ribbon icon using custom icon this.addRibbonIcon('typst-pdf-export', 'Export to PDF with Typst', (event: MouseEvent) => { - this.handleRibbonClick(event); + this.eventHandlers.handleRibbonClick(event); }); // Register commands this.commandRegistry.registerCommands(); // Register event handlers - this.registerEventHandlers(); + this.eventHandlers.registerEventHandlers(); // Add settings tab this.addSettingTab(new ObsidianTypstPDFExportSettingTab(this.app, this)); @@ -237,103 +242,6 @@ export class obsidianTypstPDFExport extends Plugin { } } - - private registerEventHandlers(): void { - // Add context menu item - this.registerEvent( - this.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.exportFile(file); - }); - }); - } - }) - ); - - // Add editor menu item - this.registerEvent( - this.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.exportCurrentNote(view); - }); - }); - }) - ); - - // 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(this.isMarkdownFile); - - 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); - }); - }); - } - }) - ); - } - - - /** - * Handle ribbon icon click - */ - private handleRibbonClick(event: MouseEvent): void { - const activeView = this.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.exportFile(activeView.file!); - }) - ); - - menu.addItem((item) => - item - .setTitle('Export with configuration…') - .setIcon('settings') - .onClick(() => { - this.showExportModal(activeView); - }) - ); - - menu.showAtMouseEvent(event); - } - /** * Export the current note with default settings */ @@ -386,7 +294,7 @@ export class obsidianTypstPDFExport extends Plugin { /** * Show the export configuration modal for multiple files */ - private async showExportModalForFiles(files: TFile[]): Promise { + async showExportModalForFiles(files: TFile[]): Promise { if (files.length === 0) { new Notice('No files to export'); return; @@ -421,7 +329,7 @@ export class obsidianTypstPDFExport extends Plugin { /** * Export a file with default configuration */ - private async exportFile(file: TFile): Promise { + async exportFile(file: TFile): Promise { const config: ExportConfig = { template: this.settings.exportDefaults.template, format: this.settings.exportDefaults.format, @@ -451,7 +359,7 @@ export class obsidianTypstPDFExport extends Plugin { /** * Export multiple files with default configuration */ - private async exportFiles(files: TFile[]): Promise { + async exportFiles(files: TFile[]): Promise { await this.processBatchExport( files, `Exporting ${files.length} files to PDF...`, diff --git a/src/plugin/EventHandlers.ts b/src/plugin/EventHandlers.ts new file mode 100644 index 0000000..2caf772 --- /dev/null +++ b/src/plugin/EventHandlers.ts @@ -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); + } +} \ No newline at end of file