// presenter-keys.mjs โ€” the keys of spec ยง5.5, shared by the presenter // window and the slide window. // // โ†’ Space PageDown, clicker next press // โ† PageUp, clicker back one press // Home, End first and last slide of the talk // a number, then Enter go to that slide // B or . black screen // O core or full // P open the presenter window // R reset the timer // Esc close what is open; leave a field // // A key held down is one press: its repeats are ignored. PageUp and // PageDown move the talk even from inside a field, because a clicker // sends them; every other key typed into a field is the field's. // // createKeys({act}) returns the keydown handler. act receives one of // {move: {op}}, {jump: n}, {open: 'presenter'}, {timer: 'reset'} or // {escape: true}; the window decides what each does. const MOVES = { ArrowRight: 'next', ' ': 'next', PageDown: 'next', ArrowLeft: 'back', PageUp: 'back', Home: 'first', End: 'last', b: 'black', B: 'black', '.': 'black', o: 'mode', O: 'mode' } // The keys a clicker sends, which work wherever focus is. const CLICKER = new Set(['PageDown', 'PageUp']) /** Whether the event was typed into something that takes text. */ export function inField (target) { if (!target) return false if (target.isContentEditable) return true const tag = String(target.tagName || '').toUpperCase() return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' } export function createKeys ({ act }) { // The digits typed so far, waiting for Enter. let number = '' return function onKey (e) { if (e.metaKey || e.ctrlKey || e.altKey) return const k = e.key if (k === 'Escape') { number = '' act({ escape: true }) return } if (inField(e.target) && !CLICKER.has(k)) return // One press per press of the key, however long it is held. if (e.repeat) { if (k in MOVES) e.preventDefault() return } if (/^[0-9]$/.test(k)) { number = (number + k).slice(-4) e.preventDefault() return } if (k === 'Enter') { if (!number) return const n = Number(number) number = '' e.preventDefault() if (n > 0) act({ jump: n }) return } number = '' let action = null if (k in MOVES) action = { move: { op: MOVES[k] } } else if (k === 'p' || k === 'P') action = { open: 'presenter' } else if (k === 'r' || k === 'R') action = { timer: 'reset' } if (!action) return e.preventDefault() act(action) } } /** * Presses the keys static/presskeys.js kept before the window's script * mounted, through handle (createKeys's handler), in order, as if they * were pressed now; it stops keeping them. Returns how many there were. * Called once, right after the window's own keydown listener is on, so * no key falls between the two. */ export function takeEarlyKeys (handle, win = globalThis) { const take = win && typeof win.slidesEarlyKeys === 'function' ? win.slidesEarlyKeys : null if (!take) return 0 const kept = take() || [] for (const k of kept) { handle({ key: k.key, repeat: !!k.repeat, target: k.target, metaKey: false, ctrlKey: false, altKey: false, preventDefault () {} }) } return kept.length }