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.
This commit is contained in:
AlexKucera committed 2025-09-03 10:16:48 +02:00
1 parent 2bec4963b3
commit 6eedeaace3
6 files changed
+417 -107

No files matched your search

+26 -1
View File
@@ -31,6 +31,21 @@ export interface obsidianTypstPDFExportSettings {
template: string; template: string;
/** Default export format */ /** Default export format */
format: ExportFormat; 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 */ /** Typography settings */
@@ -89,7 +104,17 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = {
exportDefaults: { exportDefaults: {
template: 'default', 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: { typography: {
+63
View File
@@ -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';
}
}
+33 -20
View File
@@ -3,16 +3,19 @@
* Handles template selection, format options, and output settings * Handles template selection, format options, and output settings
*/ */
import { Setting } from 'obsidian'; import { Setting, App } from 'obsidian';
import { ModalSection, ModalState, ValidationResult } from '../types'; import { ModalSection, ModalState, ValidationResult } from '../types';
import { ExportFormat } from '../../core/settings'; import { ExportFormat } from '../../core/settings';
import { FolderSuggest } from '../../components/FolderSuggest';
export class GeneralSection implements ModalSection { export class GeneralSection implements ModalSection {
private container: HTMLElement | null = null; private container: HTMLElement | null = null;
private templateDropdown: any = null; private templateDropdown: any = null;
private outputFolderInput: HTMLInputElement | null = 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 = containerEl.createDiv('export-section');
this.container.createEl('h3', { text: 'General Settings' }); this.container.createEl('h3', { text: 'General Settings' });
@@ -28,8 +31,11 @@ export class GeneralSection implements ModalSection {
.setName('Typst template') .setName('Typst template')
.setDesc('Select template for document styling') .setDesc('Select template for document styling')
.addDropdown(dropdown => { .addDropdown(dropdown => {
// Populate with available templates // Filter out universal-wrapper.pandoc.typ and populate with available templates
state.settings.availableTemplates.forEach(template => { const filteredTemplates = state.settings.availableTemplates
.filter(template => template !== 'universal-wrapper.pandoc.typ');
filteredTemplates.forEach(template => {
dropdown.addOption(template, this.getTemplateDisplayName(template)); dropdown.addOption(template, this.getTemplateDisplayName(template));
}); });
@@ -74,6 +80,11 @@ export class GeneralSection implements ModalSection {
state.updateSettings({ outputFolder: value }); state.updateSettings({ outputFolder: value });
}); });
this.outputFolderInput = input.inputEl; 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 { updateAvailableTemplates(templates: string[]): void {
if (!this.templateDropdown) return; if (!this.templateDropdown) return;
const currentValue = this.templateDropdown.getValue(); const currentValue = this.templateDropdown.getValue();
// Clear and repopulate dropdown // Filter out universal-wrapper.pandoc.typ and clear/repopulate dropdown
this.templateDropdown.selectEl.empty(); const filteredTemplates = templates.filter(template => template !== 'universal-wrapper.pandoc.typ');
templates.forEach(template => {
this.templateDropdown.addOption(template, this.getTemplateDisplayName(template)); this.templateDropdown.selectEl.empty();
}); filteredTemplates.forEach(template => {
this.templateDropdown.addOption(template, this.getTemplateDisplayName(template));
// Restore selection if still available });
if (templates.includes(currentValue)) {
this.templateDropdown.setValue(currentValue); // Restore selection if still available
} else if (templates.length > 0) { if (filteredTemplates.includes(currentValue)) {
this.templateDropdown.setValue(templates[0]); this.templateDropdown.setValue(currentValue);
} } else if (filteredTemplates.length > 0) {
this.templateDropdown.setValue(filteredTemplates[0]);
} }
}
getId(): string { getId(): string {
return 'general'; return 'general';
+129
View File
@@ -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<string, HTMLInputElement> = 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';
}
}
+157 -73
View File
@@ -3,15 +3,15 @@
* Handles font selection and size configuration * Handles font selection and size configuration
*/ */
import { Setting } from 'obsidian'; import { Setting, App } from 'obsidian';
import { ModalSection, ModalState, ValidationResult } from '../types'; import { ModalSection, ModalState, ValidationResult } from '../types';
export class TypographySection implements ModalSection { export class TypographySection implements ModalSection {
private container: HTMLElement | null = null; private container: HTMLElement | null = null;
private fontInputs: Map<string, HTMLInputElement> = new Map(); private app: App | null = null;
private sizeSliders: Map<string, any> = new Map();
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 = containerEl.createDiv('export-section');
this.container.createEl('h3', { text: 'Typography' }); this.container.createEl('h3', { text: 'Typography' });
@@ -22,71 +22,175 @@ export class TypographySection implements ModalSection {
private createFontSettings(state: ModalState): void { private createFontSettings(state: ModalState): void {
if (!this.container) return; if (!this.container) return;
// Body font // Body font dropdown
const bodyFontSetting = new Setting(this.container) new Setting(this.container)
.setName('Body font') .setName('Body font')
.setDesc('Primary font for document text') .setDesc('Primary font for document text')
.addText(text => { .addDropdown(async (dropdown) => {
const input = text try {
.setPlaceholder('Concourse OT') const fonts = await this.getAvailableFonts(state);
.setValue(state.typography.fontFamily) fonts.forEach(font => {
.onChange(value => { dropdown.addOption(font, font);
state.updateTypography({ fontFamily: value });
}); });
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) // Heading font dropdown
// For now, we'll use the same font for all text types new Setting(this.container)
// but this can be expanded later .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) // Monospace font dropdown
// Similar to heading font, can be expanded later 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 { private createFontSizeSettings(state: ModalState): void {
if (!this.container) return; if (!this.container) return;
const sizesContainer = this.container.createDiv('font-sizes'); // Body font size
sizesContainer.createEl('h4', { text: 'Text Formatting' }); new Setting(this.container)
.setName('Body font size')
// Font size
new Setting(sizesContainer)
.setName('Font size')
.setDesc('Base font size for document text') .setDesc('Base font size for document text')
.addDropdown(dropdown => { .addDropdown(dropdown => {
dropdown dropdown
.addOptions({ .addOption('9pt', '9pt - Small')
'9pt': '9pt - Small', .addOption('10pt', '10pt - Compact')
'10pt': '10pt - Compact', .addOption('11pt', '11pt - Standard')
'11pt': '11pt - Standard', .addOption('12pt', '12pt - Large')
'12pt': '12pt - Large', .addOption('14pt', '14pt - Extra Large')
'14pt': '14pt - Extra Large' .setValue(state.templateVariables.bodyFontSize || '11pt')
})
.setValue(state.typography.fontSize)
.onChange(value => { .onChange(value => {
state.updateTypography({ fontSize: value }); state.updateTemplateVariables({ bodyFontSize: value });
}); });
}); });
}
// Line height
new Setting(sizesContainer) // Helper methods for fonts - similar to settings tab
.setName('Line height') private async getAvailableFonts(state: ModalState): Promise<string[]> {
.setDesc('Spacing between lines of text') try {
.addDropdown(dropdown => { // Access the plugin instance through the app
dropdown // @ts-ignore - accessing private plugin manager
.addOptions({ const plugin = this.app?.plugins?.plugins?.['obsidian-typst-pdf-export'];
'1.0': 'Single', if (plugin && plugin.getCachedFonts) {
'1.15': 'Slightly Loose', return await plugin.getCachedFonts();
'1.5': 'One and a Half', }
'1.8': 'Relaxed',
'2.0': 'Double' // Fallback to hardcoded fonts if plugin not accessible
}) return [
.setValue(state.typography.lineHeight) 'Times New Roman',
.onChange(value => { 'Arial',
state.updateTypography({ lineHeight: value }); '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 errors: string[] = [];
const warnings: string[] = []; const warnings: string[] = [];
// Check if fonts are likely available // No validation needed for dropdowns with predefined options
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.`);
}
});
return { return {
isValid: errors.length === 0, 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 { getId(): string {
return 'typography'; return 'typography';
} }
+9 -13
View File
@@ -13,12 +13,10 @@ export interface ExportConfig {
outputFolder?: string; outputFolder?: string;
/** Template variables for this export */ /** Template variables for this export */
templateVariables?: Record<string, any>; templateVariables?: Record<string, any>;
/** Typography settings for this export */ /** Open PDF after export */
typography?: { openAfterExport?: boolean;
fontFamily?: string; /** Preserve folder structure when exporting */
fontSize?: string; preserveFolderStructure?: boolean;
lineHeight?: string;
};
} }
export interface ExportConfigModalSettings extends ExportConfig { export interface ExportConfigModalSettings extends ExportConfig {
@@ -39,7 +37,7 @@ export interface ExportConfigModalSettings extends ExportConfig {
} }
export interface ModalSection { export interface ModalSection {
render(containerEl: HTMLElement, state: ModalState): void; render(containerEl: HTMLElement, state: ModalState, app?: any): void;
validate?(): ValidationResult; validate?(): ValidationResult;
getId(): string; getId(): string;
} }
@@ -52,14 +50,12 @@ export interface ValidationResult {
export interface ModalState { export interface ModalState {
settings: ExportConfigModalSettings; settings: ExportConfigModalSettings;
typography: { templateVariables: Record<string, any>;
fontFamily: string;
fontSize: string;
lineHeight: string;
};
updateTypography(updates: Partial<ModalState['typography']>): void;
updateSettings(updates: Partial<ExportConfigModalSettings>): void; updateSettings(updates: Partial<ExportConfigModalSettings>): void;
updateTemplateVariables(updates: Record<string, any>): void; updateTemplateVariables(updates: Record<string, any>): void;
buildExportConfig(): ExportConfig; buildExportConfig(): ExportConfig;
notifyChange(): void; notifyChange(): void;
setProgress(percent: number, operation: string): void;
reset(): void;
onChange(listener: () => void): void;
} }