Refactors margin input settings

Introduces a helper function to streamline the creation of margin input settings in the plugin settings tab. This improves code readability and reduces redundancy.
This commit is contained in:
AlexKucera committed 2025-09-03 22:46:10 +02:00
1 parent f4d43a7236
commit d4c4a1be73
1 file changed
+56 -40
+56 -40
View File
@@ -1220,6 +1220,29 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
})); }));
} }
/**
* Helper method to create a margin input setting
*/
private createMarginInput(
containerEl: HTMLElement,
name: string,
placeholder: string,
getCurrentValue: () => number,
setNewValue: (value: number) => void,
defaultValue: number
): void {
new Setting(containerEl)
.setName(name)
.setDesc(`${name.toLowerCase()} in centimeters`)
.addText(text => text
.setPlaceholder(placeholder)
.setValue(this.formatSingleMarginForDisplay(getCurrentValue()))
.onChange(async (value) => {
setNewValue(this.parseMarginValue(value, defaultValue));
await this.plugin.saveSettings();
}));
}
private createPageSetupSection(containerEl: HTMLElement): void { private createPageSetupSection(containerEl: HTMLElement): void {
containerEl.createEl('h3', { text: 'Page Setup' }); containerEl.createEl('h3', { text: 'Page Setup' });
@@ -1252,49 +1275,42 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); }));
new Setting(containerEl) // Test with helper method for top margin only
.setName('Top margin') this.createMarginInput(
.setDesc('Top page margin in centimeters') containerEl,
.addText(text => text 'Top margin',
.setPlaceholder('2.5') '2.5',
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.top)) () => this.plugin.settings.pageSetup.margins.top,
.onChange(async (value) => { (value) => { this.plugin.settings.pageSetup.margins.top = value; },
this.plugin.settings.pageSetup.margins.top = this.parseMarginValue(value, 2.5); 2.5
await this.plugin.saveSettings(); );
}));
new Setting(containerEl) this.createMarginInput(
.setName('Bottom margin') containerEl,
.setDesc('Bottom page margin in centimeters') 'Bottom margin',
.addText(text => text '2.0',
.setPlaceholder('2.0') () => this.plugin.settings.pageSetup.margins.bottom,
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.bottom)) (value) => { this.plugin.settings.pageSetup.margins.bottom = value; },
.onChange(async (value) => { 2.0
this.plugin.settings.pageSetup.margins.bottom = this.parseMarginValue(value, 2.0); );
await this.plugin.saveSettings();
}));
new Setting(containerEl) this.createMarginInput(
.setName('Left margin') containerEl,
.setDesc('Left page margin in centimeters') 'Left margin',
.addText(text => text '2.5',
.setPlaceholder('2.5') () => this.plugin.settings.pageSetup.margins.left,
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.left)) (value) => { this.plugin.settings.pageSetup.margins.left = value; },
.onChange(async (value) => { 2.5
this.plugin.settings.pageSetup.margins.left = this.parseMarginValue(value, 2.5); );
await this.plugin.saveSettings();
}));
new Setting(containerEl) this.createMarginInput(
.setName('Right margin') containerEl,
.setDesc('Right page margin in centimeters') 'Right margin',
.addText(text => text '1.5',
.setPlaceholder('1.5') () => this.plugin.settings.pageSetup.margins.right,
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.right)) (value) => { this.plugin.settings.pageSetup.margins.right = value; },
.onChange(async (value) => { 1.5
this.plugin.settings.pageSetup.margins.right = this.parseMarginValue(value, 1.5); );
await this.plugin.saveSettings();
}));
} }
private createBehaviorSection(containerEl: HTMLElement): void { private createBehaviorSection(containerEl: HTMLElement): void {