| 1 | // presskeys.js — the presenting windows' keys from before their script |
| 2 | // is there (spec §0.3: a press is never lost). A plain blocking script |
| 3 | // in the <head> of the presenter window and of the slide window that |
| 4 | // drives (templates/presenter.html and present.html), so it listens |
| 5 | // before anything is painted. The window's own script is a module that |
| 6 | // mounts once it has arrived; a clicker pressed before then would |
| 7 | // otherwise do nothing. |
| 8 | // |
| 9 | // It keeps the keys that move the talk (→ ← Space PageDown PageUp, a |
| 10 | // number and its Enter), in order, and nothing else: every other key |
| 11 | // does what it always does. In a field only PageDown and PageUp, as the |
| 12 | // window's own keys (fixtures/presenter-keys.mjs). The window's script, |
| 13 | // mounting, calls slidesEarlyKeys() once: the listener goes, and the |
| 14 | // keys kept are handed over to be pressed as if then |
| 15 | // (presenter-keys.mjs's takeEarlyKeys). |
| 16 | // |
| 17 | // No words, no network: it only remembers. |
| 18 | (function () { |
| 19 | var MOVES = { ArrowRight: 1, ArrowLeft: 1, ' ': 1, PageDown: 1, PageUp: 1 }; |
| 20 | var CLICKER = { PageDown: 1, PageUp: 1 }; |
| 21 | var kept = []; |
| 22 | var digits = false; |
| 23 | |
| 24 | function inField(t) { |
| 25 | if (!t) return false; |
| 26 | if (t.isContentEditable) return true; |
| 27 | var tag = String(t.tagName || '').toUpperCase(); |
| 28 | return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT'; |
| 29 | } |
| 30 | |
| 31 | function keep(e) { |
| 32 | if (e.metaKey || e.ctrlKey || e.altKey) return; |
| 33 | var k = e.key; |
| 34 | if (inField(e.target) && !CLICKER[k]) return; |
| 35 | var digit = /^[0-9]$/.test(k); |
| 36 | // Enter is kept only after a number: otherwise it is the |
| 37 | // focused control's. |
| 38 | if (!MOVES[k] && !digit && !(k === 'Enter' && digits)) return; |
| 39 | digits = digit; |
| 40 | kept.push({ key: k, repeat: e.repeat, target: e.target }); |
| 41 | e.preventDefault(); |
| 42 | } |
| 43 | |
| 44 | document.addEventListener('keydown', keep); |
| 45 | window.slidesEarlyKeys = function () { |
| 46 | document.removeEventListener('keydown', keep); |
| 47 | window.slidesEarlyKeys = null; |
| 48 | return kept.splice(0); |
| 49 | }; |
| 50 | })(); |
| 51 | |