setting.ts 4.47 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
const updateTheme = (newPrimaryColor?: string) => {
  const timeOut = 0;
  const hideMessage = message.loading('正在切换主题!', timeOut);
hz's avatar
lint  
hz committed
18
  themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
19
};
20

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

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

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

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

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