// fixtures/editor-fonts.mjs — the font gate every change this person // makes in the editor passes (internal/slides/fontgate.go; AGENTS.md, // "Fonts: never a flash"). Docs' fontGatePlugin and wait, adopted, and // kept out of editor-entry.js so that file stays wiring. // // Paul, 2026-10-26: "No flashing, ever!" A change this person makes — // an op batch (a theme, a font override, a code block, a new slide, // a duplicate, a paste of blocks), a code mark typed, pasted or // dropped in a slide's text, an undo, a redo — that would put slide // text in a family whose four faces are not all in is cancelled // before anything changes. Then, like a short modal: the stage, the // strip, the inspector and the Slide menu go inert, and the status // strip under the bar says a font is loading, at once. Input // meanwhile goes nowhere, which a person can see; nothing is queued // or replayed, because a replayed keystroke is not the keystroke // (Docs' review 7). Once the family is in, the change is made, once, // as it was asked for. If the family fails or misses the deadline the // change is dropped, editing comes back and the strip says why. // // One wait at a time: anything else that would change the deck during // it is refused, and the strip says so, so the deck cannot move under // the change. A collaborator's update waits for the wait to end before // it reaches Yjs (onUpdates, free()), in order. // // A wait never begins in an IME composition: a composition that picks // up a family that is not in draws it in its fallback for the visit // instead (it is not on screen, so nothing moves). // // createEditorFontGate takes the tab's font session (fonts-gate.mjs's // globalThis.slidesFonts, or the system session), the editor's root, // getDoc() for the live Y.Doc and onRefused() for a change that did not // happen (the inspector refills from the deck). It returns gate(keys, // run), free() (resolves when no wait is on; every waiter is released, // in order, however a wait ends), familiesAfter(mutate), afterHistory() // (an undo or a redo just made), cold(), plugin() and a waiting getter. import * as Y from 'yjs' import { ySyncPluginKey } from 'y-prosemirror' import { Plugin } from 'prosemirror-state' import { view } from './view.mjs' import { ACTION_TIMEOUT_MS } from './font-session.mjs' import { FAMILY_KEYS } from './font-families.mjs' import { fontStatus } from './font-status.mjs' import { createInert } from './editor-inert.mjs' export function createEditorFontGate ({ fonts, root, getDoc, onRefused = () => {} }) { let waiting = false let released = [] const free = () => (waiting ? new Promise(resolve => released.push(resolve)) : Promise.resolve()) const fontNote = fontStatus(root.querySelector('[slides-fonts-status]')) // How long a font that could not be loaded is said, unless dismissed. const UNAVAILABLE_NOTE_MS = 8000 const MONO = 'mono' // A deck's uploaded families are fetched under the deck's own base // (fontgate.go). A change can bring in one this tab has never heard // of — a pick here, a collaborator's — so on a session that can learn // them (font-session.mjs's learn) every change is checked. const base = root.dataset && root.dataset.deck ? `/d/${root.dataset.deck}` : '' const learns = () => !!base && typeof fonts.learn === 'function' // Whether some family could still need a wait: once every family is // on (or committed), nothing can, and the checks below cost nothing. const coldFamilies = () => fonts.web && (learns() || (fonts.keys || FAMILY_KEYS).some(k => !fonts.inUse.includes(k))) // familiesAfter is the families of the deck as mutate(scratch) would // leave it, worked out on a copy so the deck itself is untouched. An // uploaded family the changed deck names is learned from it, so the // change waits for it like any family it brings in. function familiesAfter (mutate) { const scratch = new Y.Doc() Y.applyUpdate(scratch, Y.encodeStateAsUpdate(getDoc())) if (learns()) { const before = view(scratch) settleUnknown(before) // The deck's own sets first: a change worked out before and never // made (refused, or a wait still on) left its sets learned. fonts.learn(before.meta && before.meta.fontFiles, base) } mutate(scratch) const after = view(scratch) if (learns()) fonts.learn(after.meta && after.meta.fontFiles, base) return fonts.familiesOfView(after) } // learnLive learns the deck's sets as the deck has them now: after a // change that was worked out and not made. function learnLive () { if (!learns()) return const m = view(getDoc()).meta fonts.learn(m && m.fontFiles, base) } // afterHistory follows an undo or a redo, in the same task: the deck // may now have another set of an uploaded family's faces (one this tab // had on the page before, so loaded), which goes on in place of the // set there now. A set that is not ready leaves the page's own set // where it is (font-session.mjs's commit). Undo and redo pass the gate // on the families in use (editor-entry.js); this is the rest. function afterHistory () { if (!learns()) return learnLive() const keys = fonts.familiesOfView(view(getDoc())).filter(k => k.startsWith('u:')) if (keys.length) fonts.commitFallback(keys) } // A family the deck shows already that this tab never learned (a // change taken in without a check: a resync) is on screen in its // stack's next family. Learned now, it is committed there, so it // never swaps in. function settleUnknown (before) { const known = fonts.keys || [] const unknown = fonts.familiesOfView(before).filter(k => k.startsWith('u:') && !known.includes(k)) if (!unknown.length) return fonts.learn(before.meta && before.meta.fontFiles, base) fonts.commitFallback(unknown) } // refused tells the person a change did not happen and lets the // inspector show the deck again. It never throws: whatever it calls is // told off in the console, and the caller's own cleanup still runs. function refused (kind, ms = 0) { try { fontNote.say(kind, ms) } catch (err) { console.error('fonts:', err) } try { onRefused() } catch (err) { console.error('fonts:', err) } } // release lets everything that waited for this wait go, in the order // it queued (a collaborator's batch, a stage install, a resync). function release () { const waiters = released released = [] for (const r of waiters) r() } // gateLocal runs a person's own change now if its families are in, // after a wait if they are coming, and never if a wait is on. It // resolves to whether the change was made, and never rejects. function gateLocal (keys, run) { if (waiting) { learnLive() refused('refused') return Promise.resolve(false) } const cold = keys.filter(k => !fonts.ready(k)) if (!cold.length) { fonts.use(keys) return Promise.resolve(run() !== false) } return wait(keys, run) } // wait holds the change while its families load, then runs it once, // or refuses it. Whichever way it ends — loaded, failed, timed out, or // anything in here throwing — the editor comes back and every waiter // queued behind it is released, in order, in the finally: a refusal // that threw before the release once left a collaborator's batch, and // every batch after it, stuck for the rest of the visit (Astra, #27). async function wait (keys, run) { waiting = true let loaded = false let ran = false try { setInert(true) try { loaded = await fonts.waitFor(keys, ACTION_TIMEOUT_MS) } catch { loaded = false } if (loaded) fonts.use(keys) waiting = false // Editing back, and focus with it, before the change: so the // change and its selection are painted in one frame. setInert(false) ran = loaded && run() !== false } catch (err) { console.error('fonts:', err) ran = false } finally { waiting = false try { setInert(false) } catch (err) { console.error('fonts:', err) } try { if (ran) fontNote.clear() } catch (err) { console.error('fonts:', err) } // Loaded but no longer possible (its view went away): not a font // that failed, and not said as one. if (!ran) { try { learnLive() } catch (err) { console.error('fonts:', err) } refused(loaded ? 'interrupted' : 'unavailable', UNAVAILABLE_NOTE_MS) } release() } return ran } // The editor goes inert while a change waits (editor-inert.mjs). const setInert = createInert(root, () => fontNote.say('loading')) // The ProseMirror half: one plugin per view bound to a slide's text // (stage.mjs's plugins; the notes are drawn in the chrome's face and // get none). The one mark that brings in a family is code. const hasCode = (doc, marks) => { const c = doc.type.schema.marks.c if (marks && c.isInSet(marks)) return true let found = false doc.descendants(n => { if (found) return false if (n.isText && c.isInSet(n.marks)) found = true return !found }) return found } function fontGatePlugin () { let pmView = null return new Plugin({ view (v) { pmView = v return { destroy () { if (pmView === v) pmView = null } } }, filterTransaction (tr, state) { // Yjs's own changes (a collaborator, undo, redo, a resync) reach // the view as they are; they were checked before they reached Yjs. if (tr.getMeta(ySyncPluginKey)) return true if (!tr.docChanged && !tr.storedMarksSet) return true if (waiting) { fontNote.say('refused') return false } if (!coldFamilies() || fonts.inUse.includes(MONO)) return true const marks = tr.storedMarksSet ? tr.storedMarks : state.storedMarks if (!hasCode(tr.doc, marks)) return true if (fonts.ready(MONO)) { fonts.use([MONO]) return true } if (pmView && pmView.composing) { fonts.commitFallback([MONO]) return true } const v = pmView wait([MONO], () => { if (!v || pmView !== v || v.state.doc !== tr.before) return false // Through the view's own dispatch, which keeps a change made // while a resync is pending as its transactions too // (editor-entry.js's aroundDispatch). v.dispatch(tr) return true }) return false } }) } return { gate: gateLocal, free, familiesAfter, afterHistory, cold: coldFamilies, plugin: fontGatePlugin, get waiting () { return waiting } } }