Skip to content

Commit 892c7e1

Browse files
committed
fix hook
1 parent 982da9d commit 892c7e1

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

docs/.vuepress/theme/lib/client/components/Comment.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ console.log("[Debug Comment]", [giscusOptions.value, options.value])
5050
:loading="giscusOptions.loading"
5151
/>
5252
<div class="v-nc-comment-placeholder" v-else>
53-
<h2 id="v-nc-comment" tabindex="-1">
54-
<a class="header-anchor text" href="#v-nc-comment" aria-hidden="true"
55-
>评论已关闭</a
56-
>
57-
</h2>
53+
<span class="text">评论已关闭</span>
5854
</div>
5955
</div>
6056
</template>

docs/.vuepress/theme/lib/client/components/Page.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import PageNav from "./PageNav.vue"
66
import SubToc from "./SubToc.vue"
77
import Comment from "./Comment.vue"
88
9-
import { computed, ref, watch } from "vue"
9+
import { computed, ref } from "vue"
1010
import { usePageData, usePageFrontmatter, useRoute } from "vuepress/client"
1111
import { useThemeData } from "@vuepress/plugin-theme-data/client"
1212
@@ -52,13 +52,15 @@ const isCommentActive = computed(() =>
5252
)
5353
const commentOption = ref()
5454
55-
// non-flash hook
5655
const $route = useRoute()
57-
Hook.onRoute(() => {
58-
if (!isCommentActive.value) commentOption.value = undefined
59-
else commentOption.value = themeData.value.giscus ?? undefined
60-
console.log(isCommentActive.value, commentOption.value)
61-
}, $route)
56+
Hook.onRoute(
57+
() => {
58+
if (!isCommentActive.value) commentOption.value = undefined
59+
else commentOption.value = themeData.value.giscus ?? undefined
60+
},
61+
$route,
62+
{ immediate: true }
63+
)
6264
6365
const pageTitle = computed(() => pageData.value.title)
6466
setupHeaders()

docs/.vuepress/theme/lib/client/components/SubToc.vue

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script lang="ts" setup>
22
import { computed, ref } from "vue"
3-
import { usePageData, usePageFrontmatter } from "vuepress/client"
3+
import { usePageData, usePageFrontmatter, useRoute } from "vuepress/client"
44
import { PageHeader } from "vuepress/shared"
55
66
import { useSubTocFly } from "../composables/useSubTocFly"
77
import { usePluginState } from "../composables/useComponentUtils"
88
import { defaultConstants, DefaultThemePageFrontmatter } from "../../shared"
9+
import { Hook } from "../composables/useHook"
910
1011
const pageData = usePageData()
1112
const pageFrontmatter = usePageFrontmatter<DefaultThemePageFrontmatter>()
@@ -19,19 +20,7 @@ const computedStateSource = computed(() =>
1920
const isCommentActive = computed(() =>
2021
usePluginState("comment", computedStateSource.value)
2122
)
22-
const tocHeaders = computed<PageHeader[]>(() => {
23-
const headers: PageHeader[] = pageData.value.headers ?? []
24-
if (!isCommentActive.value) return headers
25-
else if (headers[headers.length - 1].title !== "评论")
26-
headers.push({
27-
level: 2,
28-
link: `#v-nc-comment`,
29-
slug: `v-nc-comment`,
30-
title: `评论`,
31-
children: [],
32-
})
33-
return headers
34-
})
23+
const tocHeaders = ref<PageHeader[]>(pageData.value.headers ?? [])
3524
3625
const tocOptions = {
3726
containerTag: "nav",
@@ -47,6 +36,23 @@ const tocOptions = {
4736
const toc = ref<HTMLDivElement | null>(null)
4837
const tocFly = ref<HTMLDivElement | null>(null)
4938
useSubTocFly(toc, tocFly)
39+
40+
const $route = useRoute()
41+
Hook.onRoute(
42+
() => {
43+
if (!isCommentActive || !document) return
44+
if (document.querySelector("h2#v-nc-comment"))
45+
tocHeaders.value.push({
46+
level: 2,
47+
link: `#v-nc-comment`,
48+
slug: `v-nc-comment`,
49+
title: `评论`,
50+
children: [],
51+
})
52+
},
53+
$route,
54+
{ immediate: true }
55+
)
5056
</script>
5157

5258
<template>

docs/.vuepress/theme/lib/client/composables/useHook.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import type { RouteLocationNormalizedLoadedGeneric } from "vue-router"
44
export const Hook = {
55
onRoute: (
66
callback: () => void,
7-
route: RouteLocationNormalizedLoadedGeneric
7+
route: RouteLocationNormalizedLoadedGeneric,
8+
options?: { immediate?: boolean }
89
) => {
910
watch(
1011
() => route.path,
1112
() => {
12-
nextTick(callback)
13+
if (!options?.immediate) nextTick(callback)
14+
else callback()
1315
},
1416
{ immediate: true }
1517
)

0 commit comments

Comments
 (0)