From 819b1279877efd33f20cd3b0b728960d3044e75e Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 17:04:29 +0200 Subject: [PATCH] Improves export config modal and template vars Refactors the export configuration modal to provide a better user experience and more accurate default values. - Removes the batch export feature from the right-click menu. - Updates the template variables to derive from the settings tab. - Modifies the reset function in the export config modal to reset to plugin defaults. - Improves font handling and font size settings in the TypographySection of the export config modal, including slider for font size. - Enhances default template variable initialization in the ModalState. --- main.ts | 46 +++----- src/modal/ExportConfigModal.ts | 23 +++- src/modal/sections/TypographySection.ts | 147 +++++++++++++----------- src/modal/state/ModalState.ts | 50 ++++---- src/modal/types.ts | 2 +- 5 files changed, 141 insertions(+), 127 deletions(-) diff --git a/main.ts b/main.ts index bb8cd76..b766b7d 100644 --- a/main.ts +++ b/main.ts @@ -288,17 +288,6 @@ export class obsidianTypstPDFExport extends Plugin { }) ); - menu.addSeparator(); - - menu.addItem((item) => - item - .setTitle('Batch export...') - .setIcon('folder-output') - .onClick(() => { - this.batchExportNotes(); - }) - ); - menu.showAtMouseEvent(event); } @@ -339,21 +328,21 @@ export class obsidianTypstPDFExport extends Plugin { openAfterExport: this.settings.behavior.openAfterExport, preserveFolderStructure: this.settings.behavior.preserveFolderStructure, availableTemplates: availableTemplates, - // Template variables from plugin defaults + // Template variables from settings tab values templateVariables: { - pageSize: this.settings.exportDefaults.pageSize, - orientation: this.settings.exportDefaults.orientation, - flipped: this.settings.exportDefaults.orientation === 'landscape', - marginTop: this.settings.exportDefaults.marginTop, - marginBottom: this.settings.exportDefaults.marginBottom, - marginLeft: this.settings.exportDefaults.marginLeft, - marginRight: this.settings.exportDefaults.marginRight, - bodyFont: this.settings.exportDefaults.bodyFont, - headingFont: this.settings.exportDefaults.headingFont, - monospaceFont: this.settings.exportDefaults.monospaceFont, - bodyFontSize: this.settings.exportDefaults.bodyFontSize, + 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.exportDefaults.orientation === 'landscape' && this.settings.exportDefaults.format === 'single-page' + ...(this.settings.pageSetup.orientation === 'landscape' && this.settings.exportDefaults.format === 'single-page' ? { width: 'auto' } : {}) } @@ -551,15 +540,6 @@ export class obsidianTypstPDFExport extends Plugin { new Notice('Export cancelled'); } } - - /** - * Batch export multiple notes - */ - private async batchExportNotes(): Promise { - // This would show a modal to select multiple files - // Implementation deferred for now - new Notice('Batch export feature coming soon'); - } /** diff --git a/src/modal/ExportConfigModal.ts b/src/modal/ExportConfigModal.ts index 8de4e74..c5b77f0 100644 --- a/src/modal/ExportConfigModal.ts +++ b/src/modal/ExportConfigModal.ts @@ -192,8 +192,27 @@ export class ExportConfigModal extends Modal { } private handleReset(): void { - // Reset state to defaults - this.state.reset(); + // Create plugin defaults object from settings tab values + const pluginDefaults = { + template: this.plugin.settings.exportDefaults.template, + format: this.plugin.settings.exportDefaults.format, + outputFolder: this.plugin.settings.outputFolder, + openAfterExport: this.plugin.settings.behavior.openAfterExport, + preserveFolderStructure: this.plugin.settings.behavior.preserveFolderStructure, + pageSize: this.plugin.settings.pageSetup.size, + orientation: this.plugin.settings.pageSetup.orientation, + marginTop: this.plugin.settings.pageSetup.margins.top.toString(), + marginBottom: this.plugin.settings.pageSetup.margins.bottom.toString(), + marginLeft: this.plugin.settings.pageSetup.margins.left.toString(), + marginRight: this.plugin.settings.pageSetup.margins.right.toString(), + bodyFont: this.plugin.settings.typography.fonts.body, + headingFont: this.plugin.settings.typography.fonts.heading, + monospaceFont: this.plugin.settings.typography.fonts.monospace, + bodyFontSize: this.plugin.settings.typography.fontSizes.body + }; + + // Reset state to plugin defaults + this.state.reset(pluginDefaults); // Re-render sections to update UI this.renderSections(); diff --git a/src/modal/sections/TypographySection.ts b/src/modal/sections/TypographySection.ts index 95833cc..c537439 100644 --- a/src/modal/sections/TypographySection.ts +++ b/src/modal/sections/TypographySection.ts @@ -15,81 +15,90 @@ export class TypographySection implements ModalSection { this.container = containerEl.createDiv('export-section'); this.container.createEl('h3', { text: 'Typography' }); + // Create font settings asynchronously this.createFontSettings(state); this.createFontSizeSettings(state); } - private createFontSettings(state: ModalState): void { + private async createFontSettings(state: ModalState): Promise { if (!this.container) return; + // Get available fonts from cache + const availableFonts = await this.getAvailableFonts(state); + // Body font dropdown new Setting(this.container) .setName('Body font') .setDesc('Primary font for document text') - .addDropdown(async (dropdown) => { - try { - const fonts = await this.getAvailableFonts(state); - fonts.forEach(font => { - dropdown.addOption(font, font); - }); - - dropdown - .setValue(state.templateVariables.bodyFont || 'Concourse OT') - .onChange(value => { - state.updateTemplateVariables({ bodyFont: value }); - }); - } catch (error) { - console.error('Failed to load fonts:', error); - // Fallback to hardcoded fonts - this.addFallbackBodyFonts(dropdown, state); + .addDropdown(dropdown => { + const currentFont = state.templateVariables.bodyFont || 'Times New Roman'; + + // Add all available fonts + availableFonts.forEach(font => { + dropdown.addOption(font, font); + }); + + // Add current font if it's not in the list + if (!availableFonts.includes(currentFont)) { + dropdown.addOption(currentFont, currentFont); } + + // Set current value and change handler + dropdown + .setValue(currentFont) + .onChange(value => { + state.updateTemplateVariables({ bodyFont: value }); + }); }); - // Heading font dropdown + // Heading font dropdown new Setting(this.container) .setName('Heading font') .setDesc('Font for headings and titles') - .addDropdown(async (dropdown) => { - try { - const fonts = await this.getAvailableFonts(state); - fonts.forEach(font => { - dropdown.addOption(font, font); - }); - - dropdown - .setValue(state.templateVariables.headingFont || 'Concourse OT') - .onChange(value => { - state.updateTemplateVariables({ headingFont: value }); - }); - } catch (error) { - console.error('Failed to load fonts:', error); - // Fallback to hardcoded fonts - this.addFallbackBodyFonts(dropdown, state); + .addDropdown(dropdown => { + const currentFont = state.templateVariables.headingFont || 'Times New Roman'; + + // Add all available fonts + availableFonts.forEach(font => { + dropdown.addOption(font, font); + }); + + // Add current font if it's not in the list + if (!availableFonts.includes(currentFont)) { + dropdown.addOption(currentFont, currentFont); } + + // Set current value and change handler + dropdown + .setValue(currentFont) + .onChange(value => { + state.updateTemplateVariables({ headingFont: value }); + }); }); // Monospace font dropdown new Setting(this.container) .setName('Monospace font') .setDesc('Font for code blocks and inline code') - .addDropdown(async (dropdown) => { - try { - const fonts = await this.getAvailableFonts(state); - // Filter for common monospace fonts or show all - fonts.forEach(font => { - dropdown.addOption(font, font); - }); - - dropdown - .setValue(state.templateVariables.monospaceFont || 'Source Code Pro') - .onChange(value => { - state.updateTemplateVariables({ monospaceFont: value }); - }); - } catch (error) { - console.error('Failed to load fonts:', error); - // Fallback to hardcoded monospace fonts - this.addFallbackMonospaceFonts(dropdown, state); + .addDropdown(dropdown => { + const currentFont = state.templateVariables.monospaceFont || 'Courier New'; + + // Add all available fonts + availableFonts.forEach(font => { + dropdown.addOption(font, font); + }); + + // Add current font if it's not in the list + if (!availableFonts.includes(currentFont)) { + dropdown.addOption(currentFont, currentFont); } + + // Set current value and change handler + dropdown + .setValue(currentFont) + .onChange(value => { + state.updateTemplateVariables({ monospaceFont: value }); + }); }); } @@ -99,19 +108,14 @@ export class TypographySection implements ModalSection { // Body font size new Setting(this.container) .setName('Body font size') - .setDesc('Base font size for document text') - .addDropdown(dropdown => { - dropdown - .addOption('9pt', '9pt - Small') - .addOption('10pt', '10pt - Compact') - .addOption('11pt', '11pt - Standard') - .addOption('12pt', '12pt - Large') - .addOption('14pt', '14pt - Extra Large') - .setValue(state.templateVariables.bodyFontSize || '11pt') - .onChange(value => { - state.updateTemplateVariables({ bodyFontSize: value }); - }); - }); + .setDesc('Base font size for document text (in points)') + .addSlider(slider => slider + .setLimits(8, 16, 0.5) + .setValue(state.templateVariables.bodyFontSize || 11) + .setDynamicTooltip() + .onChange(value => { + state.updateTemplateVariables({ bodyFontSize: value }); + })); } // Helper methods for fonts - similar to settings tab @@ -157,7 +161,7 @@ export class TypographySection implements ModalSection { } } - private addFallbackBodyFonts(dropdown: any, state: ModalState): void { + private addFallbackBodyFonts(dropdown: any, state: ModalState, fontType: 'body' | 'heading'): void { dropdown .addOption('Concourse OT', 'Concourse OT') .addOption('Times New Roman', 'Times New Roman') @@ -167,10 +171,17 @@ export class TypographySection implements ModalSection { .addOption('Calibri', 'Calibri') .addOption('Cambria', 'Cambria') .addOption('Palatino', 'Palatino') - .addOption('Book Antiqua', 'Book Antiqua') - .setValue(state.templateVariables.bodyFont || state.templateVariables.headingFont || 'Concourse OT') + .addOption('Book Antiqua', 'Book Antiqua'); + + // Set the appropriate value based on font type + const currentValue = fontType === 'body' + ? state.templateVariables.bodyFont + : state.templateVariables.headingFont; + + dropdown + .setValue(currentValue || 'Concourse OT') .onChange((value: string) => { - if (dropdown.containerEl.closest('.setting-item')?.querySelector('.setting-item-name')?.textContent?.includes('Body')) { + if (fontType === 'body') { state.updateTemplateVariables({ bodyFont: value }); } else { state.updateTemplateVariables({ headingFont: value }); diff --git a/src/modal/state/ModalState.ts b/src/modal/state/ModalState.ts index d6aa94a..af9c30c 100644 --- a/src/modal/state/ModalState.ts +++ b/src/modal/state/ModalState.ts @@ -36,8 +36,10 @@ export class ModalState implements IModalState { ...initialSettings }; - // Initialize template variables with defaults, then saved state + // Use ONLY the passed templateVariables, with minimal fallbacks + // Priority: initialSettings > savedState > absolute minimal fallbacks this.templateVariables = { + // Only provide absolute minimal fallbacks for required fields pageSize: 'a4', orientation: 'portrait', flipped: false, @@ -45,12 +47,14 @@ export class ModalState implements IModalState { marginBottom: '0.8in', marginLeft: '1in', marginRight: '0.6in', - bodyFont: 'Concourse OT', - headingFont: 'Concourse OT', - monospaceFont: 'Source Code Pro', + bodyFont: 'Times New Roman', // Generic fallback + headingFont: 'Times New Roman', // Generic fallback + monospaceFont: 'Courier New', // Generic fallback bodyFontSize: '11pt', + // Apply saved state if any ...savedState?.templateVariables, - ...this.settings.templateVariables + // Apply passed settings (this should be the plugin defaults) + ...initialSettings.templateVariables }; // Update settings to include merged template variables @@ -131,34 +135,34 @@ export class ModalState implements IModalState { /** * Reset state to defaults */ - reset(): void { - // Reset to plugin defaults, not localStorage defaults + reset(pluginDefaults: any): void { + // Reset to plugin defaults, not hardcoded defaults this.settings = { ...this.settings, - template: 'default.typ', - format: ExportFormat.Standard, - outputFolder: 'exports', + template: pluginDefaults.template, + format: pluginDefaults.format, + outputFolder: pluginDefaults.outputFolder || 'exports', templateVariables: {}, progressPercent: 0, currentOperation: '', isExporting: false, canCancel: false, - openAfterExport: false, - preserveFolderStructure: false + openAfterExport: pluginDefaults.openAfterExport || false, + preserveFolderStructure: pluginDefaults.preserveFolderStructure || false }; this.templateVariables = { - pageSize: 'a4', - orientation: 'portrait', - flipped: false, - marginTop: '1in', - marginBottom: '0.8in', - marginLeft: '1in', - marginRight: '0.6in', - bodyFont: 'Concourse OT', - headingFont: 'Concourse OT', - monospaceFont: 'Source Code Pro', - bodyFontSize: '11pt' + pageSize: pluginDefaults.pageSize, + orientation: pluginDefaults.orientation, + flipped: pluginDefaults.orientation === 'landscape', + marginTop: pluginDefaults.marginTop, + marginBottom: pluginDefaults.marginBottom, + marginLeft: pluginDefaults.marginLeft, + marginRight: pluginDefaults.marginRight, + bodyFont: pluginDefaults.bodyFont, + headingFont: pluginDefaults.headingFont, + monospaceFont: pluginDefaults.monospaceFont, + bodyFontSize: pluginDefaults.bodyFontSize }; this.settings.templateVariables = this.templateVariables; diff --git a/src/modal/types.ts b/src/modal/types.ts index 484f037..4561705 100644 --- a/src/modal/types.ts +++ b/src/modal/types.ts @@ -56,6 +56,6 @@ export interface ModalState { buildExportConfig(): ExportConfig; notifyChange(): void; setProgress(percent: number, operation: string): void; - reset(): void; + reset(pluginDefaults: any): void; onChange(listener: () => void): void; } \ No newline at end of file