You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

  1. import { ref } from "vue";
  2. import { getConfig } from "@/config";
  3. import { useLayout } from "./useLayout";
  4. import { themeColorsType } from "../types";
  5. import { useGlobal } from "@pureadmin/utils";
  6. import { useEpThemeStoreHook } from "@/store/modules/epTheme";
  7. import {
  8. darken,
  9. lighten,
  10. toggleTheme
  11. } from "@pureadmin/theme/dist/browser-utils";
  12. export function useDataThemeChange() {
  13. const { layoutTheme, layout } = useLayout();
  14. const themeColors = ref<Array<themeColorsType>>([
  15. /* 道奇蓝(默认) */
  16. { color: "#1b2a47", themeColor: "default" },
  17. /* 亮白色 */
  18. { color: "#ffffff", themeColor: "light" },
  19. /* 猩红色 */
  20. { color: "#f5222d", themeColor: "dusk" },
  21. /* 橙红色 */
  22. { color: "#fa541c", themeColor: "volcano" },
  23. /* 金色 */
  24. { color: "#fadb14", themeColor: "yellow" },
  25. /* 绿宝石 */
  26. { color: "#13c2c2", themeColor: "mingQing" },
  27. /* 酸橙绿 */
  28. { color: "#52c41a", themeColor: "auroraGreen" },
  29. /* 深粉色 */
  30. { color: "#eb2f96", themeColor: "pink" },
  31. /* 深紫罗兰色 */
  32. { color: "#722ed1", themeColor: "saucePurple" }
  33. ]);
  34. const { $storage } = useGlobal<GlobalPropertiesApi>();
  35. const dataTheme = ref<boolean>($storage?.layout?.darkMode);
  36. const body = document.documentElement as HTMLElement;
  37. /** 设置导航主题色 */
  38. function setLayoutThemeColor(theme = "default") {
  39. layoutTheme.value.theme = theme;
  40. toggleTheme({
  41. scopeName: `layout-theme-${theme}`
  42. });
  43. $storage.layout = {
  44. layout: layout.value,
  45. theme,
  46. darkMode: dataTheme.value,
  47. sidebarStatus: $storage.layout?.sidebarStatus,
  48. epThemeColor: $storage.layout?.epThemeColor
  49. };
  50. if (theme === "default" || theme === "light") {
  51. setEpThemeColor(getConfig().EpThemeColor);
  52. } else {
  53. const colors = themeColors.value.find(v => v.themeColor === theme);
  54. setEpThemeColor(colors.color);
  55. }
  56. }
  57. function setPropertyPrimary(mode: string, i: number, color: string) {
  58. document.documentElement.style.setProperty(
  59. `--el-color-primary-${mode}-${i}`,
  60. dataTheme.value ? darken(color, i / 10) : lighten(color, i / 10)
  61. );
  62. }
  63. /** 设置 `element-plus` 主题色 */
  64. const setEpThemeColor = (color: string) => {
  65. useEpThemeStoreHook().setEpThemeColor(color);
  66. document.documentElement.style.setProperty("--el-color-primary", color);
  67. for (let i = 1; i <= 2; i++) {
  68. setPropertyPrimary("dark", i, color);
  69. }
  70. for (let i = 1; i <= 9; i++) {
  71. setPropertyPrimary("light", i, color);
  72. }
  73. };
  74. /** 日间、夜间主题切换 */
  75. function dataThemeChange() {
  76. /* 如果当前是light夜间主题,默认切换到default主题 */
  77. if (useEpThemeStoreHook().epTheme === "light" && dataTheme.value) {
  78. setLayoutThemeColor("default");
  79. } else {
  80. setLayoutThemeColor(useEpThemeStoreHook().epTheme);
  81. }
  82. if (dataTheme.value) {
  83. document.documentElement.classList.add("dark");
  84. } else {
  85. document.documentElement.classList.remove("dark");
  86. }
  87. }
  88. return {
  89. body,
  90. dataTheme,
  91. layoutTheme,
  92. themeColors,
  93. dataThemeChange,
  94. setEpThemeColor,
  95. setLayoutThemeColor
  96. };
  97. }