| 1 | // fixtures/deck-fonts.mjs — the deck's two font fields when a family |
| 2 | // from the person's own library is in play (internal/slides/deckfonts.go). |
| 3 | // |
| 4 | // A family is offered in each field's "Your fonts" group, its value the |
| 5 | // family's CSS name. Choosing one asks the server to record it for the |
| 6 | // deck (POST /d/{sqid}/fonts, which answers the family as |
| 7 | // meta.fontFiles carries it), then commits ONE set_meta: the family in |
| 8 | // fontFiles, and the role's stack set to the family in front of the |
| 9 | // theme's own stack for that role. One change, so one undo takes it all |
| 10 | // back. Choosing anything else writes meta.fonts as deckOp always has, |
| 11 | // and in the same op drops from fontFiles every family neither role's |
| 12 | // stack now leads with. |
| 13 | // |
| 14 | // The pure half (leadFamily … plainFontOp) is held by |
| 15 | // deck-fonts.test.mjs; mountDeckFonts is the wiring inspector.mjs uses. |
| 16 | import { deckOp } from './inspector-ops.mjs' |
| 17 | import { safeFontStack, leadFamily } from './font-families.mjs' |
| 18 | |
| 19 | export { leadFamily } |
| 20 | |
| 21 | const ROLES = ['heading', 'body'] |
| 22 | |
| 23 | /** What a font field shows for a stack: the uploaded family it leads with, else the stack itself. */ |
| 24 | export function fontChoice (stack, fontFiles) { |
| 25 | const lead = leadFamily(stack) |
| 26 | if (lead && (fontFiles || []).some(f => same(f.css, lead))) return lead |
| 27 | return stack || '' |
| 28 | } |
| 29 | |
| 30 | /** The theme's own stack for role ('heading' or 'body'), from the font manifest (fontgate.go). */ |
| 31 | export function themeStack (meta, manifest, role) { |
| 32 | const themes = (manifest && manifest.themes) || {} |
| 33 | const theme = meta && themes[meta.theme] ? meta.theme : manifest && manifest.defaultTheme |
| 34 | const pair = themes[theme] |
| 35 | return (pair && pair[ROLES.indexOf(role)]) || 'sans-serif' |
| 36 | } |
| 37 | |
| 38 | // A browser matches family names without regard to case, and so does |
| 39 | // the document (font-files.mjs): Brand and BRAND are one name. |
| 40 | const same = (a, b) => typeof a === 'string' && typeof b === 'string' && a.toLowerCase() === b.toLowerCase() |
| 41 | |
| 42 | // The families a pair of stacks still leads with. |
| 43 | function keepUsed (fontFiles, fonts) { |
| 44 | const used = ROLES.map(r => leadFamily(fonts && fonts[r])).filter(Boolean) |
| 45 | return (fontFiles || []).filter(f => used.some(n => same(n, f.css))) |
| 46 | } |
| 47 | |
| 48 | /** The set_meta for choosing family (a deck.FontFamily) for role. */ |
| 49 | export function pickOp (role, family, meta, manifest) { |
| 50 | const fonts = { heading: '', body: '', ...(meta && meta.fonts ? meta.fonts : {}) } |
| 51 | const stack = `'${family.css}', ${themeStack(meta, manifest, role)}` |
| 52 | // fontPattern's 200: a long name keeps its quote and a generic family. |
| 53 | fonts[role] = safeFontStack(stack) || `'${family.css}', sans-serif` |
| 54 | // The deck named the family (POST /d/{sqid}/fonts): a name it already |
| 55 | // lists is this same family, now perhaps with more faces, and is |
| 56 | // replaced; any other family keeps its own name and place. |
| 57 | const files = (meta && meta.fontFiles ? meta.fontFiles : []).filter(f => !same(f.css, family.css)) |
| 58 | files.push(family) |
| 59 | return { op: 'set_meta', fonts: { heading: fonts.heading, body: fonts.body }, fontFiles: keepUsed(files, fonts) } |
| 60 | } |
| 61 | |
| 62 | /** deckOp for a font field's other choices, dropping in the same op the families no role leads with now. */ |
| 63 | export function plainFontOp (field, value, meta) { |
| 64 | const op = deckOp(field, value, meta) |
| 65 | const had = (meta && meta.fontFiles) || [] |
| 66 | if (!had.length) return op |
| 67 | const kept = keepUsed(had, op.fonts || {}) |
| 68 | if (kept.length !== had.length) op.fontFiles = kept.length ? kept : null |
| 69 | return op |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Mounts the font fields' behaviour. base is the deck's /d/{sqid}; |
| 74 | * getMeta() is the deck's meta now; commit(ops) applies a batch; |
| 75 | * refill() puts the fields back to what the deck has (a refused pick); |
| 76 | * manifest is the font manifest, read off the page when not given. |
| 77 | * Returns change(field, select), which commits for a font field's change |
| 78 | * and resolves once a pick's answer has been dealt with. |
| 79 | * |
| 80 | * Only a field's newest choice is ever written: every change to a field |
| 81 | * starts a new generation for it, and a pick whose answer comes back |
| 82 | * after a later choice for the same field (Theme's, another family) |
| 83 | * commits nothing. The other field's choices do not count. |
| 84 | */ |
| 85 | export function mountDeckFonts ({ base, getMeta, commit, refill, fetchFn = (...args) => globalThis.fetch(...args), manifest = pageManifest() }) { |
| 86 | const generation = { heading: 0, body: 0 } |
| 87 | |
| 88 | async function pick (role, css, gen) { |
| 89 | const current = () => generation[role] === gen |
| 90 | try { |
| 91 | const res = await fetchFn(`${base}/fonts`, { |
| 92 | method: 'POST', |
| 93 | credentials: 'same-origin', |
| 94 | headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, |
| 95 | body: JSON.stringify({ family: css }) |
| 96 | }) |
| 97 | if (!res.ok) throw new Error(`deck fonts: ${res.status}`) |
| 98 | const family = await res.json() |
| 99 | if (!current()) return |
| 100 | // The deck as it is when the answer comes, not when the field |
| 101 | // changed: an edit in between is kept. |
| 102 | commit([pickOp(role, family, getMeta(), manifest)]) |
| 103 | } catch (err) { |
| 104 | console.error(err) |
| 105 | if (current()) refill() |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return { |
| 110 | change (field, select) { |
| 111 | const role = field.slice(5) |
| 112 | const gen = ++generation[role] |
| 113 | const opt = select.selectedOptions && select.selectedOptions[0] |
| 114 | if (opt && opt.parentElement && opt.parentElement.tagName === 'OPTGROUP') return pick(role, select.value, gen) |
| 115 | commit([plainFontOp(field, select.value, getMeta())]) |
| 116 | return Promise.resolve() |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function pageManifest () { |
| 122 | try { |
| 123 | const tag = globalThis.document && document.querySelector('meta[name="slides-fonts"]') |
| 124 | return tag ? JSON.parse(tag.getAttribute('content') || 'null') : null |
| 125 | } catch { return null } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Fills a font field with a stack. When the stack leads with one of the |
| 130 | * deck's uploaded families that this person's "Your fonts" group does |
| 131 | * not offer (a co-editor's), the field shows it anyway, as a selected |
| 132 | * option they cannot choose, labelled with the family's own name (data, |
| 133 | * as a deck title is): the field never looks blank, and anything they |
| 134 | * own is still theirs to switch to. setValue is inspector-block.mjs's. |
| 135 | */ |
| 136 | export function fillFontField (select, stack, fontFiles, setValue) { |
| 137 | if (!select) return |
| 138 | const value = fontChoice(stack, fontFiles) |
| 139 | for (const o of [...select.querySelectorAll('option[data-deck-font]')]) if (o.value !== value) o.remove() |
| 140 | const family = (fontFiles || []).find(f => f.css === value) |
| 141 | if (family && ![...select.options].some(o => o.value === value)) { |
| 142 | const o = select.ownerDocument.createElement('option') |
| 143 | o.value = value |
| 144 | o.textContent = family.family || family.css |
| 145 | o.disabled = true |
| 146 | o.setAttribute('data-deck-font', '') |
| 147 | select.append(o) |
| 148 | } |
| 149 | setValue(select, value) |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Asks the library for this person's families again (GET /fonts as |
| 154 | * JSON, deckfonts.go's yourFonts) and offers them in each field's group, |
| 155 | * labelled label (the page's own words): a family uploaded in the Fonts |
| 156 | * tab Deck settings opened is offered once the editor's window has focus |
| 157 | * again. refill() then puts the fields back to what the deck has. |
| 158 | */ |
| 159 | export async function refreshYourFonts ({ selects, label, refill, fetchFn = (...args) => globalThis.fetch(...args) }) { |
| 160 | const res = await fetchFn('/fonts', { credentials: 'same-origin', headers: { Accept: 'application/json' } }) |
| 161 | if (!res.ok) return |
| 162 | const families = await res.json() |
| 163 | if (!Array.isArray(families)) return |
| 164 | for (const select of selects) { |
| 165 | if (!select) continue |
| 166 | const doc = select.ownerDocument |
| 167 | let group = select.querySelector('optgroup') |
| 168 | if (!families.length) { |
| 169 | if (group) group.remove() |
| 170 | continue |
| 171 | } |
| 172 | if (!group) { |
| 173 | group = doc.createElement('optgroup') |
| 174 | group.label = label || '' |
| 175 | select.insertBefore(group, select.querySelector('option[data-deck-font]')) |
| 176 | } |
| 177 | group.replaceChildren(...families.map(f => { |
| 178 | const o = doc.createElement('option') |
| 179 | o.value = f.value |
| 180 | o.textContent = f.label |
| 181 | return o |
| 182 | })) |
| 183 | } |
| 184 | refill() |
| 185 | } |
| 186 | |