Commit ab481066 authored by 陈帅's avatar 陈帅

Optimization: Reduce the dependency of the setting component

parent e2fda6ec
...@@ -44,7 +44,6 @@ ...@@ -44,7 +44,6 @@
"react-fittext": "^1.0.0", "react-fittext": "^1.0.0",
"rollbar": "^2.4.2", "rollbar": "^2.4.2",
"setprototypeof": "^1.1.0", "setprototypeof": "^1.1.0",
"umi-plugin-locale": "^1.0.1",
"url-polyfill": "^1.0.13" "url-polyfill": "^1.0.13"
}, },
"devDependencies": { "devDependencies": {
...@@ -87,7 +86,8 @@ ...@@ -87,7 +86,8 @@
"stylelint-config-standard": "^18.0.0", "stylelint-config-standard": "^18.0.0",
"umi": "^1.3.15", "umi": "^1.3.15",
"umi-plugin-dva": "^0.9.1", "umi-plugin-dva": "^0.9.1",
"umi-plugin-routes": "^0.1.5" "umi-plugin-routes": "^0.1.5",
"umi-plugin-locale": "^1.0.1"
}, },
"optionalDependencies": { "optionalDependencies": {
"puppeteer": "^1.6.0" "puppeteer": "^1.6.0"
......
...@@ -20,24 +20,9 @@ const Body = ({ children, title, style }) => ( ...@@ -20,24 +20,9 @@ const Body = ({ children, title, style }) => (
@connect(({ setting }) => ({ setting })) @connect(({ setting }) => ({ setting }))
class SettingDarwer extends PureComponent { class SettingDarwer extends PureComponent {
componentDidMount() {
const {
setting: { themeColor, colorWeak },
} = this.props;
// Determine if the component is remounted
if (themeColor !== '#1890FF' && themeColor !== window['antd_pro_less_color']) {
window.less.refresh().then(() => {
this.colorChange(themeColor);
});
}
if (colorWeak === 'open') {
document.body.className = 'colorWeak';
}
}
getLayOutSetting = () => { getLayOutSetting = () => {
const { const {
setting: { grid, fixedHeader, autoHideHeader, fixSiderbar }, setting: { grid, fixedHeader, layout, autoHideHeader, fixSiderbar },
} = this.props; } = this.props;
return [ return [
{ {
...@@ -77,6 +62,7 @@ class SettingDarwer extends PureComponent { ...@@ -77,6 +62,7 @@ class SettingDarwer extends PureComponent {
}, },
{ {
title: '固定 Siderbar', title: '固定 Siderbar',
hide: layout === 'topmenu',
action: [ action: [
<Switch <Switch
size="small" size="small"
...@@ -106,13 +92,6 @@ class SettingDarwer extends PureComponent { ...@@ -106,13 +92,6 @@ class SettingDarwer extends PureComponent {
nextState.autoHideHeader = false; nextState.autoHideHeader = false;
} }
} }
if (key === 'colorWeak') {
if (value) {
document.body.className = 'colorWeak';
} else {
document.body.className = '';
}
}
this.setState(nextState, () => { this.setState(nextState, () => {
const { dispatch } = this.props; const { dispatch } = this.props;
dispatch({ dispatch({
...@@ -127,25 +106,6 @@ class SettingDarwer extends PureComponent { ...@@ -127,25 +106,6 @@ class SettingDarwer extends PureComponent {
this.changeSetting('collapse', !setting.collapse); this.changeSetting('collapse', !setting.collapse);
}; };
colorChange = color => {
this.changeSetting('themeColor', color);
const hideMessage = message.loading('正在编译主题!', 0);
setTimeout(() => {
window.less
.modifyVars({
'@primary-color': color,
'@input-hover-border-color': color,
})
.then(() => {
window['antd_pro_less_color'] = color;
hideMessage();
})
.catch(() => {
message.error(`Failed to update theme`);
});
}, 200);
};
render() { render() {
const { setting } = this.props; const { setting } = this.props;
const { collapse, silderTheme, themeColor, layout, colorWeak } = setting; const { collapse, silderTheme, themeColor, layout, colorWeak } = setting;
...@@ -197,7 +157,10 @@ class SettingDarwer extends PureComponent { ...@@ -197,7 +157,10 @@ class SettingDarwer extends PureComponent {
/> />
</Body> </Body>
<ThemeColor value={themeColor} onChange={this.colorChange} /> <ThemeColor
value={themeColor}
onChange={color => this.changeSetting('themeColor', color)}
/>
<Divider /> <Divider />
......
import { message } from 'antd';
const defaultSetting = { const defaultSetting = {
collapse: false, collapse: false,
silderTheme: 'dark', silderTheme: 'dark',
...@@ -10,6 +11,34 @@ const defaultSetting = { ...@@ -10,6 +11,34 @@ const defaultSetting = {
colorWeak: false, colorWeak: false,
}; };
const buildLessAndWeak = (themeColor, colorWeak) => {
// Determine if the component is remounted
if (themeColor && themeColor !== '#1890FF' && themeColor !== window['antd_pro_less_color']) {
window.less.refresh().then(() => {
const hideMessage = message.loading('正在编译主题!', 0);
setTimeout(() => {
window.less
.modifyVars({
'@primary-color': themeColor,
'@input-hover-border-color': themeColor,
})
.then(() => {
window['antd_pro_less_color'] = themeColor;
hideMessage();
})
.catch(() => {
message.error(`Failed to update theme`);
});
}, 200);
});
}
if (colorWeak) {
document.body.className = 'colorWeak';
} else {
document.body.className = '';
}
};
export default { export default {
namespace: 'setting', namespace: 'setting',
state: defaultSetting, state: defaultSetting,
...@@ -23,6 +52,8 @@ export default { ...@@ -23,6 +52,8 @@ export default {
setting[key] = value === '1' ? true : value; setting[key] = value === '1' ? true : value;
} }
}); });
const { themeColor, colorWeak } = setting;
buildLessAndWeak(themeColor, colorWeak);
return { return {
...state, ...state,
...setting, ...setting,
...@@ -47,6 +78,8 @@ export default { ...@@ -47,6 +78,8 @@ export default {
urlParams.searchParams.set(key, value); urlParams.searchParams.set(key, value);
} }
}); });
const { themeColor, colorWeak } = payload;
buildLessAndWeak(themeColor, colorWeak);
window.history.replaceState(null, 'setting', urlParams.href); window.history.replaceState(null, 'setting', urlParams.href);
return { return {
...state, ...state,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<body> <body>
<div id="root"></div> <div id="root"></div>
<link rel="stylesheet/less" type="text/css" href="/static/color.less" /> <link rel="stylesheet/less" type="text/css" href="/color.less" />
<script> <script>
window.less = { window.less = {
async: false, async: false,
......
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