92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import type { Form } from 'lib/type/TableData'
|
|
|
|
export function formatConfig(config: Array<any>) {
|
|
if (typeof config === 'object') {
|
|
config = JSON.parse(JSON.stringify(config))
|
|
}
|
|
config?.forEach((item: any) => {
|
|
let defaultValue: any = ''
|
|
if (item.type === 'text') {
|
|
item.type = 'input'
|
|
item.rows = 5
|
|
defaultValue = item.value || ''
|
|
} else if (item.type === 'radio') {
|
|
item.type = 'select'
|
|
item.items = item.option.map((item: any) => {
|
|
return {
|
|
key: item.value,
|
|
name: item.name
|
|
}
|
|
})
|
|
delete item.option
|
|
defaultValue = item.value || ''
|
|
} else if (item.type === 'checkbox') {
|
|
item.multiple = true
|
|
item.type = 'select'
|
|
item.items = item.option.map((item: any) => {
|
|
return {
|
|
key: item.value,
|
|
name: item.name
|
|
}
|
|
})
|
|
delete item.option
|
|
defaultValue = item.value.split(',')
|
|
if (defaultValue.length === 1 && defaultValue[0] === '') {
|
|
defaultValue = []
|
|
}
|
|
} else if (item.type === 'switch') {
|
|
item.openValue = true
|
|
item.closeValue = false
|
|
defaultValue = !!item.value
|
|
} else if (item.type === 'json') {
|
|
item.type = 'jsonInput'
|
|
defaultValue = item.value || ''
|
|
} else {
|
|
defaultValue = item.value || ''
|
|
}
|
|
item.getValue = () => defaultValue
|
|
delete item.value
|
|
})
|
|
return config
|
|
}
|
|
|
|
export function updateFormData(form: Form, data: Array<any>) {
|
|
while (form.data.length > 0) {
|
|
form.data.pop()
|
|
}
|
|
for (const d of data) {
|
|
form.data.push(d)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export async function translateRichText(
|
|
text: string,
|
|
translateApi: (texts: string[]) => Promise<string[]>
|
|
): Promise<string> {
|
|
if (!text) return text
|
|
|
|
const placeholderPrefix = `__RICH_TRANSLATE_`
|
|
const textFragments: string[] = []
|
|
|
|
const placeholderHtml = text.replace(
|
|
/>([^<]*[\u4e00-\u9fff][^<]*)</g,
|
|
(match, captured: string) => {
|
|
const index = textFragments.length
|
|
textFragments.push(captured)
|
|
return `>${placeholderPrefix}${index}__<`
|
|
}
|
|
)
|
|
|
|
if (textFragments.length === 0) return text
|
|
|
|
const translatedFragments = await translateApi(textFragments)
|
|
|
|
let result = placeholderHtml
|
|
for (let i = 0; i < textFragments.length; i++) {
|
|
result = result.replace(`${placeholderPrefix}${i}__`, translatedFragments[i])
|
|
}
|
|
|
|
return result
|
|
} |