forked from OtterMind/Chat2DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
55 lines (47 loc) · 1.83 KB
/
Copy pathindex.ts
File metadata and controls
55 lines (47 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import antdDarkTheme from './background/dark';
import antdDarkDimmedTheme from './background/darkDimmed';
import antdLightTheme from './background/light';
import { ThemeType, PrimaryColorType } from '@/constants';
import { ITheme } from '@/typings/theme';
import lodash from 'lodash';
import { theme } from 'antd';
const antdThemeConfigs = {
[ThemeType.Dark]: antdDarkTheme,
[ThemeType.Light]: antdLightTheme,
[ThemeType.DarkDimmed]: antdDarkDimmedTheme,
};
export function getAntdThemeConfig(_theme: ITheme) {
const antdThemeConfig = lodash.cloneDeep(antdThemeConfigs[_theme.backgroundColor]);
antdThemeConfig.token = {
...antdThemeConfig.token,
...(antdThemeConfig.antdPrimaryColor[_theme.primaryColor as PrimaryColorType] || {}),
};
const token = theme.getDesignToken(antdThemeConfig);
injectThemeVar(token as any, _theme.backgroundColor, _theme.primaryColor);
return antdThemeConfig;
}
// TODO: 只插入一次
export function injectThemeVar(token: { [key in string]: string }, _theme: ThemeType, primaryColor: PrimaryColorType) {
let css = '';
Object.keys(token).map((t) => {
const attributeName = camelToDash(t);
let value = token[t];
// 将需要px的数字带上px
const joinPxArr = ['fontSize', 'borderRadius', 'borderRadiusLG'];
if (joinPxArr.includes(t)) {
value = value + 'px';
}
css = css + `--${attributeName}: ${value};\n`;
});
const container = `html[theme='${_theme}'],html[primary-color='${primaryColor}']{
${css}
}`;
const style = document.createElement('style'); // 创建style标签
style.type = 'text/css';
style.appendChild(document.createTextNode(container));
document.head.appendChild(style); // 将style标签插入到head标签中
window._AppThemePack = token;
}
function camelToDash(str: string) {
return str.replace(/([A-Z])/g, '-$1').toLowerCase();
}