Skip to content

Commit d6d5555

Browse files
authored
Merge pull request #192 from antidis/antidis/fix-keyboard-shortcuts-error
Prevent keyboard shortcuts from running when the document's active element is a form element
2 parents 1a22df3 + e1276fd commit d6d5555

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

addon/components/docs-keyboard-shortcuts/component.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import { later } from "@ember/runloop";
44
import layout from './template';
55
import { EKMixin, keyUp } from 'ember-keyboard';
66
import { inject as service } from '@ember/service';
7+
import { formElementHasFocus } from '../../keyboard-config';
78

89
/**
910
A component that enables keyboard shortcuts. Press '?' to toggle the keyboard shortcuts dialog.
1011
1112
@class DocsKeyboardShortcuts
1213
@public
1314
*/
15+
1416
export default Component.extend(EKMixin, {
1517
layout,
1618

@@ -23,26 +25,34 @@ export default Component.extend(EKMixin, {
2325
}),
2426

2527
goto: on(keyUp('KeyG'), function() {
26-
this.set('isGoingTo', true);
27-
later(() => {
28-
this.set('isGoingTo', false);
29-
}, 500);
28+
if (!formElementHasFocus()) {
29+
this.set('isGoingTo', true);
30+
later(() => {
31+
this.set('isGoingTo', false);
32+
}, 500);
33+
}
3034
}),
3135

3236
gotoDocs: on(keyUp('KeyD'), function() {
33-
if (this.get('isGoingTo')) {
34-
this.get('router').transitionTo('docs');
37+
if (!formElementHasFocus()) {
38+
if (this.get('isGoingTo')) {
39+
this.get('router').transitionTo('docs');
40+
}
3541
}
3642
}),
3743

3844
gotoHome: on(keyUp('KeyH'), function() {
39-
if (this.get('isGoingTo')) {
40-
this.get('router').transitionTo('index');
45+
if (!formElementHasFocus()) {
46+
if (this.get('isGoingTo')) {
47+
this.get('router').transitionTo('index');
48+
}
4149
}
4250
}),
4351

4452
toggleKeyboardShortcuts: on(keyUp('shift+Slash'), function() {
45-
this.toggleProperty('isShowingKeyboardShortcuts');
53+
if (!formElementHasFocus()) {
54+
this.toggleProperty('isShowingKeyboardShortcuts');
55+
}
4656
}),
4757

4858
actions: {

addon/components/docs-viewer/component.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Component from '@ember/component';
33
import layout from './template';
44
import { EKMixin, keyDown } from 'ember-keyboard';
55
import { on } from '@ember/object/evented';
6+
import { formElementHasFocus } from '../../keyboard-config';
67

78
/**
89
The main docs viewer component for Ember-CLI addon docs. This component must be placed
@@ -30,6 +31,7 @@ import { on } from '@ember/object/evented';
3031
@yield {Component} viewer.main
3132
@public
3233
*/
34+
3335
export default Component.extend(EKMixin, {
3436
layout,
3537
docsRoutes: service(),
@@ -53,18 +55,19 @@ export default Component.extend(EKMixin, {
5355
},
5456

5557
nextPage: on(keyDown('KeyJ'), keyDown('ArrowRight'), function() {
56-
if (this.searchIsNotFocused() && this.get('docsRoutes.next')) {
57-
this.get('router').transitionTo(...this.get('docsRoutes.next.route'));
58+
if (!formElementHasFocus()) {
59+
if (this.get('docsRoutes.next')) {
60+
this.get('router').transitionTo(...this.get('docsRoutes.next.route'));
61+
}
5862
}
5963
}),
6064

6165
previousPage: on(keyDown('KeyK'), keyDown('ArrowLeft'), function() {
62-
if (this.searchIsNotFocused() && this.get('docsRoutes.previous')) {
63-
this.get('router').transitionTo(...this.get('docsRoutes.previous.route'));
66+
if (!formElementHasFocus()) {
67+
if (this.get('docsRoutes.previous')) {
68+
this.get('router').transitionTo(...this.get('docsRoutes.previous.route'));
69+
}
6470
}
6571
}),
6672

67-
searchIsNotFocused() {
68-
return !(document.querySelector('[data-search-box-input]') === document.activeElement);
69-
}
7073
});

addon/controllers/application.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EKMixin, keyUp } from 'ember-keyboard';
33
import { inject as service } from '@ember/service';
44
import { on } from '@ember/object/evented';
55
import { later } from '@ember/runloop';
6+
import { formElementHasFocus } from '../keyboard-config';
67

78
export default Controller.extend(EKMixin, {
89

@@ -15,26 +16,34 @@ export default Controller.extend(EKMixin, {
1516
}),
1617

1718
goto: on(keyUp('KeyG'), function() {
18-
this.set('isGoingTo', true);
19-
later(() => {
20-
this.set('isGoingTo', false);
21-
}, 500);
19+
if (!formElementHasFocus()) {
20+
this.set('isGoingTo', true);
21+
later(() => {
22+
this.set('isGoingTo', false);
23+
}, 500);
24+
}
2225
}),
2326

2427
gotoDocs: on(keyUp('KeyD'), function() {
25-
if (this.get('isGoingTo')) {
26-
this.get('router').transitionTo('docs');
28+
if (!formElementHasFocus()) {
29+
if (this.get('isGoingTo')) {
30+
this.get('router').transitionTo('docs');
31+
}
2732
}
2833
}),
2934

3035
gotoHome: on(keyUp('KeyH'), function() {
31-
if (this.get('isGoingTo')) {
32-
this.get('router').transitionTo('index');
36+
if (!formElementHasFocus()) {
37+
if (this.get('isGoingTo')) {
38+
this.get('router').transitionTo('index');
39+
}
3340
}
3441
}),
3542

3643
toggleKeyboardShortcuts: on(keyUp('shift+Slash'), function() {
37-
this.toggleProperty('isShowingKeyboardShortcuts');
44+
if (!formElementHasFocus()) {
45+
this.toggleProperty('isShowingKeyboardShortcuts');
46+
}
3847
}),
3948

4049
actions: {

addon/keyboard-config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const TAGNAMES_THAT_WHEN_FOCUSED_PREVENT_KEYBOARD_SHORTCUTS = [ 'INPUT', 'SELECT', 'TEXTAREA' ];
2+
3+
export function formElementHasFocus() {
4+
return TAGNAMES_THAT_WHEN_FOCUSED_PREVENT_KEYBOARD_SHORTCUTS.includes(document.activeElement.tagName);
5+
}

0 commit comments

Comments
 (0)