site-layout.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <script lang="ts" setup>
  2. import {
  3. computed,
  4. nextTick,
  5. onBeforeUnmount,
  6. onMounted,
  7. ref,
  8. watch,
  9. } from 'vue';
  10. // import { useAntdDesignTokens } from '@vben/hooks';
  11. // import { initPreferences } from '@vben/preferences';
  12. import { ConfigProvider, theme } from 'ant-design-vue';
  13. import mediumZoom from 'medium-zoom';
  14. import { useRoute } from 'vitepress';
  15. import DefaultTheme from 'vitepress/theme';
  16. const { Layout } = DefaultTheme;
  17. const route = useRoute();
  18. // const { tokens } = useAntdDesignTokens();
  19. const initZoom = () => {
  20. // mediumZoom('[data-zoomable]', { background: 'var(--vp-c-bg)' });
  21. mediumZoom('.VPContent img', { background: 'var(--vp-c-bg)' });
  22. };
  23. const isDark = ref(true);
  24. watch(
  25. () => route.path,
  26. () => nextTick(() => initZoom()),
  27. );
  28. function setVxeTheme(name?: string) {
  29. const theme = !name || name === 'default' ? 'light' : name;
  30. if (typeof document !== 'undefined') {
  31. const documentElement = document.documentElement;
  32. if (documentElement) {
  33. documentElement.dataset.vxeUiTheme = theme;
  34. }
  35. }
  36. }
  37. watch(isDark, (dark) => {
  38. setVxeTheme(dark ? 'dark' : 'light');
  39. });
  40. // initPreferences({
  41. // namespace: 'docs',
  42. // });
  43. onMounted(() => {
  44. initZoom();
  45. });
  46. // 使用该函数
  47. const observer = watchDarkModeChange((dark) => {
  48. isDark.value = dark;
  49. });
  50. onBeforeUnmount(() => {
  51. observer?.disconnect();
  52. });
  53. function watchDarkModeChange(callback: (isDark: boolean) => void) {
  54. if (typeof window === 'undefined') {
  55. return;
  56. }
  57. const htmlElement = document.documentElement;
  58. const observer = new MutationObserver(() => {
  59. const isDark = htmlElement.classList.contains('dark');
  60. callback(isDark);
  61. });
  62. observer.observe(htmlElement, {
  63. attributeFilter: ['class'],
  64. attributes: true,
  65. });
  66. const initialIsDark = htmlElement.classList.contains('dark');
  67. callback(initialIsDark);
  68. return observer;
  69. }
  70. const tokenTheme = computed(() => {
  71. const algorithm = isDark.value
  72. ? [theme.darkAlgorithm]
  73. : [theme.defaultAlgorithm];
  74. return {
  75. algorithm,
  76. // token: tokens,
  77. };
  78. });
  79. </script>
  80. <template>
  81. <ConfigProvider :theme="tokenTheme">
  82. <Layout />
  83. </ConfigProvider>
  84. </template>
  85. <style>
  86. .medium-zoom-overlay,
  87. .medium-zoom-image--opened {
  88. z-index: 2147483647;
  89. }
  90. </style>