feat(frontend): add font size, namespace UI prefs per user
Build on the font picker with three additions per user feedback:
- Font size selector (Small / Normal / Large) wired through a new
--app-font-scale CSS variable; the body font-size and every
--td-font-size-* token are wrapped in calc(... * var(--app-font-scale))
so TDesign components scale together with the body
- localStorage keys for theme, font family, mono font and font size are
now namespaced as WeKnora_${userId}_${key}; falls back to "anon" before
login. Each user keeps their own preferences in the same browser
- auth store calls reloadFontFromStorage / reloadThemeFromStorage on
setUser (when the active user changes) and in logout, so a user switch
swaps the applied preferences without a reload
https://claude.ai/code/session_01S1FVPf4jRfWXkTSnpMmMp7
This commit is contained in:
@@ -192,7 +192,7 @@ html,
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-size: 14px;
|
||||
font-size: calc(14px * var(--app-font-scale));
|
||||
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
@@ -18,8 +18,11 @@ export type MonoFontKey =
|
||||
| 'consolas'
|
||||
| 'monospace'
|
||||
|
||||
const SANS_STORAGE_KEY = 'WeKnora_font_sans'
|
||||
const MONO_STORAGE_KEY = 'WeKnora_font_mono'
|
||||
export type FontSizeKey = 'small' | 'normal' | 'large'
|
||||
|
||||
const SANS_KEY = 'font_sans'
|
||||
const MONO_KEY = 'font_mono'
|
||||
const SIZE_KEY = 'font_size'
|
||||
|
||||
export const SANS_STACKS: Record<FontKey, string> = {
|
||||
system:
|
||||
@@ -50,8 +53,15 @@ export const MONO_STACKS: Record<MonoFontKey, string> = {
|
||||
monospace: 'monospace',
|
||||
}
|
||||
|
||||
export const FONT_SCALES: Record<FontSizeKey, number> = {
|
||||
small: 0.875,
|
||||
normal: 1,
|
||||
large: 1.125,
|
||||
}
|
||||
|
||||
const DEFAULT_SANS: FontKey = 'system'
|
||||
const DEFAULT_MONO: MonoFontKey = 'system'
|
||||
const DEFAULT_SIZE: FontSizeKey = 'normal'
|
||||
|
||||
const isFontKey = (v: string | null): v is FontKey =>
|
||||
!!v && Object.prototype.hasOwnProperty.call(SANS_STACKS, v)
|
||||
@@ -59,41 +69,88 @@ const isFontKey = (v: string | null): v is FontKey =>
|
||||
const isMonoFontKey = (v: string | null): v is MonoFontKey =>
|
||||
!!v && Object.prototype.hasOwnProperty.call(MONO_STACKS, v)
|
||||
|
||||
const currentSans = ref<FontKey>(
|
||||
isFontKey(localStorage.getItem(SANS_STORAGE_KEY))
|
||||
? (localStorage.getItem(SANS_STORAGE_KEY) as FontKey)
|
||||
: DEFAULT_SANS,
|
||||
)
|
||||
const isFontSizeKey = (v: string | null): v is FontSizeKey =>
|
||||
v === 'small' || v === 'normal' || v === 'large'
|
||||
|
||||
const currentMono = ref<MonoFontKey>(
|
||||
isMonoFontKey(localStorage.getItem(MONO_STORAGE_KEY))
|
||||
? (localStorage.getItem(MONO_STORAGE_KEY) as MonoFontKey)
|
||||
: DEFAULT_MONO,
|
||||
)
|
||||
function readUserId(): string {
|
||||
try {
|
||||
const raw = localStorage.getItem('weknora_user')
|
||||
if (!raw) return 'anon'
|
||||
const parsed = JSON.parse(raw)
|
||||
return parsed?.id ? String(parsed.id) : 'anon'
|
||||
} catch {
|
||||
return 'anon'
|
||||
}
|
||||
}
|
||||
|
||||
function applyFont(sans: FontKey, mono: MonoFontKey) {
|
||||
function nsKey(suffix: string): string {
|
||||
return `WeKnora_${readUserId()}_${suffix}`
|
||||
}
|
||||
|
||||
function loadSans(): FontKey {
|
||||
const v = localStorage.getItem(nsKey(SANS_KEY))
|
||||
return isFontKey(v) ? v : DEFAULT_SANS
|
||||
}
|
||||
|
||||
function loadMono(): MonoFontKey {
|
||||
const v = localStorage.getItem(nsKey(MONO_KEY))
|
||||
return isMonoFontKey(v) ? v : DEFAULT_MONO
|
||||
}
|
||||
|
||||
function loadSize(): FontSizeKey {
|
||||
const v = localStorage.getItem(nsKey(SIZE_KEY))
|
||||
return isFontSizeKey(v) ? v : DEFAULT_SIZE
|
||||
}
|
||||
|
||||
const currentSans = ref<FontKey>(loadSans())
|
||||
const currentMono = ref<MonoFontKey>(loadMono())
|
||||
const currentSize = ref<FontSizeKey>(loadSize())
|
||||
|
||||
function applyFont() {
|
||||
const root = document.documentElement
|
||||
root.style.setProperty('--app-font-family', SANS_STACKS[sans])
|
||||
root.style.setProperty('--app-font-family-mono', MONO_STACKS[mono])
|
||||
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]))
|
||||
}
|
||||
|
||||
export function useFont() {
|
||||
function setSansFont(key: FontKey) {
|
||||
currentSans.value = key
|
||||
localStorage.setItem(SANS_STORAGE_KEY, key)
|
||||
applyFont(currentSans.value, currentMono.value)
|
||||
localStorage.setItem(nsKey(SANS_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
function setMonoFont(key: MonoFontKey) {
|
||||
currentMono.value = key
|
||||
localStorage.setItem(MONO_STORAGE_KEY, key)
|
||||
applyFont(currentSans.value, currentMono.value)
|
||||
localStorage.setItem(nsKey(MONO_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
return { currentSans, currentMono, setSansFont, setMonoFont }
|
||||
function setFontSize(key: FontSizeKey) {
|
||||
currentSize.value = key
|
||||
localStorage.setItem(nsKey(SIZE_KEY), key)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
return {
|
||||
currentSans,
|
||||
currentMono,
|
||||
currentSize,
|
||||
setSansFont,
|
||||
setMonoFont,
|
||||
setFontSize,
|
||||
}
|
||||
}
|
||||
|
||||
/** Call once in main.ts to apply persisted font preferences before mount. */
|
||||
export function initFont() {
|
||||
applyFont(currentSans.value, currentMono.value)
|
||||
applyFont()
|
||||
}
|
||||
|
||||
/** Re-read preferences from storage (call after login / logout). */
|
||||
export function reloadFontFromStorage() {
|
||||
currentSans.value = loadSans()
|
||||
currentMono.value = loadMono()
|
||||
currentSize.value = loadSize()
|
||||
applyFont()
|
||||
}
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
import { ref, watch } from 'vue'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export type ThemeMode = 'light' | 'dark' | 'system'
|
||||
|
||||
const STORAGE_KEY = 'WeKnora_theme'
|
||||
const STORAGE_SUFFIX = 'theme'
|
||||
|
||||
function readUserId(): string {
|
||||
try {
|
||||
const raw = localStorage.getItem('weknora_user')
|
||||
if (!raw) return 'anon'
|
||||
const parsed = JSON.parse(raw)
|
||||
return parsed?.id ? String(parsed.id) : 'anon'
|
||||
} catch {
|
||||
return 'anon'
|
||||
}
|
||||
}
|
||||
|
||||
function nsKey(): string {
|
||||
return `WeKnora_${readUserId()}_${STORAGE_SUFFIX}`
|
||||
}
|
||||
|
||||
function loadTheme(): ThemeMode {
|
||||
const v = localStorage.getItem(nsKey())
|
||||
if (v === 'light' || v === 'dark' || v === 'system') return v
|
||||
return 'light'
|
||||
}
|
||||
|
||||
// Shared reactive state across all consumers
|
||||
const currentTheme = ref<ThemeMode>(
|
||||
(localStorage.getItem(STORAGE_KEY) as ThemeMode) || 'light'
|
||||
)
|
||||
const currentTheme = ref<ThemeMode>(loadTheme())
|
||||
|
||||
function getSystemTheme(): 'light' | 'dark' {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
|
||||
@@ -58,7 +77,7 @@ function applyTheme(mode: ThemeMode) {
|
||||
export function useTheme() {
|
||||
function setTheme(mode: ThemeMode) {
|
||||
currentTheme.value = mode
|
||||
localStorage.setItem(STORAGE_KEY, mode)
|
||||
localStorage.setItem(nsKey(), mode)
|
||||
applyTheme(mode)
|
||||
}
|
||||
|
||||
@@ -67,9 +86,8 @@ export function useTheme() {
|
||||
|
||||
/** Call once in main.ts to initialise theme and listen for OS changes. */
|
||||
export function initTheme() {
|
||||
const saved = (localStorage.getItem(STORAGE_KEY) as ThemeMode) || 'light'
|
||||
currentTheme.value = saved
|
||||
applyTheme(saved)
|
||||
currentTheme.value = loadTheme()
|
||||
applyTheme(currentTheme.value)
|
||||
|
||||
// React to OS theme changes when user chose "system"
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
||||
@@ -78,3 +96,9 @@ export function initTheme() {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Re-read preferences from storage (call after login / logout). */
|
||||
export function reloadThemeFromStorage() {
|
||||
currentTheme.value = loadTheme()
|
||||
applyTheme(currentTheme.value)
|
||||
}
|
||||
|
||||
@@ -2586,6 +2586,13 @@ export default {
|
||||
consolas: 'Consolas',
|
||||
monospace: 'Generic Monospace',
|
||||
},
|
||||
fontSize: 'Font Size',
|
||||
fontSizeDescription: 'Adjust the overall interface text size',
|
||||
size: {
|
||||
small: 'Small',
|
||||
normal: 'Normal',
|
||||
large: 'Large',
|
||||
},
|
||||
},
|
||||
platform: {
|
||||
subtitle: 'Enterprise-level Intelligent Document Retrieval Framework',
|
||||
|
||||
@@ -1956,6 +1956,13 @@ export default {
|
||||
consolas: "Consolas",
|
||||
monospace: "일반 고정폭",
|
||||
},
|
||||
fontSize: "글꼴 크기",
|
||||
fontSizeDescription: "전체 인터페이스 텍스트 크기를 조정합니다",
|
||||
size: {
|
||||
small: "작게",
|
||||
normal: "보통",
|
||||
large: "크게",
|
||||
},
|
||||
},
|
||||
platform: {
|
||||
subtitle: "엔터프라이즈급 지능형 문서 검색 프레임워크",
|
||||
|
||||
@@ -2393,6 +2393,13 @@ export default {
|
||||
consolas: 'Consolas',
|
||||
monospace: 'Стандартный моноширинный',
|
||||
},
|
||||
fontSize: 'Размер шрифта',
|
||||
fontSizeDescription: 'Регулирует общий размер текста в интерфейсе',
|
||||
size: {
|
||||
small: 'Маленький',
|
||||
normal: 'Обычный',
|
||||
large: 'Крупный',
|
||||
},
|
||||
},
|
||||
platform: {
|
||||
subtitle: 'Корпоративная платформа интеллектуального поиска документов',
|
||||
|
||||
@@ -1934,6 +1934,13 @@ export default {
|
||||
consolas: "Consolas",
|
||||
monospace: "通用等宽",
|
||||
},
|
||||
fontSize: "字体大小",
|
||||
fontSizeDescription: "调整整体界面文字大小",
|
||||
size: {
|
||||
small: "小",
|
||||
normal: "正常",
|
||||
large: "大",
|
||||
},
|
||||
},
|
||||
platform: {
|
||||
subtitle: "企业级智能文档检索框架",
|
||||
|
||||
@@ -3,6 +3,15 @@ import { ref, computed } from 'vue'
|
||||
import type { UserInfo, TenantInfo, KnowledgeBaseInfo } from '@/api/auth'
|
||||
import type { TenantInfo as TenantInfoFromAPI } from '@/api/tenant'
|
||||
import i18n from '@/i18n'
|
||||
import { reloadFontFromStorage } from '@/composables/useFont'
|
||||
import { reloadThemeFromStorage } from '@/composables/useTheme'
|
||||
|
||||
// Per-user UI preferences are namespaced by user id in localStorage.
|
||||
// Reload them whenever the active user changes.
|
||||
function reloadUserPreferences() {
|
||||
reloadFontFromStorage()
|
||||
reloadThemeFromStorage()
|
||||
}
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
// 状态
|
||||
@@ -45,9 +54,13 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
|
||||
// 操作方法
|
||||
const setUser = (userData: UserInfo) => {
|
||||
const previousId = user.value?.id
|
||||
user.value = userData
|
||||
// 保存到localStorage
|
||||
localStorage.setItem('weknora_user', JSON.stringify(userData))
|
||||
if (previousId !== userData.id) {
|
||||
reloadUserPreferences()
|
||||
}
|
||||
}
|
||||
|
||||
const setTenant = (tenantData: TenantInfo) => {
|
||||
@@ -140,6 +153,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
reloadUserPreferences()
|
||||
}
|
||||
|
||||
const initFromStorage = () => {
|
||||
|
||||
@@ -97,6 +97,25 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 字体大小 -->
|
||||
<div class="setting-row">
|
||||
<div class="setting-info">
|
||||
<label>{{ $t('font.fontSize') }}</label>
|
||||
<p class="desc">{{ $t('font.fontSizeDescription') }}</p>
|
||||
</div>
|
||||
<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>
|
||||
<t-radio-button value="normal">{{ $t('font.size.normal') }}</t-radio-button>
|
||||
<t-radio-button value="large">{{ $t('font.size.large') }}</t-radio-button>
|
||||
</t-radio-group>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 记忆功能开关 -->
|
||||
<div class="setting-row">
|
||||
<div class="setting-info">
|
||||
@@ -154,19 +173,28 @@ import {
|
||||
MONO_STACKS,
|
||||
type FontKey,
|
||||
type MonoFontKey,
|
||||
type FontSizeKey,
|
||||
} from '@/composables/useFont'
|
||||
|
||||
const { t, locale } = useI18n()
|
||||
const settingsStore = useSettingsStore()
|
||||
const authStore = useAuthStore()
|
||||
const { currentTheme, setTheme } = useTheme()
|
||||
const { currentSans, currentMono, setSansFont, setMonoFont } = useFont()
|
||||
const {
|
||||
currentSans,
|
||||
currentMono,
|
||||
currentSize,
|
||||
setSansFont,
|
||||
setMonoFont,
|
||||
setFontSize,
|
||||
} = useFont()
|
||||
|
||||
// 本地状态
|
||||
const localLanguage = ref('zh-CN')
|
||||
const localTheme = ref<ThemeMode>(currentTheme.value)
|
||||
const localSansFont = ref<FontKey>(currentSans.value)
|
||||
const localMonoFont = ref<MonoFontKey>(currentMono.value)
|
||||
const localFontSize = ref<FontSizeKey>(currentSize.value)
|
||||
|
||||
const sansFontOptions = computed<{ value: FontKey; label: string; preview: string }[]>(() => [
|
||||
{ value: 'system', label: t('font.sans.system'), preview: SANS_STACKS.system },
|
||||
@@ -273,6 +301,11 @@ const handleMonoFontChange = (val: MonoFontKey) => {
|
||||
setMonoFont(val)
|
||||
MessagePlugin.success(t('common.success'))
|
||||
}
|
||||
|
||||
const handleFontSizeChange = (val: FontSizeKey) => {
|
||||
setFontSize(val)
|
||||
MessagePlugin.success(t('common.success'))
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
|
||||
Reference in New Issue
Block a user