Skip to content

Commit 7cf47a9

Browse files
authored
Merge pull request #261 from hzsrc/master
高性能方案实现ant-design-vue在运行时动态改变主题色(利用webpack-theme-color-replacer)
2 parents d407264 + 4582b94 commit 7cf47a9

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"opencollective": "^1.0.3",
5656
"opencollective-postinstall": "^2.0.2",
5757
"vue-svg-icon-loader": "^2.1.1",
58-
"vue-template-compiler": "^2.5.22"
58+
"vue-template-compiler": "^2.5.22",
59+
"webpack-theme-color-replacer": "^1.1.4"
5960
},
6061
"eslintConfig": {
6162
"root": true,

src/components/SettingDrawer/settingConfig.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { message } from 'ant-design-vue/es'
22
// import defaultSettings from '../defaultSettings';
3+
import themeColor from './themeColor.js'
34

4-
let lessNodesAppended
5+
// let lessNodesAppended
56

67
const colorList = [
78
{
@@ -30,11 +31,19 @@ const colorList = [
3031
}
3132
]
3233

34+
const updateTheme = newPrimaryColor => {
35+
const hideMessage = message.loading('正在切换主题!', 0)
36+
themeColor.changeColor(newPrimaryColor).finally(t => {
37+
hideMessage()
38+
})
39+
}
40+
41+
/*
3342
const updateTheme = primaryColor => {
3443
// Don't compile less in production!
3544
/* if (process.env.NODE_ENV === 'production') {
3645
return;
37-
} */
46+
} * /
3847
// Determine if the component is remounted
3948
if (!primaryColor) {
4049
return
@@ -86,6 +95,7 @@ const updateTheme = primaryColor => {
8695
buildIt()
8796
}
8897
}
98+
*/
8999

90100
const updateColorWeak = colorWeak => {
91101
// document.body.className = colorWeak ? 'colorWeak' : '';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const client = require('webpack-theme-color-replacer/client')
2+
3+
export default {
4+
primaryColor: '#1890ff',
5+
getAntdSerials (color) {
6+
// 淡化(即less的tint)
7+
var lightens = new Array(9).fill().map((t, i) => {
8+
return client.varyColor.lighten(color, i / 10)
9+
})
10+
// 此处为了简化,采用了darken。实际按color.less需求可以引入tinycolor, colorPalette变换得到颜色值
11+
var darkens = new Array(6).fill().map((t, i) => {
12+
return client.varyColor.darken(color, i / 10)
13+
})
14+
return lightens.concat(darkens)
15+
},
16+
changeColor (newColor) {
17+
var lastColor = this.lastColor || this.primaryColor
18+
var options = {
19+
cssUrl: '/css/theme-colors.css',
20+
oldColors: this.getAntdSerials(lastColor), // current colors array. The same as `matchColors`
21+
newColors: this.getAntdSerials(newColor) // new colors array, one-to-one corresponde with `oldColors`
22+
}
23+
var promise = client.changer.changeColor(options, Promise)
24+
this.lastColor = lastColor
25+
return promise
26+
}
27+
}

vue.config.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const webpack = require('webpack')
3+
const ThemeColorReplacer = require('webpack-theme-color-replacer')
34

45
function resolve (dir) {
56
return path.join(__dirname, dir)
@@ -24,7 +25,12 @@ module.exports = {
2425
configureWebpack: {
2526
plugins: [
2627
// Ignore all locale files of moment.js
27-
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
28+
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
29+
// 生成仅包含颜色的替换样式(主题色等)
30+
new ThemeColorReplacer({
31+
fileName: 'css/theme-colors.css',
32+
matchColors: getAntdSerials('#1890ff') // 主色系列
33+
})
2834
]
2935
},
3036

@@ -98,3 +104,14 @@ module.exports = {
98104
// babel-loader no-ignore node_modules/*
99105
transpileDependencies: []
100106
}
107+
108+
function getAntdSerials (color) {
109+
var lightens = new Array(9).fill().map((t, i) => {
110+
return ThemeColorReplacer.varyColor.lighten(color, i / 10)
111+
})
112+
// 此处为了简化,采用了darken。实际按color.less需求可以引入tinycolor, colorPalette变换得到颜色值
113+
var darkens = new Array(6).fill().map((t, i) => {
114+
return ThemeColorReplacer.varyColor.darken(color, i / 10)
115+
})
116+
return lightens.concat(darkens)
117+
}

0 commit comments

Comments
 (0)