You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

75 lines
2.4 KiB

import * as keys from './keys';
import * as inputActions from '../actions/input';
import * as operationActions from '../actions/operation';
import * as commandActions from '../actions/command';
import * as consoleActions from '../actions/console';
import reducers from '../reducers';
import messages from '../messages';
import * as store from '../store'
let prevInput = [];
const backgroundStore = store.createStore(reducers, (e) => {
console.error('Vim-Vixen:', e);
});
backgroundStore.subscribe(() => {
let currentInput = backgroundStore.getState().input
if (JSON.stringify(prevInput) === JSON.stringify(currentInput)) {
return
}
prevInput = currentInput;
if (currentInput.keys.length === 0) {
return;
}
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return keyQueueChanged(tabs[0], backgroundStore.getState());
}
});
});
backgroundStore.subscribe(() => {
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
if (tabs.length > 0) {
return browser.tabs.sendMessage(tabs[0].id, {
type: messages.STATE_UPDATE,
state: backgroundStore.getState()
});
}
});
});
const keyQueueChanged = (sendToTab, state) => {
let prefix = keys.asKeymapChars(state.input.keys);
let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
return keys.startsWith(prefix);
});
if (matched.length == 0) {
backgroundStore.dispatch(inputActions.clearKeys());
return Promise.resolve();
} 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));
backgroundStore.dispatch(inputActions.clearKeys());
return browser.tabs.sendMessage(sendToTab.id, action);
};
browser.runtime.onMessage.addListener((message) => {
switch (message.type) {
case messages.KEYDOWN:
backgroundStore.dispatch(inputActions.keyPress(message.code, message.ctrl));
break;
case messages.CONSOLE_BLURRED:
backgroundStore.dispatch(consoleActions.hide());
break;
case messages.CONSOLE_ENTERED:
backgroundStore.dispatch(commandActions.exec(message.text));
break;
case messages.CONSOLE_CHANGEED:
backgroundStore.dispatch(commandActions.complete(message.text));
break;
}
});