forked from vuepress/core
/
NavbarItems.vue
184 lines (165 loc) · 5.17 KB
/
NavbarItems.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<script setup lang="ts">
import AutoLink from '@theme/AutoLink.vue'
import NavbarDropdown from '@theme/NavbarDropdown.vue'
import { useRouteLocale, useSiteLocaleData } from '@vuepress/client'
import { isLinkHttp, isString } from '@vuepress/shared'
import { computed, ref } from 'vue'
import type { ComputedRef } from 'vue'
import { useRouter } from 'vue-router'
import type {
NavbarGroup,
NavbarItem,
ResolvedNavbarItem,
} from '../../shared/index.js'
import {
DeviceType,
useNavLink,
useThemeLocaleData,
useUpdateDeviceStatus,
} from '../composables/index.js'
import { resolveRepoType } from '../utils/index.js'
/**
* Get navbar config of select language dropdown
*/
const useNavbarSelectLanguage = (): ComputedRef<ResolvedNavbarItem[]> => {
const router = useRouter()
const routeLocale = useRouteLocale()
const siteLocale = useSiteLocaleData()
const themeLocale = useThemeLocaleData()
return computed<ResolvedNavbarItem[]>(() => {
const localePaths = Object.keys(siteLocale.value.locales)
// do not display language selection dropdown if there is only one language
if (localePaths.length < 2) {
return []
}
const currentPath = router.currentRoute.value.path
const currentFullPath = router.currentRoute.value.fullPath
const languageDropdown: ResolvedNavbarItem = {
text: themeLocale.value.selectLanguageText ?? 'unknown language',
ariaLabel:
themeLocale.value.selectLanguageAriaLabel ??
themeLocale.value.selectLanguageText ??
'unknown language',
children: localePaths.map((targetLocalePath) => {
// target locale config of this language link
const targetSiteLocale =
siteLocale.value.locales?.[targetLocalePath] ?? {}
const targetThemeLocale =
themeLocale.value.locales?.[targetLocalePath] ?? {}
const targetLang = `${targetSiteLocale.lang}`
const text = targetThemeLocale.selectLanguageName ?? targetLang
let link
if (targetLang === siteLocale.value.lang) {
// if the target language is current language
// stay at current link
link = currentFullPath
} else {
// if the target language is not current language
// try to link to the corresponding page of current page
// or fallback to homepage
const targetLocalePage = currentPath.replace(
routeLocale.value,
targetLocalePath
)
if (
router.getRoutes().some((item) => item.path === targetLocalePage)
) {
// try to keep current hash and params across languages
link = currentFullPath.replace(currentPath, targetLocalePage)
} else {
link = targetThemeLocale.home ?? targetLocalePath
}
}
return {
text,
link,
}
}),
}
return [languageDropdown]
})
}
/**
* Get navbar config of repository link
*/
const useNavbarRepo = (): ComputedRef<ResolvedNavbarItem[]> => {
const themeLocale = useThemeLocaleData()
const repo = computed(() => themeLocale.value.repo)
const repoType = computed(() =>
repo.value ? resolveRepoType(repo.value) : null
)
const repoLink = computed(() => {
if (repo.value && !isLinkHttp(repo.value)) {
return `https://github.com/${repo.value}`
}
return repo.value
})
const repoLabel = computed(() => {
if (!repoLink.value) return null
if (themeLocale.value.repoLabel) return themeLocale.value.repoLabel
if (repoType.value === null) return 'Source'
return repoType.value
})
return computed(() => {
if (!repoLink.value || !repoLabel.value) {
return []
}
return [
{
text: repoLabel.value,
link: repoLink.value,
},
]
})
}
const resolveNavbarItem = (
item: NavbarItem | NavbarGroup | string
): ResolvedNavbarItem => {
if (isString(item)) {
return useNavLink(item)
}
if ((item as NavbarGroup).children) {
return {
...item,
children: (item as NavbarGroup).children.map(resolveNavbarItem),
}
}
return item as ResolvedNavbarItem
}
const useNavbarConfig = (): ComputedRef<ResolvedNavbarItem[]> => {
const themeLocale = useThemeLocaleData()
return computed(() => (themeLocale.value.navbar || []).map(resolveNavbarItem))
}
const isMobile = ref(false)
const navbarConfig = useNavbarConfig()
const navbarSelectLanguage = useNavbarSelectLanguage()
const navbarRepo = useNavbarRepo()
const navbarLinks = computed(() => [
...navbarConfig.value,
...navbarSelectLanguage.value,
...navbarRepo.value,
])
useUpdateDeviceStatus(
DeviceType.MOBILE,
(mobileDesktopBreakpoint: number): void => {
// avoid overlapping of long title and long navbar links
if (window.innerWidth < mobileDesktopBreakpoint) {
isMobile.value = true
} else {
isMobile.value = false
}
}
)
</script>
<template>
<nav v-if="navbarLinks.length" class="navbar-items">
<div v-for="item in navbarLinks" :key="item.text" class="navbar-item">
<NavbarDropdown
v-if="item.children"
:item="item"
:class="isMobile ? 'mobile' : ''"
/>
<AutoLink v-else :item="item" />
</div>
</nav>
</template>