|
13 | 13 | class="w-full h-full" |
14 | 14 | :options="scrollbarOptions" |
15 | 15 | defer |
16 | | - @os-initialized="scrollbarInitialized" |
| 16 | + @os-initialized="scrollbarChanged" |
17 | 17 | > |
18 | 18 | <div :style="{ padding: articlePadding }"> |
19 | 19 | <article |
|
44 | 44 | import scrollbarOptions from '../../config/scrollbarOptions'; |
45 | 45 | import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue'; |
46 | 46 | import { computed, nextTick, onMounted, onUnmounted, ref, watch, watchEffect } from 'vue'; |
47 | | - import { Content, onContentUpdated, useData, useRoute } from 'vitepress'; |
| 47 | + import { Content, useData, useRoute } from 'vitepress'; |
48 | 48 | import emitter from '../../emitter'; |
49 | 49 | import NotFound from 'vitepress/dist/client/theme-default/NotFound.vue'; |
50 | 50 |
|
51 | | - const { page, theme, frontmatter } = useData(); |
| 51 | + const { page, theme, frontmatter, hash } = useData(); |
52 | 52 | const route = useRoute(); |
53 | 53 | const scrollbars = ref<InstanceType<typeof OverlayScrollbarsComponent> | null>(null); |
54 | 54 | const article = ref<HTMLElement | null>(null); |
|
57 | 57 | const showDatetime = computed(() => frontmatter.value.lastUpdated !== false); |
58 | 58 |
|
59 | 59 | // Listen for routing changes and scroll to the top |
60 | | - watch(() => route.path, () => { |
| 60 | + watch(() => [ route.path, hash ], ([ , hashValue ]) => { |
61 | 61 | nextTick(() => { |
62 | | - scrollbars.value?.osInstance()?.elements()?.viewport?.scrollTo({ top: 0 }); |
| 62 | + if (hashValue) { |
| 63 | + setTimeout(() => { |
| 64 | + hashChange(); |
| 65 | + }); |
| 66 | + } else { |
| 67 | + scrollbars.value?.osInstance()?.elements()?.viewport?.scrollTo({ top: 0 }); |
| 68 | + } |
63 | 69 | }); |
64 | | - }, { |
65 | | - immediate: true |
66 | | - }); |
| 70 | + }, { immediate: true }); |
67 | 71 |
|
68 | 72 | // Listen for hash changes |
69 | | - function hashChange() { |
| 73 | + const hashChange = () => { |
| 74 | + // console.log('hashChange', location.hash); |
70 | 75 | if (location.hash) { |
71 | 76 | const _hashText = decodeURIComponent(location.hash.replace('#', '')); |
72 | 77 | emitter.emit('scroll-to-hash', _hashText); |
73 | 78 | } |
74 | | - } |
| 79 | + }; |
75 | 80 |
|
76 | | - function scrollbarInitialized() { |
77 | | - // console.log('scrollbarInitialized'); |
| 81 | + function scrollbarChanged() { |
| 82 | + // console.log('scrollbarChanged'); |
78 | 83 | setTimeout(() => { |
79 | 84 | hashChange(); |
80 | 85 | }, 0); |
81 | 86 | } |
82 | 87 |
|
83 | | - onContentUpdated(() => { |
84 | | - // console.log('content updated'); |
85 | | - setTimeout(() => { |
86 | | - hashChange(); |
87 | | - }, 0); |
88 | | - }); |
89 | | -
|
90 | 88 | const resizeObserver = ref<ResizeObserver | null>(null); |
91 | 89 | const articlePadding = ref<string>('calc(var(--base-size))'); |
92 | 90 |
|
|
139 | 137 | } |
140 | 138 | }); |
141 | 139 | resizeObserver.value.observe(article.value!); |
142 | | -
|
143 | | - scrollbars.value?.osInstance()?.update(); |
144 | 140 | }); |
145 | 141 |
|
146 | 142 | onUnmounted(() => { |
|
0 commit comments