aboutsummaryrefslogtreecommitdiff
path: root/src/background/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/index.js')
-rw-r--r--src/background/index.js126
1 files changed, 34 insertions, 92 deletions
diff --git a/src/background/index.js b/src/background/index.js
index 8913a83..e72cab0 100644
--- a/src/background/index.js
+++ b/src/background/index.js
@@ -1,103 +1,45 @@
-import * as actions from '../shared/actions';
-import * as tabs from './tabs';
-import * as zooms from './zooms';
-import KeyQueue from './key-queue';
+import * as keys from './keys';
+import * as inputActions from '../actions/input';
+import backgroundReducers from '../reducers/background';
+import commandReducer from '../reducers/command';
+import inputReducers from '../reducers/input';
-const queue = new KeyQueue();
+let inputState = inputReducers(undefined, {});
-const keyPressHandle = (request, sender) => {
- let action = queue.push({
- code: request.code,
- ctrl: request.ctrl
- });
- if (!action) {
+const keyQueueChanged = (sender, prevState, state) => {
+ if (state.keys.length === 0) {
return Promise.resolve();
}
- if (actions.isBackgroundAction(action[0])) {
- return doBackgroundAction(sender, action);
- } else if (actions.isContentAction(action[0])) {
- return Promise.resolve({
- type: 'response.action',
- action: action
- });
+ let prefix = keys.asKeymapChars(state.keys);
+ let matched = Object.keys(keys.defaultKeymap).filter((keys) => {
+ return keys.startsWith(prefix);
+ });
+ if (matched.length == 0) {
+ return handleMessage(inputActions.clearKeys(), sender);
+ } else if (matched.length > 1 || matched.length === 1 && prefix !== matched[0]) {
+ return Promise.resolve();
}
- return Promise.resolve();
+ let action = keys.defaultKeymap[matched];
+ return handleMessage(inputActions.clearKeys(), sender).then(() => {
+ return backgroundReducers(undefined, action, sender).then(() => {
+ return browser.tabs.sendMessage(sender.tab.id, action);
+ });
+ });
};
-const doBackgroundAction = (sender, action) => {
- switch(action[0]) {
- case actions.TABS_CLOSE:
- return tabs.closeTab(sender.tab.id);
- case actions.TABS_REOPEN:
- return tabs.reopenTab();
- case actions.TABS_PREV:
- return tabs.selectPrevTab(sender.tab.index, actions[1] || 1);
- case actions.TABS_NEXT:
- return tabs.selectNextTab(sender.tab.index, actions[1] || 1);
- case actions.TABS_RELOAD:
- return tabs.reload(sender.tab, actions[1] || false);
- case actions.ZOOM_IN:
- return zooms.zoomIn();
- case actions.ZOOM_OUT:
- return zooms.zoomOut();
- case actions.ZOOM_NEUTRAL:
- return zooms.neutral();
- }
- return Promise.resolve();
-}
-
-const normalizeUrl = (string) => {
- try {
- return new URL(string).href
- } catch (e) {
- return 'http://' + string;
+const handleMessage = (action, sender) => {
+ let nextInputState = inputReducers(inputState, action);
+ if (JSON.stringify(nextInputState) !== JSON.stringify(inputState)) {
+ let prevState = inputState;
+ inputState = nextInputState;
+ return keyQueueChanged(sender, prevState, inputState);
}
-}
-
-const cmdBuffer = (sender, arg) => {
- if (isNaN(arg)) {
- return tabs.selectByKeyword(sender.tab, arg);
- } else {
- let index = parseInt(arg, 10) - 1;
- return tabs.selectAt(index);
- }
-}
-
-const cmdEnterHandle = (request, sender) => {
- let words = request.text.split(' ').filter((s) => s.length > 0);
- switch (words[0]) {
- case 'open':
- return browser.tabs.update(sender.tab.id, { url: normalizeUrl(words[1]) });
- case 'tabopen':
- return browser.tabs.create({ url: normalizeUrl(words[1]) });
- case 'b':
- case 'buffer':
- return cmdBuffer(sender, words[1]);
- }
- throw new Error(words[0] + ' command is not defined');
+ return backgroundReducers(undefined, action, sender).then(() => {
+ return commandReducer(undefined, action, sender).then(() => {
+ return browser.tabs.sendMessage(sender.tab.id, action);
+ });
+ });
};
-browser.runtime.onMessage.addListener((request, sender) => {
- switch (request.type) {
- case 'event.keypress':
- return keyPressHandle(request, sender);
- case 'event.cmd.enter':
- return cmdEnterHandle(request, sender);
- case 'event.cmd.tabs.completion':
- return tabs.getCompletions(request.text).then((tabs) => {
- let items = tabs.map((tab) => {
- return {
- caption: tab.title,
- content: tab.title,
- url: tab.url,
- icon: tab.favIconUrl
- }
- });
- return {
- name: "Buffers",
- items: items
- };
- });
- }
-});
+browser.runtime.onMessage.addListener(handleMessage);