oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/phone-frame.mjs — the phone frame's scale (spec §6.1).
2//
3// In phone view the editable stage, and in Both the read-only preview,
4// is drawn as a phone: a 390×844 screen (editor.css, "The phone
5// frame"). This keeps --slides-phone-scale on each frame at the size
6// that fits the phone into the stages, never above 1: the stages'
7// width over 390, and where the page does not scroll (from 1100px, the
8// desk is the window) their height over 844 as well — there the stages
9// ask for the phone's height and get what the pane can spare, and CSS
10// says so with --slides-phone-room: block. Elsewhere the stages are as
11// tall as what is in them, so their height is no bound. CSS zooms the
12// frame by the scale.
13//
14// A ResizeObserver on the stages re-measures whenever the window, the
15// view or the notes card change their size; it runs before the frame
16// is painted, so a frame is never seen at the old scale. A view switch
17// that leaves the stages' size as it was calls update() itself (the
18// editor's applyView), before the frame is painted too. onChange(scale)
19// hears each new scale: what is placed in screen pixels over the stage
20// (the canvas overlay, the presence marks) has to be placed again.
21
22/** The phone's screen, in CSS px. */
23export const PHONE_INLINE = 390
24export const PHONE_BLOCK = 844
25
26/**
27 * The scale that fits a phone into availableBlock px of height and
28 * availableInline px of width (no bound when left out): the smaller
29 * of the two ratios, never above 1, 0 for no room. Rounded down to
30 * four decimals so the scaled phone never ends outside the room.
31 */
32export function phoneScale (availableBlock, availableInline = Infinity) {
33 if (!(availableBlock > 0) || !(availableInline > 0)) return 0
34 const fit = Math.min(availableBlock / PHONE_BLOCK, availableInline / PHONE_INLINE)
35 return Math.min(1, Math.floor(fit * 1e4) / 1e4)
36}
37
38/**
39 * Keeps --slides-phone-scale on the phone frames inside stagesEl (the
40 * read-only [slides-stage-phone] and the editable stage's
41 * [slides-stage-frame]) equal to phoneScale of the stages' room.
42 * Returns {update, destroy}.
43 */
44export function mountPhoneFrame ({ stagesEl, onChange = () => {} }) {
45 let last = null
46 function update () {
47 const room = getComputedStyle(stagesEl).getPropertyValue('--slides-phone-room').trim()
48 const scale = phoneScale(room === 'block' ? stagesEl.clientHeight : Infinity, stagesEl.clientWidth)
49 if (scale === last) return
50 last = scale
51 for (const frame of stagesEl.querySelectorAll('[slides-stage-phone], [slides-stage-frame]')) {
52 frame.style.setProperty('--slides-phone-scale', String(scale))
53 }
54 onChange(scale)
55 }
56 const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(update) : null
57 if (ro) ro.observe(stagesEl)
58 update()
59 return {
60 // Measure again now: a view changed what bounds the phone, which
61 // from 1100px need not change the stages' size (they fill the pane
62 // over the notes card in every view), so the observer may not hear.
63 update,
64 destroy () {
65 if (ro) ro.disconnect()
66 }
67 }
68}
69