configure.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { useStorage } from '@vueuse/core'
  2. import { mergeWith } from 'lodash-es'
  3. import { acceptHMRUpdate, defineStore } from 'pinia'
  4. import { ref, watch } from 'vue'
  5. import type { WatermarkProps } from 'naive-ui'
  6. export interface ConfigureOptions {
  7. menuCollapsed: boolean
  8. showFooter: boolean
  9. showLogo: boolean
  10. showTabs: boolean
  11. showTabClose: boolean
  12. showNavigation: boolean
  13. showBreadcrumb: boolean
  14. showWatermark: boolean
  15. showNoise: boolean
  16. showTopLoadingBar: boolean
  17. enableNavigationTransition: boolean
  18. enableTextSelect: boolean
  19. watermarkOptions: Partial<WatermarkProps>
  20. noiseOpacity: number
  21. }
  22. const DEFAULT_CONFIGURE_OPTIONS: ConfigureOptions = {
  23. menuCollapsed: false,
  24. showFooter: true,
  25. showTabs: true,
  26. showTabClose: true,
  27. showLogo: true,
  28. showNoise: true,
  29. showWatermark: false,
  30. showNavigation: true,
  31. showBreadcrumb: true,
  32. showTopLoadingBar: true,
  33. enableNavigationTransition: true,
  34. enableTextSelect: true,
  35. watermarkOptions: {
  36. content: 'Watermark',
  37. fontColor: '#D81E1E96',
  38. fontSize: 16,
  39. width: 384,
  40. height: 384,
  41. xGap: 0,
  42. yGap: 0,
  43. xOffset: 12,
  44. yOffset: 60,
  45. globalRotate: 0,
  46. rotate: -20,
  47. textAlign: 'center',
  48. cross: true,
  49. fontStyle: 'normal',
  50. fontWeight: 400,
  51. lineHeight: 16,
  52. image: undefined,
  53. imageHeight: 64,
  54. imageWidth: 64,
  55. imageOpacity: 0.5,
  56. },
  57. noiseOpacity: 0.02,
  58. }
  59. export const useConfigureStore = defineStore('configureStore', () => {
  60. const configure = useStorage<ConfigureOptions>('configure', DEFAULT_CONFIGURE_OPTIONS)
  61. const modify = (options: Partial<ConfigureOptions>) => {
  62. configure.value = mergeWith({}, configure.value, options, (objValue, srcValue) => {
  63. if (Array.isArray(objValue) && Array.isArray(srcValue)) {
  64. return srcValue
  65. }
  66. })
  67. return null
  68. }
  69. const reset = () => {
  70. configure.value = structuredClone(DEFAULT_CONFIGURE_OPTIONS)
  71. }
  72. watch(
  73. () => configure.value.enableTextSelect,
  74. (newValue) => {
  75. document.documentElement.style.userSelect = newValue ? 'auto' : 'none'
  76. },
  77. {
  78. immediate: true,
  79. },
  80. )
  81. return {
  82. configure,
  83. reset,
  84. modify,
  85. }
  86. })
  87. if (import.meta.hot) {
  88. import.meta.hot.accept(acceptHMRUpdate(useConfigureStore, import.meta.hot))
  89. }