fix(frontend): harden font/theme preferences for production edge cases

Edge-case review identified several issues that would degrade UX or
silently break under unusual conditions:

- Anon settings chosen on the login screen were lost on first login
  because the storage namespace switched from "anon" to the user id;
  resolveStorageValue now falls through user -> anon -> legacy, so
  pre-login choices carry over and v1 unscoped keys are still picked up.
- localStorage.setItem can throw under quota / private mode; setters now
  swallow the error so the UI still updates for the session.
- applyFont reduced to String(undefined) when currentSize held an
  invalid value; runtime fallbacks to the default scale guard against
  manipulated storage.
- Added var(--app-font-scale, 1) defaults to all 32 calc() expressions
  in theme.css and the body font-size, so a missing variable cannot
  void the calc() value.
- Validation guards in setSansFont / setMonoFont / setFontSize / setTheme
  reject invalid keys instead of writing them to storage.
- GeneralSettings.vue now watches the composable refs so the form stays
  in sync if preferences change externally (e.g. user switch while on
  the page).
- Dropped the unverified t-radio-group variant prop in favour of the
  default style.

https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
This commit is contained in:
Claude
2026-05-02 08:36:03 +00:00
committed by lyingbug
parent bafcbd5e5f
commit 2e0b423057
5 changed files with 97 additions and 18 deletions
+1 -1
View File
@@ -192,7 +192,7 @@ html,
height: 100%;
margin: 0;
padding: 0;
font-size: calc(14px * var(--app-font-scale));
font-size: calc(14px * var(--app-font-scale, 1));
font-family: var(--app-font-family);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
File diff suppressed because one or more lines are too long
+49 -9
View File
@@ -83,22 +83,55 @@ function readUserId(): string {
}
}
function safeGetItem(key: string): string | null {
try {
return localStorage.getItem(key)
} catch {
return null
}
}
function safeSetItem(key: string, value: string): void {
try {
localStorage.setItem(key, value)
} catch {
// Quota / disabled storage / private mode — preference applies to this session only.
}
}
/**
* Look up a stored preference, falling back through:
* 1. The current user's namespace
* 2. The "anon" namespace (settings chosen on the login screen)
* 3. A legacy un-namespaced key (from earlier versions of this branch)
*/
function resolveStorageValue(suffix: string): string | null {
const userId = readUserId()
const userValue = safeGetItem(`WeKnora_${userId}_${suffix}`)
if (userValue !== null) return userValue
if (userId !== 'anon') {
const anonValue = safeGetItem(`WeKnora_anon_${suffix}`)
if (anonValue !== null) return anonValue
}
return safeGetItem(`WeKnora_${suffix}`)
}
function nsKey(suffix: string): string {
return `WeKnora_${readUserId()}_${suffix}`
}
function loadSans(): FontKey {
const v = localStorage.getItem(nsKey(SANS_KEY))
const v = resolveStorageValue(SANS_KEY)
return isFontKey(v) ? v : DEFAULT_SANS
}
function loadMono(): MonoFontKey {
const v = localStorage.getItem(nsKey(MONO_KEY))
const v = resolveStorageValue(MONO_KEY)
return isMonoFontKey(v) ? v : DEFAULT_MONO
}
function loadSize(): FontSizeKey {
const v = localStorage.getItem(nsKey(SIZE_KEY))
const v = resolveStorageValue(SIZE_KEY)
return isFontSizeKey(v) ? v : DEFAULT_SIZE
}
@@ -108,27 +141,34 @@ const currentSize = ref<FontSizeKey>(loadSize())
function applyFont() {
const root = document.documentElement
root.style.setProperty('--app-font-family', SANS_STACKS[currentSans.value])
root.style.setProperty('--app-font-family-mono', MONO_STACKS[currentMono.value])
root.style.setProperty('--app-font-scale', String(FONT_SCALES[currentSize.value]))
if (!root) return
const sansStack = SANS_STACKS[currentSans.value] ?? SANS_STACKS[DEFAULT_SANS]
const monoStack = MONO_STACKS[currentMono.value] ?? MONO_STACKS[DEFAULT_MONO]
const scale = FONT_SCALES[currentSize.value] ?? FONT_SCALES[DEFAULT_SIZE]
root.style.setProperty('--app-font-family', sansStack)
root.style.setProperty('--app-font-family-mono', monoStack)
root.style.setProperty('--app-font-scale', String(scale))
}
export function useFont() {
function setSansFont(key: FontKey) {
if (!isFontKey(key)) return
currentSans.value = key
localStorage.setItem(nsKey(SANS_KEY), key)
safeSetItem(nsKey(SANS_KEY), key)
applyFont()
}
function setMonoFont(key: MonoFontKey) {
if (!isMonoFontKey(key)) return
currentMono.value = key
localStorage.setItem(nsKey(MONO_KEY), key)
safeSetItem(nsKey(MONO_KEY), key)
applyFont()
}
function setFontSize(key: FontSizeKey) {
if (!isFontSizeKey(key)) return
currentSize.value = key
localStorage.setItem(nsKey(SIZE_KEY), key)
safeSetItem(nsKey(SIZE_KEY), key)
applyFont()
}
+38 -4
View File
@@ -2,7 +2,7 @@ import { ref } from 'vue'
export type ThemeMode = 'light' | 'dark' | 'system'
const STORAGE_SUFFIX = 'theme'
const THEME_KEY = 'theme'
function readUserId(): string {
try {
@@ -15,12 +15,45 @@ function readUserId(): string {
}
}
function safeGetItem(key: string): string | null {
try {
return localStorage.getItem(key)
} catch {
return null
}
}
function safeSetItem(key: string, value: string): void {
try {
localStorage.setItem(key, value)
} catch {
// Quota / disabled storage / private mode — preference applies to this session only.
}
}
/**
* Look up a stored preference, falling back through:
* 1. The current user's namespace
* 2. The "anon" namespace (settings chosen on the login screen)
* 3. A legacy un-namespaced key (from earlier versions of this branch)
*/
function resolveStorageValue(suffix: string): string | null {
const userId = readUserId()
const userValue = safeGetItem(`WeKnora_${userId}_${suffix}`)
if (userValue !== null) return userValue
if (userId !== 'anon') {
const anonValue = safeGetItem(`WeKnora_anon_${suffix}`)
if (anonValue !== null) return anonValue
}
return safeGetItem(`WeKnora_${suffix}`)
}
function nsKey(): string {
return `WeKnora_${readUserId()}_${STORAGE_SUFFIX}`
return `WeKnora_${readUserId()}_${THEME_KEY}`
}
function loadTheme(): ThemeMode {
const v = localStorage.getItem(nsKey())
const v = resolveStorageValue(THEME_KEY)
if (v === 'light' || v === 'dark' || v === 'system') return v
return 'light'
}
@@ -76,8 +109,9 @@ function applyTheme(mode: ThemeMode) {
export function useTheme() {
function setTheme(mode: ThemeMode) {
if (mode !== 'light' && mode !== 'dark' && mode !== 'system') return
currentTheme.value = mode
localStorage.setItem(nsKey(), mode)
safeSetItem(nsKey(), mode)
applyTheme(mode)
}
@@ -106,7 +106,6 @@
<div class="setting-control">
<t-radio-group
v-model="localFontSize"
variant="default-filled"
@change="handleFontSizeChange"
>
<t-radio-button value="small">{{ $t('font.size.small') }}</t-radio-button>
@@ -160,7 +159,7 @@
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { ref, onMounted, computed, watch } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { useI18n } from 'vue-i18n'
import { useSettingsStore } from '@/stores/settings'
@@ -196,6 +195,12 @@ const localSansFont = ref<FontKey>(currentSans.value)
const localMonoFont = ref<MonoFontKey>(currentMono.value)
const localFontSize = ref<FontSizeKey>(currentSize.value)
// Keep the form in sync if preferences change externally (e.g. on user switch).
watch(currentTheme, (val) => { localTheme.value = val })
watch(currentSans, (val) => { localSansFont.value = val })
watch(currentMono, (val) => { localMonoFont.value = val })
watch(currentSize, (val) => { localFontSize.value = val })
const sansFontOptions = computed<{ value: FontKey; label: string; preview: string }[]>(() => [
{ value: 'system', label: t('font.sans.system'), preview: SANS_STACKS.system },
{ value: 'pingfang', label: t('font.sans.pingfang'), preview: SANS_STACKS.pingfang },