1 Star 0 Fork 33

潘志群/码多多 全能知识库(Python 版)

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
theme.ts 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
zero 提交于 2025-05-26 15:35 +08:00 . init
import colors from 'css-color-function'
const lightConfig: any = {
'dark-2': 'shade(20%)',
'light-3': 'tint(30%)',
'light-5': 'tint(50%)',
'light-7': 'tint(70%)',
'light-8': 'tint(80%)',
'light-9': 'tint(90%)'
}
const darkConfig: any = {
'light-3': 'shade(20%)',
'light-5': 'shade(30%)',
'light-7': 'shade(50%)',
'light-8': 'shade(60%)',
'light-9': 'shade(70%)',
'dark-2': 'tint(20%)'
}
const themeId: string = 'theme-vars'
const themeUtil: any = {
/**
* 生成变量
* 可选值: [primary, success, warning, danger, error, info]
*
* @param {string} color 十六进制颜色
* @param {string} type 颜色类型
* @param {boolean} isDark 暗黑模式
* @returns objects
*/
generateVars(color: string, type: string = 'primary', isDark: boolean = false) {
const colors: any = {
[`--el-color-${type}`]: color
}
const config: Record<string, string> = isDark ? darkConfig : lightConfig
for (const key in config) {
colors[`--el-color-${type}-${key}`] = `color(${color} ${config[key]})`
}
return colors
},
/**
* 生成主题
*
* @param {Record<string, string>} options
* @param {boolean} isDark
* @returns {string}
*/
generateTheme(options: Record<string, string>, isDark: boolean = false): string {
const varsMap: Record<string, string> = Object.keys(options).reduce((prev: any, key: string) => {
return Object.assign(prev, this.generateVars(options[key], key, isDark))
}, {})
return Object.keys(varsMap).reduce((prev: string, key: string): string => {
const color = colors.convert(varsMap[key])
return `${prev}${key}:${color};`
}, '')
},
/**
* 设置主题
*
* @param {Record<string, string>} options
* @param {isDark} isDark
* @returns {void}
*/
setTheme(options: Record<string, string>, isDark: boolean = false): void {
const theme: string = this.generateTheme(options, isDark)
const themeElem: string = `:root{${theme}}`
let style: HTMLElement | null = document.getElementById(themeId)
if (style) {
style.innerHTML = themeElem
return
}
style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.setAttribute('id', themeId)
style.innerHTML = themeElem
document.head.append(style)
}
}
export default themeUtil
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pan_codee/ai-python.git
git@gitee.com:pan_codee/ai-python.git
pan_codee
ai-python
码多多 全能知识库(Python 版)
master

搜索帮助