fix: reduce latency of font loading

This commit is contained in:
Steffen Becker committed 2026-05-29 23:14:24 +02:00
1 parent 239775f6e4
commit 1f09aa5c17
1 file changed
+12 -2
+12 -2
View File
@@ -71,14 +71,22 @@
try { localStorage.setItem(RECENT_FONTS_KEY, JSON.stringify(fonts)); } catch { /* quota or disabled */ }
}
onMount(() => { recentFonts = loadRecents(); });
function ensureDetectionRan() {
if (detectionRan) return;
detectionRan = true;
detectedFonts = detectAvailableFonts(CANDIDATE_FONTS);
}
onMount(() => {
recentFonts = loadRecents();
// Run detection during browser idle time after first paint so the picker
// is primed before the user ever clicks it, without delaying initial render.
const schedule = typeof requestIdleCallback === 'function'
? (cb: () => void) => requestIdleCallback(cb, { timeout: 1000 })
: (cb: () => void) => setTimeout(cb, 0);
schedule(() => ensureDetectionRan());
});
let extraFontsList = $derived.by(() => {
const source = allInstalledFonts ?? detectedFonts;
const out: string[] = [];
@@ -298,6 +306,8 @@
// Save selection before the picker button steals focus.
savedFrom = editor.state.selection.from;
savedTo = editor.state.selection.to;
// Detection normally runs on mount; this is the safety net if the user
// somehow clicks the picker before the idle callback has fired.
if (!fontOpen) ensureDetectionRan();
fontOpen = !fontOpen;
}