| 1 | // fixtures/fonts-gate.mjs — shows a document's sheet once its faces are |
| 2 | // ready and its layout is final (polish-audit/fonts/NO-FOUT.md §2). |
| 3 | // |
| 4 | // fonts-hold.js has already set <html data-docs-fonts="pending">, which |
| 5 | // hides the sheet, and data-docs-previews="pending", which hides the font |
| 6 | // control. Until this script says so, the stylesheet uses no web face at |
| 7 | // all: everything is laid out in its system fallback, hidden. |
| 8 | // |
| 9 | // 1. Wait for the faces the server listed (data-docs-fonts-need), until |
| 10 | // the deadline. A family still missing is committed to its fallback |
| 11 | // for the visit; nothing is on screen yet, so nothing moves. |
| 12 | // 2. Turn the web faces on (data-docs-faces="web"), still hidden. |
| 13 | // 3. On a paged editor page, wait for the editor to paginate in those |
| 14 | // faces. If it has not by the layout deadline, freeze the layout |
| 15 | // (data-docs-layout="frozen": the editor will not add pages under a |
| 16 | // reader for the rest of the visit). |
| 17 | // 4. Show the sheet, once. Then, on an editor page, settle every face |
| 18 | // of every family in use, which the editor waits for before it lets |
| 19 | // anyone type. |
| 20 | // |
| 21 | // The picker's previews are gated separately and never hold the paper. |
| 22 | // |
| 23 | // If the hold's fail-safe has already shown the page (this script was |
| 24 | // slow), or there was no hold (its script never arrived), the page is in |
| 25 | // its system faces and stays there: this turns nothing on. |
| 26 | |
| 27 | import { createFontSession, createSystemSession, faceURLs, GATE_DEADLINE_MS } from './font-session.mjs' |
| 28 | import { fontStatus } from './font-status.mjs' |
| 29 | import { FONTS } from './fonts.mjs' |
| 30 | |
| 31 | // The editor gets this long, from navigation start, to paginate before |
| 32 | // the layout is frozen. |
| 33 | const LAYOUT_DEADLINE_MS = 3500 |
| 34 | |
| 35 | const root = document.documentElement |
| 36 | const canvas = document.querySelector('[docs-canvas]') |
| 37 | const holding = root.getAttribute('data-docs-fonts') === 'pending' |
| 38 | if (holding) clearTimeout(globalThis.__docsFontsHold) |
| 39 | |
| 40 | const session = createFontSession({ fontSet: document.fonts, root }) |
| 41 | let settle |
| 42 | const editorSettled = new Promise((resolve) => (settle = resolve)) |
| 43 | let fontsReady |
| 44 | const fontsReadyPromise = new Promise((resolve) => (fontsReady = resolve)) |
| 45 | |
| 46 | globalThis.docsFonts = Object.assign(session, { |
| 47 | // web is whether the web faces are on; when false everything is in its |
| 48 | // system face for the visit and there is nothing to load or wait for. |
| 49 | web: false, |
| 50 | fontsReady: fontsReadyPromise, |
| 51 | // The editor's half: called once it has paginated in the final faces, |
| 52 | // or failed to mount. |
| 53 | settled: () => settle(), |
| 54 | }) |
| 55 | |
| 56 | const after = (ms) => new Promise((r) => setTimeout(r, Math.max(0, ms - performance.now()))) |
| 57 | |
| 58 | // How long the substitutes note stays, unless dismissed. It sits over |
| 59 | // the formatting bar, so it cannot stay for the visit. |
| 60 | const SUBSTITUTE_NOTE_MS = 10000 |
| 61 | |
| 62 | function show() { |
| 63 | session.markShown() |
| 64 | if (session.fallback.some((k) => k !== 'previews')) { |
| 65 | const strip = document.querySelector('[docs-fonts-status]') |
| 66 | fontStatus(strip).say('substitute', SUBSTITUTE_NOTE_MS) |
| 67 | makeRoom(strip) |
| 68 | } |
| 69 | if (root.getAttribute('data-docs-fonts') !== 'shown') root.setAttribute('data-docs-fonts', 'shown') |
| 70 | } |
| 71 | |
| 72 | // makeRoom keeps the substitutes note off the text. It hangs under the |
| 73 | // bars, over the tray, which is kept tall enough for one line of the |
| 74 | // status; where this note wraps to more (a narrow screen, larger text), |
| 75 | // the tray is made taller, now, while the page is still hidden, so |
| 76 | // nothing moves once it is shown. |
| 77 | function makeRoom(strip) { |
| 78 | const text = document.querySelector('[docs-text]') |
| 79 | if (!strip || !text || !canvas) return |
| 80 | const need = Math.ceil(strip.getBoundingClientRect().bottom + 4 - text.getBoundingClientRect().top) |
| 81 | if (need > 0) canvas.style.paddingBlockStart = parseFloat(getComputedStyle(canvas).paddingBlockStart) + need + 'px' |
| 82 | } |
| 83 | |
| 84 | // prefetch brings every family the picker offers into the tab after the |
| 85 | // page is shown, one face at a time whenever the page is idle and at low |
| 86 | // priority, so that choosing, pasting or receiving a font rarely has to |
| 87 | // wait (branch review 7). Not on a connection that asks to save data or |
| 88 | // is 2G: there the wait is the better cost. About 6 MB, once; the files |
| 89 | // are cached. |
| 90 | function prefetch() { |
| 91 | const c = navigator.connection |
| 92 | if (c && (c.saveData || /(^|-)2g$/.test(c.effectiveType || ''))) return |
| 93 | const idle = () => |
| 94 | new Promise((resolve) => |
| 95 | typeof requestIdleCallback === 'function' ? requestIdleCallback(() => resolve(), { timeout: 5000 }) : setTimeout(resolve, 200), |
| 96 | ) |
| 97 | // A page that is left stops prefetching (a fetch still in flight would |
| 98 | // hold a connection from the back/forward cache), and starts again if |
| 99 | // it is shown from there. |
| 100 | const { signal } = (prefetching = new AbortController()) |
| 101 | const fetchFn = (url) => |
| 102 | signal.aborted |
| 103 | ? Promise.resolve(false) |
| 104 | : fetch(url, { priority: 'low', credentials: 'same-origin', signal }).then(async (r) => { |
| 105 | if (!r.ok) return false |
| 106 | await r.arrayBuffer() |
| 107 | return true |
| 108 | }) |
| 109 | return session.prefetch({ keys: FONTS, urls: faceURLs(document.styleSheets), fetchFn, idle }) |
| 110 | } |
| 111 | let prefetching = null |
| 112 | addEventListener('pagehide', () => prefetching && prefetching.abort()) |
| 113 | addEventListener('pageshow', (e) => { |
| 114 | if (e.persisted && prefetching) prefetch() |
| 115 | }) |
| 116 | |
| 117 | async function run() { |
| 118 | let need = [] |
| 119 | try { |
| 120 | need = JSON.parse((canvas && canvas.getAttribute('data-docs-fonts-need')) || '[]') |
| 121 | } catch { |
| 122 | need = [] |
| 123 | } |
| 124 | await session.gate(need, Math.max(0, GATE_DEADLINE_MS - performance.now())) |
| 125 | session.web = true |
| 126 | root.setAttribute('data-docs-faces', 'web') |
| 127 | session.previewsDone.then((state) => root.setAttribute('data-docs-previews', state)) |
| 128 | fontsReady() |
| 129 | const editor = document.querySelector('[docs-editor]') |
| 130 | if (editor && canvas && canvas.getAttribute('data-view') === 'paged') { |
| 131 | const outcome = await Promise.race([editorSettled.then(() => 'paginated'), after(LAYOUT_DEADLINE_MS).then(() => 'late')]) |
| 132 | if (outcome === 'late') root.setAttribute('data-docs-layout', 'frozen') |
| 133 | } |
| 134 | show() |
| 135 | if (editor) { |
| 136 | await Promise.all(session.required().map((k) => session.load(k))) |
| 137 | await prefetch() |
| 138 | root.setAttribute('data-docs-fonts-prefetch', 'done') |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if (!holding) { |
| 143 | // No hold, or the fail-safe got there first: the system faces, and |
| 144 | // the layout already in view, are final. |
| 145 | root.setAttribute('data-docs-layout', 'frozen') |
| 146 | globalThis.docsFonts = createSystemSession(root) |
| 147 | } else { |
| 148 | run().catch((err) => { |
| 149 | // A gate that fails must not leave the sheet hidden. If the web |
| 150 | // faces were never turned on, it shows in its system faces. |
| 151 | console.error('fonts:', err) |
| 152 | if (root.getAttribute('data-docs-previews') === 'pending') root.setAttribute('data-docs-previews', 'system') |
| 153 | fontsReady() |
| 154 | show() |
| 155 | }) |
| 156 | } |
| 157 | |