site-layout.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // initPreferences({
  29. // namespace: 'docs',
  30. // });
  31. onMounted(() => {
  32. initZoom();
  33. });
  34. // 使用该函数
  35. const observer = watchDarkModeChange((dark) => {
  36. isDark.value = dark;
  37. });
  38. onBeforeUnmount(() => {
  39. observer?.disconnect();
  40. });
  41. function watchDarkModeChange(callback: (isDark: boolean) => void) {
  42. if (typeof window === 'undefined') {
  43. return;
  44. }
  45. const htmlElement = document.documentElement;
  46. const observer = new MutationObserver(() => {
  47. const isDark = htmlElement.classList.contains('dark');
  48. callback(isDark);
  49. });
  50. observer.observe(htmlElement, {
  51. attributeFilter: ['class'],
  52. attributes: true,
  53. });
  54. const initialIsDark = htmlElement.classList.contains('dark');
  55. callback(initialIsDark);
  56. return observer;
  57. }
  58. const tokenTheme = computed(() => {
  59. const algorithm = isDark.value
  60. ? [theme.darkAlgorithm]
  61. : [theme.defaultAlgorithm];
  62. return {
  63. algorithm,
  64. // token: tokens,
  65. };
  66. });
  67. </script>
  68. <template>
  69. <ConfigProvider :theme="tokenTheme">
  70. <Layout />
  71. </ConfigProvider>
  72. </template>
  73. <style>
  74. .medium-zoom-overlay,
  75. .medium-zoom-image--opened {
  76. z-index: 2147483647;
  77. }
  78. </style>