|
|
@@ -2,9 +2,11 @@
|
|
|
* @Author: wjc
|
|
|
* @Date: 2026-01-19 16:52:19
|
|
|
* @LastEditors: wjc
|
|
|
- * @LastEditTime: 2026-01-22 09:43:34
|
|
|
+ * @LastEditTime: 2026-01-22 16:18:24
|
|
|
* @Description:
|
|
|
*/
|
|
|
+import { store } from '@/stores'
|
|
|
+import { useUserStore } from '@/stores/modules/userStore'
|
|
|
import { TinyColor } from '@ctrl/tinycolor'
|
|
|
import { getColors } from 'theme-colors'
|
|
|
|
|
|
@@ -67,68 +69,54 @@ function setCSSVariables(
|
|
|
variables: { [key: string]: string },
|
|
|
id = '__uni-starter-styles__'
|
|
|
): void {
|
|
|
- // 获取或创建内联样式表元素
|
|
|
- const styleElement = document.querySelector(`#${id}`) || document.createElement('style')
|
|
|
-
|
|
|
- styleElement.id = id
|
|
|
-
|
|
|
- // 构建要更新的 CSS 变量的样式文本
|
|
|
- let cssText = ':root, page {'
|
|
|
- for (const key in variables) {
|
|
|
- if (Object.prototype.hasOwnProperty.call(variables, key)) {
|
|
|
- cssText += `${key}: ${variables[key]};`
|
|
|
+ if (typeof document === 'undefined') {
|
|
|
+ const userStore = useUserStore(store)
|
|
|
+ userStore.setThemeVarsAction(variables)
|
|
|
+ } else {
|
|
|
+ // 获取或创建内联样式表元素
|
|
|
+ const styleElement = document.querySelector(`#${id}`) || document.createElement('style')
|
|
|
+
|
|
|
+ styleElement.id = id
|
|
|
+
|
|
|
+ // 构建要更新的 CSS 变量的样式文本
|
|
|
+ let cssText = ':root, page {'
|
|
|
+ for (const key in variables) {
|
|
|
+ if (Object.prototype.hasOwnProperty.call(variables, key)) {
|
|
|
+ cssText += `${key}: ${variables[key]};`
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- cssText += '}'
|
|
|
+ cssText += '}'
|
|
|
|
|
|
- // 将样式文本赋值给内联样式表
|
|
|
- styleElement.textContent = cssText
|
|
|
+ // 将样式文本赋值给内联样式表
|
|
|
+ styleElement.textContent = cssText
|
|
|
|
|
|
- // 将内联样式表添加到文档头部
|
|
|
- if (!document.querySelector(`#${id}`)) {
|
|
|
- setTimeout(() => {
|
|
|
- document.head.append(styleElement)
|
|
|
- })
|
|
|
+ // 将内联样式表添加到文档头部
|
|
|
+ if (!document.querySelector(`#${id}`)) {
|
|
|
+ setTimeout(() => {
|
|
|
+ document.head.append(styleElement)
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新主要的 CSS 变量
|
|
|
- * @param preference - 当前偏好设置对象,它的颜色值将被转换成 HSL 格式并设置为 CSS 变量。
|
|
|
+ * @param theme - 当前偏好设置对象,它的颜色值将被转换成为 CSS 变量。
|
|
|
*/
|
|
|
export function updateMainColorVariables(theme: any) {
|
|
|
// 当修改到颜色变量时,更新 css 变量
|
|
|
- const root = document.documentElement
|
|
|
- if (!root) {
|
|
|
- return
|
|
|
- }
|
|
|
if (!theme) {
|
|
|
return
|
|
|
}
|
|
|
- const { colorDestructive, colorPrimary, colorSuccess, colorWarning } = theme
|
|
|
+ const { colorDanger, colorPrimary, colorSuccess, colorWarning } = theme
|
|
|
|
|
|
const colorVariables = generatorColorVariables([
|
|
|
{ color: colorPrimary, name: 'primary' },
|
|
|
- { alias: 'warning', color: colorWarning, name: 'yellow' },
|
|
|
- { alias: 'success', color: colorSuccess, name: 'green' },
|
|
|
- { alias: 'destructive', color: colorDestructive, name: 'red' },
|
|
|
+ { color: colorWarning, name: 'warning' },
|
|
|
+ { color: colorSuccess, name: 'success' },
|
|
|
+ { color: colorDanger, name: 'danger' },
|
|
|
])
|
|
|
|
|
|
- // 要设置的 CSS 变量映射
|
|
|
- const colorMappings = {
|
|
|
- '--green-500': '--success',
|
|
|
- '--primary-500': '--primary',
|
|
|
- '--red-500': '--destructive',
|
|
|
- '--yellow-500': '--warning',
|
|
|
- }
|
|
|
-
|
|
|
- // 统一处理颜色变量的更新
|
|
|
- Object.entries(colorMappings).forEach(([sourceVar, targetVar]) => {
|
|
|
- const colorValue = colorVariables[sourceVar]
|
|
|
- if (colorValue) {
|
|
|
- document.documentElement.style.setProperty(targetVar, colorValue)
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
+ console.log('colorVariables----', colorVariables)
|
|
|
setCSSVariables(colorVariables)
|
|
|
}
|