import {getComponentCls, globalComputed, reactive} from "@peryl/react-compose"; import {createCache} from "../../utils/createCache"; import {InputMode, ThemeMode, ThemeShape, ThemeSize} from "../../uses/useStyle"; import {DeepPartial} from "../../utils/type"; import {iApplicationConfiguration} from "../Application/utils/application.utils"; import {BaseColors, iThemeConfiguration} from "../Application/theme/theme"; import {deepmerge} from "./deepmerge"; import {PlainObject} from "@peryl/utils/event"; export const PageThemeUtils = (() => { const darkCache = createCache(getComponentCls('@@PAGE_THEME_IS_DARK')); const primaryCache = createCache(getComponentCls('@@PAGE_THEME_PRIMARY')); const sizeCache = createCache(getComponentCls('@@PAGE_THEME_SIZE')); const shapeCache = createCache(getComponentCls('@@PAGE_THEME_SHAPE')); const inputModeCache = createCache(getComponentCls('@@PAGE_THEME_INPUT_MODE')); const buttonModeCache = createCache(getComponentCls('@@PAGE_THEME_BUTTON_MODE')); const zoomCache = createCache(getComponentCls('@@PAGE_THEME_ZOOM')); const state = reactive({ dark: darkCache.get(), primaryKey: primaryCache.get(), size: sizeCache.get(), shape: shapeCache.get(), inputMode: inputModeCache.get(), buttonMode: buttonModeCache.get(), zoom: zoomCache.get(), }); const configuration = globalComputed((): DeepPartial => { const config: DeepPartial = {}; if (!config.theme) {config.theme = {};} if (!config.theme.base) {config.theme.base = {};} if (!config.theme.vars) {config.theme.vars = {};} let { dark, primaryKey, size, shape, inputMode, buttonMode, zoom } = state; config.theme.dark = !!dark; const themeConfig = !primaryKey ? undefined : ((): DeepPartial> | undefined => { let themeConfig = ThemePrimaryColors[primaryKey]?.themeConfig; if (themeConfig == undefined) {return undefined;} if (typeof themeConfig === "string") { return { base: { primary: themeConfig } }; } else { return themeConfig[dark ? 'dark' : 'light']; } })(); config.theme = deepmerge(config.theme, themeConfig) as PlainObject; if (!config.default) { config.default = {}; } if (!!size) { config.default.size = size; } if (!!shape) { config.default.shape = shape; if (!config.theme!.vars) { config.theme!.vars = {}; } if (shape === 'square') { Object.assign(config.theme!.vars, { 'box-size-mini-border-radius': '2px', 'box-size-small-border-radius': '2px', 'box-size-normal-border-radius': '4px', 'box-size-large-border-radius': '4px', }); } if (shape === 'none') { Object.assign(config.theme!.vars, { 'box-size-mini-border-radius': '0', 'box-size-small-border-radius': '0', 'box-size-normal-border-radius': '0', 'box-size-large-border-radius': '0', }); } } if (!!inputMode) { config.default.inputMode = inputMode; } if (!!buttonMode) { config.default.buttonMode = buttonMode; } return config; }); const toggle = (isDark?: boolean, onlyCache?: boolean) => { const dark: boolean = isDark == null ? !isDark : isDark; !onlyCache && (state.dark = dark); darkCache.set(dark); }; const primary = (val: string | null, onlyCache?: boolean) => { !onlyCache && (state.primaryKey = val); primaryCache.set(val); }; const size = (val: typeof ThemeSize.TYPE | null, onlyCache?: boolean) => { !onlyCache && (state.size = val); sizeCache.set(val); }; const shape = (val: typeof ThemeShape.TYPE | null, onlyCache?: boolean) => { !onlyCache && (state.shape = val); shapeCache.set(val); }; const inputMode = (val: typeof InputMode.TYPE | null, onlyCache?: boolean) => { !onlyCache && (state.inputMode = val); inputModeCache.set(val); }; const buttonMode = (val: typeof ThemeMode.TYPE | null, onlyCache?: boolean) => { !onlyCache && (state.buttonMode = val); buttonModeCache.set(val); }; const zoom = (val: number | null, onlyCache?: boolean) => { !onlyCache && (state.zoom = val); zoomCache.set(val); }; const clear = () => { window.localStorage.clear(); window.location.reload(); }; const variable = (varName: string) => `var(--${getComponentCls(varName)})`; return { configuration, toggle, state, primary, size, shape, inputMode, buttonMode, zoom, clear, variable, }; })(); export const ThemePrimaryColors: Record>, light: DeepPartial> } | undefined | string } | undefined> = { default: { label: '默认主题', themeConfig: undefined, }, dustRed: { label: '薄暮', themeConfig: '#f5222d' }, volcano: { label: '火山', themeConfig: '#fa541c' }, subset: { label: '日暮', themeConfig: '#fa8c16' }, gold: { label: '金盏花', themeConfig: '#faad14' }, sunrise: { label: '日出', themeConfig: '#fadb14' }, lime: { label: '青柠', themeConfig: '#a0d911' }, polar: { label: '极光绿', themeConfig: '#42b883' }, cyan: { label: '明青', themeConfig: '#13c2c2' }, daybreak: { label: '拂晓蓝', themeConfig: '#1677ff' }, geek: { label: '极客蓝', themeConfig: '#2f54eb' }, purple: { label: '酱紫', themeConfig: { dark: { base: { primary: '#7643ff', } }, light: { base: { primary: '#6a00ff', } }, } }, magenta: { label: '洋红', themeConfig: { dark: { base: { primary: '#eb2f96', } }, light: { base: { primary: '#d90178', } }, } }, dark: { label: '极夜', themeConfig: { dark: { base: { primary: '#FFFFFF', }, }, light: { base: { primary: '#000000', }, vars: { 'primary-light-1': '#f2f3f5', 'primary-1': '#f2f3f5', 'primary-light-2': '#e1e2e7', 'primary-2': '#e1e2e7', 'primary-light-3': '#c9cdd4', 'primary-3': '#c9cdd4', 'primary-light-4': '#a9aeb8', 'primary-4': '#a9aeb8', 'primary-light-5': '#86909c', 'primary-5': '#86909c', 'primary-light-6': '#0c0e10', 'primary-6': '#0b0b0b', 'primary-light-7': '#464f5e', 'primary-7': '#464f5e', 'primary-light-8': '#272e3b', 'primary-8': '#272e3b', 'primary-light-9': '#1d2129', 'primary-9': '#111318', 'primary-light-10': '#0c0e10', 'primary-10': '#0b0b0b', } }, } }, }; export default PageThemeUtils; export function getPrimaryColor(primaryKey?: string | undefined): { light: string, dark: string } { const baseColor = { light: BaseColors.light.primary, dark: BaseColors.dark.primary, }; if (!primaryKey) { return baseColor; } const themeConfig = ThemePrimaryColors[primaryKey]?.themeConfig; if (!themeConfig) {return baseColor;} return typeof themeConfig === "string" ? { dark: themeConfig, light: themeConfig, } : { dark: themeConfig.dark.base!.primary!, light: themeConfig.light.base!.primary! }; }