Keyboard shortcut tweaks
This commit is contained in:
2
dist/plyr.js
vendored
2
dist/plyr.js
vendored
File diff suppressed because one or more lines are too long
2
dist/plyr.js.map
vendored
2
dist/plyr.js.map
vendored
File diff suppressed because one or more lines are too long
@ -137,6 +137,21 @@ const listeners = {
|
|||||||
const inputEvent = this.browser.isIE ? 'change' : 'input';
|
const inputEvent = this.browser.isIE ? 'change' : 'input';
|
||||||
let last = null;
|
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
|
// Click play/pause helper
|
||||||
const togglePlay = () => {
|
const togglePlay = () => {
|
||||||
const play = this.togglePlay();
|
const play = this.togglePlay();
|
||||||
@ -153,6 +168,7 @@ const listeners = {
|
|||||||
// Get the key code for an event
|
// Get the key code for an event
|
||||||
const getKeyCode = event => (event.keyCode ? event.keyCode : event.which);
|
const getKeyCode = event => (event.keyCode ? event.keyCode : event.which);
|
||||||
|
|
||||||
|
// Handle key press
|
||||||
const handleKey = event => {
|
const handleKey = event => {
|
||||||
const code = getKeyCode(event);
|
const code = getKeyCode(event);
|
||||||
const pressed = event.type === 'keydown';
|
const pressed = event.type === 'keydown';
|
||||||
@ -197,14 +213,13 @@ const listeners = {
|
|||||||
76,
|
76,
|
||||||
79,
|
79,
|
||||||
];
|
];
|
||||||
const checkFocus = [38, 40];
|
|
||||||
|
|
||||||
if (checkFocus.includes(code)) {
|
// Check focused element
|
||||||
const focused = utils.getFocusElement();
|
// and if the focused element is not editable (e.g. text input)
|
||||||
|
// and any that accept key input http://webaim.org/techniques/keyboard/
|
||||||
if (utils.is.htmlElement(focused) && utils.getFocusElement().type === 'radio') {
|
const focused = utils.getFocusElement();
|
||||||
return;
|
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)
|
// If the code is found prevent default (e.g. prevent scrolling for arrows)
|
||||||
@ -234,7 +249,7 @@ const listeners = {
|
|||||||
case 75:
|
case 75:
|
||||||
// Space and K key
|
// Space and K key
|
||||||
if (!held) {
|
if (!held) {
|
||||||
togglePlay();
|
this.togglePlay();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -308,32 +323,9 @@ const listeners = {
|
|||||||
|
|
||||||
// Keyboard shortcuts
|
// Keyboard shortcuts
|
||||||
if (this.config.keyboard.focused) {
|
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);
|
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
|
// Detect tab focus
|
||||||
@ -355,21 +347,6 @@ const listeners = {
|
|||||||
}, 0);
|
}, 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
|
// Play
|
||||||
utils.on(this.elements.buttons.play, 'click', event => proxy(event, 'play', togglePlay));
|
utils.on(this.elements.buttons.play, 'click', event => proxy(event, 'play', togglePlay));
|
||||||
|
|
||||||
|
@ -214,6 +214,9 @@ class Plyr {
|
|||||||
this.elements.container = utils.createElement('div');
|
this.elements.container = utils.createElement('div');
|
||||||
utils.wrap(this.media, this.elements.container);
|
utils.wrap(this.media, this.elements.container);
|
||||||
|
|
||||||
|
// Allow focus to be captured
|
||||||
|
this.elements.container.setAttribute('tabindex', 0);
|
||||||
|
|
||||||
// Add style hook
|
// Add style hook
|
||||||
ui.addStyleHook.call(this);
|
ui.addStyleHook.call(this);
|
||||||
|
|
||||||
@ -287,19 +290,13 @@ class Plyr {
|
|||||||
|
|
||||||
// Rewind
|
// Rewind
|
||||||
rewind(seekTime) {
|
rewind(seekTime) {
|
||||||
this.currentTime = Math.min(
|
this.currentTime = this.currentTime - (utils.is.number(seekTime) ? seekTime : this.config.seekTime);
|
||||||
this.currentTime - (utils.is.number(seekTime) ? seekTime : this.config.seekTime),
|
|
||||||
0
|
|
||||||
);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fast forward
|
// Fast forward
|
||||||
forward(seekTime) {
|
forward(seekTime) {
|
||||||
this.currentTime = Math.max(
|
this.currentTime = this.currentTime + (utils.is.number(seekTime) ? seekTime : this.config.seekTime);
|
||||||
this.currentTime + (utils.is.number(seekTime) ? seekTime : this.config.seekTime),
|
|
||||||
this.duration
|
|
||||||
);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user