Refactors export modal settings preparation
Introduces a helper class to encapsulate the logic for preparing the export configuration modal settings. This improves code readability and maintainability by extracting the complex settings preparation logic from the main plugin file. The helper handles both single and multi-file export scenarios, ensuring consistent settings across different export contexts.
This commit is contained in:
2 files changed
+126
-64
No files matched your search
@@ -22,6 +22,7 @@ import {
|
||||
import { obsidianTypstPDFExportSettings, DEFAULT_SETTINGS, ExportFormat } from './src/core/settings';
|
||||
import { FALLBACK_FONTS, PLUGIN_DIRS } from './src/core/constants';
|
||||
import { DependencyChecker } from './src/core/DependencyChecker';
|
||||
import { ModalSettingsHelper } from './src/core/ModalSettingsHelper';
|
||||
import { PandocTypstConverter } from './src/converters/PandocTypstConverter';
|
||||
import { ExportConfigModal } from './src/modal/ExportConfigModal';
|
||||
import { ExportConfig, ExportConfigModalSettings } from './src/modal/types';
|
||||
@@ -301,36 +302,12 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
// Get available templates first
|
||||
const availableTemplates = await this.templateManager.getAvailableTemplates();
|
||||
|
||||
// Prepare modal settings with hierarchy: plugin defaults as base
|
||||
const modalSettings: Partial<ExportConfigModalSettings> = {
|
||||
notePath: file.path,
|
||||
noteTitle: file.basename,
|
||||
// Plugin defaults from settings tab
|
||||
template: this.settings.exportDefaults.template,
|
||||
format: this.settings.exportDefaults.format,
|
||||
outputFolder: this.settings.outputFolder,
|
||||
openAfterExport: this.settings.behavior.openAfterExport,
|
||||
preserveFolderStructure: this.settings.behavior.preserveFolderStructure,
|
||||
availableTemplates: availableTemplates,
|
||||
// Template variables from settings tab values
|
||||
templateVariables: {
|
||||
pageSize: this.settings.pageSetup.size,
|
||||
orientation: this.settings.pageSetup.orientation,
|
||||
flipped: this.settings.pageSetup.orientation === 'landscape',
|
||||
marginTop: this.settings.pageSetup.margins.top.toString(),
|
||||
marginBottom: this.settings.pageSetup.margins.bottom.toString(),
|
||||
marginLeft: this.settings.pageSetup.margins.left.toString(),
|
||||
marginRight: this.settings.pageSetup.margins.right.toString(),
|
||||
bodyFont: this.settings.typography.fonts.body,
|
||||
headingFont: this.settings.typography.fonts.heading,
|
||||
monospaceFont: this.settings.typography.fonts.monospace,
|
||||
bodyFontSize: this.settings.typography.fontSizes.body,
|
||||
// Auto-adjust width for single-page landscape mode
|
||||
...(this.settings.pageSetup.orientation === 'landscape' && this.settings.exportDefaults.format === 'single-page'
|
||||
? { width: 'auto' }
|
||||
: {})
|
||||
}
|
||||
};
|
||||
// Prepare modal settings using helper
|
||||
const modalSettings = ModalSettingsHelper.prepareForSingleFile(
|
||||
file,
|
||||
availableTemplates,
|
||||
this.settings
|
||||
);
|
||||
|
||||
// Show modal - ModalState will handle localStorage hierarchy automatically
|
||||
const modal = new ExportConfigModal(
|
||||
@@ -360,40 +337,12 @@ export class obsidianTypstPDFExport extends Plugin {
|
||||
// Get available templates first
|
||||
const availableTemplates = await this.templateManager.getAvailableTemplates();
|
||||
|
||||
// Use the first file for modal settings but will export all files
|
||||
const firstFile = files[0];
|
||||
|
||||
// Prepare modal settings with hierarchy: plugin defaults as base
|
||||
const modalSettings: Partial<ExportConfigModalSettings> = {
|
||||
notePath: firstFile.path,
|
||||
noteTitle: files.length === 1 ? firstFile.basename : `${files.length} files`,
|
||||
files: files,
|
||||
// Plugin defaults from settings tab
|
||||
template: this.settings.exportDefaults.template,
|
||||
format: this.settings.exportDefaults.format,
|
||||
outputFolder: this.settings.outputFolder,
|
||||
openAfterExport: this.settings.behavior.openAfterExport,
|
||||
preserveFolderStructure: this.settings.behavior.preserveFolderStructure,
|
||||
availableTemplates: availableTemplates,
|
||||
// Template variables from settings tab values
|
||||
templateVariables: {
|
||||
pageSize: this.settings.pageSetup.size,
|
||||
orientation: this.settings.pageSetup.orientation,
|
||||
flipped: this.settings.pageSetup.orientation === 'landscape',
|
||||
marginTop: this.settings.pageSetup.margins.top.toString(),
|
||||
marginBottom: this.settings.pageSetup.margins.bottom.toString(),
|
||||
marginLeft: this.settings.pageSetup.margins.left.toString(),
|
||||
marginRight: this.settings.pageSetup.margins.right.toString(),
|
||||
bodyFont: this.settings.typography.fonts.body,
|
||||
headingFont: this.settings.typography.fonts.heading,
|
||||
monospaceFont: this.settings.typography.fonts.monospace,
|
||||
bodyFontSize: this.settings.typography.fontSizes.body,
|
||||
// Auto-adjust width for single-page landscape mode
|
||||
...(this.settings.pageSetup.orientation === 'landscape' && this.settings.exportDefaults.format === 'single-page'
|
||||
? { width: 'auto' }
|
||||
: {})
|
||||
}
|
||||
};
|
||||
// Prepare modal settings using helper
|
||||
const modalSettings = ModalSettingsHelper.prepareForMultiFile(
|
||||
files,
|
||||
availableTemplates,
|
||||
this.settings
|
||||
);
|
||||
|
||||
// Show modal - ModalState will handle localStorage hierarchy automatically
|
||||
const modal = new ExportConfigModal(
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Helper utilities for preparing export modal settings
|
||||
*/
|
||||
|
||||
import { TFile } from 'obsidian';
|
||||
import { obsidianTypstPDFExportSettings } from './settings';
|
||||
import { ExportConfigModalSettings } from '../modal/types';
|
||||
|
||||
export interface ModalSettingsInput {
|
||||
/** Primary file for the export */
|
||||
file: TFile;
|
||||
/** Additional files for multi-file export (optional) */
|
||||
additionalFiles?: TFile[];
|
||||
/** Available templates list */
|
||||
availableTemplates: string[];
|
||||
/** Plugin settings to extract defaults from */
|
||||
settings: obsidianTypstPDFExportSettings;
|
||||
}
|
||||
|
||||
export class ModalSettingsHelper {
|
||||
/**
|
||||
* Prepare modal settings from plugin defaults and file context
|
||||
*/
|
||||
public static prepareModalSettings(input: ModalSettingsInput): Partial<ExportConfigModalSettings> {
|
||||
const { file, additionalFiles, availableTemplates, settings } = input;
|
||||
|
||||
// Determine if this is a multi-file export
|
||||
const allFiles = additionalFiles ? [file, ...additionalFiles] : [file];
|
||||
const isMultiFile = allFiles.length > 1;
|
||||
|
||||
// Prepare template variables from plugin settings
|
||||
const templateVariables = this.buildTemplateVariables(settings);
|
||||
|
||||
return {
|
||||
notePath: file.path,
|
||||
noteTitle: isMultiFile ? `${allFiles.length} files` : file.basename,
|
||||
files: isMultiFile ? allFiles : undefined,
|
||||
|
||||
// Plugin defaults from settings tab
|
||||
template: settings.exportDefaults.template,
|
||||
format: settings.exportDefaults.format,
|
||||
outputFolder: settings.outputFolder,
|
||||
openAfterExport: settings.behavior.openAfterExport,
|
||||
preserveFolderStructure: settings.behavior.preserveFolderStructure,
|
||||
availableTemplates,
|
||||
|
||||
// Template variables from settings
|
||||
templateVariables
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Build template variables object from plugin settings
|
||||
*/
|
||||
private static buildTemplateVariables(settings: obsidianTypstPDFExportSettings): Record<string, any> {
|
||||
const baseVariables = {
|
||||
pageSize: settings.pageSetup.size,
|
||||
orientation: settings.pageSetup.orientation,
|
||||
flipped: settings.pageSetup.orientation === 'landscape',
|
||||
marginTop: settings.pageSetup.margins.top.toString(),
|
||||
marginBottom: settings.pageSetup.margins.bottom.toString(),
|
||||
marginLeft: settings.pageSetup.margins.left.toString(),
|
||||
marginRight: settings.pageSetup.margins.right.toString(),
|
||||
bodyFont: settings.typography.fonts.body,
|
||||
headingFont: settings.typography.fonts.heading,
|
||||
monospaceFont: settings.typography.fonts.monospace,
|
||||
bodyFontSize: settings.typography.fontSizes.body
|
||||
};
|
||||
|
||||
// Auto-adjust width for single-page landscape mode
|
||||
const landscapeAdjustment = settings.pageSetup.orientation === 'landscape'
|
||||
&& settings.exportDefaults.format === 'single-page'
|
||||
? { width: 'auto' }
|
||||
: {};
|
||||
|
||||
return {
|
||||
...baseVariables,
|
||||
...landscapeAdjustment
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for single file export modal settings
|
||||
*/
|
||||
public static prepareForSingleFile(
|
||||
file: TFile,
|
||||
availableTemplates: string[],
|
||||
settings: obsidianTypstPDFExportSettings
|
||||
): Partial<ExportConfigModalSettings> {
|
||||
return this.prepareModalSettings({
|
||||
file,
|
||||
availableTemplates,
|
||||
settings
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for multi-file export modal settings
|
||||
*/
|
||||
public static prepareForMultiFile(
|
||||
files: TFile[],
|
||||
availableTemplates: string[],
|
||||
settings: obsidianTypstPDFExportSettings
|
||||
): Partial<ExportConfigModalSettings> {
|
||||
const [primaryFile, ...additionalFiles] = files;
|
||||
return this.prepareModalSettings({
|
||||
file: primaryFile,
|
||||
additionalFiles,
|
||||
availableTemplates,
|
||||
settings
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user