diff options
author | Sam Potts <me@sampotts.me> | 2017-11-05 23:09:33 +1100 |
---|---|---|
committer | Sam Potts <me@sampotts.me> | 2017-11-05 23:09:33 +1100 |
commit | 45df3198237644610aa140bbb12d676324157cb6 (patch) | |
tree | ce6dd888a809fbf38c4b23e56e267ac529b67139 /src | |
parent | 6bebbe41530873e40b3615acb33e2f1fd55fb342 (diff) | |
download | plyr-45df3198237644610aa140bbb12d676324157cb6.tar.lz plyr-45df3198237644610aa140bbb12d676324157cb6.tar.xz plyr-45df3198237644610aa140bbb12d676324157cb6.zip |
Keyboard shortcut tweaks
Diffstat (limited to 'src')
-rw-r--r-- | src/js/listeners.js | 73 | ||||
-rw-r--r-- | src/js/plyr.js | 13 |
2 files changed, 30 insertions, 56 deletions
diff --git a/src/js/listeners.js b/src/js/listeners.js index 350137c1..9b84a729 100644 --- a/src/js/listeners.js +++ b/src/js/listeners.js @@ -137,6 +137,21 @@ const listeners = { const inputEvent = this.browser.isIE ? 'change' : 'input'; let last = null; + // Trigger custom and default handlers + const proxy = (event, handlerKey, defaultHandler) => { + const customHandler = this.config.listeners[handlerKey]; + + // Execute custom handler + if (utils.is.function(customHandler)) { + customHandler.call(this, event); + } + + // Only call default handler if not prevented in custom handler + if (!event.defaultPrevented && utils.is.function(defaultHandler)) { + defaultHandler.call(this, event); + } + }; + // Click play/pause helper const togglePlay = () => { const play = this.togglePlay(); @@ -153,6 +168,7 @@ const listeners = { // Get the key code for an event const getKeyCode = event => (event.keyCode ? event.keyCode : event.which); + // Handle key press const handleKey = event => { const code = getKeyCode(event); const pressed = event.type === 'keydown'; @@ -197,14 +213,13 @@ const listeners = { 76, 79, ]; - const checkFocus = [38, 40]; - - if (checkFocus.includes(code)) { - const focused = utils.getFocusElement(); - if (utils.is.htmlElement(focused) && utils.getFocusElement().type === 'radio') { - return; - } + // Check focused element + // and if the focused element is not editable (e.g. text input) + // and any that accept key input http://webaim.org/techniques/keyboard/ + const focused = utils.getFocusElement(); + if (utils.is.htmlElement(focused) && utils.matches(focused, this.config.selectors.editable)) { + return; } // If the code is found prevent default (e.g. prevent scrolling for arrows) @@ -234,7 +249,7 @@ const listeners = { case 75: // Space and K key if (!held) { - togglePlay(); + this.togglePlay(); } break; @@ -308,32 +323,9 @@ const listeners = { // Keyboard shortcuts if (this.config.keyboard.focused) { - // Handle global presses - if (this.config.keyboard.global) { - utils.on( - window, - 'keydown keyup', - event => { - const code = getKeyCode(event); - const focused = utils.getFocusElement(); - const allowed = [48, 49, 50, 51, 52, 53, 54, 56, 57, 75, 77, 70, 67, 73, 76, 79]; - - // Only handle global key press if key is in the allowed keys - // and if the focused element is not editable (e.g. text input) - // and any that accept key input http://webaim.org/techniques/keyboard/ - if ( - allowed.includes(code) && - (!utils.is.htmlElement(focused) || !utils.matches(focused, this.config.selectors.editable)) - ) { - handleKey(event); - } - }, - false - ); - } - - // Handle presses on focused utils.on(this.elements.container, 'keydown keyup', handleKey, false); + } else if (this.config.keyboard.global) { + utils.on(window, 'keydown keyup', handleKey, false); } // Detect tab focus @@ -355,21 +347,6 @@ const listeners = { }, 0); }); - // Trigger custom and default handlers - const proxy = (event, handlerKey, defaultHandler) => { - const customHandler = this.config.listeners[handleKey]; - - // Execute custom handler - if (utils.is.function(customHandler)) { - customHandler.call(this, event); - } - - // Only call default handler if not prevented in custom handler - if (!event.defaultPrevented && utils.is.function(defaultHandler)) { - defaultHandler.call(this, event); - } - }; - // Play utils.on(this.elements.buttons.play, 'click', event => proxy(event, 'play', togglePlay)); diff --git a/src/js/plyr.js b/src/js/plyr.js index 1cff4231..5c28887e 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -214,6 +214,9 @@ class Plyr { this.elements.container = utils.createElement('div'); utils.wrap(this.media, this.elements.container); + // Allow focus to be captured + this.elements.container.setAttribute('tabindex', 0); + // Add style hook ui.addStyleHook.call(this); @@ -287,19 +290,13 @@ class Plyr { // Rewind rewind(seekTime) { - this.currentTime = Math.min( - this.currentTime - (utils.is.number(seekTime) ? seekTime : this.config.seekTime), - 0 - ); + this.currentTime = this.currentTime - (utils.is.number(seekTime) ? seekTime : this.config.seekTime); return this; } // Fast forward forward(seekTime) { - this.currentTime = Math.max( - this.currentTime + (utils.is.number(seekTime) ? seekTime : this.config.seekTime), - this.duration - ); + this.currentTime = this.currentTime + (utils.is.number(seekTime) ? seekTime : this.config.seekTime); return this; } |