2022-12-28 01:06:54 +00:00
|
|
|
import type { FontSize } from '~/types'
|
|
|
|
import { InjectionKeyFontSize } from '~/constants/symbols'
|
2022-12-29 20:08:50 +00:00
|
|
|
import { COOKIE_KEY_FONT_SIZE, COOKIE_MAX_AGE, DEFAULT_FONT_SIZE } from '~/constants'
|
2022-12-28 01:06:54 +00:00
|
|
|
import { fontSizeMap } from '~/constants/options'
|
|
|
|
|
|
|
|
export default defineNuxtPlugin((nuxt) => {
|
2022-12-29 20:08:50 +00:00
|
|
|
const cookieFontSize = useCookie<FontSize>(COOKIE_KEY_FONT_SIZE, { default: () => DEFAULT_FONT_SIZE, maxAge: COOKIE_MAX_AGE })
|
2022-12-28 01:06:54 +00:00
|
|
|
nuxt.vueApp.provide(InjectionKeyFontSize, cookieFontSize)
|
|
|
|
|
|
|
|
if (!process.server) {
|
|
|
|
watchEffect(() => {
|
2022-12-29 20:08:50 +00:00
|
|
|
document.documentElement.style.setProperty('--font-size', fontSizeMap[cookieFontSize.value || DEFAULT_FONT_SIZE])
|
2022-12-28 01:06:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
useHead({
|
|
|
|
style: [
|
|
|
|
{
|
2022-12-29 20:08:50 +00:00
|
|
|
innerHTML: `:root { --font-size: ${fontSizeMap[cookieFontSize.value || DEFAULT_FONT_SIZE]}; }`,
|
2022-12-28 01:06:54 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|