From c7498521258a63a698305eec784eb0fb5248b511 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 13:59:35 +0200 Subject: [PATCH] Simplifies margin handling to use centimeters Updates the margin settings to directly use centimeters, removing unnecessary conversions to points. This simplifies the configuration and improves accuracy in setting page margins for PDF exports. The settings tab and modal now accept and display margins in centimeters. --- main.ts | 27 +++++++++++++------------- src/converters/PandocTypstConverter.ts | 12 ++++-------- src/core/settings.ts | 18 ++++++++--------- src/modal/sections/PageSetupSection.ts | 23 +++++++++++++--------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/main.ts b/main.ts index ddc3de5..15f649e 100644 --- a/main.ts +++ b/main.ts @@ -1233,10 +1233,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { .setName('Top margin') .setDesc('Top page margin in centimeters') .addText(text => text - .setPlaceholder('2.54') + .setPlaceholder('2.5') .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.top)) .onChange(async (value) => { - this.plugin.settings.pageSetup.margins.top = this.parseMarginValue(value, 72); + this.plugin.settings.pageSetup.margins.top = this.parseMarginValue(value, 2.5); await this.plugin.saveSettings(); })); @@ -1244,10 +1244,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { .setName('Bottom margin') .setDesc('Bottom page margin in centimeters') .addText(text => text - .setPlaceholder('2.54') + .setPlaceholder('2.0') .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.bottom)) .onChange(async (value) => { - this.plugin.settings.pageSetup.margins.bottom = this.parseMarginValue(value, 72); + this.plugin.settings.pageSetup.margins.bottom = this.parseMarginValue(value, 2.0); await this.plugin.saveSettings(); })); @@ -1255,10 +1255,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { .setName('Left margin') .setDesc('Left page margin in centimeters') .addText(text => text - .setPlaceholder('2.54') + .setPlaceholder('2.5') .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.left)) .onChange(async (value) => { - this.plugin.settings.pageSetup.margins.left = this.parseMarginValue(value, 72); + this.plugin.settings.pageSetup.margins.left = this.parseMarginValue(value, 2.5); await this.plugin.saveSettings(); })); @@ -1266,10 +1266,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { .setName('Right margin') .setDesc('Right page margin in centimeters') .addText(text => text - .setPlaceholder('2.54') + .setPlaceholder('1.5') .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.right)) .onChange(async (value) => { - this.plugin.settings.pageSetup.margins.right = this.parseMarginValue(value, 72); + this.plugin.settings.pageSetup.margins.right = this.parseMarginValue(value, 1.5); await this.plugin.saveSettings(); })); } @@ -1314,15 +1314,14 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab { } // Helper methods for margin conversion - private formatSingleMarginForDisplay(marginPoints: number): string { - // Convert from points to CM (1 point = 0.035278 cm) - const marginCm = (marginPoints * 0.035278).toFixed(2); - return marginCm; + private formatSingleMarginForDisplay(marginCm: number): string { + // No conversion needed - directly return centimeters + return marginCm.toFixed(2); } - private parseMarginValue(value: string, defaultPoints: number): number { + private parseMarginValue(value: string, defaultCm: number): number { const cm = parseFloat(value.trim()); - return isNaN(cm) ? defaultPoints : cm / 0.035278; // Convert CM to points + return isNaN(cm) ? defaultCm : cm; // No conversion needed - directly use centimeters } } diff --git a/src/converters/PandocTypstConverter.ts b/src/converters/PandocTypstConverter.ts index 5c01158..a3dea0c 100644 --- a/src/converters/PandocTypstConverter.ts +++ b/src/converters/PandocTypstConverter.ts @@ -447,20 +447,16 @@ export class PandocTypstConverter { if (settings.pageSetup.margins) { const margins = settings.pageSetup.margins; if (!existingVars.marginTop && !existingVars.margin_top && margins.top !== undefined) { - const topCm = (margins.top * 0.0352778).toFixed(2); - args.push('-V', `margin_top=${topCm}cm`); + args.push('-V', `margin_top=${margins.top}cm`); } if (!existingVars.marginRight && !existingVars.margin_right && margins.right !== undefined) { - const rightCm = (margins.right * 0.0352778).toFixed(2); - args.push('-V', `margin_right=${rightCm}cm`); + args.push('-V', `margin_right=${margins.right}cm`); } if (!existingVars.marginBottom && !existingVars.margin_bottom && margins.bottom !== undefined) { - const bottomCm = (margins.bottom * 0.0352778).toFixed(2); - args.push('-V', `margin_bottom=${bottomCm}cm`); + args.push('-V', `margin_bottom=${margins.bottom}cm`); } if (!existingVars.marginLeft && !existingVars.margin_left && margins.left !== undefined) { - const leftCm = (margins.left * 0.0352778).toFixed(2); - args.push('-V', `margin_left=${leftCm}cm`); + args.push('-V', `margin_left=${margins.left}cm`); } } } diff --git a/src/core/settings.ts b/src/core/settings.ts index 8793464..db68da0 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -107,10 +107,10 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = { format: ExportFormat.Standard, pageSize: 'a4', orientation: 'portrait', - marginTop: '1in', - marginBottom: '0.8in', - marginLeft: '1in', - marginRight: '0.6in', + marginTop: '2.5', + marginBottom: '2.0', + marginLeft: '2.5', + marginRight: '1.5', bodyFont: 'Concourse OT', headingFont: 'Concourse OT', monospaceFont: 'Source Code Pro', @@ -134,10 +134,10 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = { size: 'a4', orientation: 'portrait', margins: { - top: 72, - right: 72, - bottom: 72, - left: 72 + top: 2.54, + right: 1.52, + bottom: 2.03, + left: 2.54 } }, @@ -147,4 +147,4 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = { exportConcurrency: 3, debugMode: false } -}; \ No newline at end of file +};; \ No newline at end of file diff --git a/src/modal/sections/PageSetupSection.ts b/src/modal/sections/PageSetupSection.ts index bcc60ef..f125a42 100644 --- a/src/modal/sections/PageSetupSection.ts +++ b/src/modal/sections/PageSetupSection.ts @@ -70,22 +70,22 @@ export class PageSetupSection implements ModalSection { 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")', + text: 'Enter margins in centimeters (e.g., "2.5", "1.8", "3.0")', cls: 'setting-item-description' }); const marginDefaults = { - top: '1in', - bottom: '0.8in', - left: '1in', - right: '0.6in' + top: '2.5', + bottom: '2.0', + left: '2.5', + right: '1.5' }; // 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")`) + .setDesc(`${side} text margin in centimeters`) .addText(text => { const input = text .setPlaceholder(marginDefaults[side as keyof typeof marginDefaults]) @@ -108,10 +108,15 @@ export class PageSetupSection implements ModalSection { 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)$/; + // Check for valid margin format (number only, centimeters assumed) + const marginRegex = /^\d*\.?\d+$/; if (!marginRegex.test(value)) { - errors.push(`${side.charAt(0).toUpperCase() + side.slice(1)} margin must include a unit (e.g., "1in", "2cm", "72pt")`); + errors.push(`${side.charAt(0).toUpperCase() + side.slice(1)} margin must be a number in centimeters (e.g., "2.5")`); + } else { + const num = parseFloat(value); + if (num <= 0) { + errors.push(`${side.charAt(0).toUpperCase() + side.slice(1)} margin must be greater than 0`); + } } } });