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.
This commit is contained in:
AlexKucera committed 2025-09-03 13:59:35 +02:00
1 parent 4b4f0e55ed
commit c749852125
4 files changed
+40 -40

No files matched your search

+13 -14
View File
@@ -1233,10 +1233,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
.setName('Top margin') .setName('Top margin')
.setDesc('Top page margin in centimeters') .setDesc('Top page margin in centimeters')
.addText(text => text .addText(text => text
.setPlaceholder('2.54') .setPlaceholder('2.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.top)) .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.top))
.onChange(async (value) => { .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(); await this.plugin.saveSettings();
})); }));
@@ -1244,10 +1244,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
.setName('Bottom margin') .setName('Bottom margin')
.setDesc('Bottom page margin in centimeters') .setDesc('Bottom page margin in centimeters')
.addText(text => text .addText(text => text
.setPlaceholder('2.54') .setPlaceholder('2.0')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.bottom)) .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.bottom))
.onChange(async (value) => { .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(); await this.plugin.saveSettings();
})); }));
@@ -1255,10 +1255,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
.setName('Left margin') .setName('Left margin')
.setDesc('Left page margin in centimeters') .setDesc('Left page margin in centimeters')
.addText(text => text .addText(text => text
.setPlaceholder('2.54') .setPlaceholder('2.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.left)) .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.left))
.onChange(async (value) => { .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(); await this.plugin.saveSettings();
})); }));
@@ -1266,10 +1266,10 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
.setName('Right margin') .setName('Right margin')
.setDesc('Right page margin in centimeters') .setDesc('Right page margin in centimeters')
.addText(text => text .addText(text => text
.setPlaceholder('2.54') .setPlaceholder('1.5')
.setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.right)) .setValue(this.formatSingleMarginForDisplay(this.plugin.settings.pageSetup.margins.right))
.onChange(async (value) => { .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(); await this.plugin.saveSettings();
})); }));
} }
@@ -1314,15 +1314,14 @@ class ObsidianTypstPDFExportSettingTab extends PluginSettingTab {
} }
// Helper methods for margin conversion // Helper methods for margin conversion
private formatSingleMarginForDisplay(marginPoints: number): string { private formatSingleMarginForDisplay(marginCm: number): string {
// Convert from points to CM (1 point = 0.035278 cm) // No conversion needed - directly return centimeters
const marginCm = (marginPoints * 0.035278).toFixed(2); return marginCm.toFixed(2);
return marginCm;
} }
private parseMarginValue(value: string, defaultPoints: number): number { private parseMarginValue(value: string, defaultCm: number): number {
const cm = parseFloat(value.trim()); 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
} }
} }
+4 -8
View File
@@ -447,20 +447,16 @@ export class PandocTypstConverter {
if (settings.pageSetup.margins) { if (settings.pageSetup.margins) {
const margins = settings.pageSetup.margins; const margins = settings.pageSetup.margins;
if (!existingVars.marginTop && !existingVars.margin_top && margins.top !== undefined) { if (!existingVars.marginTop && !existingVars.margin_top && margins.top !== undefined) {
const topCm = (margins.top * 0.0352778).toFixed(2); args.push('-V', `margin_top=${margins.top}cm`);
args.push('-V', `margin_top=${topCm}cm`);
} }
if (!existingVars.marginRight && !existingVars.margin_right && margins.right !== undefined) { if (!existingVars.marginRight && !existingVars.margin_right && margins.right !== undefined) {
const rightCm = (margins.right * 0.0352778).toFixed(2); args.push('-V', `margin_right=${margins.right}cm`);
args.push('-V', `margin_right=${rightCm}cm`);
} }
if (!existingVars.marginBottom && !existingVars.margin_bottom && margins.bottom !== undefined) { if (!existingVars.marginBottom && !existingVars.margin_bottom && margins.bottom !== undefined) {
const bottomCm = (margins.bottom * 0.0352778).toFixed(2); args.push('-V', `margin_bottom=${margins.bottom}cm`);
args.push('-V', `margin_bottom=${bottomCm}cm`);
} }
if (!existingVars.marginLeft && !existingVars.margin_left && margins.left !== undefined) { if (!existingVars.marginLeft && !existingVars.margin_left && margins.left !== undefined) {
const leftCm = (margins.left * 0.0352778).toFixed(2); args.push('-V', `margin_left=${margins.left}cm`);
args.push('-V', `margin_left=${leftCm}cm`);
} }
} }
} }
+9 -9
View File
@@ -107,10 +107,10 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = {
format: ExportFormat.Standard, format: ExportFormat.Standard,
pageSize: 'a4', pageSize: 'a4',
orientation: 'portrait', orientation: 'portrait',
marginTop: '1in', marginTop: '2.5',
marginBottom: '0.8in', marginBottom: '2.0',
marginLeft: '1in', marginLeft: '2.5',
marginRight: '0.6in', marginRight: '1.5',
bodyFont: 'Concourse OT', bodyFont: 'Concourse OT',
headingFont: 'Concourse OT', headingFont: 'Concourse OT',
monospaceFont: 'Source Code Pro', monospaceFont: 'Source Code Pro',
@@ -134,10 +134,10 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = {
size: 'a4', size: 'a4',
orientation: 'portrait', orientation: 'portrait',
margins: { margins: {
top: 72, top: 2.54,
right: 72, right: 1.52,
bottom: 72, bottom: 2.03,
left: 72 left: 2.54
} }
}, },
@@ -147,4 +147,4 @@ export const DEFAULT_SETTINGS: obsidianTypstPDFExportSettings = {
exportConcurrency: 3, exportConcurrency: 3,
debugMode: false debugMode: false
} }
}; };;
+14 -9
View File
@@ -70,22 +70,22 @@ export class PageSetupSection implements ModalSection {
const marginsContainer = this.container.createDiv('margins-container'); const marginsContainer = this.container.createDiv('margins-container');
marginsContainer.createEl('h4', { text: 'Text Margins' }); marginsContainer.createEl('h4', { text: 'Text Margins' });
marginsContainer.createEl('p', { 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' cls: 'setting-item-description'
}); });
const marginDefaults = { const marginDefaults = {
top: '1in', top: '2.5',
bottom: '0.8in', bottom: '2.0',
left: '1in', left: '2.5',
right: '0.6in' right: '1.5'
}; };
// Create margin text inputs // Create margin text inputs
['top', 'bottom', 'left', 'right'].forEach(side => { ['top', 'bottom', 'left', 'right'].forEach(side => {
new Setting(marginsContainer) new Setting(marginsContainer)
.setName(`${side.charAt(0).toUpperCase() + side.slice(1)} margin`) .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 => { .addText(text => {
const input = text const input = text
.setPlaceholder(marginDefaults[side as keyof typeof marginDefaults]) .setPlaceholder(marginDefaults[side as keyof typeof marginDefaults])
@@ -108,10 +108,15 @@ export class PageSetupSection implements ModalSection {
this.marginInputs.forEach((input, side) => { this.marginInputs.forEach((input, side) => {
const value = input.value.trim(); const value = input.value.trim();
if (value) { if (value) {
// Check for valid margin format (number + unit) // Check for valid margin format (number only, centimeters assumed)
const marginRegex = /^[\d.]+\s*(in|cm|mm|pt)$/; const marginRegex = /^\d*\.?\d+$/;
if (!marginRegex.test(value)) { 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`);
}
} }
} }
}); });