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 {
containerEl.createEl('h3', { text: 'Page Setup' });
@@ -1252,49 +1275,42 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Top margin')
.setDesc('Top page margin in centimeters')
.addText(text => text
.setPlaceholder('2.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.top))
.onChange(async (value) => {
this.plugin.settings.pageSetup.margins.top = this.parseMarginValue(value, 2.5);
await this.plugin.saveSettings();
}));
// Test with helper method for top margin only
this.createMarginInput(
containerEl,
'Top margin',
'2.5',
() => this.plugin.settings.pageSetup.margins.top,
(value) => { this.plugin.settings.pageSetup.margins.top = value; },
2.5
);
new Setting(containerEl)
.setName('Bottom margin')
.setDesc('Bottom page margin in centimeters')
.addText(text => text
.setPlaceholder('2.0')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.bottom))
.onChange(async (value) => {
this.plugin.settings.pageSetup.margins.bottom = this.parseMarginValue(value, 2.0);
await this.plugin.saveSettings();
}));
this.createMarginInput(
containerEl,
'Bottom margin',
'2.0',
() => this.plugin.settings.pageSetup.margins.bottom,
(value) => { this.plugin.settings.pageSetup.margins.bottom = value; },
2.0
);
new Setting(containerEl)
.setName('Left margin')
.setDesc('Left page margin in centimeters')
.addText(text => text
.setPlaceholder('2.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.left))
.onChange(async (value) => {
this.plugin.settings.pageSetup.margins.left = this.parseMarginValue(value, 2.5);
await this.plugin.saveSettings();
}));
this.createMarginInput(
containerEl,
'Left margin',
'2.5',
() => this.plugin.settings.pageSetup.margins.left,
(value) => { this.plugin.settings.pageSetup.margins.left = value; },
2.5
);
new Setting(containerEl)
.setName('Right margin')
.setDesc('Right page margin in centimeters')
.addText(text => text
.setPlaceholder('1.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.right))
.onChange(async (value) => {
this.plugin.settings.pageSetup.margins.right = this.parseMarginValue(value, 1.5);
await this.plugin.saveSettings();
}));
this.createMarginInput(
containerEl,
'Right margin',
'1.5',
() => this.plugin.settings.pageSetup.margins.right,
(value) => { this.plugin.settings.pageSetup.margins.right = value; },
1.5
);
}
private createBehaviorSection(containerEl: HTMLElement): void {