1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-15 05:19:58 +00:00
elk/composables/setups.ts

72 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2024-02-24 16:46:14 +00:00
import type { Directions, LocaleObject } from '@nuxtjs/i18n'
2022-11-30 00:22:35 +00:00
export function setupPageHeader() {
2023-01-04 13:57:12 +00:00
const { locale, locales, t } = useI18n()
const colorMode = useColorMode()
const buildInfo = useBuildInfo()
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) => {
acc[l.code!] = l.dir ?? 'ltr'
return acc
}, {} as Record<string, Directions>)
useHydratedHead({
2022-11-29 21:13:43 +00:00
htmlAttrs: {
2023-01-04 13:57:12 +00:00
lang: () => locale.value,
dir: () => localeMap[locale.value] ?? 'ltr',
class: () => enablePinchToZoom.value ? ['enable-pinch-to-zoom'] : [],
2022-11-29 21:13:43 +00:00
},
meta: [{
name: 'viewport',
content: () => `width=device-width,initial-scale=1${enablePinchToZoom.value ? '' : ',maximum-scale=1,user-scalable=0'},viewport-fit=cover`,
}],
2023-04-16 21:35:25 +01:00
titleTemplate: (title?: string) => {
let titleTemplate = title ?? ''
if (titleTemplate.match(/&[a-z0-9#]+;/gi)) {
titleTemplate = unescapeTitleTemplate(titleTemplate, [
['"', ['&#34;', '&quot;']],
['&', ['&#38;', '&amp;']],
['\'', ['&#39;', '&apos;']],
['\u003C', ['&#60;', '&lt;']],
['\u003E', ['&#62;', '&gt;']],
])
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('"') ? '"' : ''}`
}
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-04 13:26:30 +00:00
return titleTemplate
},
2024-02-24 16:46:14 +00:00
link: (import.meta.client && useAppConfig().pwaEnabled)
? () => [{
key: 'webmanifest',
rel: 'manifest',
href: `/manifest-${locale.value}${colorMode.value === 'dark' ? '-dark' : ''}.webmanifest`,
}]
: [],
})
}
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()
}