Commit b1e03cbd authored by afc163's avatar afc163 Committed by ι™ˆεΈ…

refactor less compile logic

parent 0a5a9fc8
...@@ -71,5 +71,5 @@ ...@@ -71,5 +71,5 @@
.productionHint { .productionHint {
font-size: 12px; font-size: 12px;
color: @text-color-secondary; color: @text-color-secondary;
margin-top: 4px; margin-top: 8px;
} }
...@@ -2,15 +2,13 @@ import { message } from 'antd'; ...@@ -2,15 +2,13 @@ import { message } from 'antd';
import defaultSetting from '../defaultSetting'; import defaultSetting from '../defaultSetting';
let lessNodesAppended; let lessNodesAppended;
const buildSettings = (themeColor, colorWeak) => { const updateTheme = themeColor => {
document.body.className = colorWeak ? 'colorWeak' : '';
// Determine if the component is remounted // Determine if the component is remounted
if (!themeColor || themeColor === '#1890FF' || themeColor === window['antd_pro_less_color']) { if (!themeColor) {
return; return;
} }
const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0); const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
if (!lessNodesAppended) { if (!lessNodesAppended) {
console.log('append less nodes');
const lessStyleNode = document.createElement('link'); const lessStyleNode = document.createElement('link');
const lessConfigNode = document.createElement('script'); const lessConfigNode = document.createElement('script');
const lessScriptNode = document.createElement('script'); const lessScriptNode = document.createElement('script');
...@@ -19,19 +17,18 @@ const buildSettings = (themeColor, colorWeak) => { ...@@ -19,19 +17,18 @@ const buildSettings = (themeColor, colorWeak) => {
lessConfigNode.innerHTML = ` lessConfigNode.innerHTML = `
window.less = { window.less = {
async: true, async: true,
env: 'production', env: 'production'
javascriptEnabled: true
}; };
`; `;
lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js'; lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/2.7.3/less.min.js';
lessScriptNode.async = true; lessScriptNode.async = true;
lessScriptNode.onload = () => { lessScriptNode.onload = () => {
buildIt(); buildIt();
lessScriptNode.onload = null; lessScriptNode.onload = null;
}; };
document.head.appendChild(lessStyleNode); document.body.appendChild(lessStyleNode);
document.head.appendChild(lessConfigNode); document.body.appendChild(lessConfigNode);
document.head.appendChild(lessScriptNode); document.body.appendChild(lessScriptNode);
lessNodesAppended = true; lessNodesAppended = true;
} else { } else {
buildIt(); buildIt();
...@@ -40,23 +37,26 @@ const buildSettings = (themeColor, colorWeak) => { ...@@ -40,23 +37,26 @@ const buildSettings = (themeColor, colorWeak) => {
if (!window.less) { if (!window.less) {
return; return;
} }
window.less setTimeout(() => {
.modifyVars({ window.less
'@primary-color': themeColor, .modifyVars({
'@input-hover-border-color': themeColor, '@primary-color': themeColor,
}) })
.then(() => { .then(() => {
console.log('start to compile'); hideMessage();
window['antd_pro_less_color'] = themeColor; })
hideMessage(); .catch(() => {
}) message.error('Failed to update theme');
.catch(() => { hideMessage();
message.error(`Failed to update theme`); });
hideMessage(); }, 200);
});
} }
}; };
const updateColorWeak = colorWeak => {
document.body.className = colorWeak ? 'colorWeak' : '';
};
export default { export default {
namespace: 'setting', namespace: 'setting',
state: defaultSetting, state: defaultSetting,
...@@ -71,7 +71,10 @@ export default { ...@@ -71,7 +71,10 @@ export default {
} }
}); });
const { themeColor, colorWeak } = setting; const { themeColor, colorWeak } = setting;
buildSettings(themeColor, colorWeak); if (state.themeColor !== themeColor) {
updateTheme(themeColor);
}
updateColorWeak(colorWeak);
return { return {
...state, ...state,
...setting, ...setting,
...@@ -97,7 +100,10 @@ export default { ...@@ -97,7 +100,10 @@ export default {
} }
}); });
const { themeColor, colorWeak } = payload; const { themeColor, colorWeak } = payload;
buildSettings(themeColor, colorWeak); if (state.themeColor !== themeColor) {
updateTheme(themeColor);
}
updateColorWeak(colorWeak);
window.history.replaceState(null, 'setting', urlParams.href); window.history.replaceState(null, 'setting', urlParams.href);
return { return {
...state, ...state,
......
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