2022-12-26 15:12:04 +00:00
|
|
|
import type { Directions } from 'vue-i18n-routing'
|
|
|
|
import type { LocaleObject } from '#i18n'
|
2022-11-28 09:01:14 +00:00
|
|
|
|
2022-11-30 00:22:35 +00:00
|
|
|
export function setupPageHeader() {
|
2023-01-04 13:57:12 +00:00
|
|
|
const { locale, locales, t } = useI18n()
|
2023-01-10 20:35:34 +00:00
|
|
|
const colorMode = useColorMode()
|
2023-01-08 11:17:09 +00:00
|
|
|
const buildInfo = useBuildInfo()
|
2023-02-01 14:43:27 +00:00
|
|
|
const enablePinchToZoom = usePreferences('enablePinchToZoom')
|
2022-11-30 00:22:35 +00:00
|
|
|
|
2023-01-04 13:57:12 +00:00
|
|
|
const localeMap = (locales.value as LocaleObject[]).reduce((acc, l) => {
|
2023-02-11 20:43:21 +00:00
|
|
|
acc[l.code!] = l.dir ?? 'ltr'
|
2022-12-26 15:12:04 +00:00
|
|
|
return acc
|
|
|
|
}, {} as Record<string, Directions>)
|
|
|
|
|
2023-04-01 12:14:12 +01:00
|
|
|
useHead({
|
2022-11-29 21:13:43 +00:00
|
|
|
htmlAttrs: {
|
2023-01-04 13:57:12 +00:00
|
|
|
lang: () => locale.value,
|
2023-02-11 20:43:21 +00:00
|
|
|
dir: () => localeMap[locale.value] ?? 'ltr',
|
2023-02-01 14:43:27 +00:00
|
|
|
class: () => enablePinchToZoom.value ? ['enable-pinch-to-zoom'] : [],
|
2022-11-29 21:13:43 +00:00
|
|
|
},
|
2023-02-03 09:05:17 +00:00
|
|
|
meta: [{
|
|
|
|
name: 'viewport',
|
|
|
|
content: () => `width=device-width,initial-scale=1${enablePinchToZoom.value ? '' : ',maximum-scale=1,user-scalable=0'},viewport-fit=cover`,
|
|
|
|
}],
|
2023-01-04 13:26:30 +00:00
|
|
|
titleTemplate: (title) => {
|
2023-01-25 13:12:55 +00: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('"') ? '"' : ''}`
|
|
|
|
}
|
|
|
|
|
2023-02-14 14:42:53 +00:00
|
|
|
if (titleTemplate.length)
|
|
|
|
titleTemplate += ' | '
|
|
|
|
|
|
|
|
titleTemplate += t('app_name')
|
2023-01-04 13:26:30 +00:00
|
|
|
if (buildInfo.env !== 'release')
|
|
|
|
titleTemplate += ` (${buildInfo.env})`
|
2023-01-25 13:12:55 +00:00
|
|
|
|
2023-01-04 13:26:30 +00:00
|
|
|
return titleTemplate
|
|
|
|
},
|
2023-03-19 12:12:20 +00:00
|
|
|
link: (process.client && useAppConfig().pwaEnabled)
|
2023-01-06 12:48:43 +00:00
|
|
|
? () => [{
|
|
|
|
key: 'webmanifest',
|
|
|
|
rel: 'manifest',
|
2023-01-10 20:35:34 +00:00
|
|
|
href: `/manifest-${locale.value}${colorMode.value === 'dark' ? '-dark' : ''}.webmanifest`,
|
2023-01-06 12:48:43 +00:00
|
|
|
}]
|
|
|
|
: [],
|
2022-11-28 09:01:14 +00:00
|
|
|
})
|
|
|
|
}
|
2023-01-25 13:12:55 +00: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()
|
|
|
|
}
|