setting.ts 4.48 KB
Newer Older
1
import { message } from 'antd';
陈小聪's avatar
陈小聪 committed
2 3
import { Reducer } from 'redux';
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 16 17 18 19
const updateTheme = (newPrimaryColor?: string) => {
  const timeOut = 0;
  const hideMessage = message.loading('正在切换主题!', timeOut);
  themeColorClient.changeColor(newPrimaryColor)
    .finally(() => hideMessage());
20
};
21

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

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

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

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

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