diff --git a/main.ts b/main.ts index 4ade2ba..9c70cf5 100644 --- a/main.ts +++ b/main.ts @@ -35,15 +35,17 @@ import { TemplateManager } from './src/templates/TemplateManager'; 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 * as path from 'path'; import * as fs from 'fs'; export class obsidianTypstPDFExport extends Plugin { settings: obsidianTypstPDFExportSettings; - private converter: PandocTypstConverter; + converter: PandocTypstConverter; templateManager: TemplateManager; - private embeddedTemplateManager: EmbeddedTemplateManager; - private currentExportController: AbortController | null = null; + embeddedTemplateManager: EmbeddedTemplateManager; + currentExportController: AbortController | null = null; + private lifecycle: PluginLifecycle; // Type predicate to filter for markdown TFiles private isMarkdownFile(file: TAbstractFile): file is TFile { @@ -51,31 +53,11 @@ export class obsidianTypstPDFExport extends Plugin { } async onload() { - await this.loadSettings(); + // Initialize lifecycle manager + this.lifecycle = new PluginLifecycle(this); - // Initialize embedded template manager - const vaultPath = (this.app.vault.adapter as any).basePath; - const pluginDir = path.join(vaultPath, this.manifest.dir!); - this.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir); - - // Extract any missing templates from embedded data - try { - const extractionResult = this.embeddedTemplateManager.extractAllMissingTemplates(); - if (extractionResult.failed.length > 0) { - console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed); - new Notice(`Warning: Failed to extract some templates. Plugin may not work correctly.`); - } - } catch (error) { - console.error('Error during template extraction:', error); - new Notice(`Error extracting templates: ${error.message}`); - } - - // Initialize components with executable paths from settings - this.converter = new PandocTypstConverter(this, { - pandocPath: this.settings.pandocPath, - typstPath: this.settings.typstPath - }); - this.templateManager = new TemplateManager(this); + // Initialize core plugin functionality + await this.lifecycle.initialize(); // Register custom icon addIcon('typst-pdf-export', ` @@ -96,15 +78,6 @@ export class obsidianTypstPDFExport extends Plugin { // Add settings tab this.addSettingTab(new ObsidianTypstPDFExportSettingTab(this.app, this)); - - // Clean up any leftover temp directories from previous sessions - this.cleanupStartupTempDirectories(); - - // Check dependencies on startup (async, don't await) - this.checkDependenciesAsync(); - - // Cache available fonts (async, don't await) - this.cacheAvailableFonts(); } /** @@ -146,7 +119,7 @@ export class obsidianTypstPDFExport extends Plugin { /** * Cache available fonts from typst to a file for quick access */ - private async cacheAvailableFonts(): Promise { + async cacheAvailableFonts(): Promise { try { const { spawn } = require('child_process'); @@ -741,7 +714,7 @@ ${dependencyResult.allAvailable new Notice(message, 12000); // Show for 12 seconds (longer due to more content) } - private async checkDependenciesAsync(): Promise { + async checkDependenciesAsync(): Promise { // Check dependencies silently on startup try { const missingDeps = DependencyChecker.checkDependenciesSync( @@ -1324,38 +1297,10 @@ ${dependencyResult.allAvailable } } - /** - * Clean up leftover temp directories from previous sessions - */ - private cleanupStartupTempDirectories(): void { - try { - const vaultPath = (this.app.vault.adapter as any).basePath; - const cleanupManager = TempDirectoryManager.create(vaultPath, this.app.vault.configDir); - const result = cleanupManager.cleanupAllTempDirs(); - - if (this.settings.behavior.debugMode) { - } - } catch (error) { - console.warn('Export: Startup temp directory cleanup failed (non-critical):', error); - // Don't throw - this shouldn't prevent plugin from loading - } - } onunload() { - // Cancel any ongoing exports - if (this.currentExportController) { - this.currentExportController.abort(); - } - - // Clean up temp directories on plugin unload - try { - const vaultPath = (this.app.vault.adapter as any).basePath; - const cleanupManager = TempDirectoryManager.create(vaultPath, this.app.vault.configDir); - cleanupManager.cleanupAllTempDirs(); - } catch (error) { - console.warn('Export: Failed to clean up temp directories during unload:', error); - } - + // Use lifecycle manager for cleanup + this.lifecycle.cleanup(); } } diff --git a/src/plugin/PluginLifecycle.ts b/src/plugin/PluginLifecycle.ts new file mode 100644 index 0000000..18e9327 --- /dev/null +++ b/src/plugin/PluginLifecycle.ts @@ -0,0 +1,94 @@ +/** + * Plugin Lifecycle Management + * Handles plugin loading, unloading, initialization, and cleanup + */ + +import { Notice } from 'obsidian'; +import type { obsidianTypstPDFExport } from '../../main'; +import { PandocTypstConverter } from '../converters/PandocTypstConverter'; +import { TemplateManager } from '../templates/TemplateManager'; +import { EmbeddedTemplateManager } from '../templates/embeddedTemplates'; +import { TempDirectoryManager } from '../core/TempDirectoryManager'; +import * as path from 'path'; + +export class PluginLifecycle { + constructor(private plugin: obsidianTypstPDFExport) {} + + /** + * Initialize the plugin during onload + */ + async initialize(): Promise { + // Load settings first + await this.plugin.loadSettings(); + + // Initialize embedded template manager + const vaultPath = (this.plugin.app.vault.adapter as any).basePath; + const pluginDir = path.join(vaultPath, this.plugin.manifest.dir!); + this.plugin.embeddedTemplateManager = new EmbeddedTemplateManager(pluginDir); + + // Extract any missing templates from embedded data + try { + const extractionResult = this.plugin.embeddedTemplateManager.extractAllMissingTemplates(); + if (extractionResult.failed.length > 0) { + console.warn(`Failed to extract ${extractionResult.failed.length} templates:`, extractionResult.failed); + new Notice(`Warning: Failed to extract some templates. Plugin may not work correctly.`); + } + } catch (error) { + console.error('Error during template extraction:', error); + new Notice(`Error extracting templates: ${error.message}`); + } + + // Initialize components with executable paths from settings + this.plugin.converter = new PandocTypstConverter(this.plugin, { + pandocPath: this.plugin.settings.pandocPath, + typstPath: this.plugin.settings.typstPath + }); + this.plugin.templateManager = new TemplateManager(this.plugin); + + // Clean up any leftover temp directories from previous sessions + this.cleanupStartupTempDirectories(); + + // Check dependencies on startup (async, don't await) + this.plugin.checkDependenciesAsync(); + + // Cache available fonts (async, don't await) + this.plugin.cacheAvailableFonts(); + } + + /** + * Clean up resources during onunload + */ + cleanup(): void { + // Cancel any ongoing exports + if (this.plugin.currentExportController) { + this.plugin.currentExportController.abort(); + } + + // Clean up temp directories on plugin unload + try { + const vaultPath = (this.plugin.app.vault.adapter as any).basePath; + const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir); + cleanupManager.cleanupAllTempDirs(); + } catch (error) { + console.warn('Export: Failed to clean up temp directories during unload:', error); + } + } + + /** + * Clean up leftover temp directories from previous sessions + */ + private cleanupStartupTempDirectories(): void { + try { + const vaultPath = (this.plugin.app.vault.adapter as any).basePath; + const cleanupManager = TempDirectoryManager.create(vaultPath, this.plugin.app.vault.configDir); + const result = cleanupManager.cleanupAllTempDirs(); + + if (this.plugin.settings.behavior.debugMode) { + // Debug logging was here but empty in original + } + } catch (error) { + console.warn('Export: Startup temp directory cleanup failed (non-critical):', error); + // Don't throw - this shouldn't prevent plugin from loading + } + } +} \ No newline at end of file