2023-01-12 20:52:52 +03:00
|
|
|
import type { FontSize } from '~/composables/settings'
|
2022-12-29 23:08:50 +03:00
|
|
|
import { COOKIE_KEY_FONT_SIZE, COOKIE_MAX_AGE, DEFAULT_FONT_SIZE } from '~/constants'
|
2022-12-28 04:06:54 +03:00
|
|
|
import { fontSizeMap } from '~/constants/options'
|
|
|
|
|
2023-01-12 20:52:52 +03:00
|
|
|
export default defineNuxtPlugin(() => {
|
|
|
|
const userSettings = useUserSettings()
|
2022-12-29 23:08:50 +03:00
|
|
|
const cookieFontSize = useCookie<FontSize>(COOKIE_KEY_FONT_SIZE, { default: () => DEFAULT_FONT_SIZE, maxAge: COOKIE_MAX_AGE })
|
2023-01-12 20:52:52 +03:00
|
|
|
if (!cookieFontSize.value || !fontSizeMap[cookieFontSize.value])
|
|
|
|
cookieFontSize.value = DEFAULT_FONT_SIZE
|
2022-12-28 04:06:54 +03:00
|
|
|
|
2023-01-12 20:52:52 +03:00
|
|
|
if (process.server) {
|
|
|
|
userSettings.value.fontSize = cookieFontSize.value
|
2022-12-28 04:06:54 +03:00
|
|
|
useHead({
|
|
|
|
style: [
|
2023-01-12 20:52:52 +03:00
|
|
|
{ innerHTML: `:root { --font-size: ${fontSizeMap[cookieFontSize.value]}; }` },
|
2022-12-28 04:06:54 +03:00
|
|
|
],
|
|
|
|
})
|
2023-01-12 20:52:52 +03:00
|
|
|
return
|
2022-12-28 04:06:54 +03:00
|
|
|
}
|
2023-01-12 20:52:52 +03:00
|
|
|
|
|
|
|
userSettings.value.fontSize = cookieFontSize.value
|
|
|
|
watch(() => userSettings.value.fontSize, (size) => {
|
|
|
|
document.documentElement.style.setProperty('--font-size', fontSizeMap[size])
|
|
|
|
cookieFontSize.value = size
|
|
|
|
}, { immediate: true })
|
2022-12-28 04:06:54 +03:00
|
|
|
})
|