feat: 完善导出设置与打印预览
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import {
|
||||
defaultExportConfig,
|
||||
exportConfigSchema,
|
||||
type ExportConfig
|
||||
} from "@md-to-pdf/core";
|
||||
|
||||
export const EXPORT_CONFIG_STORAGE_KEY = "md-to-pdf.export-config.v2";
|
||||
|
||||
type SettingsStorage = Pick<Storage, "getItem" | "setItem">;
|
||||
|
||||
function cloneDefaultExportConfig() {
|
||||
return structuredClone(defaultExportConfig);
|
||||
}
|
||||
|
||||
export function parseStoredExportConfig(rawValue: string | null): ExportConfig {
|
||||
if (!rawValue) {
|
||||
return cloneDefaultExportConfig();
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = exportConfigSchema.safeParse(JSON.parse(rawValue));
|
||||
return parsed.success ? parsed.data : cloneDefaultExportConfig();
|
||||
} catch {
|
||||
return cloneDefaultExportConfig();
|
||||
}
|
||||
}
|
||||
|
||||
export function loadExportConfig(
|
||||
storage: SettingsStorage = window.localStorage
|
||||
) {
|
||||
return parseStoredExportConfig(storage.getItem(EXPORT_CONFIG_STORAGE_KEY));
|
||||
}
|
||||
|
||||
export function saveExportConfig(
|
||||
config: ExportConfig,
|
||||
storage: SettingsStorage = window.localStorage
|
||||
) {
|
||||
const parsed = exportConfigSchema.parse(config);
|
||||
storage.setItem(EXPORT_CONFIG_STORAGE_KEY, JSON.stringify(parsed));
|
||||
}
|
||||
|
||||
export function resetExportConfig() {
|
||||
return cloneDefaultExportConfig();
|
||||
}
|
||||
Reference in New Issue
Block a user