1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import * as keys from './keys';
import * as inputActions from '../actions/input';
import * as operationActions from '../actions/operation';
import backgroundReducers from '../reducers/background';
import reducers from '../reducers';
import commandReducer from '../reducers/command';
import * as store from '../store'
const backgroundStore = store.createStore(reducers, (e) => {
console.error('Vim-Vixen:', e);
});
backgroundStore.subscribe(() => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return keyQueueChanged(tabs[0], backgroundStore.getState());
}
});
});
const keyQueueChanged = (sendToTab, state) => {
if (state.input.keys.length === 0) {
return Promise.resolve();
}
let prefix = keys.asKeymapChars(state.input.keys);
let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
return keys.startsWith(prefix);
});
if (matched.length == 0) {
return handleMessage(inputActions.clearKeys(), sendToTab);
} else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) {
return Promise.resolve();
}
let action = keys.defaultKeymap[matched];
backgroundStore.dispatch(operationActions.exec(action, sendToTab));
return handleMessage(inputActions.clearKeys(), sendToTab).then(() => {
return backgroundReducers(undefined, action, sendToTab).then(() => {
return browser.tabs.sendMessage(sendToTab.id, action);
});
});
};
const handleMessage = (action, sendToTab) => {
backgroundStore.dispatch(action);
return backgroundReducers(undefined, action, sendToTab).then(() => {
return commandReducer(undefined, action, sendToTab).then(() => {
return browser.tabs.sendMessage(sendToTab.id, action);
});
});
};
browser.runtime.onMessage.addListener((action, sender) => {
handleMessage(action, sender.tab);
});
|