feat: 将配置导出功能从JSON改为TOML格式并添加保存对话框
This commit is contained in:
@@ -452,16 +452,57 @@ const resetParams = () => {
|
||||
error.value = null
|
||||
}
|
||||
|
||||
// 将参数转换为 TOML 格式
|
||||
const tomlStringify = (obj: any, indent: string = ''): string => {
|
||||
let result = ''
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (value === null || value === undefined) continue
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
result += `${indent}${key} = [${value.join(', ')}]\n`
|
||||
} else if (typeof value === 'object') {
|
||||
result += `\n${indent}[${key}]\n`
|
||||
result += tomlStringify(value, indent)
|
||||
} else if (typeof value === 'string') {
|
||||
result += `${indent}${key} = "${value}"\n`
|
||||
} else if (typeof value === 'boolean') {
|
||||
result += `${indent}${key} = ${value}\n`
|
||||
} else {
|
||||
result += `${indent}${key} = ${value}\n`
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 导出配置
|
||||
const exportConfig = () => {
|
||||
const config = JSON.stringify(params, null, 2)
|
||||
const blob = new Blob([config], { type: 'application/json' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = 'egm_config.json'
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
const exportConfig = async () => {
|
||||
try {
|
||||
if (window.pywebview) {
|
||||
const response = await window.pywebview.api.export_config(params)
|
||||
if (response.success) {
|
||||
logRef.value?.addLog('info', response.message)
|
||||
} else {
|
||||
logRef.value?.addLog('warning', response.message)
|
||||
}
|
||||
} else {
|
||||
// 开发模式下的模拟
|
||||
logRef.value?.addLog('info', '导出配置(开发模式,直接下载)')
|
||||
const config = tomlStringify(params)
|
||||
const blob = new Blob([config], { type: 'text/plain' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
const timestamp = new Date().toISOString().slice(0, 19).replace(/[:-]/g, '')
|
||||
a.download = `egm_config_${timestamp}.toml`
|
||||
a.click()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '导出失败'
|
||||
logRef.value?.addLog('error', e.message || '导出失败')
|
||||
}
|
||||
}
|
||||
|
||||
// 声明 pywebview API 类型
|
||||
@@ -470,6 +511,7 @@ declare global {
|
||||
pywebview?: {
|
||||
api: {
|
||||
calculate: (params: AllParameters) => Promise<any>
|
||||
export_config: (params: AllParameters) => Promise<any>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user