diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-13 12:00:39 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2017-08-13 12:00:39 +0900 |
commit | 082450928a35ea8d35fdbcd176dbd972e45acfec (patch) | |
tree | 5973294649d322b1dc6edd2bbb07658aa499fab3 /src/background | |
parent | e7342e9c237e1deea69ed66e07b7f79cd534056e (diff) |
use key-queue for input
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/index.js | 56 | ||||
-rw-r--r-- | src/background/key-queue.js | 28 | ||||
-rw-r--r-- | src/background/keys.js | 21 | ||||
-rw-r--r-- | src/background/tabs.js | 8 |
4 files changed, 90 insertions, 23 deletions
diff --git a/src/background/index.js b/src/background/index.js index 8c9c418..604ea92 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,28 +1,46 @@ +import * as actions from '../shared/actions'; import * as tabs from './tabs'; +import KeyQueue from './key-queue'; -const KEY_MAP = { - 'tabs.prev': KeyboardEvent.DOM_VK_H, - 'tabs.next': KeyboardEvent.DOM_VK_L, - 'scroll.up': KeyboardEvent.DOM_VK_K, - 'scroll.down': KeyboardEvent.DOM_VK_J -}; +const queue = new KeyQueue(); -browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - let response = null; +const keyDownHandle = (request) => { + return queue.push({ + code: request.code, + shift: request.shift, + ctrl: request.ctrl, + alt: request.alt, + meta: request.meta + }) +} - switch (request.code) { - case KEY_MAP['tabs.prev']: - tabs.selectPrevTab(sender.tab.index); - break; - case KEY_MAP['tabs.next']: - tabs.selectNextTab(sender.tab.index); +const doBackgroundAction = (sender, action) => { + switch(action[0]) { + case actions.TABS_PREV: + tabs.selectPrevTab(sender.tab.index, actions[1] || 1); break; - case KEY_MAP['scroll.up']: - response = 'scroll.up' + case actions.TABS_NEXT: + tabs.selectNextTab(sender.tab.index, actions[1] || 1); break; - case KEY_MAP['scroll.down']: - response = 'scroll.down' + } +} + +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + let action = null; + + switch (request.type) { + case 'event.keydown': + action = keyDownHandle(request); break; } - sendResponse(response); + + if (action == null) { + return; + } + + if (actions.isBackgroundAction(action[0])) { + doBackgroundAction(sender, action); + } else if (actions.isContentAction(action[0])) { + sendResponse(action); + } }); diff --git a/src/background/key-queue.js b/src/background/key-queue.js new file mode 100644 index 0000000..e14e995 --- /dev/null +++ b/src/background/key-queue.js @@ -0,0 +1,28 @@ +import * as keys from './keys'; +import * as actions from '../shared/actions'; + +const DEFAULT_KEYMAP = [ + { keys: [{ code: KeyboardEvent.DOM_VK_K }], action: [ actions.SCROLL_UP, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_J }], action: [ actions.SCROLL_DOWN, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_H }], action: [ actions.TABS_PREV, 1 ]}, + { keys: [{ code: KeyboardEvent.DOM_VK_L }], action: [ actions.TABS_NEXT, 1 ]}, +] + +export default class KeyQueue { + + constructor(keymap) { + this.data = []; + this.keymap = keymap; + } + + push(key) { + this.data.push(key); + for (let map of DEFAULT_KEYMAP) { + if (keys.keysEquals(map.keys, this.data)) { + this.data = []; + return map.action + } + } + return null; + } +} diff --git a/src/background/keys.js b/src/background/keys.js new file mode 100644 index 0000000..d6a94fb --- /dev/null +++ b/src/background/keys.js @@ -0,0 +1,21 @@ +const keyEquals = (key1, key2) => { + return (key1.code === key2.code) && + ((key1.shift || false) === (key2.shift || false)) && + ((key1.ctrl || false) === (key2.ctrl || false)) && + ((key1.alt || false) === (key2.alt || false)) && + ((key1.meta || false) === (key2.meta || false)); +}; + +const keysEquals = (keys1, keys2) => { + if (keys1.length !== keys2.length) { + return false; + } + for (let i = 0; i < keys1.length; ++i) { + if (!keyEquals(keys1[i], keys2[i])) { + return false; + } + } + return true; +} + +export { keyEquals, keysEquals }; diff --git a/src/background/tabs.js b/src/background/tabs.js index 20ef286..000bd7d 100644 --- a/src/background/tabs.js +++ b/src/background/tabs.js @@ -1,20 +1,20 @@ -const selectPrevTab = (current) => { +const selectPrevTab = (current, count) => { chrome.tabs.query({ currentWindow: true }, (tabs) => { if (tabs.length < 2) { return; } - let select = (current - 1) % tabs.length + let select = (current - count) % tabs.length let id = tabs[select].id; chrome.tabs.update(id, { active: true }) }); }; -const selectNextTab = (current) => { +const selectNextTab = (current, count) => { chrome.tabs.query({ currentWindow: true }, (tabs) => { if (tabs.length < 2) { return; } - let select = (current + 1 + tabs.length) % tabs.length + let select = (current + count + tabs.length) % tabs.length let id = tabs[select].id; chrome.tabs.update(id, { active: true }) }); |