summaryrefslogtreecommitdiff
path: root/2024-06-18-guix-social/reveal.js/js/controllers/keyboard.js
blob: 5666b44da288dd0655b13cd0b56cb851bdec91c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import { enterFullscreen } from '../utils/util.js'

/**
 * Handles all reveal.js keyboard interactions.
 */
export default class Keyboard {

	constructor( Reveal ) {

		this.Reveal = Reveal;

		// A key:value map of keyboard keys and descriptions of
		// the actions they trigger
		this.shortcuts = {};

		// Holds custom key code mappings
		this.bindings = {};

		this.onDocumentKeyDown = this.onDocumentKeyDown.bind( this );

	}

	/**
	 * Called when the reveal.js config is updated.
	 */
	configure( config, oldConfig ) {

		if( config.navigationMode === 'linear' ) {
			this.shortcuts['→  ,  ↓  ,  SPACE  ,  N  ,  L  ,  J'] = 'Next slide';
			this.shortcuts['←  ,  ↑  ,  P  ,  H  ,  K']           = 'Previous slide';
		}
		else {
			this.shortcuts['N  ,  SPACE']   = 'Next slide';
			this.shortcuts['P  ,  Shift SPACE']             = 'Previous slide';
			this.shortcuts['←  ,  H'] = 'Navigate left';
			this.shortcuts['→  ,  L'] = 'Navigate right';
			this.shortcuts['↑  ,  K'] = 'Navigate up';
			this.shortcuts['↓  ,  J'] = 'Navigate down';
		}

		this.shortcuts['Alt + ←/&#8593/→/↓']        = 'Navigate without fragments';
		this.shortcuts['Shift + ←/&#8593/→/↓']      = 'Jump to first/last slide';
		this.shortcuts['B  ,  .']                       = 'Pause';
		this.shortcuts['F']                             = 'Fullscreen';
		this.shortcuts['G']                             = 'Jump to slide';
		this.shortcuts['ESC, O']                        = 'Slide overview';

	}

	/**
	 * Starts listening for keyboard events.
	 */
	bind() {

		document.addEventListener( 'keydown', this.onDocumentKeyDown, false );

	}

	/**
	 * Stops listening for keyboard events.
	 */
	unbind() {

		document.removeEventListener( 'keydown', this.onDocumentKeyDown, false );

	}

	/**
	 * Add a custom key binding with optional description to
	 * be added to the help screen.
	 */
	addKeyBinding( binding, callback ) {

		if( typeof binding === 'object' && binding.keyCode ) {
			this.bindings[binding.keyCode] = {
				callback: callback,
				key: binding.key,
				description: binding.description
			};
		}
		else {
			this.bindings[binding] = {
				callback: callback,
				key: null,
				description: null
			};
		}

	}

	/**
	 * Removes the specified custom key binding.
	 */
	removeKeyBinding( keyCode ) {

		delete this.bindings[keyCode];

	}

	/**
	 * Programmatically triggers a keyboard event
	 *
	 * @param {int} keyCode
	 */
	triggerKey( keyCode ) {

		this.onDocumentKeyDown( { keyCode } );

	}

	/**
	 * Registers a new shortcut to include in the help overlay
	 *
	 * @param {String} key
	 * @param {String} value
	 */
	registerKeyboardShortcut( key, value ) {

		this.shortcuts[key] = value;

	}

	getShortcuts() {

		return this.shortcuts;

	}

	getBindings() {

		return this.bindings;

	}

