From c913dcdec775094daa9a64fa40034b3122320660 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 9 Oct 2017 15:30:05 +0900 Subject: [PATCH] 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 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 }); }); });