aboutsummaryrefslogtreecommitdiff
path: root/src/components/follow.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-10-08 10:54:56 +0900
committerGitHub <noreply@github.com>2017-10-08 10:54:56 +0900
commit0f54a203dba38acdd080a928cee95f875fe84706 (patch)
tree64ca61c590f16fb3af5d304e92872fa375b37532 /src/components/follow.js
parentd995ab0030522f380d165f309ffc72b582366ddb (diff)
parent38fee747603d37a99f1a8d156f41ea3d7c400b78 (diff)
Merge pull request #20 from ueokande/prevent-page-keymaps
Prevent page keymaps
Diffstat (limited to 'src/components/follow.js')
-rw-r--r--src/components/follow.js76
1 files changed, 17 insertions, 59 deletions
diff --git a/src/components/follow.js b/src/components/follow.js
index 9221759..eedbd4d 100644
--- a/src/components/follow.js
+++ b/src/components/follow.js
@@ -5,21 +5,6 @@ import HintKeyProducer from 'content/hint-key-producer';
const DEFAULT_HINT_CHARSET = 'abcdefghijklmnopqrstuvwxyz';
-const availableKey = (keyCode) => {
- return (
- KeyboardEvent.DOM_VK_0 <= keyCode && keyCode <= KeyboardEvent.DOM_VK_9 ||
- KeyboardEvent.DOM_VK_A <= keyCode && keyCode <= KeyboardEvent.DOM_VK_Z
- );
-};
-
-const isNumericKey = (code) => {
- return KeyboardEvent.DOM_VK_0 <= code && code <= KeyboardEvent.DOM_VK_9;
-};
-
-const isAlphabeticKey = (code) => {
- return KeyboardEvent.DOM_VK_A <= code && code <= KeyboardEvent.DOM_VK_Z;
-};
-
const inWindow = (window, element) => {
let {
top, left, bottom, right
@@ -37,9 +22,6 @@ export default class FollowComponent {
this.store = store;
this.hintElements = {};
this.state = {};
-
- let doc = wrapper.ownerDocument;
- doc.addEventListener('keydown', this.onKeyDown.bind(this));
}
update() {
@@ -49,56 +31,50 @@ export default class FollowComponent {
this.create();
} else if (prevState.enabled && !this.state.enabled) {
this.remove();
- } else if (JSON.stringify(prevState.keys) !==
- JSON.stringify(this.state.keys)) {
+ } else if (prevState.keys !== this.state.keys) {
this.updateHints();
}
}
- onKeyDown(e) {
+ key(key) {
if (!this.state.enabled) {
- return;
+ return false;
}
- let { keyCode } = e;
- switch (keyCode) {
- case KeyboardEvent.DOM_VK_ENTER:
- case KeyboardEvent.DOM_VK_RETURN:
- this.activate(this.hintElements[
- FollowComponent.codeChars(this.state.keys)].target);
+ switch (key) {
+ case 'Enter':
+ this.activate(this.hintElements[this.state.keys].target);
return;
- case KeyboardEvent.DOM_VK_ESCAPE:
+ case 'Escape':
this.store.dispatch(followActions.disable());
return;
- case KeyboardEvent.DOM_VK_BACK_SPACE:
- case KeyboardEvent.DOM_VK_DELETE:
+ case 'Backspace':
+ case 'Delete':
this.store.dispatch(followActions.backspace());
break;
default:
- if (availableKey(keyCode)) {
- this.store.dispatch(followActions.keyPress(keyCode));
+ if (DEFAULT_HINT_CHARSET.includes(key)) {
+ this.store.dispatch(followActions.keyPress(key));
}
break;
}
-
- e.stopPropagation();
- e.preventDefault();
+ return true;
}
updateHints() {
- let chars = FollowComponent.codeChars(this.state.keys);
+ let keys = this.state.keys;
let shown = Object.keys(this.hintElements).filter((key) => {
- return key.startsWith(chars);
+ return key.startsWith(keys);
});
let hidden = Object.keys(this.hintElements).filter((key) => {
- return !key.startsWith(chars);
+ return !key.startsWith(keys);
});
if (shown.length === 0) {
this.remove();
return;
} else if (shown.length === 1) {
- this.activate(this.hintElements[chars].target);
- this.remove();
+ this.activate(this.hintElements[keys].target);
+ this.store.dispatch(followActions.disable());
}
shown.forEach((key) => {
@@ -177,24 +153,6 @@ export default class FollowComponent {
});
}
- static codeChars(codes) {
- const CHARCODE_ZERO = '0'.charCodeAt(0);
- const CHARCODE_A = 'a'.charCodeAt(0);
-
- let chars = '';
-
- for (let code of codes) {
- if (isNumericKey(code)) {
- chars += String.fromCharCode(
- code - KeyboardEvent.DOM_VK_0 + CHARCODE_ZERO);
- } else if (isAlphabeticKey(code)) {
- chars += String.fromCharCode(
- code - KeyboardEvent.DOM_VK_A + CHARCODE_A);
- }
- }
- return chars;
- }
-
static getTargetElements(doc) {
let all = doc.querySelectorAll('a,button,input,textarea');
let filtered = Array.prototype.filter.call(all, (element) => {