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

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

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

陈小聪's avatar
陈小聪 committed
75
const updateColorWeak: (colorWeak: string) => void = colorWeak => {
陈帅's avatar
陈帅 committed
76 77 78 79
  const root = document.getElementById('root');
  if (root) {
    root.className = colorWeak ? 'colorWeak' : '';
  }
afc163's avatar
afc163 committed
80 81
};

陈小聪's avatar
陈小聪 committed
82
const SettingModel: SettingModelType = {
陈帅's avatar
陈帅 committed
83
  namespace: 'settings',
afc163's avatar
afc163 committed
84
  state: defaultSettings,
jim's avatar
jim committed
85
  reducers: {
jim's avatar
jim committed
86
    getSetting(state) {
陈小聪's avatar
陈小聪 committed
87
      const setting: any = {};
jim's avatar
jim committed
88 89 90 91
      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
92
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
93 94
        }
      });
afc163's avatar
afc163 committed
95
      const { primaryColor, colorWeak } = setting;
陈帅's avatar
陈帅 committed
96

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