command as action/reducer
This commit is contained in:
parent
14d13e2c3a
commit
bf890a6d9d
5 changed files with 71 additions and 50 deletions
35
src/actions/command.js
Normal file
35
src/actions/command.js
Normal file
|
@ -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');
|
||||||
|
}
|
|
@ -33,4 +33,8 @@ export default {
|
||||||
// User input
|
// User input
|
||||||
INPUT_KEY_PRESS: 'input.key,press',
|
INPUT_KEY_PRESS: 'input.key,press',
|
||||||
INPUT_CLEAR_KEYS: 'input.clear.keys',
|
INPUT_CLEAR_KEYS: 'input.clear.keys',
|
||||||
|
|
||||||
|
COMMAND_OPEN_URL: 'command.open.url',
|
||||||
|
COMMAND_TABOPEN_URL: 'command.tabopen.url',
|
||||||
|
COMMAND_BUFFER: 'command.buffer',
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,51 +1,11 @@
|
||||||
import * as tabs from './tabs';
|
|
||||||
import * as keys from './keys';
|
import * as keys from './keys';
|
||||||
import * as inputActions from '../actions/input';
|
import * as inputActions from '../actions/input';
|
||||||
import backgroundReducers from '../reducers/background';
|
import backgroundReducers from '../reducers/background';
|
||||||
|
import commandReducer from '../reducers/command';
|
||||||
import inputReducers from '../reducers/input';
|
import inputReducers from '../reducers/input';
|
||||||
|
|
||||||
let inputState = inputReducers(undefined, {});
|
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) => {
|
const keyQueueChanged = (sender, prevState, state) => {
|
||||||
if (state.keys.length === 0) {
|
if (state.keys.length === 0) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
|
@ -75,7 +35,11 @@ const handleMessage = (action, sender) => {
|
||||||
inputState = nextInputState;
|
inputState = nextInputState;
|
||||||
return keyQueueChanged(sender, prevState, inputState);
|
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);
|
browser.runtime.onMessage.addListener(handleMessage);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import './console.scss';
|
import './console.scss';
|
||||||
import * as backgroundActions from '../actions/background';
|
import * as backgroundActions from '../actions/background';
|
||||||
import * as consoleActions from '../actions/console';
|
import * as consoleActions from '../actions/console';
|
||||||
|
import * as commandActions from '../actions/command';
|
||||||
import Completion from './completion';
|
import Completion from './completion';
|
||||||
import consoleReducer from '../reducers/console';
|
import consoleReducer from '../reducers/console';
|
||||||
|
|
||||||
|
@ -10,13 +11,6 @@ var completion = null;
|
||||||
var completionOrigin = "";
|
var completionOrigin = "";
|
||||||
let state = consoleReducer(undefined, {});
|
let state = consoleReducer(undefined, {});
|
||||||
|
|
||||||
const keydownMessage = (input) => {
|
|
||||||
return {
|
|
||||||
type: 'vimvixen.command.enter',
|
|
||||||
value: input.value
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleBlur = () => {
|
const handleBlur = () => {
|
||||||
return browser.runtime.sendMessage(consoleActions.hide());
|
return browser.runtime.sendMessage(consoleActions.hide());
|
||||||
};
|
};
|
||||||
|
@ -58,7 +52,7 @@ const handleKeydown = (e) => {
|
||||||
case KeyboardEvent.DOM_VK_ESCAPE:
|
case KeyboardEvent.DOM_VK_ESCAPE:
|
||||||
return input.blur();
|
return input.blur();
|
||||||
case KeyboardEvent.DOM_VK_RETURN:
|
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:
|
case KeyboardEvent.DOM_VK_TAB:
|
||||||
if (e.shiftKey) {
|
if (e.shiftKey) {
|
||||||
completePrev();
|
completePrev();
|
||||||
|
|
24
src/reducers/command.js
Normal file
24
src/reducers/command.js
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue