update at 2026-02-11 18:37:30
This commit is contained in:
@@ -13,20 +13,43 @@ interface FontListItem {
|
||||
export function useFontLoader() {
|
||||
const fontStore = useFontStore()
|
||||
|
||||
async function fetchFontListWithFallback(): Promise<FontListItem[]> {
|
||||
const candidates = ['/fonts.json', '/frontend/dist/fonts.json']
|
||||
const errors: string[] = []
|
||||
|
||||
for (const url of candidates) {
|
||||
const requestUrl = `${url}?_ts=${Date.now()}`
|
||||
try {
|
||||
console.log(`Fetching ${requestUrl}...`)
|
||||
const response = await fetch(requestUrl, { cache: 'no-store' })
|
||||
console.log(`${url} response:`, response.status, response.statusText)
|
||||
|
||||
if (!response.ok) {
|
||||
errors.push(`${url}: HTTP ${response.status}`)
|
||||
continue
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
if (!Array.isArray(data)) {
|
||||
errors.push(`${url}: JSON 不是数组`)
|
||||
continue
|
||||
}
|
||||
|
||||
return data as FontListItem[]
|
||||
} catch (error) {
|
||||
errors.push(`${url}: ${error instanceof Error ? error.message : String(error)}`)
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(errors.join(' | '))
|
||||
}
|
||||
|
||||
async function loadFontList() {
|
||||
console.log('Starting to load font list...')
|
||||
fontStore.isLoadingFonts = true
|
||||
|
||||
try {
|
||||
console.log('Fetching /fonts.json...')
|
||||
const response = await fetch('/fonts.json')
|
||||
console.log('Response status:', response.status, response.statusText)
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load fonts.json: ${response.statusText}`)
|
||||
}
|
||||
|
||||
const fontList: FontListItem[] = await response.json()
|
||||
const fontList = await fetchFontListWithFallback()
|
||||
console.log('Loaded font list:', fontList.length, 'fonts')
|
||||
|
||||
// 转换为 FontInfo
|
||||
@@ -50,7 +73,7 @@ export function useFontLoader() {
|
||||
console.log(`Successfully loaded ${fontList.length} fonts`)
|
||||
} catch (error) {
|
||||
console.error('Failed to load font list:', error)
|
||||
alert('加载字体列表失败,请刷新页面重试')
|
||||
alert(`加载字体列表失败:${error instanceof Error ? error.message : '未知错误'}`)
|
||||
} finally {
|
||||
fontStore.isLoadingFonts = false
|
||||
console.log('Font loading finished')
|
||||
|
||||
Reference in New Issue
Block a user