setting.ts 4.63 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import { Reducer } from 'redux';
陈帅's avatar
陈帅 committed
2 3 4
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable promise/catch-or-return */
import { message } from 'antd';
陈小聪's avatar
陈小聪 committed
5
import defaultSettings, { DefaultSettings } from '../../config/defaultSettings';
6
import themeColorClient from '../components/SettingDrawer/themeColorClient';
陈帅's avatar
陈帅 committed
7

陈小聪's avatar
陈小聪 committed
8
export interface SettingModelType {
陈帅's avatar
陈帅 committed
9
  namespace: 'settings';
陈小聪's avatar
陈小聪 committed
10 11
  state: DefaultSettings;
  reducers: {
陈帅's avatar
陈帅 committed
12 13
    getSetting: Reducer<DefaultSettings>;
    changeSetting: Reducer<DefaultSettings>;
陈小聪's avatar
陈小聪 committed
14 15
  };
}
16

huangzheng's avatar
lint  
huangzheng committed
17
const updateTheme = (newPrimaryColor?: string) => {
18 19 20 21 22
  if (newPrimaryColor) {
    const timeOut = 0;
    const hideMessage = message.loading('正在切换主题!', timeOut);
    themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
  }
23
};
24

25
/*
陈小聪's avatar
陈小聪 committed
26 27 28
let lessNodesAppended: boolean;

const updateTheme: (primaryColor?: string) => void = primaryColor => {
kennylbj's avatar
kennylbj committed
29
  // Don't compile less in production!
陈帅's avatar
陈帅 committed
30 31
  // preview.pro.ant.design only do not use in your production;
  // preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
32
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION !== 'site') {
afc163's avatar
afc163 committed
33 34
    return;
  }
35
  // Determine if the component is remounted
afc163's avatar
afc163 committed
36
  if (!primaryColor) {
afc163's avatar
afc163 committed
37
    return;
38
  }
afc163's avatar
afc163 committed
39
  const hideMessage = message.loading('正在编译主题!', 0);
愚道's avatar
愚道 committed
40
  function buildIt() {
陈小聪's avatar
陈小聪 committed
41
    if (!(window as any).less) {
陈帅's avatar
陈帅 committed
42 43
      console.log('no less');
      return;
愚道's avatar
愚道 committed
44 45
    }
    setTimeout(() => {
陈小聪's avatar
陈小聪 committed
46
      (window as any).less
愚道's avatar
愚道 committed
47 48 49 50 51
        .modifyVars({
          '@primary-color': primaryColor,
        })
        .then(() => {
          hideMessage();
陈帅's avatar
陈帅 committed
52
          return true;
愚道's avatar
愚道 committed
53 54 55 56 57 58 59
        })
        .catch(() => {
          message.error('Failed to update theme');
          hideMessage();
        });
    }, 200);
  }
afc163's avatar
afc163 committed
60
  if (!lessNodesAppended) {
61
    // insert less.js and color.less
afc163's avatar
afc163 committed
62 63 64 65 66 67 68 69
    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
70 71
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
72 73
      };
    `;
afc163's avatar
afc163 committed
74
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
75 76 77 78 79
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
80 81 82
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
83
    lessNodesAppended = true;
84
  } else {
afc163's avatar
afc163 committed
85 86
    buildIt();
  }
87
};
88
*/
89

陈帅's avatar
陈帅 committed
90
const updateColorWeak: (colorWeak: boolean) => void = colorWeak => {
陈帅's avatar
陈帅 committed
91 92 93 94
  const root = document.getElementById('root');
  if (root) {
    root.className = colorWeak ? 'colorWeak' : '';
  }
afc163's avatar
afc163 committed
95 96
};

陈小聪's avatar
陈小聪 committed
97
const SettingModel: SettingModelType = {
陈帅's avatar
陈帅 committed
98
  namespace: 'settings',
afc163's avatar
afc163 committed
99
  state: defaultSettings,
jim's avatar
jim committed
100
  reducers: {
陈帅's avatar
陈帅 committed
101 102
    getSetting(state = defaultSettings) {
      const setting: Partial<DefaultSettings> = {};
jim's avatar
jim committed
103 104 105 106
      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
107
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
108 109
        }
      });
afc163's avatar
afc163 committed
110
      const { primaryColor, colorWeak } = setting;
陈帅's avatar
陈帅 committed
111

拷钉's avatar
拷钉 committed
112
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
113
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
114
      }
陈帅's avatar
陈帅 committed
115
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
116 117 118 119
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
120
    },
陈帅's avatar
陈帅 committed
121
    changeSetting(state = defaultSettings, { payload }) {
jim's avatar
jim committed
122
      const urlParams = new URL(window.location.href);
afc163's avatar
afc163 committed
123
      Object.keys(defaultSettings).forEach(key => {
jim's avatar
jim committed
124 125 126 127
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
128 129 130
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
131
        }
jim's avatar
jim committed
132 133 134 135
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
afc163's avatar
afc163 committed
136
        if (defaultSettings[key] !== value) {
jim's avatar
jim committed
137 138
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
139
      });
140
      const { primaryColor, colorWeak, contentWidth } = payload;
拷钉's avatar
拷钉 committed
141
      if (primaryColor && state.primaryColor !== primaryColor) {
afc163's avatar
afc163 committed
142
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
143
      }
144 145
      if (state.contentWidth !== contentWidth && window.dispatchEvent) {
        window.dispatchEvent(new Event('resize'));
146
      }
拷钉's avatar
拷钉 committed
147
      updateColorWeak(!!colorWeak);
jim's avatar
jim committed
148 149 150 151 152 153 154 155
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};
陈小聪's avatar
陈小聪 committed
156
export default SettingModel;