1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-27 14:28:10 +09:00
elk/plugins/setup-head-script.server.ts
renovate[bot] 0fba07e6e5
chore(deps): update dependency @antfu/eslint-config to ^2.19.0 (#2726)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
2024-08-16 14:52:08 +00:00

39 lines
1.1 KiB
TypeScript

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