elk/plugins/setup-head-script.server.ts

36 lines
1,004 B
TypeScript
Raw Normal View History

import { STORAGE_KEY_CURRENT_USER_HANDLE, STORAGE_KEY_SETTINGS } from '~/constants'
import { fontSizeMap } from '~/constants/options'
/**
* Injecting scripts before renders
*/
export default defineNuxtPlugin(() => {
useHead({
script: [
{
innerHTML: `
;(function() {
2023-01-15 15:15:03 +00:00
const handle = localStorage.getItem('${STORAGE_KEY_CURRENT_USER_HANDLE}') || '[anonymous]'
const allSettings = JSON.parse(localStorage.getItem('${STORAGE_KEY_SETTINGS}') || '{}')
const settings = allSettings[handle]
if (!settings) { return }
const html = document.querySelector('html')
${process.dev ? 'console.log({ settings })' : ''}
2023-01-14 10:09:17 +00:00
if (settings.fontSize) {
const fontSizeMap = ${JSON.stringify(fontSizeMap)}
2023-01-14 10:09:17 +00:00
html.style.setProperty('--font-size', fontSizeMap[settings.fontSize])
}
2023-01-14 10:09:17 +00:00
if (settings.language) {
html.setAttribute('lang', settings.language)
}
if (settings.zenMode) {
html.classList.add('zen')
}
})()`.trim().replace(/\s*\n+\s*/g, ';'),
},
],
})
})