Skip to content

Commit

Permalink
fix(CopyrightInfo): 🎨 use computed instead of watch
Browse files Browse the repository at this point in the history
  • Loading branch information
BeiyanYunyi committed Dec 11, 2024
1 parent 8b07c2a commit d23c719
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "vitepress dev",
"preview": "vitepress preview"
"build": "unbuild"
},
"peerDependencies": {
"@iconify-json/carbon": "^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/components/AppFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { theme } = useData<PjtsThemeConfig>()
// 定义一个 ref 来存储动态 key
const componentKey = ref(0)
const frontmatter = ref<Record<string, any>>({})
const frontmatter = ref(route.data?.frontmatter)
const isFrontmatterLoaded = ref(false)
// 更新 key 和 frontmatter 的函数
Expand Down
49 changes: 27 additions & 22 deletions src/components/CopyrightInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import { data } from '../plugins/CopyrightLoader.data'
const orgName = useData().theme.value.org
// 初始化需要更新的变量
const attrs = ref<Record<string, any> | null>(null)
const route = useRoute() // 获取当前路由对象
// 定义一个函数,基于当前路由路径更新数据
function updateData() {
const paths = route.path.replace('.md', '').split('/').filter((item: string) => item !== '')
attrs.value = searchClosestInTrie(data, paths)
}
const attrs = computed(() => {
const paths = route.path
.replace('.md', '')
.split('/')
.filter((item: string) => item !== '')
return searchClosestInTrie(data, paths)
})
function searchClosestInTrie(
that: Trie<Record<string, any>>,
Expand All @@ -25,7 +24,11 @@ function searchClosestInTrie(
if (path.length === 0)
return node.value
if (path[0] in node.children) {
let value = searchClosestInTrie(that, path.slice(1), node.children[path[0]])
let value = searchClosestInTrie(
that,
path.slice(1),
node.children[path[0]],
)
if (value === null)
value = node.value
return value
Expand All @@ -45,15 +48,6 @@ const authors = computed(() => {
const displayAuthors = computed(() => {
return `${authors.value.join(' , ')}`
})
// 监听页面路由变化,路由变化时更新数据
watch(
() => route.path,
() => {
updateData() // 路由变化时调用更新逻辑
},
{ immediate: true }, // 确保在初次加载时也能更新数据
)
</script>

<template>
Expand All @@ -66,24 +60,35 @@ watch(
<span>这篇文章 </span>
<span>{{ `“${attrs!.title}”` }}</span>
<span> 由 </span>
<a v-if="attrs?.copyright?.url" :href="attrs.copyright.url">{{ displayAuthors }}</a>
<a v-if="attrs?.copyright?.url" :href="attrs.copyright.url">{{
displayAuthors
}}</a>
<span v-else>{{ displayAuthors }}</span>
<span> 创作</span>
<span v-if="attrs?.copyright?.org && attrs?.copyright?.license">
,{{ attrs.copyright.org }} 在
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
<a
v-if="attrs?.copyright?.licenseUrl"
:href="attrs.copyright.licenseUrl"
>{{ attrs.copyright.license }}</a>
<span v-else>{{ attrs.copyright.license }}</span>
许可下使用
</span>
<span v-else-if="orgName && attrs?.copyright?.license">
,{{ orgName }} 在
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
<a
v-if="attrs?.copyright?.licenseUrl"
:href="attrs.copyright.licenseUrl"
>{{ attrs.copyright.license }}</a>
<span v-else>{{ attrs.copyright.license }}</span>
许可下使用
</span>
<span v-else-if="attrs?.copyright?.license">
,Project Trans 在
<a v-if="attrs?.copyright?.licenseUrl" :href="attrs.copyright.licenseUrl">{{ attrs.copyright.license }}</a>
<a
v-if="attrs?.copyright?.licenseUrl"
:href="attrs.copyright.licenseUrl"
>{{ attrs.copyright.license }}</a>
<span v-else>{{ attrs.copyright.license }}</span>
许可下使用
</span>
Expand Down
2 changes: 1 addition & 1 deletion src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import DefaultTheme from 'vitepress/theme-without-fonts'
import { onMounted } from 'vue'

import Layout from './Layout.vue'
import { addFontSwitchListener } from './plugins/fontSwitcher.js'
import { addFontSwitchListener } from './plugins/fontSwitcher'
import './style.css'

import '@nolebase/vitepress-plugin-enhanced-readabilities/client/style.css'
Expand Down

0 comments on commit d23c719

Please sign in to comment.