| 1 | // fixtures/font-status.mjs — what the page says about its fonts, in one |
| 2 | // place: a status strip laid over the formatting bar (views/show.html's |
| 3 | // [docs-fonts-status]). |
| 4 | // |
| 5 | // Over the bar, never over the document (branch review 7: floating notes |
| 6 | // covered text, and the substitutes note never went away). The strip is |
| 7 | // in the page from the start and takes no space of its own, so saying |
| 8 | // something moves nothing. While a font loads the bar is inert anyway; |
| 9 | // the other notes go away on their own, or when dismissed. |
| 10 | // |
| 11 | // Its text is in a polite live region that is always in the page, empty |
| 12 | // when there is nothing to say, so a screen reader hears each change. |
| 13 | // The copy is the catalog's, on the element's data-* attributes. |
| 14 | // |
| 15 | // The fonts gate (fonts-gate.mjs) and the editor (editor-entry.js) each |
| 16 | // make one of these over the same element, so what is being said is read |
| 17 | // from the element, not kept here. |
| 18 | |
| 19 | export function fontStatus(el, { setTimer = setTimeout, clearTimer = clearTimeout } = {}) { |
| 20 | if (!el) return { say() {}, tell() {}, clear() {}, state: '' } |
| 21 | const text = el.querySelector('[docs-fonts-status-text]') |
| 22 | const dismiss = el.querySelector('[docs-fonts-status-dismiss]') |
| 23 | let timer = null |
| 24 | const state = () => el.getAttribute('data-state') || '' |
| 25 | |
| 26 | // clear(kind) clears only if kind is still what is said. |
| 27 | function clear(kind) { |
| 28 | if (kind && state() !== kind) return |
| 29 | clearTimer(timer) |
| 30 | el.removeAttribute('data-state') |
| 31 | if (text) text.textContent = '' |
| 32 | } |
| 33 | if (dismiss) dismiss.addEventListener('click', () => clear()) |
| 34 | |
| 35 | return { |
| 36 | // say shows one of the element's messages: loading, refused, |
| 37 | // unavailable or substitute. ms, when given, takes it down after. |
| 38 | say(kind, ms = 0) { |
| 39 | clearTimer(timer) |
| 40 | el.setAttribute('data-state', kind) |
| 41 | if (text) text.textContent = el.getAttribute('data-' + kind) || '' |
| 42 | if (ms) timer = setTimer(() => clear(kind), ms) |
| 43 | }, |
| 44 | // tell says a sentence of the page's own (an image notice, Docs |
| 45 | // images spec §2), as a 'notice' the strip can dismiss. |
| 46 | tell(sentence, ms = 0) { |
| 47 | clearTimer(timer) |
| 48 | el.setAttribute('data-state', 'notice') |
| 49 | if (text) text.textContent = sentence |
| 50 | if (ms) timer = setTimer(() => clear('notice'), ms) |
| 51 | }, |
| 52 | clear, |
| 53 | get state() { |
| 54 | return state() |
| 55 | }, |
| 56 | } |
| 57 | } |
| 58 | |