	/**
	 * Handler for the document level 'keydown' event.
	 *
	 * @param {object} event
	 */
	onDocumentKeyDown( event ) {

		let config = this.Reveal.getConfig();

		// If there's a condition specified and it returns false,
		// ignore this event
		if( typeof config.keyboardCondition === 'function' && config.keyboardCondition(event) === false ) {
			return true;
		}

		// If keyboardCondition is set, only capture keyboard events
		// for embedded decks when they are focused
		if( config.keyboardCondition === 'focused' && !this.Reveal.isFocused() ) {
			return true;
		}

		// Shorthand
		let keyCode = event.keyCode;

		// Remember if auto-sliding was paused so we can toggle it
		let autoSlideWasPaused = !this.Reveal.isAutoSliding();

		this.Reveal.onUserInput( event );

		// Is there a focused element that could be using the keyboard?
		let activeElementIsCE = document.activeElement && document.activeElement.isContentEditable === true;
		let activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
		let activeElementIsNotes = document.activeElement && document.activeElement.className && /speaker-notes/i.test( document.activeElement.className);

		// Whitelist certain modifiers for slide navigation shortcuts
		let keyCodeUsesModifier = [32, 37, 38, 39, 40, 63, 78, 80, 191].indexOf( event.keyCode ) !== -1;

		// Prevent all other events when a modifier is pressed
		let unusedModifier = 	!( keyCodeUsesModifier && event.shiftKey || event.altKey ) &&
								( event.shiftKey || event.altKey || event.ctrlKey || event.metaKey );

		// Disregard the event if there's a focused element or a
		// keyboard modifier key is present
		if( activeElementIsCE || activeElementIsInput || activeElementIsNotes || unusedModifier ) return;

		// While paused only allow resume keyboard events; 'b', 'v', '.'
		let resumeKeyCodes = [66,86,190,191,112];
		let key;

		// Custom key bindings for togglePause should be able to resume
		if( typeof config.keyboard === 'object' ) {
			for( key in config.keyboard ) {
				if( config.keyboard[key] === 'togglePause' ) {
					resumeKeyCodes.push( parseInt( key, 10 ) );
				}
			}
		}

		if( this.Reveal.isPaused() && resumeKeyCodes.indexOf( keyCode ) === -1 ) {
			return false;
		}

		// Use linear navigation if we're configured to OR if
		// the presentation is one-dimensional
		let useLinearMode = config.navigationMode === 'linear' || !this.Reveal.hasHorizontalSlides() || !this.Reveal.hasVerticalSlides();

		let triggered = false;

		// 1. User defined key bindings
		if( typeof config.keyboard === 'object' ) {

			for( key in config.keyboard ) {

				// Check if this binding matches the pressed key
				if( parseInt( key, 10 ) === keyCode ) {

					let value = config.keyboard[ key ];

					// Callback function
					if( typeof value === 'function' ) {
						value.apply( null, [ event ] );
					}
					// String shortcuts to reveal.js API
					else if( typeof value === 'string' && typeof this.Reveal[ value ] === 'function' ) {
						this.Reveal[ value ].call();
					}

					triggered = true;

				}

			}

		}

		// 2. Registered custom key bindings
		if( triggered === false ) {

			for( key in this.bindings ) {

				// Check if this binding matches the pressed key
				if( parseInt( key, 10 ) === keyCode ) {

					let action = this.bindings[ key ].callback;

					// Callback function
					if( typeof action === 'function' ) {
						action.apply( null, [ event ] );
					}
					// String shortcuts to reveal.js API
					else if( typeof action === 'string' && typeof this.Reveal[ action ] === 'function' ) {
						this.Reveal[ action ].call();
					}

					triggered = true;
				}
			}
		}

		// 3. System defined key bindings
		if( triggered === false ) {

			// Assume true and try to prove false
			triggered = true;

			// P, PAGE UP
			if( keyCode === 80 || keyCode === 33 ) {
				this.Reveal.prev({skipFragments: event.altKey});
			}
			// N, PAGE DOWN
			else if( keyCode === 78 || keyCode === 34 ) {
				this.Reveal.next({skipFragments: event.altKey});
			}
			// H, LEFT
			else if( keyCode === 72 || keyCode === 37 ) {
				if( event.shiftKey ) {
					this.Reveal.slide( 0 );
				}
				else if( !this.Reveal.overview.isActive() && useLinearMode ) {
					if( config.rtl ) {
						this.Reveal.next({skipFragments: event.altKey});
					}
					else {
						this.Reveal.prev({skipFragments: event.altKey});
					}
				}
				else {
					this.Reveal.left({skipFragments: event.altKey});
				}
			}
			// L, RIGHT
			else if( keyCode === 76 || keyCode === 39 ) {
				if( event.shiftKey ) {
					this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 );
				}
				else if( !this.Reveal.overview.isActive() && useLinearMode ) {
					if( config.rtl ) {
						this.Reveal.prev({skipFragments: event.altKey});
					}
					else {
						this.Reveal.next({skipFragments: event.altKey});
					}
				}
				else {
					this.Reveal.right({skipFragments: event.altKey});
				}
			}
			// K, UP
			else if( keyCode === 75 || keyCode === 38 ) {
				if( event.shiftKey ) {
					this.Reveal.slide( undefined, 0 );
				}
				else if( !this.Reveal.overview.isActive() && useLinearMode ) {
					this.Reveal.prev({skipFragments: event.altKey});
				}
				else {
					this.Reveal.up({skipFragments: event.altKey});
				}
			}
			// J, DOWN
			else if( keyCode === 74 || keyCode === 40 ) {
				if( event.shiftKey ) {
					this.Reveal.slide( undefined, Number.MAX_VALUE );
				}
				else if( !this.Reveal.overview.isActive() && useLinearMode ) {
					this.Reveal.next({skipFragments: event.altKey});
				}
				else {
					this.Reveal.down({skipFragments: event.altKey});
				}
			}
			// HOME
			else if( keyCode === 36 ) {
				this.Reveal.slide( 0 );
			}
			// END
			else if( keyCode === 35 ) {
				this.Reveal.slide( this.Reveal.getHorizontalSlides().length - 1 );
			}
			// SPACE
			else if( keyCode === 32 ) {
				if( this.Reveal.overview.isActive() ) {
					this.Reveal.overview.deactivate();
				}
				if( event.shiftKey ) {
					this.Reveal.prev({skipFragments: event.altKey});
				}
				else {
					this.Reveal.next({skipFragments: event.altKey});
				}
			}
			// TWO-SPOT, SEMICOLON, B, V, PERIOD, LOGITECH PRESENTER TOOLS "BLACK SCREEN" BUTTON
			else if( [58, 59, 66, 86, 190].includes( keyCode ) || ( keyCode === 191 && !event.shiftKey ) ) {
				this.Reveal.togglePause();
			}
			// F
			else if( keyCode === 70 ) {
				enterFullscreen( config.embedded ? this.Reveal.getViewportElement() : document.documentElement );
			}
			// A
			else if( keyCode === 65 ) {
				if( config.autoSlideStoppable ) {
					this.Reveal.toggleAutoSlide( autoSlideWasPaused );
				}
			}
			// G
			else if( keyCode === 71 ) {
				if( config.jumpToSlide ) {
					this.Reveal.toggleJumpToSlide();
				}
			}
			// ?
			else if( ( keyCode === 63 || keyCode === 191 ) && event.shiftKey ) {
				this.Reveal.toggleHelp();
			}
			// F1
			else if( keyCode === 112 ) {
				this.Reveal.toggleHelp();
			}
			else {
				triggered = false;
			}

		}

		// If the input resulted in a triggered action we should prevent
		// the browsers default behavior
		if( triggered ) {
			event.preventDefault && event.preventDefault();
		}
		// ESC or O key
		else if( keyCode === 27 || keyCode === 79 ) {
			if( this.Reveal.closeOverlay() === false ) {
				this.Reveal.overview.toggle();
			}

			event.preventDefault && event.preventDefault();
		}

		// If auto-sliding is enabled we need to cue up
		// another timeout
		this.Reveal.cueAutoSlide();

	}

}