// fixtures/fonts-gate.mjs — shows a page's slides once their families // are on and their layout is final (fontgate.go; Docs' fonts-gate.mjs, // adopted). Built into internal/slides/static/fontsgate..js and // loaded as a module on every slide surface: the viewer, the projector // and the follow view, the editor. // // fonts-hold.js has already set , // which hides every slide. No stylesheet declares a web face, so until // this script adds one everything is laid out in its system faces, // hidden. // // 1. Load every family the server listed (the manifest's need: the // catalog's, and the deck's own uploaded ones its stacks lead // with), until the deadline. A family still missing is committed to // its fallback for the visit; nothing is on screen yet, so nothing // moves. The rest are put on (their faces join document.fonts), // still hidden. // 2. On the editor, wait for the editor's first mount — its views bound // to the slide, the stored viewport applied — which it reports with // settled(), until the layout deadline. On the presenter window, wait // for the pictures of the slides it opens on (Now and Next) to be // decoded, until the same deadline: a slow picture never puts a // preview up without it, and a broken one never holds the window. // 3. Show the slides, once. On the editor, then bring every other // family into the tab at idle, so that an action rarely waits. // // It leaves the session on globalThis.slidesFonts for the editor bundle // and for static/present.js. If the hold's fail-safe has already shown // the page (this script was slow), or there was no hold (its script // never arrived), the page is in its system faces and stays there: this // turns nothing on. import { createFontSession, createSystemSession, GATE_DEADLINE_MS } from './font-session.mjs' import { familiesOfView, familiesOfSection } from './font-families.mjs' import { fontStatus } from './font-status.mjs' import { mountViewerNav } from './viewer-nav.mjs' import { mountScenePages } from './scene-pages.mjs' // The editor gets this long, from navigation start, to mount before the // slides are shown anyway. It will still mount after that, and its first // mount moves nothing on a stage that is already in view // (editor-entry.js, shownBeforeMount). const LAYOUT_DEADLINE_MS = 3500 // The presenter window's first pictures get as long, from navigation // start, before its slides are shown anyway (step 2). const PICTURES_DEADLINE_MS = LAYOUT_DEADLINE_MS // How long the substitutes note stays, unless dismissed. const SUBSTITUTE_NOTE_MS = 10000 const root = document.documentElement const holding = root.getAttribute('data-slides-fonts') === 'pending' if (holding) clearTimeout(globalThis.__slidesFontsHold) let manifest = null try { const meta = document.querySelector('meta[name="slides-fonts"]') manifest = meta ? JSON.parse(meta.getAttribute('content') || 'null') : null } catch { manifest = null } let settle const editorSettled = new Promise(resolve => { settle = resolve }) let fontsOn const fontsReady = new Promise(resolve => { fontsOn = resolve }) const after = ms => new Promise(resolve => setTimeout(resolve, Math.max(0, ms - performance.now()))) function show (session) { session.markShown() if (root.getAttribute('data-slides-fonts') !== 'shown') root.setAttribute('data-slides-fonts', 'shown') if (session.fallback.length) fontStatus(document.querySelector('[slides-fonts-status]')).say('substitute', SUBSTITUTE_NOTE_MS) } // prefetch brings every family the editor could need into the tab once // the slides are shown, one at a time when the page is idle. Not on a // connection that asks to save data or is 2G: there the wait is the // better cost. // // A page that is left (another page, or into the back/forward cache) // starts no more files until it is shown again from that cache: a // request from a cached page would hold one of the browser's six // connections to this host for nobody (Docs #86, dadf9c1). let away = false let resume = [] addEventListener('pagehide', () => { away = true }) addEventListener('pageshow', e => { if (!e.persisted) return away = false const waiting = resume resume = [] for (const r of waiting) r() }) function prefetch (session) { const c = navigator.connection if (c && (c.saveData || /(^|-)2g$/.test(c.effectiveType || ''))) return Promise.resolve() const idle = () => new Promise(resolve => typeof requestIdleCallback === 'function' ? requestIdleCallback(() => resolve(), { timeout: 5000 }) : setTimeout(resolve, 200)) .then(() => (away ? new Promise(resolve => resume.push(resolve)) : undefined)) return session.prefetch({ idle }) } async function run (session) { await session.gate(manifest.need || [], Math.max(0, GATE_DEADLINE_MS - performance.now())) fontsOn() const editor = document.querySelector('[slides-editor]') if (editor) await Promise.race([editorSettled, after(LAYOUT_DEADLINE_MS)]) const presenter = document.querySelector('[presenter]') if (presenter) await Promise.race([picturesOf(presenter), after(PICTURES_DEADLINE_MS)]) show(session) if (editor) { await prefetch(session) root.setAttribute('data-slides-fonts-prefetch', 'done') } } // picturesOf resolves once every picture on the presenter window's // slides is decoded or has failed. function picturesOf (presenter) { const imgs = [...presenter.querySelectorAll('[presenter-panel] [slides-slide] img')] return Promise.all(imgs.map(img => (typeof img.decode === 'function' ? img.decode() : Promise.resolve()).catch(() => {}))) } function withHelpers (session) { // The manifest with every uploaded family the session knows, those it // learned during the visit included (font-session.mjs's learn), for // reading a rendered slide. const known = () => ({ ...manifest, uploaded: session.uploaded }) return Object.assign(session, { fontsReady, // The editor's half: called once it has mounted in the final faces, // or failed to. settled: () => settle(), familiesOfView: view => (manifest ? familiesOfView(view, manifest) : []), familiesOfSection: html => (manifest ? familiesOfSection(html, known()) : []), // A slide the server rendered, about to go on a stage or a // projector: its families are made ready first (content from // elsewhere — committed, never held back, past the timeout). prepareSection: html => (session.web && manifest ? session.prepare(familiesOfSection(html, known())) : Promise.resolve()) }) } if (!holding || !manifest || typeof FontFace !== 'function' || !document.fonts) { // No hold, or the fail-safe got there first, or nothing to load with: // the system faces, and the layout already in view, are final. if (holding) root.setAttribute('data-slides-fonts', 'shown') const session = withHelpers(createSystemSession()) fontsOn() globalThis.slidesFonts = session mountViewerNav({ fonts: session }) mountScenePages() } else { const session = withHelpers(createFontSession({ manifest, fontSet: document.fonts, FontFaceCtor: FontFace, root })) globalThis.slidesFonts = session // The viewer changes slides in place, never through a page load whose // slide is hidden again (viewer-nav.mjs). mountViewerNav({ fonts: session }) // Scenes run over the viewer's and the follow view's slide // (scene-pages.mjs). mountScenePages() run(session).catch(err => { // A gate that fails must not leave the slides hidden. console.error('fonts:', err) fontsOn() show(session) }) }