🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
♻️ refactor(theme): take the per-language font stack from @lobehub/ui (#21842)
Innei7134c0bba047
diff --git a/src/const/font.test.ts b/src/const/font.test.tsdeleted file mode 100644index 173d8c7fe0c..00000000000--- a/src/const/font.test.ts+++ /dev/null@@ -1,44 +0,0 @@-import { describe, expect, it } from 'vitest';--import { genFontFamily } from './font';--describe('genFontFamily', () => {- it('puts japanese families ahead of the SC fallback for ja-JP', () => {- const stack = genFontFamily({ locale: 'ja-JP' });-- expect(stack.indexOf('"Hiragino Sans"')).toBeGreaterThan(-1);- expect(stack.indexOf('"Hiragino Sans"')).toBeLessThan(stack.indexOf('"HarmonyOS Sans SC"'));- });-- it('keeps the SC stack first for zh-CN and unknown locales', () => {- for (const locale of ['zh-CN', 'de-DE', undefined]) {- const stack = genFontFamily({ locale });-- expect(stack).toContain('"HarmonyOS Sans SC"');- expect(stack).not.toContain('"PingFang TC"');- expect(stack).not.toContain('"Hiragino Sans"');- }- });-- it('prefers TC families for zh-TW', () => {- const stack = genFontFamily({ locale: 'zh-TW' });-- expect(stack.indexOf('"PingFang TC"')).toBeLessThan(stack.indexOf('"PingFang SC"'));- });-- it('puts the user font first, then the env custom font', () => {- const stack = genFontFamily({- customFontFamily: 'Env Font',- locale: 'en-US',- userFontFamily: ' LXGW WenKai ',- });-- expect(stack.startsWith('"LXGW WenKai","Env Font",Geist')).toBe(true);- });-- it('leaves an already composed font-family list untouched', () => {- expect(- genFontFamily({ customFontFamily: 'Foo, "Bar Baz"' }).startsWith('Foo, "Bar Baz",Geist'),- ).toBe(true);- });-});diff --git a/src/const/font.ts b/src/const/font.tsdeleted file mode 100644index 4563d764c0d..00000000000--- a/src/const/font.ts+++ /dev/null@@ -1,107 +0,0 @@-const FONT_EN = [- 'Geist',- '-apple-system',- 'BlinkMacSystemFont',- 'Segoe UI Variable Display',- 'Segoe UI',- 'Roboto',- 'Helvetica Neue',- 'Arial',-];--const FONT_SC = [- 'HarmonyOS Sans SC',- 'PingFang SC',- 'Hiragino Sans GB',- 'Microsoft YaHei UI',- 'Microsoft YaHei',- 'Source Han Sans SC',- 'Noto Sans CJK SC',-];--const FONT_TC = [- 'PingFang TC',- 'Hiragino Sans CNS',- 'Microsoft JhengHei UI',- 'Microsoft JhengHei',- 'Source Han Sans TC',- 'Noto Sans CJK TC',-];--const FONT_JP = [- 'Hiragino Sans',- 'Hiragino Kaku Gothic ProN',- 'Yu Gothic UI',- 'Yu Gothic',- 'Meiryo',- 'Source Han Sans JP',- 'Noto Sans CJK JP',-];--const FONT_KR = ['Apple SD Gothic Neo', 'Malgun Gothic', 'Source Han Sans KR', 'Noto Sans CJK KR'];--const FONT_CODE = [- 'Geist Mono',- 'ui-monospace',- 'SFMono-Regular',- 'SF Mono',- 'Menlo',- 'Cascadia Code',- 'Consolas',- 'Liberation Mono',-];--const FALLBACK = ['ui-sans-serif', 'system-ui', 'sans-serif'];--const FALLBACK_CODE = ['monospace'];--const FONT_EMOJI = ['Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'];--// Han glyphs differ per region, so the locale's own CJK family must win over the-// SC families that stay in the stack as the last-resort CJK fallback.-const LOCALE_CJK_FONTS: Record<string, string[]> = {- 'ja-JP': FONT_JP,- 'ko-KR': FONT_KR,- 'zh-TW': FONT_TC,-};--// user / env values may already be a full CSS font-family list, so leave those alone-const quote = (font: string) =>- font.includes(',') || font.includes('"') || !font.includes(' ') ? font : `"${font}"`;--interface GenFontFamilyParams {- customFontFamily?: string;- locale?: string;- userFontFamily?: string;-}--export const genFontFamily = ({- customFontFamily,- locale,- userFontFamily,-}: GenFontFamilyParams = {}) =>- [- userFontFamily?.trim(),- customFontFamily?.trim(),- ...FONT_EN,- ...(locale ? (LOCALE_CJK_FONTS[locale] ?? []) : []),- ...FONT_SC,- ...FALLBACK,- ...FONT_EMOJI,- ]- .filter(Boolean)- .map((font) => quote(font as string))- .join(',');--export const genFontFamilyCode = ({ locale, userFontFamily }: GenFontFamilyParams = {}) =>- [- userFontFamily?.trim(),- ...FONT_CODE,- ...(locale ? (LOCALE_CJK_FONTS[locale] ?? []) : []),- ...FONT_SC,- ...FALLBACK_CODE,- ...FONT_EMOJI,- ]- .filter(Boolean)- .map((font) => quote(font as string))- .join(',');diff --git a/src/layout/GlobalProvider/AppTheme.tsx b/src/layout/GlobalProvider/AppTheme.tsxindex f73a6ee3c88..b928ba7dd50 100644--- a/src/layout/GlobalProvider/AppTheme.tsx+++ b/src/layout/GlobalProvider/AppTheme.tsx@@ -11,7 +11,6 @@ import { type ReactNode } from 'react';import { memo, useEffect, useMemo, useState } from 'react';import Link from '@/components/Link';-import { genFontFamily, genFontFamilyCode } from '@/const/font';import { LOBE_THEME_NEUTRAL_COLOR, LOBE_THEME_PRIMARY_COLOR } from '@/const/theme';import { getUILocaleAndResources } from '@/libs/getUILocaleAndResources';import type { UILocaleResources } from '@/libs/getUILocaleAndResources.utils';@@ -23,6 +22,9 @@ import { useUserStore } from '@/store/user';import { preferenceSelectors, userGeneralSettingsSelectors } from '@/store/user/selectors';import { GlobalStyle } from '@/styles';import { setCookie } from '@/utils/client/cookie';+import { resolveLang } from '@/utils/client/switchLang';++import { resolveFontFamilyOverride } from './fontFamilyOverride';const styles = createStaticStyles(({ css, cssVar }) => ({app: css`@@ -107,22 +109,15 @@ const AppTheme = memo<AppThemeProps>(preferenceSelectors.fontFamily(s),preferenceSelectors.terminalFontFamily(s),]);- const fontFamily = useMemo(+ const { fontFamily, fontFamilyCode } = useMemo(() =>- genFontFamily({+ resolveFontFamilyOverride({customFontFamily,- locale: resolveUILocale(language).normalizedLocale,+ locale: resolveLang(language),userFontFamily,+ userFontFamilyCode,}),- [customFontFamily, language, userFontFamily],- );- const fontFamilyCode = useMemo(- () =>- genFontFamilyCode({- locale: resolveUILocale(language).normalizedLocale,- userFontFamily: userFontFamilyCode,- }),- [language, userFontFamilyCode],+ [customFontFamily, language, userFontFamily, userFontFamilyCode],);const [uiResources, setUIResources] = useState<UILocaleResources>();const [uiLocale, setUILocale] = useState(() => resolveUILocale(language).uiLocale);@@ -158,8 +153,10 @@ const AppTheme = memo<AppThemeProps>(useEffect(() => {const { style } = document.documentElement;- style.setProperty('--lobe-font-family', fontFamily);- style.setProperty('--lobe-font-family-code', fontFamilyCode);+ if (fontFamily) style.setProperty('--lobe-font-family', fontFamily);+ else style.removeProperty('--lobe-font-family');+ if (fontFamilyCode) style.setProperty('--lobe-font-family-code', fontFamilyCode);+ else style.removeProperty('--lobe-font-family-code');}, [fontFamily, fontFamilyCode]);useEffect(() => {diff --git a/src/layout/GlobalProvider/fontFamilyOverride.test.ts b/src/layout/GlobalProvider/fontFamilyOverride.test.tsnew file mode 100644index 00000000000..ba69e938bce--- /dev/null+++ b/src/layout/GlobalProvider/fontFamilyOverride.test.ts@@ -0,0 +1,38 @@+import { describe, expect, it } from 'vitest';++import { resolveFontFamilyOverride } from './fontFamilyOverride';++describe('resolveFontFamilyOverride', () => {+ it('leaves both stacks to theme.css when no custom font is set', () => {+ expect(+ resolveFontFamilyOverride({+ customFontFamily: ' ',+ locale: 'ja-JP',+ userFontFamily: '',+ userFontFamilyCode: undefined,+ }),+ ).toEqual({ fontFamily: undefined, fontFamilyCode: undefined });+ });++ it('puts the user font first, then the env custom font, ahead of the locale stack', () => {+ const { fontFamily, fontFamilyCode } = resolveFontFamilyOverride({+ customFontFamily: 'Env Font',+ locale: 'zh-CN',+ userFontFamily: ' LXGW WenKai ',+ });++ expect(fontFamily?.startsWith('"LXGW WenKai","Env Font","HarmonyOS Sans",')).toBe(true);+ expect(fontFamilyCode).toBeUndefined();+ });++ it('overrides only the code stack when only a terminal font is set', () => {+ const { fontFamily, fontFamilyCode } = resolveFontFamilyOverride({+ locale: 'ja-JP',+ userFontFamilyCode: 'JetBrains Mono',+ });++ expect(fontFamily).toBeUndefined();+ expect(fontFamilyCode?.startsWith('"JetBrains Mono","Geist Mono",')).toBe(true);+ expect(fontFamilyCode).toContain('"Hiragino Sans"');+ });+});diff --git a/src/layout/GlobalProvider/fontFamilyOverride.ts b/src/layout/GlobalProvider/fontFamilyOverride.tsnew file mode 100644index 00000000000..638ed4bc9a6--- /dev/null+++ b/src/layout/GlobalProvider/fontFamilyOverride.ts@@ -0,0 +1,25 @@+import { genFontFamily, genFontFamilyCode } from '@lobehub/ui';++interface FontFamilyOverrideParams {+ customFontFamily?: string;+ locale?: string;+ userFontFamily?: string;+ userFontFamilyCode?: string;+}++const hasFont = (font?: string) => !!font?.trim();++export const resolveFontFamilyOverride = ({+ customFontFamily,+ locale,+ userFontFamily,+ userFontFamilyCode,+}: FontFamilyOverrideParams) => ({+ fontFamily:+ hasFont(userFontFamily) || hasFont(customFontFamily)+ ? genFontFamily({ locale, prepend: [userFontFamily, customFontFamily] })+ : undefined,+ fontFamilyCode: hasFont(userFontFamilyCode)+ ? genFontFamilyCode({ locale, prepend: [userFontFamilyCode] })+ : undefined,+});