_bodyConfig.ts 3.1 KB

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