setting.ts 3.8 KB
Newer Older
1
import { message } from 'antd';
2 3
import { Reducer } from 'redux';
import defaultSettings, { DefaultSettings } from '../../config/defaultSettings';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
4

5 6 7 8 9 10 11 12 13 14 15
export interface SettingModelType {
  namespace: 'setting';
  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
  if (APP_TYPE !== 'site') {
afc163's avatar
afc163 committed
18 19
    return;
  }
20
  // Determine if the component is remounted
afc163's avatar
afc163 committed
21
  if (!primaryColor) {
afc163's avatar
afc163 committed
22
    return;
23
  }
afc163's avatar
afc163 committed
24
  const hideMessage = message.loading('ζ­£εœ¨ηΌ–θ―‘δΈ»ι’˜οΌ', 0);
ζ„šι“'s avatar
ζ„šι“ committed
25
  function buildIt() {
26 27
    if (!(window as any).less) {
      console.log('no less');
ζ„šι“'s avatar
ζ„šι“ committed
28 29 30
      return;
    }
    setTimeout(() => {
31
      (window as any).less
ζ„šι“'s avatar
ζ„šι“ committed
32 33 34 35 36 37 38 39 40 41 42 43
        .modifyVars({
          '@primary-color': primaryColor,
        })
        .then(() => {
          hideMessage();
        })
        .catch(() => {
          message.error('Failed to update theme');
          hideMessage();
        });
    }, 200);
  }
afc163's avatar
afc163 committed
44
  if (!lessNodesAppended) {
45
    // insert less.js and color.less
afc163's avatar
afc163 committed
46 47 48 49 50 51 52 53
    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,
54 55
        env: 'production',
        javascriptEnabled: true
afc163's avatar
afc163 committed
56 57
      };
    `;
58
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
afc163's avatar
afc163 committed
59 60 61 62 63
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null;
    };
afc163's avatar
afc163 committed
64 65 66
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
afc163's avatar
afc163 committed
67
    lessNodesAppended = true;
68
  } else {
afc163's avatar
afc163 committed
69 70
    buildIt();
  }
71 72
};

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

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