Refactors typography settings with helper
Uses a helper method to create the font dropdown settings, reducing code duplication and improving maintainability. The shared logic for setting up font selection dropdowns is now encapsulated in a single function.
This commit is contained in:
1 file changed
+55
-63
@@ -1146,74 +1146,66 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to create a font dropdown setting
|
||||||
|
*/
|
||||||
|
private createFontDropdown(
|
||||||
|
containerEl: HTMLElement,
|
||||||
|
name: string,
|
||||||
|
desc: string,
|
||||||
|
getCurrentValue: () => string,
|
||||||
|
setNewValue: (value: string) => void
|
||||||
|
): void {
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName(name)
|
||||||
|
.setDesc(desc)
|
||||||
|
.addDropdown(async (dropdown) => {
|
||||||
|
try {
|
||||||
|
const fonts = await this.getAvailableFonts();
|
||||||
|
fonts.forEach(font => {
|
||||||
|
dropdown.addOption(font, font);
|
||||||
|
});
|
||||||
|
|
||||||
|
dropdown
|
||||||
|
.setValue(getCurrentValue())
|
||||||
|
.onChange(async (value) => {
|
||||||
|
setNewValue(value);
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to load fonts:', error);
|
||||||
|
dropdown.addOption(getCurrentValue(), getCurrentValue());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private createTypographyDefaultsSection(containerEl: HTMLElement): void {
|
private createTypographyDefaultsSection(containerEl: HTMLElement): void {
|
||||||
containerEl.createEl('h3', { text: 'Typography Defaults' });
|
containerEl.createEl('h3', { text: 'Typography Defaults' });
|
||||||
|
|
||||||
new Setting(containerEl)
|
// Use helper method for all three font dropdowns
|
||||||
.setName('Body font')
|
this.createFontDropdown(
|
||||||
.setDesc('Default font for body text')
|
containerEl,
|
||||||
.addDropdown(async (dropdown) => {
|
'Body font',
|
||||||
try {
|
'Default font for body text',
|
||||||
const fonts = await this.getAvailableFonts();
|
() => this.plugin.settings.typography.fonts.body,
|
||||||
fonts.forEach(font => {
|
(value) => { this.plugin.settings.typography.fonts.body = value; }
|
||||||
dropdown.addOption(font, font);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
dropdown
|
this.createFontDropdown(
|
||||||
.setValue(this.plugin.settings.typography.fonts.body)
|
containerEl,
|
||||||
.onChange(async (value) => {
|
'Heading font',
|
||||||
this.plugin.settings.typography.fonts.body = value;
|
'Default font for headings',
|
||||||
await this.plugin.saveSettings();
|
() => this.plugin.settings.typography.fonts.heading,
|
||||||
});
|
(value) => { this.plugin.settings.typography.fonts.heading = value; }
|
||||||
} catch (error) {
|
);
|
||||||
console.error('Failed to load fonts:', error);
|
|
||||||
dropdown.addOption(this.plugin.settings.typography.fonts.body, this.plugin.settings.typography.fonts.body);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
new Setting(containerEl)
|
this.createFontDropdown(
|
||||||
.setName('Heading font')
|
containerEl,
|
||||||
.setDesc('Default font for headings')
|
'Monospace font',
|
||||||
.addDropdown(async (dropdown) => {
|
'Default font for code and monospace text',
|
||||||
try {
|
() => this.plugin.settings.typography.fonts.monospace,
|
||||||
const fonts = await this.getAvailableFonts();
|
(value) => { this.plugin.settings.typography.fonts.monospace = value; }
|
||||||
fonts.forEach(font => {
|
);
|
||||||
dropdown.addOption(font, font);
|
|
||||||
});
|
|
||||||
|
|
||||||
dropdown
|
|
||||||
.setValue(this.plugin.settings.typography.fonts.heading)
|
|
||||||
.onChange(async (value) => {
|
|
||||||
this.plugin.settings.typography.fonts.heading = value;
|
|
||||||
await this.plugin.saveSettings();
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to load fonts:', error);
|
|
||||||
dropdown.addOption(this.plugin.settings.typography.fonts.heading, this.plugin.settings.typography.fonts.heading);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
new Setting(containerEl)
|
|
||||||
.setName('Monospace font')
|
|
||||||
.setDesc('Default font for code and monospace text')
|
|
||||||
.addDropdown(async (dropdown) => {
|
|
||||||
try {
|
|
||||||
const fonts = await this.getAvailableFonts();
|
|
||||||
fonts.forEach(font => {
|
|
||||||
dropdown.addOption(font, font);
|
|
||||||
});
|
|
||||||
|
|
||||||
dropdown
|
|
||||||
.setValue(this.plugin.settings.typography.fonts.monospace)
|
|
||||||
.onChange(async (value) => {
|
|
||||||
this.plugin.settings.typography.fonts.monospace = value;
|
|
||||||
await this.plugin.saveSettings();
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to load fonts:', error);
|
|
||||||
dropdown.addOption(this.plugin.settings.typography.fonts.monospace, this.plugin.settings.typography.fonts.monospace);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Body font size')
|
.setName('Body font size')
|
||||||
|
|||||||
Reference in new issue
Block a user