feat(app): add font cache management hooks
This commit is contained in:
parent
768b527155
commit
dac6f171e3
|
|
@ -23,6 +23,10 @@
|
|||
"identifier": "fs:allow-mkdir",
|
||||
"allow": [{ "path": "**" }]
|
||||
},
|
||||
{
|
||||
"identifier": "fs:allow-remove",
|
||||
"allow": [{ "path": "**" }]
|
||||
},
|
||||
{
|
||||
"identifier": "fs:allow-watch",
|
||||
"allow": [{ "path": "**" }]
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ type FontCacheManifest = {
|
|||
entries: Partial<Record<string, FontCacheEntry>>
|
||||
}
|
||||
|
||||
export interface DownloadedFontCacheSummary {
|
||||
count: number
|
||||
byteLength: number
|
||||
updatedAt: number | null
|
||||
}
|
||||
|
||||
const CACHE_DIR = 'font-cache/v1'
|
||||
const MANIFEST_PATH = `${CACHE_DIR}/manifest.json`
|
||||
const EMPTY_MANIFEST: FontCacheManifest = { version: 1, entries: {} }
|
||||
|
|
@ -59,6 +65,25 @@ async function writeManifest(manifest: FontCacheManifest) {
|
|||
})
|
||||
}
|
||||
|
||||
export async function downloadedFontCacheSummary(): Promise<DownloadedFontCacheSummary> {
|
||||
const manifest = await readManifest()
|
||||
const entries = Object.values(manifest.entries).filter((entry): entry is FontCacheEntry => !!entry)
|
||||
return {
|
||||
count: entries.length,
|
||||
byteLength: entries.reduce((sum, entry) => sum + entry.byteLength, 0),
|
||||
updatedAt: entries.length > 0 ? Math.max(...entries.map((entry) => entry.updatedAt)) : null
|
||||
}
|
||||
}
|
||||
|
||||
export async function clearDownloadedFontCache(): Promise<void> {
|
||||
const { BaseDirectory, remove } = await import('@tauri-apps/plugin-fs')
|
||||
try {
|
||||
await remove(CACHE_DIR, { baseDir: BaseDirectory.AppLocalData, recursive: true })
|
||||
} catch (error) {
|
||||
console.warn('Downloaded font cache clear skipped:', error)
|
||||
}
|
||||
}
|
||||
|
||||
export function createTauriDownloadedFontCache(): DownloadedFontCache {
|
||||
return {
|
||||
async read(family, style) {
|
||||
|
|
@ -1,7 +1,11 @@
|
|||
import { IS_TAURI } from '@open-pencil/core/constants'
|
||||
import { fontManager, styleToWeight, type LocalFontAccessState } from '@open-pencil/core/text'
|
||||
|
||||
import { createTauriDownloadedFontCache } from '@/app/editor/font-cache'
|
||||
import {
|
||||
clearDownloadedFontCache as clearTauriDownloadedFontCache,
|
||||
createTauriDownloadedFontCache,
|
||||
downloadedFontCacheSummary as tauriDownloadedFontCacheSummary
|
||||
} from '@/app/editor/fonts/cache'
|
||||
|
||||
if (typeof navigator !== 'undefined') {
|
||||
fontManager.setFallbackUserAgent(navigator.userAgent)
|
||||
|
|
@ -49,6 +53,20 @@ export async function requestLocalFontAccess(): Promise<string[]> {
|
|||
return fontManager.listFamilies()
|
||||
}
|
||||
|
||||
export async function downloadedFontCacheSummary() {
|
||||
if (!IS_TAURI) return { count: 0, byteLength: 0, updatedAt: null }
|
||||
return tauriDownloadedFontCacheSummary()
|
||||
}
|
||||
|
||||
export async function clearDownloadedFontCache(): Promise<void> {
|
||||
if (!IS_TAURI) return
|
||||
await clearTauriDownloadedFontCache()
|
||||
}
|
||||
|
||||
export async function predownloadFallbackFonts() {
|
||||
return fontManager.ensureFallbackPack()
|
||||
}
|
||||
|
||||
function registerFontFaces(fonts: TauriFontFamily[]): void {
|
||||
if (typeof document === 'undefined') return
|
||||
for (const { family } of fonts) {
|
||||
Loading…
Reference in a new issue