| 1 | // presenter-keys.mjs — the keys of spec §5.5, shared by the presenter |
| 2 | // window and the slide window. |
| 3 | // |
| 4 | // → Space PageDown, clicker next press |
| 5 | // ← PageUp, clicker back one press |
| 6 | // Home, End first and last slide of the talk |
| 7 | // a number, then Enter go to that slide |
| 8 | // B or . black screen |
| 9 | // O core or full |
| 10 | // P open the presenter window |
| 11 | // R reset the timer |
| 12 | // Esc close what is open; leave a field |
| 13 | // |
| 14 | // A key held down is one press: its repeats are ignored. PageUp and |
| 15 | // PageDown move the talk even from inside a field, because a clicker |
| 16 | // sends them; every other key typed into a field is the field's. |
| 17 | // |
| 18 | // createKeys({act}) returns the keydown handler. act receives one of |
| 19 | // {move: {op}}, {jump: n}, {open: 'presenter'}, {timer: 'reset'} or |
| 20 | // {escape: true}; the window decides what each does. |
| 21 | |
| 22 | const MOVES = { |
| 23 | ArrowRight: 'next', |
| 24 | ' ': 'next', |
| 25 | PageDown: 'next', |
| 26 | ArrowLeft: 'back', |
| 27 | PageUp: 'back', |
| 28 | Home: 'first', |
| 29 | End: 'last', |
| 30 | b: 'black', |
| 31 | B: 'black', |
| 32 | '.': 'black', |
| 33 | o: 'mode', |
| 34 | O: 'mode' |
| 35 | } |
| 36 | |
| 37 | // The keys a clicker sends, which work wherever focus is. |
| 38 | const CLICKER = new Set(['PageDown', 'PageUp']) |
| 39 | |
| 40 | /** Whether the event was typed into something that takes text. */ |
| 41 | export function inField (target) { |
| 42 | if (!target) return false |
| 43 | if (target.isContentEditable) return true |
| 44 | const tag = String(target.tagName || '').toUpperCase() |
| 45 | return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' |
| 46 | } |
| 47 | |
| 48 | export function createKeys ({ act }) { |
| 49 | // The digits typed so far, waiting for Enter. |
| 50 | let number = '' |
| 51 | |
| 52 | return function onKey (e) { |
| 53 | if (e.metaKey || e.ctrlKey || e.altKey) return |
| 54 | const k = e.key |
| 55 | if (k === 'Escape') { |
| 56 | number = '' |
| 57 | act({ escape: true }) |
| 58 | return |
| 59 | } |
| 60 | if (inField(e.target) && !CLICKER.has(k)) return |
| 61 | // One press per press of the key, however long it is held. |
| 62 | if (e.repeat) { |
| 63 | if (k in MOVES) e.preventDefault() |
| 64 | return |
| 65 | } |
| 66 | if (/^[0-9]$/.test(k)) { |
| 67 | number = (number + k).slice(-4) |
| 68 | e.preventDefault() |
| 69 | return |
| 70 | } |
| 71 | if (k === 'Enter') { |
| 72 | if (!number) return |
| 73 | const n = Number(number) |
| 74 | number = '' |
| 75 | e.preventDefault() |
| 76 | if (n > 0) act({ jump: n }) |
| 77 | return |
| 78 | } |
| 79 | number = '' |
| 80 | let action = null |
| 81 | if (k in MOVES) action = { move: { op: MOVES[k] } } |
| 82 | else if (k === 'p' || k === 'P') action = { open: 'presenter' } |
| 83 | else if (k === 'r' || k === 'R') action = { timer: 'reset' } |
| 84 | if (!action) return |
| 85 | e.preventDefault() |
| 86 | act(action) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Presses the keys static/presskeys.js kept before the window's script |
| 92 | * mounted, through handle (createKeys's handler), in order, as if they |
| 93 | * were pressed now; it stops keeping them. Returns how many there were. |
| 94 | * Called once, right after the window's own keydown listener is on, so |
| 95 | * no key falls between the two. |
| 96 | */ |
| 97 | export function takeEarlyKeys (handle, win = globalThis) { |
| 98 | const take = win && typeof win.slidesEarlyKeys === 'function' ? win.slidesEarlyKeys : null |
| 99 | if (!take) return 0 |
| 100 | const kept = take() || [] |
| 101 | for (const k of kept) { |
| 102 | handle({ key: k.key, repeat: !!k.repeat, target: k.target, metaKey: false, ctrlKey: false, altKey: false, preventDefault () {} }) |
| 103 | } |
| 104 | return kept.length |
| 105 | } |
| 106 | |