diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/actions/command.js | 35 | ||||
-rw-r--r-- | src/actions/index.js | 4 | ||||
-rw-r--r-- | src/background/index.js | 48 | ||||
-rw-r--r-- | src/console/console.js | 10 | ||||
-rw-r--r-- | src/reducers/command.js | 24 |
5 files changed, 71 insertions, 50 deletions
diff --git a/src/actions/command.js b/src/actions/command.js new file mode 100644 index 0000000..982255c --- /dev/null +++ b/src/actions/command.js @@ -0,0 +1,35 @@ +import actions from '../actions'; + +const normalizeUrl = (string) => { + try { + return new URL(string).href + } catch (e) { + return 'http://' + string; + } +} + +export function exec(line) { + let name = line.split(' ')[0]; + let remaining = line.replace(name + ' ', ''); + + switch (name) { + case 'open': + // TODO use search engined and pass keywords to them + return { + type: actions.COMMAND_OPEN_URL, + url: normalizeUrl(remaining) + }; + case 'tabopen': + return { + type: actions.COMMAND_TABOPEN_URL, + url: remaining + }; + case 'b': + case 'buffer': + return { + type: actions.COMMAND_BUFFER, + keywords: remaining + }; + } + throw new Error(name + ' command is not defined'); +} diff --git a/src/actions/index.js b/src/actions/index.js index 135dd4a..63d5f6f 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -33,4 +33,8 @@ export default { // User input INPUT_KEY_PRESS: 'input.key,press', INPUT_CLEAR_KEYS: 'input.clear.keys', + + COMMAND_OPEN_URL: 'command.open.url', + COMMAND_TABOPEN_URL: 'command.tabopen.url', + COMMAND_BUFFER: 'command.buffer', }; diff --git a/src/background/index.js b/src/background/index.js index 4d75b33..e72cab0 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,51 +1,11 @@ -import * as tabs from './tabs'; 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'; let inputState = inputReducers(undefined, {}); -const normalizeUrl = (string) => { - try { - return new URL(string).href - } catch (e) { - return 'http://' + string; - } -} - -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'); -}; - -browser.runtime.onMessage.addListener((request, sender) => { - switch (request.type) { - case 'event.cmd.enter': - return cmdEnterHandle(request, sender); - default: - return browser.tabs.sendMessage(sender.tab.id, request); - } -}); - const keyQueueChanged = (sender, prevState, state) => { if (state.keys.length === 0) { return Promise.resolve(); @@ -75,7 +35,11 @@ const handleMessage = (action, sender) => { inputState = nextInputState; return keyQueueChanged(sender, prevState, inputState); } - return backgroundReducers(undefined, action, sender); + return backgroundReducers(undefined, action, sender).then(() => { + return commandReducer(undefined, action, sender).then(() => { + return browser.tabs.sendMessage(sender.tab.id, action); + }); + }); }; browser.runtime.onMessage.addListener(handleMessage); diff --git a/src/console/console.js b/src/console/console.js index f83f79c..25ab36d 100644 --- a/src/console/console.js +++ b/src/console/console.js @@ -1,6 +1,7 @@ import './console.scss'; import * as backgroundActions from '../actions/background'; import * as consoleActions from '../actions/console'; +import * as commandActions from '../actions/command'; import Completion from './completion'; import consoleReducer from '../reducers/console'; @@ -10,13 +11,6 @@ var completion = null; var completionOrigin = ""; let state = consoleReducer(undefined, {}); -const keydownMessage = (input) => { - return { - type: 'vimvixen.command.enter', - value: input.value - }; -}; - const handleBlur = () => { return browser.runtime.sendMessage(consoleActions.hide()); }; @@ -58,7 +52,7 @@ const handleKeydown = (e) => { case KeyboardEvent.DOM_VK_ESCAPE: return input.blur(); case KeyboardEvent.DOM_VK_RETURN: - return browser.runtime.sendMessage(keydownMessage(e.target)); + return browser.runtime.sendMessage(commandActions.exec(e.target.value)); case KeyboardEvent.DOM_VK_TAB: if (e.shiftKey) { completePrev(); diff --git a/src/reducers/command.js b/src/reducers/command.js new file mode 100644 index 0000000..7e03593 --- /dev/null +++ b/src/reducers/command.js @@ -0,0 +1,24 @@ +import * as tabs from '../background/tabs'; +import actions from '../actions'; + +const cmdBuffer = (sender, arg) => { + if (isNaN(arg)) { + return tabs.selectByKeyword(sender.tab, arg); + } else { + let index = parseInt(arg, 10) - 1; + return tabs.selectAt(index); + } +} + +export default function reducer(state, action, sender) { + switch (action.type) { + case actions.COMMAND_OPEN_URL: + return browser.tabs.update(sender.tab.id, { url: action.url }); + case actions.COMMAND_TABOPEN_URL: + return browser.tabs.create({ url: action.url }); + case actions.COMMAND_BUFFER: + return cmdBuffer(sender, action.keywords); + default: + return Promise.resolve(); + } +} |