_bodyConfig.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import objectPath from "object-path"
  2. import { ThemeModeComponent } from "@@/vb-dom/theme"
  3. import type LayoutConfigTypes from "@@/types/LayoutConfigTypes"
  4. import layoutConfig from "@@/config/LayoutConfig"
  5. import { localCache } from "@@/utils/cache"
  6. export const LS_CONFIG_NAME_KEY = `config_${import.meta.env.VITE_APP_NAME}_${
  7. import.meta.env.VITE_APP_VERSION
  8. }`
  9. export const THEME_MODE_LS_KEY = "VB_THEME_MODE_VALUE"
  10. export const THEME_MENU_MODE_LS_KEY = "VB_THEME_MODE_MENU"
  11. export const useBodyConfigStore = defineStore("bodyConfig", () => {
  12. const classes = ref<any>({})
  13. function getClasses(key: string) {
  14. return classes.value[key]
  15. }
  16. function addBodyClassname(className: string) {
  17. document.body.classList.add(className)
  18. }
  19. function removeBodyClassName(className: string) {
  20. document.body.classList.remove(className)
  21. }
  22. function addBodyAttribute(payload: { qualifiedName: string; value: string }) {
  23. const { qualifiedName, value } = payload
  24. document.body.setAttribute(qualifiedName, value)
  25. }
  26. function removeBodyAttribute(payload: { qualifiedName: string }) {
  27. const { qualifiedName } = payload
  28. document.body.removeAttribute(qualifiedName)
  29. }
  30. function addClassname(payload: { position: string; className: string }) {
  31. const { position, className } = payload
  32. if (!classes.value[position]) {
  33. classes.value[position] = [className]
  34. } else {
  35. classes.value[position].push(className)
  36. }
  37. }
  38. const config = ref<LayoutConfigTypes>(layoutConfig)
  39. const initial = ref<LayoutConfigTypes>(layoutConfig)
  40. function getLayoutConfig(path: string, defaultValue?: string) {
  41. return objectPath.get(config.value, path, defaultValue)
  42. }
  43. function setLayoutConfigProperty(property: string, value: any) {
  44. objectPath.set(config.value, property, value)
  45. localCache.setJSON(LS_CONFIG_NAME_KEY, config.value)
  46. }
  47. function resetLayoutConfig() {
  48. config.value = Object.assign({}, initial.value)
  49. }
  50. function removeCacheLayoutConfig() {
  51. localCache.setJSON(LS_CONFIG_NAME_KEY, layoutConfig)
  52. overrideLayoutConfig()
  53. }
  54. function overrideLayoutConfig() {
  55. config.value = initial.value = Object.assign(
  56. {},
  57. initial.value,
  58. localCache.getJSON(LS_CONFIG_NAME_KEY) || {}
  59. )
  60. }
  61. const mode = ref<"light" | "dark" | "system">(
  62. localCache.get(THEME_MODE_LS_KEY) as "light" | "dark" | "system"
  63. )
  64. function setThemeMode(payload: "light" | "dark" | "system") {
  65. let currentMode = payload
  66. localCache.set(THEME_MODE_LS_KEY, currentMode)
  67. localCache.set(THEME_MENU_MODE_LS_KEY, currentMode)
  68. mode.value = currentMode
  69. if (currentMode === "system") {
  70. currentMode = ThemeModeComponent.getSystemMode()
  71. }
  72. document.documentElement.setAttribute("data-bs-theme", currentMode)
  73. ThemeModeComponent.init()
  74. }
  75. return {
  76. getClasses,
  77. addBodyClassname,
  78. removeBodyClassName,
  79. addBodyAttribute,
  80. removeBodyAttribute,
  81. addClassname,
  82. config,
  83. getLayoutConfig,
  84. setLayoutConfigProperty,
  85. resetLayoutConfig,
  86. removeCacheLayoutConfig,
  87. overrideLayoutConfig,
  88. mode,
  89. setThemeMode
  90. }
  91. })