parse json in settings
This commit is contained in:
parent
355c0c6457
commit
c913dcdec7
7 changed files with 24 additions and 28 deletions
0
src/background/actions/settings.js
Normal file
0
src/background/actions/settings.js
Normal file
|
@ -22,15 +22,7 @@ export default class BackgroundComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
let state = this.store.getState();
|
this.settings = this.store.getState();
|
||||||
this.updateSettings(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
updateSettings(setting) {
|
|
||||||
if (!setting.settings.json) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.settings = JSON.parse(setting.settings.json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(message, sender) {
|
onMessage(message, sender) {
|
||||||
|
@ -58,7 +50,7 @@ export default class BackgroundComponent {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
case messages.SETTINGS_QUERY:
|
case messages.SETTINGS_QUERY:
|
||||||
return Promise.resolve(this.store.getState().settings);
|
return Promise.resolve(this.store.getState().value);
|
||||||
case messages.CONSOLE_QUERY_COMPLETIONS:
|
case messages.CONSOLE_QUERY_COMPLETIONS:
|
||||||
return commands.complete(message.text, this.settings);
|
return commands.complete(message.text, this.settings);
|
||||||
case messages.SETTINGS_RELOAD:
|
case messages.SETTINGS_RELOAD:
|
||||||
|
|
|
@ -34,8 +34,7 @@ const reloadSettings = () => {
|
||||||
return browser.runtime.sendMessage({
|
return browser.runtime.sendMessage({
|
||||||
type: messages.SETTINGS_QUERY,
|
type: messages.SETTINGS_QUERY,
|
||||||
}).then((settings) => {
|
}).then((settings) => {
|
||||||
let keymaps = JSON.parse(settings.json).keymaps;
|
store.dispatch(inputActions.setKeymaps(settings.keymaps));
|
||||||
store.dispatch(inputActions.setKeymaps(keymaps));
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,9 @@ import DefaultSettings from 'shared/default-settings';
|
||||||
|
|
||||||
const load = () => {
|
const load = () => {
|
||||||
return browser.storage.local.get('settings').then((value) => {
|
return browser.storage.local.get('settings').then((value) => {
|
||||||
if (value.settings) {
|
let settings = value.settings;
|
||||||
return set(value.settings);
|
if (settings) {
|
||||||
|
return set(settings);
|
||||||
}
|
}
|
||||||
return set(DefaultSettings);
|
return set(DefaultSettings);
|
||||||
}, console.error);
|
}, console.error);
|
||||||
|
@ -13,7 +14,7 @@ const load = () => {
|
||||||
|
|
||||||
const save = (settings) => {
|
const save = (settings) => {
|
||||||
return browser.storage.local.set({
|
return browser.storage.local.set({
|
||||||
settings
|
settings,
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
return browser.runtime.sendMessage({
|
return browser.runtime.sendMessage({
|
||||||
type: messages.SETTINGS_RELOAD
|
type: messages.SETTINGS_RELOAD
|
||||||
|
@ -24,8 +25,9 @@ const save = (settings) => {
|
||||||
const set = (settings) => {
|
const set = (settings) => {
|
||||||
return {
|
return {
|
||||||
type: actions.SETTING_SET_SETTINGS,
|
type: actions.SETTING_SET_SETTINGS,
|
||||||
settings,
|
json: settings.json,
|
||||||
|
value: JSON.parse(settings.json),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export { load, save, set };
|
export { load, save };
|
||||||
|
|
|
@ -35,11 +35,12 @@ export default class SettingComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
let { settings } = this.store.getState();
|
let settings = this.store.getState();
|
||||||
|
|
||||||
let doc = this.wrapper.ownerDocument;
|
let doc = this.wrapper.ownerDocument;
|
||||||
let form = doc.getElementById('vimvixen-settings-form');
|
let form = doc.getElementById('vimvixen-settings-form');
|
||||||
let plainJsonInput = form.elements['plain-json'];
|
let plainJsonInput = form.elements['plain-json'];
|
||||||
plainJsonInput.value = settings.json;
|
plainJsonInput.value = settings.json;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
import actions from 'settings/actions';
|
import actions from 'settings/actions';
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
settings: {}
|
json: '',
|
||||||
|
value: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function reducer(state = defaultState, action = {}) {
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case actions.SETTING_SET_SETTINGS:
|
case actions.SETTING_SET_SETTINGS:
|
||||||
return Object.assign({}, state, {
|
return {
|
||||||
settings: action.settings,
|
json: action.json,
|
||||||
});
|
value: action.value,
|
||||||
|
};
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,18 +5,18 @@ import settingReducer from 'settings/reducers/setting';
|
||||||
describe("setting reducer", () => {
|
describe("setting reducer", () => {
|
||||||
it('return the initial state', () => {
|
it('return the initial state', () => {
|
||||||
let state = settingReducer(undefined, {});
|
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', () => {
|
it('return next state for SETTING_SET_SETTINGS', () => {
|
||||||
let action = {
|
let action = {
|
||||||
type: actions.SETTING_SET_SETTINGS,
|
type: actions.SETTING_SET_SETTINGS,
|
||||||
settings: { value1: 'hello', value2: 'world' },
|
json: '{ "key": "value" }',
|
||||||
|
value: { key: 123 },
|
||||||
};
|
};
|
||||||
let state = settingReducer(undefined, action);
|
let state = settingReducer(undefined, action);
|
||||||
expect(state).to.have.deep.property('settings', {
|
expect(state).to.have.deep.property('json', '{ "key": "value" }');
|
||||||
value1: 'hello',
|
expect(state).to.have.deep.property('value', { key: 123 });
|
||||||
value2: 'world',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Reference in a new issue