setting.js 3.85 KB
Newer Older
1
import { message } from 'antd';
afc163's avatar
afc163 committed
2
import defaultSettings from '../defaultSettings';
3
import themeColorClient from '../components/SettingDrawer/themeColorClient';
陈帅's avatar
陈帅 committed
4

5
const updateTheme = newPrimaryColor => {
6 7 8
  const hideMessage = message.loading('正在切换主题!', 0);
  themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
};
9
/*
afc163's avatar
afc163 committed
10
let lessNodesAppended;
afc163's avatar
afc163 committed
11
const updateTheme = primaryColor => {
kennylbj's avatar
kennylbj committed
12
  // Don't compile less in production!
13 14
  // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION !== 'site') {
afc163's avatar
afc163 committed
15 16
    return;
  }
17
  // Determine if the component is remounted
afc163's avatar
afc163 committed
18
  if (!primaryColor) {
afc163's avatar
afc163 committed
19
    return;
20
  }
afc163's avatar
afc163 committed
21
  const hideMessage = message.loading('正在编译主题!', 0);
愚道's avatar
愚道 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  function buildIt() {
    if (!window.less) {
      return;
    }
    setTimeout(() => {
      window.less
        .modifyVars({
          '@primary-color': primaryColor,
        })
        .then(() => {
          hideMessage();
        })
        .catch(() => {
          message.error('Failed to update theme');
          hideMessage();
        });
    }, 200);
  }
afc163's avatar
afc163 committed
40
  if (!lessNodesAppended) {
41
    // insert less.js and color.less
afc163's avatar
afc163 committed
42 43 44 45 46 47 48 49
    const lessStyleNode = document.createElement('link');
    const lessConfigNode = document.createElement('script');
    const lessScriptNode = document.createElement('script');
    lessStyleNode.setAttribute('rel', 'stylesheet/less');
    lessStyleNode.setAttribute('href', '/color.less');
    lessConfigNode.innerHTML = `
      window.less = {
        async: true,
afc163's avatar
afc163 committed
50 51
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
52 53
      };
    `;
afc163's avatar
afc163 committed
54
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
55 56 57 58 59
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
60 61 62
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
63
    lessNodesAppended = true;
64
  } else {
afc163's avatar
afc163 committed
65 66
    buildIt();
  }
67
};
68
*/
69

afc163's avatar
afc163 committed
70 71 72 73
const updateColorWeak = colorWeak => {
  document.body.className = colorWeak ? 'colorWeak' : '';
};

jim's avatar
jim committed
74 75
export default {
  namespace: 'setting',
afc163's avatar
afc163 committed
76
  state: defaultSettings,
jim's avatar
jim committed
77
  reducers: {
jim's avatar
jim committed
78
    getSetting(state) {
jim's avatar
jim committed
79
      const setting = {};
jim's avatar
jim committed
80 81 82 83
      const urlParams = new URL(window.location.href);
      Object.keys(state).forEach(key => {
        if (urlParams.searchParams.has(key)) {
          const value = urlParams.searchParams.get(key);
jim's avatar
jim committed
84
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
85 86
        }
      });
afc163's avatar
afc163 committed
87 88 89
      const { primaryColor, colorWeak } = setting;
      if (state.primaryColor !== primaryColor) {
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
90 91
      }
      updateColorWeak(colorWeak);
jim's avatar
jim committed
92 93 94 95
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
96
    },
jim's avatar
jim committed
97 98
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
afc163's avatar
afc163 committed
99
      Object.keys(defaultSettings).forEach(key => {
jim's avatar
jim committed
100 101 102 103
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
104 105 106
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
107
        }
jim's avatar
jim committed
108 109 110 111
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
afc163's avatar
afc163 committed
112
        if (defaultSettings[key] !== value) {
jim's avatar
jim committed
113 114
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
115
      });
116
      const { primaryColor, colorWeak, contentWidth } = payload;
afc163's avatar
afc163 committed
117 118
      if (state.primaryColor !== primaryColor) {
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
119
      }
120 121
      if (state.contentWidth !== contentWidth && window.dispatchEvent) {
        window.dispatchEvent(new Event('resize'));
122
      }
afc163's avatar
afc163 committed
123
      updateColorWeak(colorWeak);
jim's avatar
jim committed
124 125 126 127 128 129 130 131
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};