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