From 436e306cbc8178bcd9e967a22d403252a8695d1d Mon Sep 17 00:00:00 2001 From: Lhc_fl Date: Fri, 6 Dec 2024 01:42:05 +0800 Subject: [PATCH] fix font importer --- packages/frontend/src/boot/common.ts | 10 +++------- packages/frontend/src/scripts/font-settings.ts | 14 ++------------ packages/frontend/src/scripts/load-font.ts | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 packages/frontend/src/scripts/load-font.ts diff --git a/packages/frontend/src/boot/common.ts b/packages/frontend/src/boot/common.ts index 8bdc1e0b7..4b3dbdfdf 100644 --- a/packages/frontend/src/boot/common.ts +++ b/packages/frontend/src/boot/common.ts @@ -24,6 +24,7 @@ import { miLocalStorage } from '@/local-storage.js'; import { fetchCustomEmojis } from '@/custom-emojis.js'; import { setupRouter } from '@/router/main.js'; import { createMainRouter } from '@/router/definition.js'; +import { loadFontStyle } from '@/scripts/load-font.js'; export async function common(createVue: () => App) { console.info(`Misskey v${version}`); @@ -234,13 +235,8 @@ export async function common(createVue: () => App) { //#region Load default font const def_arr = miLocalStorage.getItem('defaultFontFace')?.split('_'); const fontId = def_arr?.[0]; - if (fontId && fontId !== 'system-ui') { - try { - await import(`@/styles-font/${fontId}.scss`); - document.documentElement.classList.add(`default-font-${def_arr.join('_')}`); - } catch (e) { - console.warn(`Failed to load font style: ${fontId}`, e); - } + if (fontId) { + loadFontStyle(fontId); } //#endregion diff --git a/packages/frontend/src/scripts/font-settings.ts b/packages/frontend/src/scripts/font-settings.ts index ece98bb7a..34881b513 100644 --- a/packages/frontend/src/scripts/font-settings.ts +++ b/packages/frontend/src/scripts/font-settings.ts @@ -1,4 +1,5 @@ import { computed, ref, watch } from 'vue'; +import { loadFontStyle } from './load-font.js'; import type { Ref } from 'vue'; import { miLocalStorage } from '@/local-storage.js'; import { i18n } from '@/i18n.js'; @@ -52,14 +53,6 @@ function getFontId(name: string, option: string) { } } -async function loadFontStyle(fontId: string) { - try { - await import(`@/styles-font/${fontId}.scss`); - } catch (e) { - console.warn(`Failed to load font style: ${fontId}`, e); - } -} - export function getDefaultFontSettings() { const def_arr = miLocalStorage.getItem('defaultFontFace')?.split('_'); const fontFace = ref(def_arr?.[0] ?? 'system-ui'); @@ -76,10 +69,7 @@ export function getDefaultFontSettings() { miLocalStorage.setItem('defaultFontFace', newFontId); document.documentElement.classList.add(`default-font-${newFontId}`); - if (fontFace.value !== 'system-ui') { - await loadFontStyle(fontFace.value); - } - console.log(newFontId); + await loadFontStyle(fontFace.value); } watch(fontFace, (newVal) => { diff --git a/packages/frontend/src/scripts/load-font.ts b/packages/frontend/src/scripts/load-font.ts new file mode 100644 index 000000000..2f72878d4 --- /dev/null +++ b/packages/frontend/src/scripts/load-font.ts @@ -0,0 +1,16 @@ +const defaultFontsList = [ + 'roboto', + 'misskey-biz', + 'arial', + 'times', + 'system-ui', +]; + +export async function loadFontStyle(fontId: string) { + if (defaultFontsList.includes(fontId)) return; + try { + await import(`@/styles-font/${fontId}.scss`); + } catch (err) { + console.warn(`Failed to load font style: ${fontId}`, err); + } +}