Added programatic support for custom key bindings with optional descriptions to be added to the help screen

This commit is contained in:
Greg Denehy 2017-04-30 15:23:04 +09:30
parent 91c6db71ca
commit 7297474b2e
2 changed files with 70 additions and 15 deletions

View file

@ -232,7 +232,10 @@
'B , .': 'Pause', 'B , .': 'Pause',
'F': 'Fullscreen', 'F': 'Fullscreen',
'ESC, O': 'Slide overview' 'ESC, O': 'Slide overview'
}; },
// Holds custom key code mappings
registeredKeyBindings = {};
/** /**
* Starts up the presentation if the client is capable. * Starts up the presentation if the client is capable.
@ -1091,6 +1094,33 @@
} }
/**
* Add a custom key binding with optional description to be added to the help screen
*/
function addKeyBinding(binding, callback) {
if (typeof binding === 'object' && binding.code) {
registeredKeyBindings[binding.code] = {
callback: callback,
key: binding.key,
description: binding.description
}
}
else {
registeredKeyBindings[binding] = {
callback: callback,
key: null,
description: null
}
}
}
/**
* Removes the specified custom key binding
*/
function removeKeyBinding(binding) {
delete registeredKeyBindings[binding];
}
/** /**
* Extend object a with the properties of object b. * Extend object a with the properties of object b.
* If there's a conflict, object b takes precedence. * If there's a conflict, object b takes precedence.
@ -1518,6 +1548,13 @@
html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>'; html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>';
} }
// add custom key bindings that have associated descriptions
for( var binding in registeredKeyBindings ) {
if (registeredKeyBindings[binding].key && registeredKeyBindings[binding].description) {
html += '<tr><td>' + registeredKeyBindings[binding].key + '</td><td>' + registeredKeyBindings[binding].description + '</td></tr>';
}
}
html += '</table>'; html += '</table>';
dom.overlay.innerHTML = [ dom.overlay.innerHTML = [
@ -3967,7 +4004,31 @@
} }
// 2. System defined key bindings // 2. Registered custom key bindings
if( triggered === false ) {
for( key in registeredKeyBindings ) {
// Check if this binding matches the pressed key
if( parseInt( key, 10 ) === event.keyCode ) {
var value = registeredKeyBindings[ key ].callback;
// Callback function
if( typeof value === 'function' ) {
value.apply( null, [ event ] );
}
// String shortcuts to reveal.js API
else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
Reveal[ value ].call();
}
triggered = true;
}
}
}
// 3. System defined key bindings
if( triggered === false ) { if( triggered === false ) {
// Assume true and try to prove false // Assume true and try to prove false
@ -4676,6 +4737,12 @@
} }
}, },
// Adds a custom key binding
addKeyBinding: addKeyBinding,
// Removes a custom key binding
removeKeyBinding: removeKeyBinding,
// Programatically triggers a keyboard event // Programatically triggers a keyboard event
triggerKey: function( keyCode ) { triggerKey: function( keyCode ) {
onDocumentKeyDown( { keyCode: keyCode } ); onDocumentKeyDown( { keyCode: keyCode } );

View file

@ -106,19 +106,7 @@ var RevealNotes = (function() {
} }
// Open the notes when the 's' key is hit // Open the notes when the 's' key is hit
document.addEventListener( 'keydown', function( event ) { Reveal.addKeyBinding({code: 83, key: 'S', description: 'Speaker notes'}, openNotes);
// Disregard the event if the target is editable or a
// modifier is present
if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
// Disregard the event if keyboard is disabled
if ( Reveal.getConfig().keyboard === false ) return;
if( event.keyCode === 83 ) {
event.preventDefault();
openNotes();
}
}, false );
} }