2022-12-27 00:12:04 +09:00
|
|
|
import type { Directions } from 'vue-i18n-routing'
|
|
|
|
import type { LocaleObject } from '#i18n'
|
2022-11-28 18:01:14 +09:00
|
|
|
|
2022-11-30 09:22:35 +09:00
|
|
|
export function setupPageHeader() {
|
2023-01-04 22:57:12 +09:00
|
|
|
const { locale, locales, t } = useI18n()
|
2023-01-11 05:35:34 +09:00
|
|
|
const colorMode = useColorMode()
|
2023-01-08 20:17:09 +09:00
|
|
|
const buildInfo = useBuildInfo()
|
2022-11-30 09:22:35 +09:00
|
|
|
|
2023-01-04 22:57:12 +09:00
|
|
|
const localeMap = (locales.value as LocaleObject[]).reduce((acc, l) => {
|
2022-12-28 06:04:52 +09:00
|
|
|
acc[l.code!] = l.dir ?? 'auto'
|
2022-12-27 00:12:04 +09:00
|
|
|
return acc
|
|
|
|
}, {} as Record<string, Directions>)
|
|
|
|
|
2022-11-30 07:49:25 +09:00
|
|
|
useHeadFixed({
|
2022-11-30 06:13:43 +09:00
|
|
|
htmlAttrs: {
|
2023-01-04 22:57:12 +09:00
|
|
|
lang: () => locale.value,
|
|
|
|
dir: () => localeMap[locale.value] ?? 'auto',
|
2022-11-30 06:13:43 +09:00
|
|
|
},
|
2023-01-04 22:26:30 +09:00
|
|
|
titleTemplate: (title) => {
|
2023-01-25 22:12:55 +09:00
|
|
|
let titleTemplate = title ?? ''
|
|
|
|
|
|
|
|
if (titleTemplate.match(/&[a-z0-9#]+;/gi)) {
|
|
|
|
titleTemplate = unescapeTitleTemplate(titleTemplate, [
|
|
|
|
['"', ['"', '"']],
|
|
|
|
['&', ['&', '&']],
|
|
|
|
['\'', [''', ''']],
|
|
|
|
['\u003C', ['<', '<']],
|
|
|
|
['\u003E', ['>', '>']],
|
|
|
|
])
|
|
|
|
if (titleTemplate.length > 60)
|
|
|
|
titleTemplate = `${titleTemplate.slice(0, 60)}...${titleTemplate.endsWith('"') ? '"' : ''}`
|
|
|
|
|
|
|
|
if (!titleTemplate.includes('"'))
|
|
|
|
titleTemplate = `"${titleTemplate}"`
|
|
|
|
}
|
|
|
|
else if (titleTemplate.length > 60) {
|
|
|
|
titleTemplate = `${titleTemplate.slice(0, 60)}...${titleTemplate.endsWith('"') ? '"' : ''}`
|
|
|
|
}
|
|
|
|
|
|
|
|
titleTemplate += ` | ${t('app_name')}`
|
2023-01-04 22:26:30 +09:00
|
|
|
if (buildInfo.env !== 'release')
|
|
|
|
titleTemplate += ` (${buildInfo.env})`
|
2023-01-25 22:12:55 +09:00
|
|
|
|
2023-01-04 22:26:30 +09:00
|
|
|
return titleTemplate
|
|
|
|
},
|
2023-01-29 23:18:27 +09:00
|
|
|
link: process.client && useAppConfig().pwaEnabled
|
2023-01-06 21:48:43 +09:00
|
|
|
? () => [{
|
|
|
|
key: 'webmanifest',
|
|
|
|
rel: 'manifest',
|
2023-01-11 05:35:34 +09:00
|
|
|
href: `/manifest-${locale.value}${colorMode.value === 'dark' ? '-dark' : ''}.webmanifest`,
|
2023-01-06 21:48:43 +09:00
|
|
|
}]
|
|
|
|
: [],
|
2022-11-28 18:01:14 +09:00
|
|
|
})
|
|
|
|
}
|
2023-01-25 22:12:55 +09:00
|
|
|
|
|
|
|
function unescapeTitleTemplate(titleTemplate: string, replacements: [string, string[]][]) {
|
|
|
|
let result = titleTemplate
|
|
|
|
for (const [replacement, entities] of replacements) {
|
|
|
|
for (const e of entities)
|
|
|
|
result = result.replaceAll(e, replacement)
|
|
|
|
}
|
|
|
|
return result.trim()
|
|
|
|
}
|