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

39 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import { STORAGE_KEY_CURRENT_USER_HANDLE, STORAGE_KEY_SETTINGS } from '~/constants'
2023-01-23 05:18:03 +09:00
import { oldFontSizeMap } from '~/constants/options'
/**
* Injecting scripts before renders
*/
export default defineNuxtPlugin(() => {
useHead({
script: [
{
innerHTML: `
;(function() {
2023-01-16 00:15:03 +09: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 }
2023-01-16 19:26:19 +09:00
const html = document.documentElement
2024-02-25 01:46:14 +09:00
${import.meta.dev ? 'console.log({ settings })' : ''}
2023-01-14 19:09:17 +09:00
if (settings.fontSize) {
2023-01-23 05:18:03 +09:00
const oldFontSizeMap = ${JSON.stringify(oldFontSizeMap)}
html.style.setProperty('--font-size', oldFontSizeMap[settings.fontSize] || settings.fontSize)
}
2023-01-14 19:09:17 +09:00
if (settings.language) {
html.setAttribute('lang', settings.language)
}
if (settings.preferences.zenMode) {
2023-01-14 19:09:17 +09:00
html.classList.add('zen')
}
2023-01-16 19:26:19 +09:00
if (settings.themeColors) {
Object.entries(settings.themeColors).map(i => html.style.setProperty(i[0], i[1]))
}
})()`.trim().replace(/\s*\n+\s*/g, ';'),
},
],
})
})