|
| 1 | +import { ThemeDetail, ThemeType } from "api/commonSettingApi"; |
| 2 | +import { genQueryId } from "comps/utils/idGenerator"; |
| 3 | +import { trans } from "i18n"; |
| 4 | +import { saveDataAsFile } from "util/fileUtils"; |
| 5 | + |
| 6 | +export const THEME_EXPORT_VERSION = 1; |
| 7 | +export const THEME_EXPORT_TYPE = "lowcoder-theme"; |
| 8 | + |
| 9 | +export interface ThemeExportPayload { |
| 10 | + version: number; |
| 11 | + type: typeof THEME_EXPORT_TYPE; |
| 12 | + theme: ThemeType; |
| 13 | +} |
| 14 | + |
| 15 | +function isThemeDetail(value: unknown): value is ThemeDetail { |
| 16 | + return typeof value === "object" && value !== null; |
| 17 | +} |
| 18 | + |
| 19 | +function isThemeType(value: unknown): value is ThemeType { |
| 20 | + if (typeof value !== "object" || value === null) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + const theme = value as ThemeType; |
| 24 | + return typeof theme.name === "string" && isThemeDetail(theme.theme); |
| 25 | +} |
| 26 | + |
| 27 | +export function parseThemeImport(content: string): ThemeType { |
| 28 | + let data: unknown; |
| 29 | + try { |
| 30 | + data = JSON.parse(content); |
| 31 | + } catch { |
| 32 | + throw new Error(trans("theme.importParseError")); |
| 33 | + } |
| 34 | + |
| 35 | + if (typeof data !== "object" || data === null) { |
| 36 | + throw new Error(trans("theme.importFormatError")); |
| 37 | + } |
| 38 | + |
| 39 | + const payload = data as Record<string, unknown>; |
| 40 | + |
| 41 | + if (payload.type === THEME_EXPORT_TYPE && isThemeType(payload.theme)) { |
| 42 | + return payload.theme; |
| 43 | + } |
| 44 | + |
| 45 | + if (isThemeType(data)) { |
| 46 | + return data; |
| 47 | + } |
| 48 | + |
| 49 | + throw new Error(trans("theme.importFormatError")); |
| 50 | +} |
| 51 | + |
| 52 | +export function resolveUniqueThemeName(baseName: string, existingNames: Set<string>): string { |
| 53 | + const trimmed = baseName.trim(); |
| 54 | + if (!trimmed) { |
| 55 | + throw new Error(trans("theme.importNameRequired")); |
| 56 | + } |
| 57 | + if (!existingNames.has(trimmed)) { |
| 58 | + return trimmed; |
| 59 | + } |
| 60 | + |
| 61 | + const importSuffix = trans("theme.importSuffix"); |
| 62 | + let candidate = trimmed + importSuffix; |
| 63 | + if (!existingNames.has(candidate)) { |
| 64 | + return candidate; |
| 65 | + } |
| 66 | + |
| 67 | + let index = 1; |
| 68 | + while (existingNames.has(candidate + index)) { |
| 69 | + index += 1; |
| 70 | + } |
| 71 | + return candidate + index; |
| 72 | +} |
| 73 | + |
| 74 | +export function prepareImportedTheme( |
| 75 | + source: ThemeType, |
| 76 | + existingThemes: ThemeType[], |
| 77 | + fallbackName?: string |
| 78 | +): ThemeType { |
| 79 | + const existingNames = new Set(existingThemes.map((theme) => theme.name)); |
| 80 | + const name = resolveUniqueThemeName( |
| 81 | + source.name || fallbackName || trans("theme.importDefaultName"), |
| 82 | + existingNames |
| 83 | + ); |
| 84 | + |
| 85 | + return { |
| 86 | + name, |
| 87 | + id: genQueryId(), |
| 88 | + updateTime: Date.now(), |
| 89 | + theme: source.theme, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +export function exportThemeAsJSONFile(theme: ThemeType) { |
| 94 | + const exportObj: ThemeExportPayload = { |
| 95 | + version: THEME_EXPORT_VERSION, |
| 96 | + type: THEME_EXPORT_TYPE, |
| 97 | + theme, |
| 98 | + }; |
| 99 | + void saveDataAsFile({ |
| 100 | + data: exportObj, |
| 101 | + filename: `${theme.name}.json`, |
| 102 | + fileType: "json", |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +export function readThemeFile(file: File): Promise<ThemeType> { |
| 107 | + return new Promise((resolve, reject) => { |
| 108 | + const reader = new FileReader(); |
| 109 | + reader.readAsText(file, "UTF-8"); |
| 110 | + reader.onload = (event) => { |
| 111 | + try { |
| 112 | + if (!event.target?.result) { |
| 113 | + throw new Error(trans("theme.importFileError")); |
| 114 | + } |
| 115 | + resolve(parseThemeImport(event.target.result.toString())); |
| 116 | + } catch (error) { |
| 117 | + reject(error); |
| 118 | + } |
| 119 | + }; |
| 120 | + reader.onerror = () => reject(new Error(trans("theme.importFileError"))); |
| 121 | + }); |
| 122 | +} |
0 commit comments