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

陈小聪's avatar
陈小聪 committed
74
const updateColorWeak: (colorWeak: string) => void = colorWeak => {
afc163's avatar
afc163 committed
75 76 77
  document.body.className = colorWeak ? 'colorWeak' : '';
};

陈小聪's avatar
陈小聪 committed
78
const SettingModel: SettingModelType = {
陈帅's avatar
陈帅 committed
79
  namespace: 'settings',
afc163's avatar
afc163 committed
80
  state: defaultSettings,
jim's avatar
jim committed
81
  reducers: {
jim's avatar
jim committed
82
    getSetting(state) {
陈小聪's avatar
陈小聪 committed
83
      const setting: any = {};
jim's avatar
jim committed
84 85 86 87
      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
88
          setting[key] = value === '1' ? true : value;
jim's avatar
jim committed
89 90
        }
      });
afc163's avatar
afc163 committed
91 92 93
      const { primaryColor, colorWeak } = setting;
      if (state.primaryColor !== primaryColor) {
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
94 95
      }
      updateColorWeak(colorWeak);
jim's avatar
jim committed
96 97 98 99
      return {
        ...state,
        ...setting,
      };
jim's avatar
jim committed
100
    },
jim's avatar
jim committed
101 102
    changeSetting(state, { payload }) {
      const urlParams = new URL(window.location.href);
afc163's avatar
afc163 committed
103
      Object.keys(defaultSettings).forEach(key => {
jim's avatar
jim committed
104 105 106 107
        if (urlParams.searchParams.has(key)) {
          urlParams.searchParams.delete(key);
        }
      });
jim's avatar
jim committed
108 109 110
      Object.keys(payload).forEach(key => {
        if (key === 'collapse') {
          return;
jim's avatar
jim committed
111
        }
jim's avatar
jim committed
112 113 114 115
        let value = payload[key];
        if (value === true) {
          value = 1;
        }
afc163's avatar
afc163 committed
116
        if (defaultSettings[key] !== value) {
jim's avatar
jim committed
117 118
          urlParams.searchParams.set(key, value);
        }
jim's avatar
jim committed
119
      });
120
      const { primaryColor, colorWeak, contentWidth } = payload;
afc163's avatar
afc163 committed
121 122
      if (state.primaryColor !== primaryColor) {
        updateTheme(primaryColor);
afc163's avatar
afc163 committed
123
      }
124 125
      if (state.contentWidth !== contentWidth && window.dispatchEvent) {
        window.dispatchEvent(new Event('resize'));
126
      }
afc163's avatar
afc163 committed
127
      updateColorWeak(colorWeak);
jim's avatar
jim committed
128 129 130 131 132 133 134 135
      window.history.replaceState(null, 'setting', urlParams.href);
      return {
        ...state,
        ...payload,
      };
    },
  },
};
陈小聪's avatar
陈小聪 committed
136
export default SettingModel;