parent
e7342e9c23
commit
082450928a
7 changed files with 133 additions and 39 deletions
@ -1,28 +1,46 @@ |
|||||||
|
import * as actions from '../shared/actions'; |
||||||
import * as tabs from './tabs'; |
import * as tabs from './tabs'; |
||||||
|
import KeyQueue from './key-queue'; |
||||||
|
|
||||||
const KEY_MAP = { |
const queue = new KeyQueue(); |
||||||
'tabs.prev': KeyboardEvent.DOM_VK_H, |
|
||||||
'tabs.next': KeyboardEvent.DOM_VK_L, |
|
||||||
'scroll.up': KeyboardEvent.DOM_VK_K, |
|
||||||
'scroll.down': KeyboardEvent.DOM_VK_J |
|
||||||
}; |
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener((request, sender, sendResponse) => { |
const keyDownHandle = (request) => { |
||||||
let response = null; |
return queue.push({ |
||||||
|
code: request.code, |
||||||
|
shift: request.shift, |
||||||
|
ctrl: request.ctrl, |
||||||
|
alt: request.alt, |
||||||
|
meta: request.meta |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
switch (request.code) { |
const doBackgroundAction = (sender, action) => { |
||||||
case KEY_MAP['tabs.prev']: |
switch(action[0]) { |
||||||
tabs.selectPrevTab(sender.tab.index); |
case actions.TABS_PREV: |
||||||
break; |
tabs.selectPrevTab(sender.tab.index, actions[1] || 1); |
||||||
case KEY_MAP['tabs.next']: |
|
||||||
tabs.selectNextTab(sender.tab.index); |
|
||||||
break; |
break; |
||||||
case KEY_MAP['scroll.up']: |
case actions.TABS_NEXT: |
||||||
response = 'scroll.up' |
tabs.selectNextTab(sender.tab.index, actions[1] || 1); |
||||||
break; |
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; |
break; |
||||||
} |
} |
||||||
sendResponse(response); |
|
||||||
|
if (action == null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (actions.isBackgroundAction(action[0])) { |
||||||
|
doBackgroundAction(sender, action); |
||||||
|
} else if (actions.isContentAction(action[0])) { |
||||||
|
sendResponse(action); |
||||||
|
} |
||||||
}); |
}); |
||||||
|
@ -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; |
||||||
|
} |
||||||
|
} |
@ -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 }; |
@ -1,28 +1,33 @@ |
|||||||
import * as scrolls from './scrolls'; |
import * as scrolls from './scrolls'; |
||||||
|
|
||||||
const invokeEvent = (type) => { |
const invokeEvent = (action) => { |
||||||
switch (type) { |
if (typeof action === 'undefined' || action === null) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
switch (action[0]) { |
||||||
case 'scroll.up': |
case 'scroll.up': |
||||||
scrolls.scrollUp(window); |
scrolls.scrollUp(window, action[1] || 1); |
||||||
break; |
break; |
||||||
case 'scroll.down': |
case 'scroll.down': |
||||||
scrolls.scrollDown(window); |
scrolls.scrollDown(window, action[1] || 1); |
||||||
break; |
break; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
window.addEventListener("keydown", (e) => { |
window.addEventListener("keydown", (e) => { |
||||||
browser.runtime.sendMessage({ |
let request = { |
||||||
|
type: 'event.keydown', |
||||||
code: e.keyCode, |
code: e.keyCode, |
||||||
shift: e.shift, |
shift: e.shift, |
||||||
alt: e.alt, |
alt: e.alt, |
||||||
meta: e.meta, |
meta: e.meta, |
||||||
ctrl: e.ctrl, |
ctrl: e.ctrl, |
||||||
}).then((response) => { |
|
||||||
if (response) { |
|
||||||
invokeEvent(response); |
|
||||||
} |
} |
||||||
}, (err) => { |
|
||||||
|
browser.runtime.sendMessage(request) |
||||||
|
.then(invokeEvent, |
||||||
|
(err) => { |
||||||
console.log(`Vim Vixen: ${err}`); |
console.log(`Vim Vixen: ${err}`); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
|
@ -0,0 +1,22 @@ |
|||||||
|
export const TABS_PREV = 'tabs.prev'; |
||||||
|
export const TABS_NEXT = 'tabs.next'; |
||||||
|
export const SCROLL_UP = 'scroll.up'; |
||||||
|
export const SCROLL_DOWN = 'scroll.down'; |
||||||
|
|
||||||
|
const BACKGROUND_ACTION_SET = new Set([ |
||||||
|
TABS_PREV, |
||||||
|
TABS_NEXT |
||||||
|
]); |
||||||
|
|
||||||
|
const CONTENT_ACTION_SET = new Set([ |
||||||
|
SCROLL_UP, |
||||||
|
SCROLL_DOWN |
||||||
|
]); |
||||||
|
|
||||||
|
export const isBackgroundAction = (action) => { |
||||||
|
return BACKGROUND_ACTION_SET.has(action); |
||||||
|
}; |
||||||
|
|
||||||
|
export const isContentAction = (action) => { |
||||||
|
return CONTENT_ACTION_SET.has(action); |
||||||
|
}; |
Reference in new issue