Files
RawMark/web/src/common/appearance/fonts.ts
T
eezd 6b537c03e0 refactor(web): align resource APIs and forms with the starter conventions
Centralize typed resource functions under common/apis and return complete JSON envelopes while preserving raw responses, bearer session isolation, and single-owner errors.

Keep settings state and queues inside the setup store. Use Element Plus forms and dialogs for login, settings, imports, moves, and images; preserve editor interactions and themes. Update callers, tests, generated declarations, and request import constraints together.
2026-10-26 11:53:37 +08:00

29 lines
1.3 KiB
TypeScript

import type { CustomFont } from '@@/apis/fonts/type'
import { getFontsApi } from '@@/apis/fonts'
import { fontResourceRoot, prepareResources } from '@@/utils/resource-tickets'
import { shallowRef } from 'vue'
export const customFonts = shallowRef<CustomFont[]>([])
export function applyFontCatalog(fonts: CustomFont[]) {
customFonts.value = fonts
let style = document.getElementById('rm-custom-fonts')
if (!style) {
style = document.createElement('style'); style.id = 'rm-custom-fonts'; document.head.append(style)
}
// 名称仅用于界面;CSS 标识和资源路径只由服务端内容哈希构成。
const css = fonts.map(font => `@font-face { font-family: '${font.id}'; src: url('${fontResourceRoot()}${font.id}/file'); font-display: block; }`).join('\n')
if (style.textContent !== css) style.textContent = css
}
export function parseFontCatalog(value: unknown): CustomFont[] {
if (!Array.isArray(value) || value.some(font => !font || typeof font !== 'object' || !/^custom-[a-f0-9]{64}$/.test(font.id) || typeof font.name !== 'string' || !['ttf', 'otf', 'woff', 'woff2'].includes(font.format))) throw new Error('字体列表格式无效')
return value
}
export async function loadFonts() {
await prepareResources()
applyFontCatalog(parseFontCatalog((await getFontsApi()).data.fonts))
}