// fixtures/phone-frame.mjs — the phone frame's scale (spec §6.1). // // In phone view the editable stage, and in Both the read-only preview, // is drawn as a phone: a 390×844 screen (editor.css, "The phone // frame"). This keeps --slides-phone-scale on each frame at the size // that fits the phone into the stages, never above 1: the stages' // width over 390, and where the page does not scroll (from 1100px, the // desk is the window) their height over 844 as well — there the stages // ask for the phone's height and get what the pane can spare, and CSS // says so with --slides-phone-room: block. Elsewhere the stages are as // tall as what is in them, so their height is no bound. CSS zooms the // frame by the scale. // // A ResizeObserver on the stages re-measures whenever the window, the // view or the notes card change their size; it runs before the frame // is painted, so a frame is never seen at the old scale. A view switch // that leaves the stages' size as it was calls update() itself (the // editor's applyView), before the frame is painted too. onChange(scale) // hears each new scale: what is placed in screen pixels over the stage // (the canvas overlay, the presence marks) has to be placed again. /** The phone's screen, in CSS px. */ export const PHONE_INLINE = 390 export const PHONE_BLOCK = 844 /** * The scale that fits a phone into availableBlock px of height and * availableInline px of width (no bound when left out): the smaller * of the two ratios, never above 1, 0 for no room. Rounded down to * four decimals so the scaled phone never ends outside the room. */ export function phoneScale (availableBlock, availableInline = Infinity) { if (!(availableBlock > 0) || !(availableInline > 0)) return 0 const fit = Math.min(availableBlock / PHONE_BLOCK, availableInline / PHONE_INLINE) return Math.min(1, Math.floor(fit * 1e4) / 1e4) } /** * Keeps --slides-phone-scale on the phone frames inside stagesEl (the * read-only [slides-stage-phone] and the editable stage's * [slides-stage-frame]) equal to phoneScale of the stages' room. * Returns {update, destroy}. */ export function mountPhoneFrame ({ stagesEl, onChange = () => {} }) { let last = null function update () { const room = getComputedStyle(stagesEl).getPropertyValue('--slides-phone-room').trim() const scale = phoneScale(room === 'block' ? stagesEl.clientHeight : Infinity, stagesEl.clientWidth) if (scale === last) return last = scale for (const frame of stagesEl.querySelectorAll('[slides-stage-phone], [slides-stage-frame]')) { frame.style.setProperty('--slides-phone-scale', String(scale)) } onChange(scale) } const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(update) : null if (ro) ro.observe(stagesEl) update() return { // Measure again now: a view changed what bounds the phone, which // from 1100px need not change the stages' size (they fill the pane // over the notes card in every view), so the observer may not hear. update, destroy () { if (ro) ro.disconnect() } } }