Commit 0d29006f authored by hz's avatar hz Committed by 陈帅

Optimize and resolve theme color replacement issues (#4507)

* 动态主题文件名(解决缓存问题);简化客户端调用
parent b9b88595
...@@ -32,7 +32,7 @@ export default (config: any) => { ...@@ -32,7 +32,7 @@ export default (config: any) => {
) { ) {
config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [ config.plugin('webpack-theme-color-replacer').use(ThemeColorReplacer, [
{ {
fileName: 'css/theme-colors.css', fileName: 'css/theme-colors-[contenthash:8].css',
matchColors: getAntdSerials('#1890ff'), // 主色系列 matchColors: getAntdSerials('#1890ff'), // 主色系列
// 改变样式选择器,解决样式覆盖问题 // 改变样式选择器,解决样式覆盖问题
changeSelector(selector: string): string { changeSelector(selector: string): string {
...@@ -47,6 +47,7 @@ export default (config: any) => { ...@@ -47,6 +47,7 @@ export default (config: any) => {
return selector; return selector;
} }
}, },
// isJsUgly: true,
}, },
]); ]);
} }
......
...@@ -120,7 +120,7 @@ ...@@ -120,7 +120,7 @@
"umi-plugin-ga": "^1.1.3", "umi-plugin-ga": "^1.1.3",
"umi-plugin-pro": "^1.0.2", "umi-plugin-pro": "^1.0.2",
"umi-types": "^0.3.8", "umi-types": "^0.3.8",
"webpack-theme-color-replacer": "^1.1.5" "webpack-theme-color-replacer": "^1.2.15"
}, },
"optionalDependencies": { "optionalDependencies": {
"puppeteer": "^1.17.0" "puppeteer": "^1.17.0"
...@@ -163,4 +163,4 @@ ...@@ -163,4 +163,4 @@
"create-umi" "create-umi"
] ]
} }
} }
\ No newline at end of file
...@@ -4,26 +4,27 @@ import client from 'webpack-theme-color-replacer/client'; ...@@ -4,26 +4,27 @@ import client from 'webpack-theme-color-replacer/client';
import generate from '@ant-design/colors/lib/generate'; import generate from '@ant-design/colors/lib/generate';
export default { export default {
lastColor: '#1890ff', getAntdSerials(color: string): string[] {
primaryColor: '#1890ff', const lightCount = 9;
getAntdSerials(color: string) { const divide = 10;
// 淡化(即less的tint) // 淡化(即less的tint)
const lightens = new Array(9).fill(0).map((_, i) => client.varyColor.lighten(color, i / 10)); let lightens = new Array(lightCount).fill(0);
lightens = lightens.map((_, i) => client.varyColor.lighten(color, i / divide));
const colorPalettes = generate(color); const colorPalettes = generate(color);
return lightens.concat(colorPalettes); return lightens.concat(colorPalettes);
}, },
changeColor(newColor?: string) { changeColor(color?: string): Promise<void> {
const lastColor = this.lastColor || this.primaryColor; if (!color) {
return Promise.resolve();
}
const options = { const options = {
// hash模式下用相对路径 // new colors array, one-to-one corresponde with `matchColors`
cssUrl: '/css/theme-colors.css', newColors: this.getAntdSerials(color),
// current colors array. The same as `matchColors` changeUrl(cssUrl: string): string {
oldColors: this.getAntdSerials(lastColor), // while router is not `hash` mode, it needs absolute path
// new colors array, one-to-one corresponde with `oldColors` return `/${cssUrl}`;
newColors: this.getAntdSerials(newColor || this.primaryColor), },
}; };
const promise = client.changer.changeColor(options, Promise); return client.changer.changeColor(options, Promise);
this.lastColor = lastColor;
return promise;
}, },
}; };
...@@ -15,9 +15,11 @@ export interface SettingModelType { ...@@ -15,9 +15,11 @@ export interface SettingModelType {
} }
const updateTheme = (newPrimaryColor?: string) => { const updateTheme = (newPrimaryColor?: string) => {
const timeOut = 0; if (newPrimaryColor) {
const hideMessage = message.loading('正在切换主题!', timeOut); const timeOut = 0;
themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage()); const hideMessage = message.loading('正在切换主题!', timeOut);
themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
}
}; };
/* /*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment