| 1 | // fixtures/font-status.mjs — what the editor says about its fonts, in one |
| 2 | // place: the status strip under the editor's bar |
| 3 | // (templates/editor.html's [slides-fonts-status]). Docs' font-status.mjs, |
| 4 | // adopted. |
| 5 | // |
| 6 | // The strip is always in the page and keeps its own row, so saying |
| 7 | // something moves nothing (editor.css), and its text is a polite live |
| 8 | // region that is there before the text changes, so a screen reader hears |
| 9 | // each change. The copy is the catalog's, on the element's data-* |
| 10 | // attributes (fontgate.go); this file holds none. |
| 11 | // |
| 12 | // The fonts gate (fonts-gate.mjs) and the editor (editor-entry.js) each |
| 13 | // make one of these over the same element, so what is being said is read |
| 14 | // from the element, not kept here. |
| 15 | |
| 16 | export function fontStatus (el, { setTimer = setTimeout, clearTimer = clearTimeout } = {}) { |
| 17 | if (!el) return { say () {}, clear () {}, state: '' } |
| 18 | const text = el.querySelector('[slides-fonts-status-text]') |
| 19 | const dismiss = el.querySelector('[slides-fonts-status-dismiss]') |
| 20 | let timer = null |
| 21 | const state = () => el.getAttribute('data-state') || '' |
| 22 | |
| 23 | // clear(kind) clears only if kind is still what is said. |
| 24 | function clear (kind) { |
| 25 | if (kind && state() !== kind) return |
| 26 | clearTimer(timer) |
| 27 | el.removeAttribute('data-state') |
| 28 | if (text) text.textContent = '' |
| 29 | } |
| 30 | if (dismiss) dismiss.addEventListener('click', () => clear()) |
| 31 | |
| 32 | return { |
| 33 | // say shows one of the element's messages: loading, refused, |
| 34 | // unavailable or substitute. ms, when given, takes it down after. |
| 35 | say (kind, ms = 0) { |
| 36 | clearTimer(timer) |
| 37 | el.setAttribute('data-state', kind) |
| 38 | if (text) text.textContent = el.getAttribute('data-' + kind) || '' |
| 39 | if (ms) timer = setTimer(() => clear(kind), ms) |
| 40 | }, |
| 41 | clear, |
| 42 | get state () { return state() } |
| 43 | } |
| 44 | } |
| 45 | |