index.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { isHexColor, colorIsDark, lighten, darken } from '/@/utils/color';
  2. import { appStore } from '/@/store/modules/app';
  3. import { ThemeEnum } from '/@/enums/appEnum';
  4. const HEADER_BG_COLOR_VAR = '--header-bg-color';
  5. const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
  6. const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';
  7. const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
  8. const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
  9. const SIDER_LIGHTEN_1_BG_COLOR = '--sider-dark-lighten-1-bg-color';
  10. const SIDER_LIGHTEN_2_BG_COLOR = '--sider-dark-lighten-2-bg-color';
  11. export function setCssVar(prop: string, val: any, dom = document.documentElement) {
  12. dom.style.setProperty(prop, val);
  13. }
  14. function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) {
  15. const targetEl = target || document.body;
  16. let { className } = targetEl;
  17. className = className.replace(clsName, '');
  18. targetEl.className = flag ? `${className} ${clsName} ` : className;
  19. }
  20. /**
  21. * Change the status of the project's color weakness mode
  22. * @param gray
  23. */
  24. export const updateColorWeak = (colorWeak: boolean) => {
  25. toggleClass(colorWeak, 'color-weak', document.documentElement);
  26. };
  27. /**
  28. * Change project gray mode status
  29. * @param gray
  30. */
  31. export const updateGrayMode = (gray: boolean) => {
  32. toggleClass(gray, 'gray-mode', document.documentElement);
  33. };
  34. /**
  35. * Change the background color of the top header
  36. * @param color
  37. */
  38. export function updateHeaderBgColor(color: string) {
  39. if (!isHexColor(color)) return;
  40. // bg color
  41. setCssVar(HEADER_BG_COLOR_VAR, color);
  42. // hover color
  43. const hoverColor = lighten(color, 6);
  44. setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  45. setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
  46. // Determine the depth of the color value and automatically switch the theme
  47. const isDark = colorIsDark(color);
  48. appStore.commitProjectConfigState({
  49. headerSetting: {
  50. theme: isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT,
  51. },
  52. });
  53. }
  54. /**
  55. * Change the background color of the left menu
  56. * @param color bg color
  57. */
  58. export function updateSidebarBgColor(color: string) {
  59. if (!isHexColor(color)) return;
  60. setCssVar(SIDER_DARK_BG_COLOR, color);
  61. setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color, 6));
  62. setCssVar(SIDER_LIGHTEN_1_BG_COLOR, lighten(color, 4));
  63. setCssVar(SIDER_LIGHTEN_2_BG_COLOR, lighten(color, 8));
  64. // only #ffffff is light
  65. // Only when the background color is #fff, the theme of the menu will be changed to light
  66. const isLight = ['#fff', '#ffffff'].includes(color.toLowerCase());
  67. appStore.commitProjectConfigState({
  68. menuSetting: {
  69. theme: isLight ? ThemeEnum.LIGHT : ThemeEnum.DARK,
  70. },
  71. });
  72. }