index.ts 2.7 KB
Newer Older
1 2
import { isHexColor, colorIsDark, lighten, darken } from '/@/utils/color';
import { appStore } from '/@/store/modules/app';
V
vben 已提交
3
import { ThemeEnum } from '/@/enums/appEnum';
4 5 6 7 8 9 10 11 12 13

const HEADER_BG_COLOR_VAR = '--header-bg-color';
const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';

const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
const SIDER_LIGHTEN_1_BG_COLOR = '--sider-dark-lighten-1-bg-color';
const SIDER_LIGHTEN_2_BG_COLOR = '--sider-dark-lighten-2-bg-color';

14 15 16 17
export function setCssVar(prop: string, val: any, dom = document.documentElement) {
  dom.style.setProperty(prop, val);
}

18 19 20
function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) {
  const targetEl = target || document.body;
  let { className } = targetEl;
陈文彬 已提交
21
  className = className.replace(clsName, '');
22
  targetEl.className = flag ? `${className} ${clsName} ` : className;
陈文彬 已提交
23
}
24

V
vben 已提交
25 26
/**
 * Change the status of the project's color weakness mode
V
vben 已提交
27
 * @param colorWeak
V
vben 已提交
28
 */
陈文彬 已提交
29
export const updateColorWeak = (colorWeak: boolean) => {
30
  toggleClass(colorWeak, 'color-weak', document.documentElement);
陈文彬 已提交
31 32
};

V
vben 已提交
33 34 35 36
/**
 * Change project gray mode status
 * @param gray
 */
陈文彬 已提交
37
export const updateGrayMode = (gray: boolean) => {
38
  toggleClass(gray, 'gray-mode', document.documentElement);
陈文彬 已提交
39
};
40

V
vben 已提交
41 42 43 44
/**
 * Change the background color of the top header
 * @param color
 */
45 46 47
export function updateHeaderBgColor(color: string) {
  if (!isHexColor(color)) return;
  // bg color
V
vben 已提交
48 49
  setCssVar(HEADER_BG_COLOR_VAR, color);

50 51
  // hover color
  const hoverColor = lighten(color, 6);
V
vben 已提交
52 53
  setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
54

V
vben 已提交
55
  // Determine the depth of the color value and automatically switch the theme
56 57 58 59
  const isDark = colorIsDark(color);

  appStore.commitProjectConfigState({
    headerSetting: {
V
vben 已提交
60
      theme: isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT,
61 62 63 64
    },
  });
}

V
vben 已提交
65 66 67 68
/**
 * Change the background color of the left menu
 * @param color  bg color
 */
69 70 71
export function updateSidebarBgColor(color: string) {
  if (!isHexColor(color)) return;

V
vben 已提交
72 73 74 75
  setCssVar(SIDER_DARK_BG_COLOR, color);
  setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color, 6));
  setCssVar(SIDER_LIGHTEN_1_BG_COLOR, lighten(color, 4));
  setCssVar(SIDER_LIGHTEN_2_BG_COLOR, lighten(color, 8));
76 77

  // only #ffffff is light
V
vben 已提交
78
  // Only when the background color is #fff, the theme of the menu will be changed to light
79 80 81 82
  const isLight = ['#fff', '#ffffff'].includes(color.toLowerCase());

  appStore.commitProjectConfigState({
    menuSetting: {
V
vben 已提交
83
      theme: isLight ? ThemeEnum.LIGHT : ThemeEnum.DARK,
84 85 86
    },
  });
}