aboutsummaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/components')
-rw-r--r--src/components/background-input.js53
-rw-r--r--src/components/background.js20
-rw-r--r--src/components/content-input.js40
-rw-r--r--src/components/follow.js76
-rw-r--r--src/components/keymapper.js43
5 files changed, 107 insertions, 125 deletions
diff --git a/src/components/background-input.js b/src/components/background-input.js
deleted file mode 100644
index bd6ecf9..0000000
--- a/src/components/background-input.js
+++ /dev/null
@@ -1,53 +0,0 @@
-import * as inputActions from 'actions/input';
-import * as operationActions from 'actions/operation';
-
-export default class BackgroundInputComponent {
- constructor(store) {
- this.store = store;
- this.keymaps = {};
- this.prevInputs = [];
- }
-
- update(sender) {
- let state = this.store.getState();
- this.reloadSettings(state.setting);
- this.handleKeyInputs(sender, state.input);
- }
-
- reloadSettings(setting) {
- if (!setting.settings.json) {
- return;
- }
- this.keymaps = JSON.parse(setting.settings.json).keymaps;
- }
-
- handleKeyInputs(sender, input) {
- if (JSON.stringify(this.prevInputs) === JSON.stringify(input)) {
- return;
- }
- this.prevInputs = input;
-
- if (input.keys.length === 0) {
- return;
- }
- if (sender) {
- return this.handleKeysChanged(sender, input);
- }
- }
-
- handleKeysChanged(sender, input) {
- let matched = Object.keys(this.keymaps).filter((keyStr) => {
- return keyStr.startsWith(input.keys);
- });
- if (matched.length === 0) {
- this.store.dispatch(inputActions.clearKeys(), sender);
- return Promise.resolve();
- } else if (matched.length > 1 ||
- matched.length === 1 && input.keys !== matched[0]) {
- return Promise.resolve();
- }
- let operation = this.keymaps[matched];
- this.store.dispatch(operationActions.exec(operation, sender.tab), sender);
- this.store.dispatch(inputActions.clearKeys(), sender);
- }
-}
diff --git a/src/components/background.js b/src/components/background.js
index 487e3af..4961d85 100644
--- a/src/components/background.js
+++ b/src/components/background.js
@@ -1,5 +1,5 @@
import messages from 'content/messages';
-import * as inputActions from 'actions/input';
+import * as operationActions from 'actions/operation';
import * as settingsActions from 'actions/setting';
import * as tabActions from 'actions/tab';
import * as commands from 'shared/commands';
@@ -35,9 +35,10 @@ export default class BackgroundComponent {
onMessage(message, sender) {
switch (message.type) {
- case messages.KEYDOWN:
+ case messages.BACKGROUND_OPERATION:
return this.store.dispatch(
- inputActions.keyPress(message.key, message.ctrl), sender);
+ operationActions.execBackground(message.operation, sender.tab),
+ sender);
case messages.OPEN_URL:
if (message.newTab) {
return this.store.dispatch(
@@ -56,10 +57,23 @@ export default class BackgroundComponent {
text: e.message,
});
});
+ case messages.SETTINGS_QUERY:
+ return Promise.resolve(this.store.getState().setting.settings);
case messages.CONSOLE_QUERY_COMPLETIONS:
return commands.complete(message.text, this.settings);
case messages.SETTINGS_RELOAD:
this.store.dispatch(settingsActions.load());
+ return this.broadcastSettingsChanged();
}
}
+
+ broadcastSettingsChanged() {
+ return browser.tabs.query({}).then((tabs) => {
+ for (let tab of tabs) {
+ browser.tabs.sendMessage(tab.id, {
+ type: messages.SETTINGS_CHANGED,
+ });
+ }
+ });
+ }
}
diff --git a/src/components/content-input.js b/src/components/content-input.js
index 38d57fd..9568caf 100644
--- a/src/components/content-input.js
+++ b/src/components/content-input.js
@@ -1,14 +1,20 @@
-import messages from 'content/messages';
-
export default class ContentInputComponent {
constructor(target) {
this.pressed = {};
+ this.onKeyListeners = [];
target.addEventListener('keypress', this.onKeyPress.bind(this));
target.addEventListener('keydown', this.onKeyDown.bind(this));
target.addEventListener('keyup', this.onKeyUp.bind(this));
}
+ update() {
+ }
+
+ onKey(cb) {
+ this.onKeyListeners.push(cb);
+ }
+
onKeyPress(e) {
if (this.pressed[e.key] && this.pressed[e.key] !== 'keypress') {
return;
@@ -30,18 +36,32 @@ export default class ContentInputComponent {
}
capture(e) {
- if (e.target instanceof HTMLInputElement ||
- e.target instanceof HTMLTextAreaElement ||
- e.target instanceof HTMLSelectElement) {
+ if (this.fromInput(e)) {
if (e.key === 'Escape' && e.target.blur) {
e.target.blur();
}
return;
}
- browser.runtime.sendMessage({
- type: messages.KEYDOWN,
- key: e.key,
- ctrl: e.ctrlKey
- });
+ if (e.key === 'OS') {
+ return;
+ }
+
+ let stop = false;
+ for (let listener of this.onKeyListeners) {
+ stop = stop || listener(e.key, e.ctrlKey);
+ if (stop) {
+ break;
+ }
+ }
+ if (stop) {
+ e.preventDefault();
+ e.stopPropagation();
+ }
+ }
+
+ fromInput(e) {
+ return e.target instanceof HTMLInputElement ||
+ e.target instanceof HTMLTextAreaElement ||
+ e.target instanceof HTMLSelectElement;
}
}
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) => {
diff --git a/src/components/keymapper.js b/src/components/keymapper.js
new file mode 100644
index 0000000..3685a4f
--- /dev/null
+++ b/src/components/keymapper.js
@@ -0,0 +1,43 @@
+import * as inputActions from 'actions/input';
+import * as operationActions from 'actions/operation';
+
+export default class KeymapperComponent {
+ constructor(store) {
+ this.store = store;
+ }
+
+ update() {
+ }
+
+ key(key, ctrl) {
+ let keymaps = this.keymaps();
+ if (!keymaps) {
+ return;
+ }
+ this.store.dispatch(inputActions.keyPress(key, ctrl));
+
+ let input = this.store.getState().input;
+ let matched = Object.keys(keymaps).filter((keyStr) => {
+ return keyStr.startsWith(input.keys);
+ });
+ if (matched.length === 0) {
+ this.store.dispatch(inputActions.clearKeys());
+ return false;
+ } else if (matched.length > 1 ||
+ matched.length === 1 && input.keys !== matched[0]) {
+ return true;
+ }
+ let operation = keymaps[matched];
+ this.store.dispatch(operationActions.exec(operation));
+ this.store.dispatch(inputActions.clearKeys());
+ return true;
+ }
+
+ keymaps() {
+ let settings = this.store.getState().setting.settings;
+ if (!settings || !settings.json) {
+ return null;
+ }
+ return JSON.parse(settings.json).keymaps;
+ }
+}