setting.ts 4.45 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 15

const updateTheme = (newPrimaryColor:string) => {
16 17 18
  const hideMessage = message.loading('正在切换主题!', 0);
  themeColorClient.changeColor(newPrimaryColor).finally(() => hideMessage());
};
19

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

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

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

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

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