diff options
-rw-r--r-- | src/actions/command.js | 35 | ||||
-rw-r--r-- | src/actions/input.js | 9 | ||||
-rw-r--r-- | src/background/index.js | 38 | ||||
-rw-r--r-- | src/reducers/index.js | 7 | ||||
-rw-r--r-- | src/reducers/input.js | 6 | ||||
-rw-r--r-- | src/shared/default-settings.js | 11 | ||||
-rw-r--r-- | src/shared/validators/setting.js | 2 |
7 files changed, 47 insertions, 61 deletions
diff --git a/src/actions/command.js b/src/actions/command.js index dd755dc..f578afd 100644 --- a/src/actions/command.js +++ b/src/actions/command.js @@ -2,18 +2,6 @@ import * as tabs from '../background/tabs'; import * as histories from '../background/histories'; import * as consoleActions from './console'; -const DEFAULT_SEARCH_ENGINES = { - default: 'google', - engines: { - 'google': 'https://google.com/search?q={}', - 'yahoo': 'https://search.yahoo.com/search?p={}', - 'bing': 'https://www.bing.com/search?q={}', - 'duckduckgo': 'https://duckduckgo.com/?q={}', - 'twitter': 'https://twitter.com/search?q={}', - 'wikipedia': 'https://en.wikipedia.org/w/index.php?search={}' - } -}; - const normalizeUrl = (string, searchConfig) => { try { return new URL(string).href; @@ -63,7 +51,7 @@ const bufferCommand = (keywords) => { }); }; -const getOpenCompletions = (command, keywords) => { +const getOpenCompletions = (command, keywords, searchConfig) => { return histories.getCompletions(keywords).then((pages) => { let historyItems = pages.map((page) => { return { @@ -72,7 +60,7 @@ const getOpenCompletions = (command, keywords) => { url: page.url }; }); - let engineNames = Object.keys(DEFAULT_SEARCH_ENGINES.engines); + let engineNames = Object.keys(searchConfig.engines); let engineItems = engineNames.filter(name => name.startsWith(keywords)) .map(name => ({ caption: name, @@ -96,15 +84,15 @@ const getOpenCompletions = (command, keywords) => { }); }; -const doCommand = (name, remaining) => { +const doCommand = (name, remaining, settings) => { switch (name) { case 'o': case 'open': // TODO use search engined and pass keywords to them - return openCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES)); + return openCommand(normalizeUrl(remaining, settings.search)); case 't': case 'tabopen': - return tabopenCommand(normalizeUrl(remaining, DEFAULT_SEARCH_ENGINES)); + return tabopenCommand(normalizeUrl(remaining, settings.search)); case 'b': case 'buffer': return bufferCommand(remaining); @@ -112,13 +100,13 @@ const doCommand = (name, remaining) => { throw new Error(name + ' command is not defined'); }; -const getCompletions = (command, keywords) => { +const getCompletions = (command, keywords, settings) => { switch (command) { case 'o': case 'open': case 't': case 'tabopen': - return getOpenCompletions(command, keywords); + return getOpenCompletions(command, keywords, settings.search); case 'b': case 'buffer': return tabs.getCompletions(keywords).then((gotTabs) => { @@ -141,18 +129,19 @@ const getCompletions = (command, keywords) => { return Promise.resolve([]); }; -const exec = (line) => { +const exec = (line, settings) => { let name = line.split(' ')[0]; let remaining = line.replace(name + ' ', ''); - return doCommand(name, remaining).then(() => { + return doCommand(name, remaining, settings).then(() => { return consoleActions.hide(); }); }; -const complete = (line) => { +const complete = (line, settings) => { let command = line.split(' ', 1)[0]; let keywords = line.replace(command + ' ', ''); - return getCompletions(command, keywords).then(consoleActions.setCompletions); + return getCompletions(command, keywords, settings) + .then(consoleActions.setCompletions); }; export { exec, complete }; diff --git a/src/actions/input.js b/src/actions/input.js index de6de4e..07948a1 100644 --- a/src/actions/input.js +++ b/src/actions/input.js @@ -14,11 +14,4 @@ const clearKeys = () => { }; }; -const setKeymaps = (keymaps) => { - return { - type: actions.INPUT_SET_KEYMAPS, - keymaps: keymaps - }; -}; - -export { keyPress, clearKeys, setKeymaps }; +export { keyPress, clearKeys }; diff --git a/src/background/index.js b/src/background/index.js index f2411c7..040a2d5 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -3,13 +3,14 @@ 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 * as settingsActions from '../actions/setting'; import * as tabActions from '../actions/tab'; import reducers from '../reducers'; import messages from '../content/messages'; -import DefaultSettings from '../shared/default-settings'; import * as store from '../store'; let prevInput = []; +let settings = {}; const backgroundStore = store.createStore(reducers, (e, sender) => { console.error('Vim-Vixen:', e); @@ -39,10 +40,17 @@ backgroundStore.subscribe((sender) => { }); } }); +backgroundStore.subscribe(() => { + let state = backgroundStore.getState().setting; + if (!state.settings.json) { + return; + } + settings = JSON.parse(backgroundStore.getState().setting.settings.json); +}); const keyQueueChanged = (state, sender) => { let prefix = keys.asKeymapChars(state.input.keys); - let matched = Object.keys(state.input.keymaps).filter((keyStr) => { + let matched = Object.keys(settings.keymaps).filter((keyStr) => { return keyStr.startsWith(prefix); }); if (matched.length === 0) { @@ -52,24 +60,12 @@ const keyQueueChanged = (state, sender) => { matched.length === 1 && prefix !== matched[0]) { return Promise.resolve(); } - let action = state.input.keymaps[matched]; - backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender); + let action = settings.keymaps[matched]; + backgroundStore.dispatch( + operationActions.exec(action, sender.tab, settings), sender); backgroundStore.dispatch(inputActions.clearKeys(), sender); }; -const reloadSettings = () => { - browser.storage.local.get('settings').then((value) => { - let settings = null; - if (value.settings) { - settings = JSON.parse(value.settings.json); - } else { - settings = JSON.parse(DefaultSettings.json); - } - let action = inputActions.setKeymaps(settings.keymaps); - backgroundStore.dispatch(action); - }, console.error); -}; - const handleMessage = (message, sender) => { switch (message.type) { case messages.KEYDOWN: @@ -87,12 +83,12 @@ const handleMessage = (message, sender) => { consoleActions.hide(), sender); case messages.CONSOLE_ENTERED: return backgroundStore.dispatch( - commandActions.exec(message.text), sender); + commandActions.exec(message.text, settings), sender); case messages.CONSOLE_CHANGEED: return backgroundStore.dispatch( - commandActions.complete(message.text), sender); + commandActions.complete(message.text, settings), sender); case messages.SETTINGS_RELOAD: - return reloadSettings(); + backgroundStore.dispatch(settingsActions.load()); } }; @@ -105,7 +101,7 @@ browser.runtime.onMessage.addListener((message, sender) => { }); const initializeSettings = () => { - reloadSettings(); + backgroundStore.dispatch(settingsActions.load()); }; initializeSettings(); diff --git a/src/reducers/index.js b/src/reducers/index.js index 83a9a56..9beb81c 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -1,14 +1,17 @@ import inputReducer from '../reducers/input'; import consoleReducer from '../reducers/console'; +import settingReducer from '../reducers/setting'; const defaultState = { input: inputReducer(undefined, {}), - console: consoleReducer(undefined, {}) + console: consoleReducer(undefined, {}), + setting: settingReducer(undefined, {}), }; export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { input: inputReducer(state.input, action), - console: consoleReducer(state.console, action) + console: consoleReducer(state.console, action), + setting: settingReducer(state.setting, action), }); } diff --git a/src/reducers/input.js b/src/reducers/input.js index dca26e2..eb7ff24 100644 --- a/src/reducers/input.js +++ b/src/reducers/input.js @@ -2,7 +2,6 @@ import actions from '../actions'; const defaultState = { keys: [], - keymaps: {} }; export default function reducer(state = defaultState, action = {}) { @@ -20,11 +19,6 @@ export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { keys: [], }); - case actions.INPUT_SET_KEYMAPS: - return Object.assign({}, state, { - keymaps: action.keymaps, - keys: [], - }); default: return state; } diff --git a/src/shared/default-settings.js b/src/shared/default-settings.js index b186f95..3149581 100644 --- a/src/shared/default-settings.js +++ b/src/shared/default-settings.js @@ -36,6 +36,17 @@ export default { "]]": { "type": "navigate.link.next" }, "gu": { "type": "navigate.parent" }, "gU": { "type": "navigate.root" } + }, + "search": { + "default": "google", + "engines": { + "google": "https://google.com/search?q={}", + "yahoo": "https://search.yahoo.com/search?p={}", + "bing": "https://www.bing.com/search?q={}", + "duckduckgo": "https://duckduckgo.com/?q={}", + "twitter": "https://twitter.com/search?q={}", + "wikipedia": "https://en.wikipedia.org/w/index.php?search={}" + } } }` }; diff --git a/src/shared/validators/setting.js b/src/shared/validators/setting.js index df04e50..0b853a8 100644 --- a/src/shared/validators/setting.js +++ b/src/shared/validators/setting.js @@ -1,6 +1,6 @@ import operations from '../../operations'; -const VALID_TOP_KEYS = ['keymaps']; +const VALID_TOP_KEYS = ['keymaps', 'search']; const VALID_OPERATION_VALUES = Object.keys(operations).map((key) => { return operations[key]; }); |