Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions addon/components/docs-keyboard-shortcuts/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { later } from "@ember/runloop";
import layout from './template';
import { EKMixin, keyUp } from 'ember-keyboard';
import { inject as service } from '@ember/service';
import { formElementHasFocus } from '../../keyboard-config';

/**
A component that enables keyboard shortcuts. Press '?' to toggle the keyboard shortcuts dialog.

@class DocsKeyboardShortcuts
@public
*/

export default Component.extend(EKMixin, {
layout,

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

goto: on(keyUp('KeyG'), function() {
this.set('isGoingTo', true);
later(() => {
this.set('isGoingTo', false);
}, 500);
if (!formElementHasFocus()) {
this.set('isGoingTo', true);
later(() => {
this.set('isGoingTo', false);
}, 500);
}
}),

gotoDocs: on(keyUp('KeyD'), function() {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('docs');
if (!formElementHasFocus()) {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('docs');
}
}
}),

gotoHome: on(keyUp('KeyH'), function() {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('index');
if (!formElementHasFocus()) {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('index');
}
}
}),

toggleKeyboardShortcuts: on(keyUp('shift+Slash'), function() {
this.toggleProperty('isShowingKeyboardShortcuts');
if (!formElementHasFocus()) {
this.toggleProperty('isShowingKeyboardShortcuts');
}
}),

actions: {
Expand Down
17 changes: 10 additions & 7 deletions addon/components/docs-viewer/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Component from '@ember/component';
import layout from './template';
import { EKMixin, keyDown } from 'ember-keyboard';
import { on } from '@ember/object/evented';
import { formElementHasFocus } from '../../keyboard-config';

/**
The main docs viewer component for Ember-CLI addon docs. This component must be placed
Expand Down Expand Up @@ -30,6 +31,7 @@ import { on } from '@ember/object/evented';
@yield {Component} viewer.main
@public
*/

export default Component.extend(EKMixin, {
layout,
docsRoutes: service(),
Expand All @@ -53,18 +55,19 @@ export default Component.extend(EKMixin, {
},

nextPage: on(keyDown('KeyJ'), keyDown('ArrowRight'), function() {
if (this.searchIsNotFocused() && this.get('docsRoutes.next')) {
this.get('router').transitionTo(...this.get('docsRoutes.next.route'));
if (!formElementHasFocus()) {
if (this.get('docsRoutes.next')) {
this.get('router').transitionTo(...this.get('docsRoutes.next.route'));
}
}
}),

previousPage: on(keyDown('KeyK'), keyDown('ArrowLeft'), function() {
if (this.searchIsNotFocused() && this.get('docsRoutes.previous')) {
this.get('router').transitionTo(...this.get('docsRoutes.previous.route'));
if (!formElementHasFocus()) {
if (this.get('docsRoutes.previous')) {
this.get('router').transitionTo(...this.get('docsRoutes.previous.route'));
}
}
}),

searchIsNotFocused() {
return !(document.querySelector('[data-search-box-input]') === document.activeElement);
}
});
27 changes: 18 additions & 9 deletions addon/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EKMixin, keyUp } from 'ember-keyboard';
import { inject as service } from '@ember/service';
import { on } from '@ember/object/evented';
import { later } from '@ember/runloop';
import { formElementHasFocus } from '../keyboard-config';

export default Controller.extend(EKMixin, {

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

goto: on(keyUp('KeyG'), function() {
this.set('isGoingTo', true);
later(() => {
this.set('isGoingTo', false);
}, 500);
if (!formElementHasFocus()) {
this.set('isGoingTo', true);
later(() => {
this.set('isGoingTo', false);
}, 500);
}
}),

gotoDocs: on(keyUp('KeyD'), function() {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('docs');
if (!formElementHasFocus()) {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('docs');
}
}
}),

gotoHome: on(keyUp('KeyH'), function() {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('index');
if (!formElementHasFocus()) {
if (this.get('isGoingTo')) {
this.get('router').transitionTo('index');
}
}
}),

toggleKeyboardShortcuts: on(keyUp('shift+Slash'), function() {
this.toggleProperty('isShowingKeyboardShortcuts');
if (!formElementHasFocus()) {
this.toggleProperty('isShowingKeyboardShortcuts');
}
}),

actions: {
Expand Down
5 changes: 5 additions & 0 deletions addon/keyboard-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const TAGNAMES_THAT_WHEN_FOCUSED_PREVENT_KEYBOARD_SHORTCUTS = [ 'INPUT', 'SELECT', 'TEXTAREA' ];

export function formElementHasFocus() {
return TAGNAMES_THAT_WHEN_FOCUSED_PREVENT_KEYBOARD_SHORTCUTS.includes(document.activeElement.tagName);
}