Skip to content

Commit

Permalink
fix: dynamic page title with i18n menu config
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 9, 2023
1 parent 83602ef commit 51972dd
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 40 deletions.
37 changes: 37 additions & 0 deletions src/hooks/usePageTitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useAppStore } from '@/stores/app'
import { useMetaStore } from '@/stores/meta'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'

export default function usePageTitle() {
const pageTitle = ref()
const appStore = useAppStore()
const metaStore = useMetaStore()
const route = useRoute()

const updateTitle = (locale?: string | undefined) => {
const currentLocale = (locale ?? appStore.locale) === 'cn' ? 'cn' : 'en'
const menuName = String(route.name)
const routeInfo =
appStore.themeConfig.menu.menus[
menuName.charAt(0).toUpperCase() + menuName.slice(1)
]
pageTitle.value =
(routeInfo.i18n && routeInfo.i18n[currentLocale]) || routeInfo.name
metaStore.setTitle(pageTitle.value)
}

watch(
() => appStore.locale,
value => {
if (value) {
updateTitle(value)
}
}
)

return {
pageTitle,
updateTitle
}
}
20 changes: 14 additions & 6 deletions src/views/About.vue
Original file line number Diff line number Diff line change
@@ -1,39 +1,47 @@
<template>
<div>
<Breadcrumbs :current="t('menu.about')" />
<PageContent :post="pageData" />
<Breadcrumbs :current="pageTitle" />
<PageContent :post="pageData" :title="pageTitle" />
</div>
</template>

