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 6 7 8 9 10
const updateTheme = newPrimaryColor => {
  const hideMessage = message.loading('正在切换主题!', 0)
  themeColorClient.changeColor(newPrimaryColor)
    .finally(() => hideMessage())
}
/*
afc163's avatar
afc163 committed
11
let lessNodesAppended;
afc163's avatar
afc163 committed
12
const updateTheme = primaryColor => {
kennylbj's avatar
kennylbj committed
13
  // Don't compile less in production!
14 15
  // 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
16 17
    return;
  }
18
  // Determine if the component is remounted
afc163's avatar
afc163 committed
19
  if (!primaryColor) {
afc163's avatar
afc163 committed
20
    return;
21
  }
afc163's avatar
afc163 committed
22
  const hideMessage = message.loading('正在编译主题!', 0);
愚道's avatar
愚道 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  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
41
  if (!lessNodesAppended) {
42
    // insert less.js and color.less
afc163's avatar
afc163 committed
43 44 45 46 47 48 49 50
    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
51 52
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
53 54
      };
    `;
afc163's avatar
afc163 committed
55
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
56 57 58 59 60
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
61 62 63
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
64
    lessNodesAppended = true;
65
  } else {
afc163's avatar
afc163 committed
66 67
    buildIt();
  }
68
};
69
*/
70

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

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