Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 1 | import { enterFullscreen } from '../utils/util.js' |
| 2 | |
| 3 | /** |
| 4 | * Handles all reveal.js keyboard interactions. |
| 5 | */ |
| 6 | export default class Keyboard { |
| 7 | |
| 8 | constructor( Reveal ) { |
| 9 | |
| 10 | this.Reveal = Reveal; |
| 11 | |
| 12 | // A key:value map of keyboard keys and descriptions of |
| 13 | // the actions they trigger |
| 14 | this.shortcuts = {}; |
| 15 | |
| 16 | // Holds custom key code mappings |
| 17 | this.bindings = {}; |
| 18 | |
| 19 | this.onDocumentKeyDown = this.onDocumentKeyDown.bind( this ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 20 | |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Called when the reveal.js config is updated. |
| 25 | */ |
| 26 | configure( config, oldConfig ) { |
| 27 | |
| 28 | if( config.navigationMode === 'linear' ) { |
| 29 | this.shortcuts['→ , ↓ , SPACE , N , L , J'] = 'Next slide'; |
| 30 | this.shortcuts['← , ↑ , P , H , K'] = 'Previous slide'; |
| 31 | } |
| 32 | else { |
| 33 | this.shortcuts['N , SPACE'] = 'Next slide'; |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 34 | this.shortcuts['P , Shift SPACE'] = 'Previous slide'; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 35 | this.shortcuts['← , H'] = 'Navigate left'; |
| 36 | this.shortcuts['→ , L'] = 'Navigate right'; |
| 37 | this.shortcuts['↑ , K'] = 'Navigate up'; |
| 38 | this.shortcuts['↓ , J'] = 'Navigate down'; |
| 39 | } |
| 40 | |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 41 | this.shortcuts['Alt + ←/↑/→/↓'] = 'Navigate without fragments'; |
| 42 | this.shortcuts['Shift + ←/↑/→/↓'] = 'Jump to first/last slide'; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 43 | this.shortcuts['B , .'] = 'Pause'; |
| 44 | this.shortcuts['F'] = 'Fullscreen'; |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 45 | this.shortcuts['G'] = 'Jump to slide'; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 46 | this.shortcuts['ESC, O'] = 'Slide overview'; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Starts listening for keyboard events. |
| 52 | */ |
| 53 | bind() { |
| 54 | |
| 55 | document.addEventListener( 'keydown', this.onDocumentKeyDown, false ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Stops listening for keyboard events. |
| 61 | */ |
| 62 | unbind() { |
| 63 | |
| 64 | document.removeEventListener( 'keydown', this.onDocumentKeyDown, false ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 65 | |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add a custom key binding with optional description to |
| 70 | * be added to the help screen. |
| 71 | */ |
| 72 | addKeyBinding( binding, callback ) { |
| 73 | |
| 74 | if( typeof binding === 'object' && binding.keyCode ) { |
| 75 | this.bindings[binding.keyCode] = { |
| 76 | callback: callback, |
| 77 | key: binding.key, |
| 78 | description: binding.description |
| 79 | }; |
| 80 | } |
| 81 | else { |
| 82 | this.bindings[binding] = { |
| 83 | callback: callback, |
| 84 | key: null, |
| 85 | description: null |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Removes the specified custom key binding. |
| 93 | */ |
| 94 | removeKeyBinding( keyCode ) { |
| 95 | |
| 96 | delete this.bindings[keyCode]; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Programmatically triggers a keyboard event |
| 102 | * |
| 103 | * @param {int} keyCode |
| 104 | */ |
| 105 | triggerKey( keyCode ) { |
| 106 | |
| 107 | this.onDocumentKeyDown( { keyCode } ); |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Registers a new shortcut to include in the help overlay |
| 113 | * |
| 114 | * @param {String} key |
| 115 | * @param {String} value |
| 116 | */ |
| 117 | registerKeyboardShortcut( key, value ) { |
| 118 | |
| 119 | this.shortcuts[key] = value; |
| 120 | |
| 121 | } |
| 122 | |
| 123 | getShortcuts() { |
| 124 | |
| 125 | return this.shortcuts; |
| 126 | |
| 127 | } |
| 128 | |
| 129 | getBindings() { |
| 130 | |
| 131 | return this.bindings; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | /** |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 136 | * Handler for the document level 'keydown' event. |
| 137 | * |
| 138 | * @param {object} event |
| 139 | */ |
| 140 | onDocumentKeyDown( event ) { |
| 141 | |
| 142 | let config = this.Reveal.getConfig(); |
| 143 | |
| 144 | // If there's a condition specified and it returns false, |
| 145 | // ignore this event |
| 146 | if( typeof config.keyboardCondition === 'function' && config.keyboardCondition(event) === false ) { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | // If keyboardCondition is set, only capture keyboard events |
| 151 | // for embedded decks when they are focused |
| 152 | if( config.keyboardCondition === 'focused' && !this.Reveal.isFocused() ) { |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | // Shorthand |
| 157 | let keyCode = event.keyCode; |
| 158 | |
| 159 | // Remember if auto-sliding was paused so we can toggle it |
| 160 | let autoSlideWasPaused = !this.Reveal.isAutoSliding(); |
| 161 | |
| 162 | this.Reveal.onUserInput( event ); |
| 163 | |
| 164 | // Is there a focused element that could be using the keyboard? |
| 165 | let activeElementIsCE = document.activeElement && document.activeElement.isContentEditable === true; |
| 166 | let activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName ); |
| 167 | let activeElementIsNotes = document.activeElement && document.activeElement.className && /speaker-notes/i.test( document.activeElement.className); |
| 168 | |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 169 | // Whitelist certain modifiers for slide navigation shortcuts |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 170 | let keyCodeUsesModifier = [32, 37, 38, 39, 40, 63, 78, 80, 191].indexOf( event.keyCode ) !== -1; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 171 | |
| 172 | // Prevent all other events when a modifier is pressed |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 173 | let unusedModifier = !( keyCodeUsesModifier && event.shiftKey || event.altKey ) && |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 174 | ( event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ); |
| 175 | |
| 176 | // Disregard the event if there's a focused element or a |
| 177 | // keyboard modifier key is present |
| 178 | if( activeElementIsCE || activeElementIsInput || activeElementIsNotes || unusedModifier ) return; |
| 179 | |
| 180 | // While paused only allow resume keyboard events; 'b', 'v', '.' |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 181 | let resumeKeyCodes = [66,86,190,191,112]; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 182 | let key; |
| 183 | |
| 184 | // Custom key bindings for togglePause should be able to resume |
| 185 | if( typeof config.keyboard === 'object' ) { |
| 186 | for( key in config.keyboard ) { |
| 187 | if( config.keyboard[key] === 'togglePause' ) { |
| 188 | resumeKeyCodes.push( parseInt( key, 10 ) ); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if( this.Reveal.isPaused() && resumeKeyCodes.indexOf( keyCode ) === -1 ) { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | // Use linear navigation if we're configured to OR if |
| 198 | // the presentation is one-dimensional |
| 199 | let useLinearMode = config.navigationMode === 'linear' || !this.Reveal.hasHorizontalSlides() || !this.Reveal.hasVerticalSlides(); |
| 200 | |
| 201 | let triggered = false; |
| 202 | |
| 203 | // 1. User defined key bindings |
| 204 | if( typeof config.keyboard === 'object' ) { |
| 205 | |
| 206 | for( key in config.keyboard ) { |
| 207 | |
| 208 | // Check if this binding matches the pressed key |
| 209 | if( parseInt( key, 10 ) === keyCode ) { |
| 210 | |
| 211 | let value = config.keyboard[ key ]; |
| 212 | |
| 213 | // Callback function |
| 214 | if( typeof value === 'function' ) { |
| 215 | value.apply( null, [ event ] ); |
| 216 | } |
| 217 | // String shortcuts to reveal.js API |
| 218 | else if( typeof value === 'string' && typeof this.Reveal[ value ] === 'function' ) { |
| 219 | this.Reveal[ value ].call(); |
| 220 | } |
| 221 | |
| 222 | triggered = true; |
| 223 | |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
| 228 | } |
| 229 | |
| 230 | // 2. Registered custom key bindings |
| 231 | if( triggered === false ) { |
| 232 | |
| 233 | for( key in this.bindings ) { |
| 234 | |
| 235 | // Check if this binding matches the pressed key |
| 236 | if( parseInt( key, 10 ) === keyCode ) { |
| 237 | |
| 238 | let action = this.bindings[ key ].callback; |
| 239 | |
| 240 | // Callback function |
| 241 | if( typeof action === 'function' ) { |
| 242 | action.apply( null, [ event ] ); |
| 243 | } |
| 244 | // String shortcuts to reveal.js API |
| 245 | else if( typeof action === 'string' && typeof this.Reveal[ action ] === 'function' ) { |
| 246 | this.Reveal[ action ].call(); |
| 247 | } |
| 248 | |
| 249 | triggered = true; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // 3. System defined key bindings |
| 255 | if( triggered === false ) { |
| 256 | |
| 257 | // Assume true and try to prove false |
| 258 | triggered = true; |
| 259 | |
| 260 | // P, PAGE UP |
| 261 | if( keyCode === 80 || keyCode === 33 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 262 | this.Reveal.prev({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 263 | } |
| 264 | // N, PAGE DOWN |
| 265 | else if( keyCode === 78 || keyCode === 34 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 266 | this.Reveal.next({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 267 | } |
| 268 | // H, LEFT |
| 269 | else if( keyCode === 72 || keyCode === 37 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 270 | if( event.shiftKey ) { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 271 | this.Reveal.slide( 0 ); |
| 272 | } |
| 273 | else if( !this.Reveal.overview.isActive() && useLinearMode ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 274 | if( config.rtl ) { |
| 275 | this.Reveal.next({skipFragments: event.altKey}); |
| 276 | } |
| 277 | else { |
| 278 | this.Reveal.prev({skipFragments: event.altKey}); |
| 279 | } |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 280 | } |
| 281 | else { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 282 | this.Reveal.left({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | // L, RIGHT |
| 286 | else if( keyCode === 76 || keyCode === 39 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 287 | if( event.shiftKey ) { |
| 288 | this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 289 | } |
| 290 | else if( !this.Reveal.overview.isActive() && useLinearMode ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 291 | if( config.rtl ) { |
| 292 | this.Reveal.prev({skipFragments: event.altKey}); |
| 293 | } |
| 294 | else { |
| 295 | this.Reveal.next({skipFragments: event.altKey}); |
| 296 | } |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 297 | } |
| 298 | else { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 299 | this.Reveal.right({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | // K, UP |
| 303 | else if( keyCode === 75 || keyCode === 38 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 304 | if( event.shiftKey ) { |
| 305 | this.Reveal.slide( undefined, 0 ); |
| 306 | } |
| 307 | else if( !this.Reveal.overview.isActive() && useLinearMode ) { |
| 308 | this.Reveal.prev({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 309 | } |
| 310 | else { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 311 | this.Reveal.up({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | // J, DOWN |
| 315 | else if( keyCode === 74 || keyCode === 40 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 316 | if( event.shiftKey ) { |
| 317 | this.Reveal.slide( undefined, Number.MAX_VALUE ); |
| 318 | } |
| 319 | else if( !this.Reveal.overview.isActive() && useLinearMode ) { |
| 320 | this.Reveal.next({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 321 | } |
| 322 | else { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 323 | this.Reveal.down({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | // HOME |
| 327 | else if( keyCode === 36 ) { |
| 328 | this.Reveal.slide( 0 ); |
| 329 | } |
| 330 | // END |
| 331 | else if( keyCode === 35 ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 332 | this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 333 | } |
| 334 | // SPACE |
| 335 | else if( keyCode === 32 ) { |
| 336 | if( this.Reveal.overview.isActive() ) { |
| 337 | this.Reveal.overview.deactivate(); |
| 338 | } |
| 339 | if( event.shiftKey ) { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 340 | this.Reveal.prev({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 341 | } |
| 342 | else { |
Christophe Dervieux | 8afae13 | 2021-12-06 15:16:42 +0100 | [diff] [blame] | 343 | this.Reveal.next({skipFragments: event.altKey}); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | // TWO-SPOT, SEMICOLON, B, V, PERIOD, LOGITECH PRESENTER TOOLS "BLACK SCREEN" BUTTON |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 347 | else if( [58, 59, 66, 86, 190].includes( keyCode ) || ( keyCode === 191 && !event.shiftKey ) ) { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 348 | this.Reveal.togglePause(); |
| 349 | } |
| 350 | // F |
| 351 | else if( keyCode === 70 ) { |
| 352 | enterFullscreen( config.embedded ? this.Reveal.getViewportElement() : document.documentElement ); |
| 353 | } |
| 354 | // A |
| 355 | else if( keyCode === 65 ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 356 | if( config.autoSlideStoppable ) { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 357 | this.Reveal.toggleAutoSlide( autoSlideWasPaused ); |
| 358 | } |
| 359 | } |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 360 | // G |
| 361 | else if( keyCode === 71 ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 362 | if( config.jumpToSlide ) { |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 363 | this.Reveal.toggleJumpToSlide(); |
| 364 | } |
| 365 | } |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 366 | // ? |
| 367 | else if( ( keyCode === 63 || keyCode === 191 ) && event.shiftKey ) { |
| 368 | this.Reveal.toggleHelp(); |
| 369 | } |
| 370 | // F1 |
| 371 | else if( keyCode === 112 ) { |
| 372 | this.Reveal.toggleHelp(); |
| 373 | } |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 374 | else { |
| 375 | triggered = false; |
| 376 | } |
| 377 | |
| 378 | } |
| 379 | |
| 380 | // If the input resulted in a triggered action we should prevent |
| 381 | // the browsers default behavior |
| 382 | if( triggered ) { |
| 383 | event.preventDefault && event.preventDefault(); |
| 384 | } |
| 385 | // ESC or O key |
| 386 | else if( keyCode === 27 || keyCode === 79 ) { |
| 387 | if( this.Reveal.closeOverlay() === false ) { |
| 388 | this.Reveal.overview.toggle(); |
| 389 | } |
| 390 | |
| 391 | event.preventDefault && event.preventDefault(); |
| 392 | } |
| 393 | |
| 394 | // If auto-sliding is enabled we need to cue up |
| 395 | // another timeout |
| 396 | this.Reveal.cueAutoSlide(); |
| 397 | |
| 398 | } |
| 399 | |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 400 | } |