<script lang="ts">
import { defineComponent, nextTick, onMounted, ref } from 'vue'
import { computed, defineComponent, nextTick, onMounted, ref, watch } from 'vue'
import { useArticleStore } from '@/stores/article'
import { Page } from '@/models/Article.class'
import PageContent from '@/components/PageContent.vue'
import Breadcrumbs from '@/components/Breadcrumbs.vue'
import { useI18n } from 'vue-i18n'
import usePageTitle from '@/hooks/usePageTitle'
import { useAppStore } from '@/stores/app'
declare const Prism: any
export default defineComponent({
name: 'About',
components: { PageContent, Breadcrumbs },
setup() {
const appStore = useAppStore()
const articleStore = useArticleStore()
const pageData = ref(new Page())
const { t } = useI18n()
const { pageTitle, updateTitle } = usePageTitle()
const fetchArticle = async () => {
const response = await articleStore.fetchArticle('about')
pageData.value = await articleStore.fetchArticle('about')
pageData.value = response
updateTitle()
await nextTick()
Prism.highlightAll()
}
onMounted(fetchArticle)
return { pageData, t }
return {
pageTitle,
pageData,
t
}
}
})
</script>
Expand Down
29 changes: 19 additions & 10 deletions src/views/Archives.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div class="flex flex-col">
<div class="post-header">
<Breadcrumbs :current="t('menu.archives')" />
<h1 class="post-title text-white uppercase">{{ t('menu.archives') }}</h1>
<Breadcrumbs :current="pageTitle" />
<h1 class="post-title text-white uppercase">{{ pageTitle }}</h1>
</div>
<div
class="bg-ob-deep-800 px-14 py-16 rounded-2xl shadow-xl block min-h-screen"
Expand Down Expand Up @@ -48,7 +48,7 @@
:pageSize="12"
:pageTotal="pagination.pageTotal"
:page="pagination.page"
@pageChange="pageChangeHanlder"
@pageChange="pageChangeHandler"
/>
</div>
</div>
Expand All @@ -63,6 +63,7 @@ import Breadcrumbs from '@/components/Breadcrumbs.vue'
import Paginator from '@/components/Paginator.vue'
import { useCommonStore } from '@/stores/common'
import defaultCover from '@/assets/default-cover.jpg'
import usePageTitle from '@/hooks/usePageTitle'
export default defineComponent({
name: 'Archives',
Expand All @@ -76,16 +77,18 @@ export default defineComponent({
pageTotal: 0,
page: 1
})
const { pageTitle, updateTitle } = usePageTitle()
const fetchData = () => {
postStore.fetchArchives(pagination.value.page).then(data => {
pagination.value.pageTotal = data.total
archives.value = data.data
})
const fetchData = async () => {
const data = await postStore.fetchArchives(pagination.value.page)
pagination.value.pageTotal = data.total
archives.value = data.data
commonStore.setHeaderImage(defaultCover)
updateTitle()
}
const pageChangeHanlder = (page: number) => {
const pageChangeHandler = (page: number) => {
pagination.value.page = page
window.scrollTo({
top: 0,
Expand All @@ -99,7 +102,13 @@ export default defineComponent({
commonStore.resetHeaderImage()
})
return { pageChangeHanlder, pagination, archives, t }
return {
pageTitle,
pageChangeHandler,
pagination,
archives,
t
}
}
})
</script>
Expand Down
21 changes: 6 additions & 15 deletions src/views/Category.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
<template>
<div class="flex flex-col">
<div class="post-header">
<Breadcrumbs :current="t('menu.categories')" />
<Breadcrumbs :current="pageTitle" />
<h1 class="post-title text-white uppercase">
{{ t('menu.categories') }}
{{ pageTitle }}
</h1>
</div>
<div class="main-grid">
<div class="relative">
<div
class="
post-html
bg-ob-deep-800
px-14
py-16
rounded-2xl
shadow-xl
block
min-h-screen
"
class="post-html bg-ob-deep-800 px-14 py-16 rounded-2xl shadow-xl block min-h-screen"
></div>
</div>
<div class="col-span-1">
Expand All @@ -32,14 +23,14 @@
import { defineComponent } from 'vue'
import { Sidebar } from '@/components/Sidebar'
import Breadcrumbs from '@/components/Breadcrumbs.vue'
import { useI18n } from 'vue-i18n'
import usePageTitle from '@/hooks/usePageTitle'
export default defineComponent({
name: 'Category',
components: { Sidebar, Breadcrumbs },
setup() {
const { t } = useI18n()
return { t }
const { pageTitle, updateTitle } = usePageTitle()
return { pageTitle }
}
})
</script>
Expand Down
26 changes: 20 additions & 6 deletions src/views/Links.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div>
<Breadcrumbs :current="t('menu.about')" />
<Breadcrumbs :current="pageTitle" />
<div class="flex flex-col">
<div class="post-header">
<h1 v-if="pageData.title" class="post-title text-white uppercase">
{{ pageData.title }}
<h1 v-if="pageTitle" class="post-title text-white uppercase">
{{ pageTitle }}
</h1>
<ob-skeleton
v-else
Expand Down Expand Up @@ -92,7 +92,15 @@

<script lang="ts">
import '@/styles/prism-aurora-future.css'
import { Ref, computed, defineComponent, nextTick, onMounted, ref } from 'vue'
import {
Ref,
computed,
defineComponent,
nextTick,
onMounted,
ref,
watch
} from 'vue'
import PostStats from '@/components/Post/PostStats.vue'
import LinkBox from '@/components/Link/LinkBox.vue'
import LinkCard from '@/components/Link/LinkCard.vue'
Expand All @@ -106,6 +114,9 @@ import { useRoute } from 'vue-router'
import LinkCategoryList from '@/components/Link/LinkCategoryList.vue'
import LinkList from '@/components/Link/LinkList.vue'
import Comment from '@/components/Comment.vue'
import Breadcrumbs from '@/components/Breadcrumbs.vue'
import { useMetaStore } from '@/stores/meta'
import usePageTitle from '@/hooks/usePageTitle'
interface PostStatsExpose extends Ref<InstanceType<typeof PostStats>> {
getCommentCount(): void
Expand All @@ -123,7 +134,8 @@ export default defineComponent({
Title,
LinkCategoryList,
LinkList,
Comment
Comment,
Breadcrumbs
},
setup() {
const articleStore = useArticleStore()
Expand All @@ -134,10 +146,11 @@ export default defineComponent({
const { t } = useI18n()
const commentOffset = ref(0)
const contentEl = ref()
const { pageTitle, updateTitle } = usePageTitle()
const fetchArticle = async () => {
pageData.value = await articleStore.fetchArticle('friends')
updateTitle(appStore.locale)
await nextTick()
postStatsRef.value?.getCommentCount()
postStatsRef.value?.getPostView()
Expand Down Expand Up @@ -167,6 +180,7 @@ export default defineComponent({
gradientBackground: computed(() => {
return { background: appStore.themeConfig.theme.header_gradient_css }
}),
pageTitle,
jumpToComments,
postStatsRef,
pageData,
Expand Down
10 changes: 7 additions & 3 deletions src/views/Tag.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<div class="flex flex-col">
<div class="post-header">
<Breadcrumbs :current="t('menu.tags')" />
<h1 class="post-title text-white uppercase">{{ t('menu.tags') }}</h1>
<Breadcrumbs :current="pageTitle" />
<h1 class="post-title text-white uppercase">{{ pageTitle }}</h1>
</div>
<div class="bg-ob-deep-800 px-14 py-16 rounded-2xl shadow-xl block">
<TagList>
Expand Down Expand Up @@ -39,6 +39,7 @@ import { TagList, TagItem } from '@/components/Tag'
import { useCommonStore } from '@/stores/common'
import SvgIcon from '@/components/SvgIcon/index.vue'
import defaultCover from '@/assets/default-cover.jpg'
import usePageTitle from '@/hooks/usePageTitle'
export default defineComponent({
name: 'Tag',
Expand All @@ -47,9 +48,11 @@ export default defineComponent({
const commonStore = useCommonStore()
const { t } = useI18n()
const tagStore = useTagStore()
const { pageTitle, updateTitle } = usePageTitle()
const fetchData = async () => {
tagStore.fetchAllTags()
await tagStore.fetchAllTags()
updateTitle()
commonStore.setHeaderImage(defaultCover)
}
Expand All @@ -59,6 +62,7 @@ export default defineComponent({
})
return {
pageTitle,
tags: computed(() => {
if (tagStore.isLoaded && tagStore.tags.length === 0) return null
return tagStore.tags
Expand Down

0 comments on commit 51972dd

Please sign in to comment.