From 6eedeaace338013bb93bf44c79398b088926de7d Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 10:16:48 +0200 Subject: [PATCH] Adds export configuration modal sections Adds sections to the export configuration modal for behavior, page setup, and typography. These sections allow users to configure export behavior like opening the PDF after export and preserving folder structure, as well as page setup options like page size, orientation, and margins. Additionally, typography options for font selection and size configuration are included. Updates default settings for export format and adds default values for new settings. Removes the old TypographySection file. --- src/core/settings.ts | 27 ++- src/modal/sections/BehaviorSection.ts | 63 +++++++ src/modal/sections/GeneralSection.ts | 53 +++--- src/modal/sections/PageSetupSection.ts | 129 +++++++++++++ src/modal/sections/TypographySection.ts | 230 ++++++++++++++++-------- src/modal/types.ts | 22 +-- 6 files changed, 417 insertions(+), 107 deletions(-) create mode 100644 src/modal/sections/BehaviorSection.ts create mode 100644 src/modal/sections/PageSetupSection.ts diff --git a/src/core/settings.ts b/src/core/settings.ts index 8460d06..8793464 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -31,6 +31,21 @@ export interface obsidianTypstPDFExportSettings { template: string; /** Default export format */ format: ExportFormat; + /** Default page size */ + pageSize: string; + /** Default page orientation */ + orientation: 'portrait' | 'landscape'; + /** Default margins */ + marginTop: string; + marginBottom: string; + marginLeft: string; + marginRight: string; + /** Default fonts */ + bodyFont: string; + headingFont: string; + monospaceFont: string; + /** Default body font size */ + bodyFontSize: string; }; /** Typography settings */ @@ -89,7 +104,17 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = { exportDefaults: { template: 'default', - format: ExportFormat.Standard + format: ExportFormat.Standard, + pageSize: 'a4', + orientation: 'portrait', + marginTop: '1in', + marginBottom: '0.8in', + marginLeft: '1in', + marginRight: '0.6in', + bodyFont: 'Concourse OT', + headingFont: 'Concourse OT', + monospaceFont: 'Source Code Pro', + bodyFontSize: '11pt' }, typography: { diff --git a/src/modal/sections/BehaviorSection.ts b/src/modal/sections/BehaviorSection.ts new file mode 100644 index 0000000..4809282 --- /dev/null +++ b/src/modal/sections/BehaviorSection.ts @@ -0,0 +1,63 @@ +/** + * Behavior Section for Export Configuration Modal + * Handles export behavior options like opening after export and preserving folder structure + */ + +import { Setting } from 'obsidian'; +import { ModalSection, ModalState, ValidationResult } from '../types'; + +export class BehaviorSection implements ModalSection { + private container: HTMLElement | null = null; + + render(containerEl: HTMLElement, state: ModalState): void { + this.container = containerEl.createDiv('export-section'); + this.container.createEl('h3', { text: 'Export Behavior' }); + + this.createBehaviorToggles(state); + } + + private createBehaviorToggles(state: ModalState): void { + if (!this.container) return; + + // Open after export toggle + new Setting(this.container) + .setName('Open after export') + .setDesc('Automatically open the PDF file after successful export') + .addToggle(toggle => { + toggle + .setValue(state.settings.openAfterExport || false) + .onChange(value => { + state.updateSettings({ openAfterExport: value }); + }); + }); + + // Preserve folder structure toggle + new Setting(this.container) + .setName('Preserve folder structure') + .setDesc('Maintain the original folder structure when exporting multiple files') + .addToggle(toggle => { + toggle + .setValue(state.settings.preserveFolderStructure || false) + .onChange(value => { + state.updateSettings({ preserveFolderStructure: value }); + }); + }); + } + + validate(): ValidationResult { + const errors: string[] = []; + const warnings: string[] = []; + + // No validation needed for simple boolean toggles + + return { + isValid: errors.length === 0, + errors, + warnings + }; + } + + getId(): string { + return 'behavior'; + } +} \ No newline at end of file diff --git a/src/modal/sections/GeneralSection.ts b/src/modal/sections/GeneralSection.ts index 75b814b..0cf43c7 100644 --- a/src/modal/sections/GeneralSection.ts +++ b/src/modal/sections/GeneralSection.ts @@ -3,16 +3,19 @@ * Handles template selection, format options, and output settings */ -import { Setting } from 'obsidian'; +import { Setting, App } from 'obsidian'; import { ModalSection, ModalState, ValidationResult } from '../types'; import { ExportFormat } from '../../core/settings'; +import { FolderSuggest } from '../../components/FolderSuggest'; export class GeneralSection implements ModalSection { private container: HTMLElement | null = null; private templateDropdown: any = null; private outputFolderInput: HTMLInputElement | null = null; + private app: App; - render(containerEl: HTMLElement, state: ModalState): void { + render(containerEl: HTMLElement, state: ModalState, app?: App): void { + if (app) this.app = app; this.container = containerEl.createDiv('export-section'); this.container.createEl('h3', { text: 'General Settings' }); @@ -28,8 +31,11 @@ export class GeneralSection implements ModalSection { .setName('Typst template') .setDesc('Select template for document styling') .addDropdown(dropdown => { - // Populate with available templates - state.settings.availableTemplates.forEach(template => { + // Filter out universal-wrapper.pandoc.typ and populate with available templates + const filteredTemplates = state.settings.availableTemplates + .filter(template => template !== 'universal-wrapper.pandoc.typ'); + + filteredTemplates.forEach(template => { dropdown.addOption(template, this.getTemplateDisplayName(template)); }); @@ -74,6 +80,11 @@ export class GeneralSection implements ModalSection { state.updateSettings({ outputFolder: value }); }); this.outputFolderInput = input.inputEl; + + // Add folder autosuggest + if (this.app) { + new FolderSuggest(this.app, input.inputEl); + } }); } @@ -110,23 +121,25 @@ export class GeneralSection implements ModalSection { } updateAvailableTemplates(templates: string[]): void { - if (!this.templateDropdown) return; - - const currentValue = this.templateDropdown.getValue(); - - // Clear and repopulate dropdown - this.templateDropdown.selectEl.empty(); - templates.forEach(template => { - this.templateDropdown.addOption(template, this.getTemplateDisplayName(template)); - }); - - // Restore selection if still available - if (templates.includes(currentValue)) { - this.templateDropdown.setValue(currentValue); - } else if (templates.length > 0) { - this.templateDropdown.setValue(templates[0]); - } + if (!this.templateDropdown) return; + + const currentValue = this.templateDropdown.getValue(); + + // Filter out universal-wrapper.pandoc.typ and clear/repopulate dropdown + const filteredTemplates = templates.filter(template => template !== 'universal-wrapper.pandoc.typ'); + + this.templateDropdown.selectEl.empty(); + filteredTemplates.forEach(template => { + this.templateDropdown.addOption(template, this.getTemplateDisplayName(template)); + }); + + // Restore selection if still available + if (filteredTemplates.includes(currentValue)) { + this.templateDropdown.setValue(currentValue); + } else if (filteredTemplates.length > 0) { + this.templateDropdown.setValue(filteredTemplates[0]); } +} getId(): string { return 'general'; diff --git a/src/modal/sections/PageSetupSection.ts b/src/modal/sections/PageSetupSection.ts new file mode 100644 index 0000000..bcc60ef --- /dev/null +++ b/src/modal/sections/PageSetupSection.ts @@ -0,0 +1,129 @@ +/** + * Page Setup Section for Export Configuration Modal + * Handles page size, orientation with Typst flipped property, and text margin fields + */ + +import { Setting } from 'obsidian'; +import { ModalSection, ModalState, ValidationResult } from '../types'; + +export class PageSetupSection implements ModalSection { + private container: HTMLElement | null = null; + private marginInputs: Map = new Map(); + + render(containerEl: HTMLElement, state: ModalState): void { + this.container = containerEl.createDiv('export-section'); + this.container.createEl('h3', { text: 'Page Setup' }); + + this.createPageSizeSettings(state); + this.createOrientationSettings(state); + this.createMarginSettings(state); + } + + private createPageSizeSettings(state: ModalState): void { + if (!this.container) return; + + new Setting(this.container) + .setName('Page size') + .setDesc('Standard page sizes for the PDF document') + .addDropdown(dropdown => { + dropdown + .addOption('a4', 'A4 (210 × 297 mm)') + .addOption('letter', 'Letter (8.5 × 11 in)') + .addOption('legal', 'Legal (8.5 × 14 in)') + .addOption('a3', 'A3 (297 × 420 mm)') + .addOption('a5', 'A5 (148 × 210 mm)') + .setValue(state.templateVariables.pageSize || 'a4') + .onChange(value => { + state.updateTemplateVariables({ pageSize: value }); + }); + }); + } + + private createOrientationSettings(state: ModalState): void { + if (!this.container) return; + + new Setting(this.container) + .setName('Page orientation') + .setDesc('Portrait (tall) or landscape (wide) page layout') + .addDropdown(dropdown => { + dropdown + .addOption('portrait', 'Portrait') + .addOption('landscape', 'Landscape') + .setValue(state.templateVariables.orientation || 'portrait') + .onChange(value => { + const isLandscape = value === 'landscape'; + state.updateTemplateVariables({ + orientation: value, + flipped: isLandscape, + // Auto-adjust width for single-page mode when flipped + ...(isLandscape && state.settings.format === 'single-page' + ? { width: 'auto' } + : {}) + }); + }); + }); + } + + private createMarginSettings(state: ModalState): void { + if (!this.container) return; + + const marginsContainer = this.container.createDiv('margins-container'); + marginsContainer.createEl('h4', { text: 'Text Margins' }); + marginsContainer.createEl('p', { + text: 'Enter margins with units (e.g., "1in", "2.5cm", "72pt")', + cls: 'setting-item-description' + }); + + const marginDefaults = { + top: '1in', + bottom: '0.8in', + left: '1in', + right: '0.6in' + }; + + // Create margin text inputs + ['top', 'bottom', 'left', 'right'].forEach(side => { + new Setting(marginsContainer) + .setName(`${side.charAt(0).toUpperCase() + side.slice(1)} margin`) + .setDesc(`${side} text margin (e.g., "1in", "2cm", "72pt")`) + .addText(text => { + const input = text + .setPlaceholder(marginDefaults[side as keyof typeof marginDefaults]) + .setValue(state.templateVariables[`margin${side.charAt(0).toUpperCase() + side.slice(1)}`] || marginDefaults[side as keyof typeof marginDefaults]) + .onChange(value => { + const marginKey = `margin${side.charAt(0).toUpperCase() + side.slice(1)}`; + state.updateTemplateVariables({ [marginKey]: value }); + }); + + this.marginInputs.set(side, input.inputEl); + }); + }); + } + + validate(): ValidationResult { + const errors: string[] = []; + const warnings: string[] = []; + + // Validate margin formats + this.marginInputs.forEach((input, side) => { + const value = input.value.trim(); + if (value) { + // Check for valid margin format (number + unit) + const marginRegex = /^[\d.]+\s*(in|cm|mm|pt)$/; + if (!marginRegex.test(value)) { + errors.push(`${side.charAt(0).toUpperCase() + side.slice(1)} margin must include a unit (e.g., "1in", "2cm", "72pt")`); + } + } + }); + + return { + isValid: errors.length === 0, + errors, + warnings + }; + } + + getId(): string { + return 'page-setup'; + } +} \ No newline at end of file diff --git a/src/modal/sections/TypographySection.ts b/src/modal/sections/TypographySection.ts index f4626c1..95833cc 100644 --- a/src/modal/sections/TypographySection.ts +++ b/src/modal/sections/TypographySection.ts @@ -3,15 +3,15 @@ * Handles font selection and size configuration */ -import { Setting } from 'obsidian'; +import { Setting, App } from 'obsidian'; import { ModalSection, ModalState, ValidationResult } from '../types'; export class TypographySection implements ModalSection { private container: HTMLElement | null = null; - private fontInputs: Map = new Map(); - private sizeSliders: Map = new Map(); + private app: App | null = null; - render(containerEl: HTMLElement, state: ModalState): void { + render(containerEl: HTMLElement, state: ModalState, app?: App): void { + if (app) this.app = app; this.container = containerEl.createDiv('export-section'); this.container.createEl('h3', { text: 'Typography' }); @@ -22,71 +22,175 @@ export class TypographySection implements ModalSection { private createFontSettings(state: ModalState): void { if (!this.container) return; - // Body font - const bodyFontSetting = new Setting(this.container) + // Body font dropdown + new Setting(this.container) .setName('Body font') .setDesc('Primary font for document text') - .addText(text => { - const input = text - .setPlaceholder('Concourse OT') - .setValue(state.typography.fontFamily) - .onChange(value => { - state.updateTypography({ fontFamily: value }); + .addDropdown(async (dropdown) => { + try { + const fonts = await this.getAvailableFonts(state); + fonts.forEach(font => { + dropdown.addOption(font, font); }); - this.fontInputs.set('body', input.inputEl); - return text; + + 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); + } }); - // Heading font (future enhancement) - // For now, we'll use the same font for all text types - // but this can be expanded later + // 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); + } + }); - // Monospace font (future enhancement) - // Similar to heading font, can be expanded later + // 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); + } + }); } private createFontSizeSettings(state: ModalState): void { if (!this.container) return; - const sizesContainer = this.container.createDiv('font-sizes'); - sizesContainer.createEl('h4', { text: 'Text Formatting' }); - - // Font size - new Setting(sizesContainer) - .setName('Font size') + // Body font size + new Setting(this.container) + .setName('Body font size') .setDesc('Base font size for document text') .addDropdown(dropdown => { dropdown - .addOptions({ - '9pt': '9pt - Small', - '10pt': '10pt - Compact', - '11pt': '11pt - Standard', - '12pt': '12pt - Large', - '14pt': '14pt - Extra Large' - }) - .setValue(state.typography.fontSize) + .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.updateTypography({ fontSize: value }); + state.updateTemplateVariables({ bodyFontSize: value }); }); }); - - // Line height - new Setting(sizesContainer) - .setName('Line height') - .setDesc('Spacing between lines of text') - .addDropdown(dropdown => { - dropdown - .addOptions({ - '1.0': 'Single', - '1.15': 'Slightly Loose', - '1.5': 'One and a Half', - '1.8': 'Relaxed', - '2.0': 'Double' - }) - .setValue(state.typography.lineHeight) - .onChange(value => { - state.updateTypography({ lineHeight: value }); - }); + } + + // Helper methods for fonts - similar to settings tab + private async getAvailableFonts(state: ModalState): Promise { + try { + // Access the plugin instance through the app + // @ts-ignore - accessing private plugin manager + const plugin = this.app?.plugins?.plugins?.['obsidian-typst-pdf-export']; + if (plugin && plugin.getCachedFonts) { + return await plugin.getCachedFonts(); + } + + // Fallback to hardcoded fonts if plugin not accessible + return [ + 'Times New Roman', + 'Arial', + 'Helvetica', + 'Georgia', + 'Courier New', + 'Monaco', + 'SF Pro Text', + 'SF Mono', + 'Concourse OT', + 'UbuntuMono Nerd Font Mono', + 'Source Code Pro' + ]; + } catch (error) { + console.error('Failed to get cached fonts:', error); + // Return common fallback fonts + return [ + 'Times New Roman', + 'Arial', + 'Helvetica', + 'Georgia', + 'Courier New', + 'Monaco', + 'SF Pro Text', + 'SF Mono', + 'Concourse OT', + 'UbuntuMono Nerd Font Mono', + 'Source Code Pro' + ]; + } + } + + private addFallbackBodyFonts(dropdown: any, state: ModalState): void { + dropdown + .addOption('Concourse OT', 'Concourse OT') + .addOption('Times New Roman', 'Times New Roman') + .addOption('Georgia', 'Georgia') + .addOption('Arial', 'Arial') + .addOption('Helvetica', 'Helvetica') + .addOption('Calibri', 'Calibri') + .addOption('Cambria', 'Cambria') + .addOption('Palatino', 'Palatino') + .addOption('Book Antiqua', 'Book Antiqua') + .setValue(state.templateVariables.bodyFont || state.templateVariables.headingFont || 'Concourse OT') + .onChange((value: string) => { + if (dropdown.containerEl.closest('.setting-item')?.querySelector('.setting-item-name')?.textContent?.includes('Body')) { + state.updateTemplateVariables({ bodyFont: value }); + } else { + state.updateTemplateVariables({ headingFont: value }); + } + }); + } + + private addFallbackMonospaceFonts(dropdown: any, state: ModalState): void { + dropdown + .addOption('Source Code Pro', 'Source Code Pro') + .addOption('Courier New', 'Courier New') + .addOption('Monaco', 'Monaco') + .addOption('Consolas', 'Consolas') + .addOption('Menlo', 'Menlo') + .addOption('DejaVu Sans Mono', 'DejaVu Sans Mono') + .addOption('Liberation Mono', 'Liberation Mono') + .addOption('Ubuntu Mono', 'Ubuntu Mono') + .setValue(state.templateVariables.monospaceFont || 'Source Code Pro') + .onChange((value: string) => { + state.updateTemplateVariables({ monospaceFont: value }); }); } @@ -94,13 +198,7 @@ export class TypographySection implements ModalSection { const errors: string[] = []; const warnings: string[] = []; - // Check if fonts are likely available - this.fontInputs.forEach((input, type) => { - const value = input.value.trim(); - if (value && this.isUncommonFont(value)) { - warnings.push(`Font "${value}" may not be available on the system. The template will use fallback fonts if needed.`); - } - }); + // No validation needed for dropdowns with predefined options return { isValid: errors.length === 0, @@ -109,20 +207,6 @@ export class TypographySection implements ModalSection { }; } - private isUncommonFont(fontName: string): boolean { - // List of commonly available fonts - const commonFonts = [ - 'arial', 'helvetica', 'times new roman', 'georgia', - 'verdana', 'tahoma', 'trebuchet ms', 'palatino', - 'courier', 'courier new', 'monaco', 'consolas', - 'sf pro', 'roboto', 'open sans', 'lato', - 'concourse ot', 'ubuntu', 'source code pro' - ]; - - const normalized = fontName.toLowerCase().trim(); - return !commonFonts.some(font => normalized.includes(font)); - } - getId(): string { return 'typography'; } diff --git a/src/modal/types.ts b/src/modal/types.ts index 831dd79..484f037 100644 --- a/src/modal/types.ts +++ b/src/modal/types.ts @@ -13,12 +13,10 @@ export interface ExportConfig { outputFolder?: string; /** Template variables for this export */ templateVariables?: Record; - /** Typography settings for this export */ - typography?: { - fontFamily?: string; - fontSize?: string; - lineHeight?: string; - }; + /** Open PDF after export */ + openAfterExport?: boolean; + /** Preserve folder structure when exporting */ + preserveFolderStructure?: boolean; } export interface ExportConfigModalSettings extends ExportConfig { @@ -39,7 +37,7 @@ export interface ExportConfigModalSettings extends ExportConfig { } export interface ModalSection { - render(containerEl: HTMLElement, state: ModalState): void; + render(containerEl: HTMLElement, state: ModalState, app?: any): void; validate?(): ValidationResult; getId(): string; } @@ -52,14 +50,12 @@ export interface ValidationResult { export interface ModalState { settings: ExportConfigModalSettings; - typography: { - fontFamily: string; - fontSize: string; - lineHeight: string; - }; - updateTypography(updates: Partial): void; + templateVariables: Record; updateSettings(updates: Partial): void; updateTemplateVariables(updates: Record): void; buildExportConfig(): ExportConfig; notifyChange(): void; + setProgress(percent: number, operation: string): void; + reset(): void; + onChange(listener: () => void): void; } \ No newline at end of file