setting.ts 4.53 KB
Newer Older
陈小聪's avatar
陈小聪 committed
1
import { Reducer } from 'redux';
陈帅's avatar
陈帅 committed
2
import { message } from 'antd';
陈小聪's avatar
陈小聪 committed
3
import defaultSettings, { DefaultSettings } from '../../config/defaultSettings';
4
import themeColorClient from '../components/SettingDrawer/themeColorClient';
陈帅's avatar
陈帅 committed
5

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

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

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

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

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

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

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