Adds error handling for font settings initialization

Implements error handling for font settings creation to ensure
fallback to default fonts if loading fails. Reorders font size
settings setup to follow font dropdowns for logical flow.

Enhances robustness and improves user settings experience.
This commit is contained in:
AlexKucera committed 2025-11-09 10:53:46 +01:00
1 parent b0df21d9c2
commit d6d31afb16
1 file changed
+26 -20
+26 -20
View File
@@ -16,35 +16,38 @@ export class TypographySection implements ModalSection {
new Setting(this.container) new Setting(this.container)
.setName('Typography') .setName('Typography')
.setHeading(); .setHeading();
// Create font settings asynchronously // Create font settings asynchronously with explicit error handling
void this.createFontSettings(state); // Font size settings are created after font dropdowns to preserve logical ordering
this.createFontSizeSettings(state); this.createFontSettings(state).catch(error => {
console.error('Failed to create font settings:', error);
// Font settings will fall back to default fonts if this fails
});
} }
private async createFontSettings(state: ModalState): Promise<void> { private async createFontSettings(state: ModalState): Promise<void> {
if (!this.container) return; if (!this.container) return;
// Get available fonts from cache // Get available fonts from cache
const availableFonts = await this.getAvailableFonts(state); const availableFonts = await this.getAvailableFonts(state);
// Body font dropdown // Body font dropdown
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')
.addDropdown(dropdown => { .addDropdown(dropdown => {
const currentFont = String(state.templateVariables.bodyFont || 'Times New Roman'); const currentFont = String(state.templateVariables.bodyFont || 'Times New Roman');
// Add all available fonts // Add all available fonts
availableFonts.forEach(font => { availableFonts.forEach(font => {
dropdown.addOption(font, font); dropdown.addOption(font, font);
}); });
// Add current font if it's not in the list // Add current font if it's not in the list
if (!availableFonts.includes(currentFont)) { if (!availableFonts.includes(currentFont)) {
dropdown.addOption(currentFont, currentFont); dropdown.addOption(currentFont, currentFont);
} }
// Set current value and change handler // Set current value and change handler
dropdown dropdown
.setValue(currentFont) .setValue(currentFont)
@@ -52,24 +55,24 @@ export class TypographySection implements ModalSection {
state.updateTemplateVariables({ bodyFont: value }); state.updateTemplateVariables({ bodyFont: value });
}); });
}); });
// Heading font dropdown // Heading font dropdown
new Setting(this.container) new Setting(this.container)
.setName('Heading font') .setName('Heading font')
.setDesc('Font for headings and titles') .setDesc('Font for headings and titles')
.addDropdown(dropdown => { .addDropdown(dropdown => {
const currentFont = String(state.templateVariables.headingFont || 'Times New Roman'); const currentFont = String(state.templateVariables.headingFont || 'Times New Roman');
// Add all available fonts // Add all available fonts
availableFonts.forEach(font => { availableFonts.forEach(font => {
dropdown.addOption(font, font); dropdown.addOption(font, font);
}); });
// Add current font if it's not in the list // Add current font if it's not in the list
if (!availableFonts.includes(currentFont)) { if (!availableFonts.includes(currentFont)) {
dropdown.addOption(currentFont, currentFont); dropdown.addOption(currentFont, currentFont);
} }
// Set current value and change handler // Set current value and change handler
dropdown dropdown
.setValue(currentFont) .setValue(currentFont)
@@ -77,24 +80,24 @@ export class TypographySection implements ModalSection {
state.updateTemplateVariables({ headingFont: value }); state.updateTemplateVariables({ headingFont: value });
}); });
}); });
// Monospace font dropdown // Monospace font dropdown
new Setting(this.container) new Setting(this.container)
.setName('Monospace font') .setName('Monospace font')
.setDesc('Font for code blocks and inline code') .setDesc('Font for code blocks and inline code')
.addDropdown(dropdown => { .addDropdown(dropdown => {
const currentFont = String(state.templateVariables.monospaceFont || 'Courier New'); const currentFont = String(state.templateVariables.monospaceFont || 'Courier New');
// Add all available fonts // Add all available fonts
availableFonts.forEach(font => { availableFonts.forEach(font => {
dropdown.addOption(font, font); dropdown.addOption(font, font);
}); });
// Add current font if it's not in the list // Add current font if it's not in the list
if (!availableFonts.includes(currentFont)) { if (!availableFonts.includes(currentFont)) {
dropdown.addOption(currentFont, currentFont); dropdown.addOption(currentFont, currentFont);
} }
// Set current value and change handler // Set current value and change handler
dropdown dropdown
.setValue(currentFont) .setValue(currentFont)
@@ -102,8 +105,11 @@ export class TypographySection implements ModalSection {
state.updateTemplateVariables({ monospaceFont: value }); state.updateTemplateVariables({ monospaceFont: value });
}); });
}); });
// Create font size settings after font dropdowns for logical ordering
this.createFontSizeSettings(state);
} }
private createFontSizeSettings(state: ModalState): void { private createFontSizeSettings(state: ModalState): void {
if (!this.container) return; if (!this.container) return;