diff --git a/src/js/plugins/keyboard.js b/src/js/plugins/keyboard.js index ac27e15..9c65a38 100644 --- a/src/js/plugins/keyboard.js +++ b/src/js/plugins/keyboard.js @@ -1,4 +1,5 @@ import Keys from '../utils/keys'; +import DOM from '../utils/dom'; /** * Keyboard plugin. @@ -28,16 +29,8 @@ export default class Keyboard { let method; let argument; - // Check if there's a focused element that might use the keyboard. - if (document.activeElement) { - const isContentEditable = document.activeElement - .contentEditable !== 'inherit'; - const isInput = ['INPUT', 'SELECT', 'OPTION', 'TEXTAREA'] - .indexOf(document.activeElement.tagName) > -1; - - if (isInput || isContentEditable) { - return; - } + if (DOM.isFocusableElement()) { + return; } switch (event.which) { diff --git a/src/js/utils/dom.js b/src/js/utils/dom.js index 648c4c9..181ddf3 100644 --- a/src/js/utils/dom.js +++ b/src/js/utils/dom.js @@ -144,4 +144,25 @@ export default class DOM { static toArray(iterable) { return [].slice.call(iterable); } + + /** + * Checks whether the document has focus on an input or contenteditable + * element. + * @return {boolean} Whether the focused element is an input or content + * editable. + */ + static isFocusableElement() { + let result = false; + + if (document.activeElement) { + const isContentEditable = document.activeElement + .contentEditable !== 'inherit'; + const isInput = ['INPUT', 'SELECT', 'OPTION', 'TEXTAREA'] + .indexOf(document.activeElement.tagName) > -1; + + result = isInput || isContentEditable; + } + + return result; + } }