refactor: extract CommandRegistry from main.ts

Extract command registration logic into dedicated CommandRegistry
class to improve separation of concerns and reduce main.ts complexity.

- Create src/plugin/CommandRegistry.ts (~35 lines)
- Move registerCommands() method to CommandRegistry
- Extract all 3 command definitions with callbacks:
  * export-current-note
  * export-with-config
  * check-dependencies
- Update main.ts to delegate to CommandRegistry
- Change exportCurrentNote() and showExportModal() to public
- Remove old registerCommands() method from main.ts

Part of comprehensive refactoring plan (main.ts: 1792 lines → 6 modules).
Step 1.2 complete. Commands functionality preserved.

🤖 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:19:01 +02:00
1 parent 991a9ae0ec
commit 651671da2d
2 files changed
+51 -31

No files matched your search

+8 -31
View File
@@ -36,6 +36,7 @@ import { EmbeddedTemplateManager } from './src/templates/embeddedTemplates';
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 * as path from 'path';
import * as fs from 'fs';
@@ -46,6 +47,7 @@ export class obsidianTypstPDFExport extends Plugin {
embeddedTemplateManager: EmbeddedTemplateManager;
currentExportController: AbortController | null = null;
private lifecycle: PluginLifecycle;
private commandRegistry: CommandRegistry;
// Type predicate to filter for markdown TFiles
private isMarkdownFile(file: TAbstractFile): file is TFile {
@@ -59,6 +61,9 @@ export class obsidianTypstPDFExport extends Plugin {
// Initialize core plugin functionality
await this.lifecycle.initialize();
// Initialize command registry
this.commandRegistry = new CommandRegistry(this);
// Register custom icon
addIcon('typst-pdf-export', `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2">
<path d="m9.002 4.175 4.343-2.13V6l4.033-.304V8.13h-4.033c-.017.223-.001 8.368 0 9.432.001.65.889 1.217 1.551 1.217.775 0 3.102-1.217 3.102-1.217l.931 1.522s-2.741 1.774-4.033 2.129c-1.195.329-2.017.761-3.723 0-1.073-.478-2.144-1.582-2.171-2.738-.052-2.231 0-10.649 0-10.649L7.14 8.13l-.31-1.825L9.002 6z" style="fill:#828282"/>
@@ -71,7 +76,7 @@ export class obsidianTypstPDFExport extends Plugin {
});
// Register commands
this.registerCommands();
this.commandRegistry.registerCommands();
// Register event handlers
this.registerEventHandlers();
@@ -232,34 +237,6 @@ export class obsidianTypstPDFExport extends Plugin {
}
}
private registerCommands(): void {
// Export current note command
this.addCommand({
id: 'export-current-note',
name: 'Export current note(s)',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.exportCurrentNote(view);
}
});
// Export with configuration command
this.addCommand({
id: 'export-with-config',
name: 'Export with configuration…',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.showExportModal(view);
}
});
// Check dependencies command
this.addCommand({
id: 'check-dependencies',
name: 'Check Pandoc and Typst dependencies',
callback: () => {
this.showDependencyStatus();
}
});
}
private registerEventHandlers(): void {
// Add context menu item
@@ -360,7 +337,7 @@ export class obsidianTypstPDFExport extends Plugin {
/**
* Export the current note with default settings
*/
private async exportCurrentNote(view: MarkdownView): Promise<void> {
async exportCurrentNote(view: MarkdownView): Promise<void> {
const file = view.file;
if (!file) {
new Notice('No active file to export');
@@ -373,7 +350,7 @@ export class obsidianTypstPDFExport extends Plugin {
/**
* Show the export configuration modal
*/
private async showExportModal(view: MarkdownView): Promise<void> {
async showExportModal(view: MarkdownView): Promise<void> {
const file = view.file;
if (!file) {
new Notice('No active file to export');
+43
View File
@@ -0,0 +1,43 @@
/**
* Command Registration
* Handles registration of all plugin commands and their callbacks
*/
import { Editor, MarkdownView } from 'obsidian';
import type { obsidianTypstPDFExport } from '../../main';
export class CommandRegistry {
constructor(private plugin: obsidianTypstPDFExport) {}
/**
* Register all plugin commands
*/
registerCommands(): void {
// Export current note command
this.plugin.addCommand({
id: 'export-current-note',
name: 'Export current note(s)',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.plugin.exportCurrentNote(view);
}
});
// Export with configuration command
this.plugin.addCommand({
id: 'export-with-config',
name: 'Export with configuration…',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.plugin.showExportModal(view);
}
});
// Check dependencies command
this.plugin.addCommand({
id: 'check-dependencies',
name: 'Check Pandoc and Typst dependencies',
callback: () => {
this.plugin.showDependencyStatus();
}
});
}
}