From 355c0c64575adad4886647a1df3da173598b558b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 9 Oct 2017 14:30:58 +0900 Subject: save keymap in input store in content --- src/content/actions/index.js | 5 +---- src/content/actions/input.js | 9 ++++++++- src/content/components/keymapper.js | 16 ++-------------- src/content/index.js | 5 +++-- src/content/reducers/index.js | 3 --- src/content/reducers/input.js | 5 +++++ 6 files changed, 19 insertions(+), 24 deletions(-) (limited to 'src/content') diff --git a/src/content/actions/index.js b/src/content/actions/index.js index 0b3749d..f8db948 100644 --- a/src/content/actions/index.js +++ b/src/content/actions/index.js @@ -2,16 +2,13 @@ export default { // User input INPUT_KEY_PRESS: 'input.key,press', INPUT_CLEAR_KEYS: 'input.clear.keys', - INPUT_SET_KEYMAPS: 'input.set,keymaps', + INPUT_SET_KEYMAPS: 'input.set.keymaps', // Completion COMPLETION_SET_ITEMS: 'completion.set.items', COMPLETION_SELECT_NEXT: 'completions.select.next', COMPLETION_SELECT_PREV: 'completions.select.prev', - // Settings - SETTING_SET_SETTINGS: 'setting.set.settings', - // Follow FOLLOW_ENABLE: 'follow.enable', FOLLOW_DISABLE: 'follow.disable', diff --git a/src/content/actions/input.js b/src/content/actions/input.js index cc4efac..10ff835 100644 --- a/src/content/actions/input.js +++ b/src/content/actions/input.js @@ -20,4 +20,11 @@ const clearKeys = () => { }; }; -export { keyPress, clearKeys }; +const setKeymaps = (keymaps) => { + return { + type: actions.INPUT_SET_KEYMAPS, + keymaps, + }; +}; + +export { keyPress, clearKeys, setKeymaps }; diff --git a/src/content/components/keymapper.js b/src/content/components/keymapper.js index 8f2cead..655c3f2 100644 --- a/src/content/components/keymapper.js +++ b/src/content/components/keymapper.js @@ -10,14 +10,10 @@ export default class KeymapperComponent { } key(key, ctrl) { - let keymaps = this.keymaps(); - if (!keymaps) { - return; - } this.store.dispatch(inputActions.keyPress(key, ctrl)); let input = this.store.getState().input; - let matched = Object.keys(keymaps).filter((keyStr) => { + let matched = Object.keys(input.keymaps).filter((keyStr) => { return keyStr.startsWith(input.keys); }); if (matched.length === 0) { @@ -27,17 +23,9 @@ export default class KeymapperComponent { matched.length === 1 && input.keys !== matched[0]) { return true; } - let operation = keymaps[matched]; + let operation = input.keymaps[matched]; this.store.dispatch(operationActions.exec(operation)); this.store.dispatch(inputActions.clearKeys()); return true; } - - keymaps() { - let settings = this.store.getState().setting.settings; - if (!settings || !settings.json) { - return null; - } - return JSON.parse(settings.json).keymaps; - } } diff --git a/src/content/index.js b/src/content/index.js index 63bbf77..1ed8463 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -1,6 +1,6 @@ import './console-frame.scss'; import * as consoleFrames from './console-frames'; -import * as settingActions from 'settings/actions/setting'; +import * as inputActions from './actions/input'; import { createStore } from 'shared/store'; import ContentInputComponent from 'content/components/content-input'; import KeymapperComponent from 'content/components/keymapper'; @@ -34,7 +34,8 @@ const reloadSettings = () => { return browser.runtime.sendMessage({ type: messages.SETTINGS_QUERY, }).then((settings) => { - store.dispatch(settingActions.set(settings)); + let keymaps = JSON.parse(settings.json).keymaps; + store.dispatch(inputActions.setKeymaps(keymaps)); }); }; diff --git a/src/content/reducers/index.js b/src/content/reducers/index.js index a62217f..c026a19 100644 --- a/src/content/reducers/index.js +++ b/src/content/reducers/index.js @@ -1,18 +1,15 @@ -import settingReducer from 'settings/reducers/setting'; import inputReducer from './input'; import followReducer from './follow'; // Make setting reducer instead of re-use const defaultState = { input: inputReducer(undefined, {}), - setting: settingReducer(undefined, {}), follow: followReducer(undefined, {}), }; export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { input: inputReducer(state.input, action), - setting: settingReducer(state.setting, action), follow: followReducer(state.follow, action), }); } diff --git a/src/content/reducers/input.js b/src/content/reducers/input.js index 802020f..c79b206 100644 --- a/src/content/reducers/input.js +++ b/src/content/reducers/input.js @@ -2,6 +2,7 @@ import actions from 'content/actions'; const defaultState = { keys: '', + keymaps: {}, }; export default function reducer(state = defaultState, action = {}) { @@ -14,6 +15,10 @@ export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { keys: '', }); + case actions.INPUT_SET_KEYMAPS: + return Object.assign({}, state, { + keymaps: action.keymaps, + }); default: return state; } -- cgit v1.2.3 From c913dcdec775094daa9a64fa40034b3122320660 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 9 Oct 2017 15:30:05 +0900 Subject: parse json in settings --- src/background/actions/settings.js | 0 src/background/components/background.js | 12 ++---------- src/content/index.js | 3 +-- src/settings/actions/setting.js | 12 +++++++----- src/settings/components/setting.js | 3 ++- src/settings/reducers/setting.js | 10 ++++++---- test/settings/reducers/setting.test.js | 12 ++++++------ 7 files changed, 24 insertions(+), 28 deletions(-) create mode 100644 src/background/actions/settings.js (limited to 'src/content') diff --git a/src/background/actions/settings.js b/src/background/actions/settings.js new file mode 100644 index 0000000..e69de29 diff --git a/src/background/components/background.js b/src/background/components/background.js index 0570a5a..06b6900 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -22,15 +22,7 @@ export default class BackgroundComponent { } update() { - let state = this.store.getState(); - this.updateSettings(state); - } - - updateSettings(setting) { - if (!setting.settings.json) { - return; - } - this.settings = JSON.parse(setting.settings.json); + this.settings = this.store.getState(); } onMessage(message, sender) { @@ -58,7 +50,7 @@ export default class BackgroundComponent { }); }); case messages.SETTINGS_QUERY: - return Promise.resolve(this.store.getState().settings); + return Promise.resolve(this.store.getState().value); case messages.CONSOLE_QUERY_COMPLETIONS: return commands.complete(message.text, this.settings); case messages.SETTINGS_RELOAD: diff --git a/src/content/index.js b/src/content/index.js index 1ed8463..64d86bb 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -34,8 +34,7 @@ const reloadSettings = () => { return browser.runtime.sendMessage({ type: messages.SETTINGS_QUERY, }).then((settings) => { - let keymaps = JSON.parse(settings.json).keymaps; - store.dispatch(inputActions.setKeymaps(keymaps)); + store.dispatch(inputActions.setKeymaps(settings.keymaps)); }); }; diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index 697bcf0..3525f0a 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -4,8 +4,9 @@ import DefaultSettings from 'shared/default-settings'; const load = () => { return browser.storage.local.get('settings').then((value) => { - if (value.settings) { - return set(value.settings); + let settings = value.settings; + if (settings) { + return set(settings); } return set(DefaultSettings); }, console.error); @@ -13,7 +14,7 @@ const load = () => { const save = (settings) => { return browser.storage.local.set({ - settings + settings, }).then(() => { return browser.runtime.sendMessage({ type: messages.SETTINGS_RELOAD @@ -24,8 +25,9 @@ const save = (settings) => { const set = (settings) => { return { type: actions.SETTING_SET_SETTINGS, - settings, + json: settings.json, + value: JSON.parse(settings.json), }; }; -export { load, save, set }; +export { load, save }; diff --git a/src/settings/components/setting.js b/src/settings/components/setting.js index 14482a3..cf1538f 100644 --- a/src/settings/components/setting.js +++ b/src/settings/components/setting.js @@ -35,11 +35,12 @@ export default class SettingComponent { } update() { - let { settings } = this.store.getState(); + let settings = this.store.getState(); let doc = this.wrapper.ownerDocument; let form = doc.getElementById('vimvixen-settings-form'); let plainJsonInput = form.elements['plain-json']; plainJsonInput.value = settings.json; + } } diff --git a/src/settings/reducers/setting.js b/src/settings/reducers/setting.js index f7d9242..7f32f72 100644 --- a/src/settings/reducers/setting.js +++ b/src/settings/reducers/setting.js @@ -1,15 +1,17 @@ import actions from 'settings/actions'; const defaultState = { - settings: {} + json: '', + value: {} }; export default function reducer(state = defaultState, action = {}) { switch (action.type) { case actions.SETTING_SET_SETTINGS: - return Object.assign({}, state, { - settings: action.settings, - }); + return { + json: action.json, + value: action.value, + }; default: return state; } diff --git a/test/settings/reducers/setting.test.js b/test/settings/reducers/setting.test.js index 0e84247..3468d4b 100644 --- a/test/settings/reducers/setting.test.js +++ b/test/settings/reducers/setting.test.js @@ -5,18 +5,18 @@ import settingReducer from 'settings/reducers/setting'; describe("setting reducer", () => { it('return the initial state', () => { let state = settingReducer(undefined, {}); - expect(state).to.have.deep.property('settings', {}); + expect(state).to.have.deep.property('json', ''); + expect(state).to.have.deep.property('value', {}); }); it('return next state for SETTING_SET_SETTINGS', () => { let action = { type: actions.SETTING_SET_SETTINGS, - settings: { value1: 'hello', value2: 'world' }, + json: '{ "key": "value" }', + value: { key: 123 }, }; let state = settingReducer(undefined, action); - expect(state).to.have.deep.property('settings', { - value1: 'hello', - value2: 'world', - }); + expect(state).to.have.deep.property('json', '{ "key": "value" }'); + expect(state).to.have.deep.property('value', { key: 123 }); }); }); -- cgit v1.2.3