feat(font): 添加本地字体选择功能(not done) #96

This commit is contained in:
log1997
2025-12-10 17:54:59 +08:00
parent 9dece20aa8
commit 3cd7788517
6 changed files with 161 additions and 43 deletions

View File

@@ -0,0 +1,51 @@
import { onMounted, ref } from 'vue'
export function useLocalFonts() {
const fonts = ref<Map<string, { name: string, value: string }[]>>(new Map())
const disabled = ref(false)
const formatFonts = (list: FontData[]): Map<string, { name: string, value: string }[]> => {
const res: Map<string, { name: string, value: string }[]> = new Map()
for (const item of list) {
if (!res.has(item.family)) {
res.set(item.family, [])
}
const fontArray = res.get(item.family)
if (!Array.isArray(fontArray)) {
continue
}
if (item.family === item.fullName) {
fontArray.push({ name: item.style, value: item.fullName })
continue
}
const name = item.fullName.replace(item.family, '').trim()
const value = item.fullName
fontArray.push({ name, value })
}
return res
}
const getFonts = async () => {
if (!window.queryLocalFonts) {
return
}
const list = await window.queryLocalFonts()
console.log('list', list)
const res = formatFonts(list)
console.log('fontlist', res)
fonts.value = res
// 对比family相同则移入第一个元素的children里面
}
onMounted(() => {
if (!window.queryLocalFonts) {
disabled.value = true
}
})
return {
disabled,
fonts,
getFonts,
}
}