oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// presenter-notes.mjs — the presenter window's notes (spec §5.2, §10).
2//
3// The notes are one HTML string per part of the slide: the part for the
4// step the talk is at is bright, the others dimmed. Fit starts from the
5// size the presenter chose and steps down a pixel at a time until the
6// notes fit their panel, to a floor of 16px; past the floor the panel
7// scrolls inside itself (never the document), keeps the bright part in
8// view, and fades at the edge that has more. Scroll uses the chosen
9// size. Smaller and larger move the size 2px between 16 and 48 and
10// leave the mode alone. Every change is saved (the cookie
11// slides-presenter, read by the server, so the first paint matches),
12// and so is the window's arrangement, through setPanels.
13//
14// The words come from the page (data-text-nonotes); nothing here is
15// English. Tested in presenter-notes.test.mjs.
16
17export const MIN_SIZE = 16
18export const MAX_SIZE = 48
19export const SIZE_STEP = 2
20export const DEFAULT_SIZE = 24
21export const PREFS_COOKIE = 'slides-presenter'
22export const DEFAULT_PANELS = '0,0,7,8;0,8,7,4;7,0,5,12'
23
24/** The part to show bright: the step's part, or the last there is. */
25export function brightPart (parts, step) {
26 const n = Array.isArray(parts) ? parts.length : Number(parts) || 0
27 return Math.max(0, Math.min(Math.floor(Number(step)) || 0, n - 1))
28}
29
30/** The size one step smaller (dir < 0) or larger, kept in 16 to 48. */
31export function stepSize (size, dir) {
32 const s = Math.round(Number(size) / SIZE_STEP) * SIZE_STEP
33 return Math.max(MIN_SIZE, Math.min(MAX_SIZE, (Number.isFinite(s) ? s : DEFAULT_SIZE) + (dir < 0 ? -SIZE_STEP : SIZE_STEP)))
34}
35
36/** The largest size from the chosen one down to the floor at which
37 * overflows(px) is false; the floor if none is. */
38export function fitSize (size, overflows) {
39 let px = Math.max(MIN_SIZE, Math.min(MAX_SIZE, Math.round(size)))
40 while (px > MIN_SIZE && overflows(px)) px--
41 return px
42}
43
44/** "fit.24.panels" read: { notes, size, panels }, or null when it is
45 * not the cookie's shape (the server's own reading is stricter). */
46export function parsePrefs (raw) {
47 const f = String(raw ?? '').split('.')
48 if (f.length !== 3 || (f[0] !== 'fit' && f[0] !== 'scroll')) return null
49 const size = Number(f[1])
50 if (!Number.isInteger(size) || size < MIN_SIZE || size > MAX_SIZE || size % SIZE_STEP) return null
51 return { notes: f[0], size, panels: f[2] }
52}
53
54/** The cookie's value for prefs, percent-encoded: a ";" cannot be kept
55 * raw in a cookie. */
56export function formatPrefs (p) {
57 return encodeURIComponent(`${p.notes}.${p.size}.${p.panels || DEFAULT_PANELS}`)
58}
59
60/** The preferences in the cookie, or null. */
61export function readPrefs (doc = globalThis.document) {
62 try {
63 for (const part of String(doc?.cookie || '').split(';')) {
64 const at = part.indexOf('=')
65 if (at > 0 && part.slice(0, at).trim() === PREFS_COOKIE) return parsePrefs(decodeURIComponent(part.slice(at + 1).trim()))
66 }
67 } catch { /* unreadable: as if absent */ }
68 return null
69}
70
71/** Writes the cookie: path /p, a year, SameSite=Lax. */
72export function writePrefs (p, doc = globalThis.document, loc = globalThis.location) {
73 const secure = loc?.protocol === 'https:' ? '; Secure' : ''
74 try {
75 if (doc) doc.cookie = `${PREFS_COOKIE}=${formatPrefs(p)}; Path=/p; Max-Age=31536000; SameSite=Lax${secure}`
76 } catch { /* cookies blocked: this window only */ }
77}
78
79/** The edges with more to read: 'top', 'bottom', 'both' or ''. */
80export function fadeEdges (scrollTop, clientHeight, scrollHeight) {
81 const top = scrollTop > 1
82 const bottom = scrollTop + clientHeight < scrollHeight - 1
83 return top && bottom ? 'both' : top ? 'top' : bottom ? 'bottom' : ''
84}
85
86/** Where to scroll so the part [top, bottom) shows: nowhere (null) if
87 * it already does, else its top. Positions are in the scrolled
88 * content. */
89export function scrollFor (top, bottom, viewTop, viewHeight) {
90 if (top >= viewTop && bottom <= viewTop + viewHeight) return null
91 return Math.max(0, top)
92}
93
94export function mountNotes ({ root, prefs, savePrefs = writePrefs }) {
95 const notes = root.querySelector('[presenter-notes]')
96 const tools = name => root.querySelector('[presenter-notes-' + name + ']')
97 const buttons = { fit: tools('fit'), scroll: tools('scroll'), smaller: tools('smaller'), larger: tools('larger') }
98 let shown = null // what the panel holds, to tell new notes from a new step
99 const state = {
100 notes: prefs.notes === 'scroll' ? 'scroll' : 'fit',
101 size: prefs.size || DEFAULT_SIZE,
102 panels: prefs.panels || DEFAULT_PANELS
103 }
104
105 function fade () {
106 notes.setAttribute('data-fade', fadeEdges(notes.scrollTop, notes.clientHeight, notes.scrollHeight))
107 }
108
109 function keepInView () {
110 const s = notes.querySelector('[data-part="now"]')
111 if (!s) return
112 const origin = notes.getBoundingClientRect().top - notes.scrollTop
113 const top = s.getBoundingClientRect().top - origin
114 const to = scrollFor(top - 12, top + s.offsetHeight, notes.scrollTop, notes.clientHeight)
115 if (to !== null) notes.scrollTop = to
116 }
117
118 // layout sets the size (shrunk to fit in Fit), then keeps the bright
119 // part in view wherever the panel scrolls.
120 function layout () {
121 notes.setAttribute('data-notes-mode', state.notes)
122 let px = state.size
123 if (state.notes === 'fit') {
124 px = fitSize(state.size, p => {
125 notes.style.fontSize = p + 'px'
126 return notes.scrollHeight > notes.clientHeight
127 })
128 }
129 notes.style.fontSize = px + 'px'
130 const scrolls = notes.scrollHeight > notes.clientHeight
131 notes.setAttribute('data-notes-scrolls', scrolls ? 'true' : 'false')
132 if (scrolls) keepInView()
133 else notes.scrollTop = 0
134 fade()
135 buttons.fit?.setAttribute('aria-pressed', state.notes === 'fit' ? 'true' : 'false')
136 buttons.scroll?.setAttribute('aria-pressed', state.notes === 'scroll' ? 'true' : 'false')
137 }
138
139 function change (patch) {
140 Object.assign(state, patch)
141 savePrefs({ notes: state.notes, size: state.size, panels: state.panels })
142 layout()
143 }
144
145 buttons.fit?.addEventListener('click', () => change({ notes: 'fit' }))
146 buttons.scroll?.addEventListener('click', () => change({ notes: 'scroll' }))
147 buttons.smaller?.addEventListener('click', () => change({ size: stepSize(state.size, -1) }))
148 buttons.larger?.addEventListener('click', () => change({ size: stepSize(state.size, 1) }))
149 notes.addEventListener('scroll', fade)
150 if (typeof ResizeObserver === 'function') {
151 let last = ''
152 new ResizeObserver(() => {
153 const box = notes.clientWidth + 'x' + notes.clientHeight
154 if (box === last) return
155 last = box
156 layout()
157 }).observe(notes)
158 }
159
160 function show (parts, step) {
161 const list = Array.isArray(parts) ? parts : []
162 const key = JSON.stringify(list)
163 if (key !== shown) notes.scrollTop = 0 // new notes begin at their beginning
164 shown = key
165 if (!list.length) {
166 const p = document.createElement('p')
167 p.setAttribute('presenter-nonotes', '')
168 p.textContent = root.dataset.textNonotes || ''
169 notes.replaceChildren(p)
170 } else {
171 const bright = brightPart(list, step)
172 notes.replaceChildren(...list.map((html, i) => {
173 const section = document.createElement('section')
174 section.setAttribute('presenter-part', '')
175 if (list.length > 1) section.setAttribute('data-part', i < bright ? 'past' : i > bright ? 'later' : 'now')
176 section.innerHTML = html
177 return section
178 }))
179 }
180 layout()
181 }
182
183 // The empty talk has no notes, not even "no notes": nothing is up.
184 function clear () {
185 shown = null
186 notes.scrollTop = 0
187 notes.replaceChildren()
188 }
189
190 // The server drew the first paint; this sizes and places it.
191 layout()
192
193 // Arranging saves the panels through here (presenter-arrange.mjs), so
194 // the cookie has one writer and keeps the notes' part.
195 function setPanels (panels) {
196 change({ panels })
197 }
198
199 return { show, clear, setPanels }
200}
201