Clean setting
This commit is contained in:
parent
96649fef63
commit
df890379ca
5 changed files with 144 additions and 143 deletions
|
@ -1,8 +1,9 @@
|
|||
import actions from 'settings/actions';
|
||||
import messages from 'shared/messages';
|
||||
import DefaultSettings from 'shared/settings/default';
|
||||
import * as settingsStorage from 'shared/settings/storage';
|
||||
import * as validator from 'shared/settings/validator';
|
||||
import KeymapsForm from '../components/form/keymaps-form';
|
||||
import * as settingsValues from 'shared/settings/values';
|
||||
import * as settingsStorage from 'shared/settings/storage';
|
||||
|
||||
const load = async() => {
|
||||
let settings = await settingsStorage.loadRaw();
|
||||
|
@ -10,6 +11,18 @@ const load = async() => {
|
|||
};
|
||||
|
||||
const save = async(settings) => {
|
||||
try {
|
||||
if (settings.source === 'json') {
|
||||
let value = JSON.parse(settings.json);
|
||||
validator.validate(value);
|
||||
}
|
||||
} catch (e) {
|
||||
return {
|
||||
type: actions.SETTING_SHOW_ERROR,
|
||||
error: e.toString(),
|
||||
json: settings.json,
|
||||
};
|
||||
}
|
||||
await settingsStorage.save(settings);
|
||||
await browser.runtime.sendMessage({
|
||||
type: messages.SETTINGS_RELOAD
|
||||
|
@ -17,21 +30,39 @@ const save = async(settings) => {
|
|||
return set(settings);
|
||||
};
|
||||
|
||||
const set = (settings) => {
|
||||
let value = JSON.parse(DefaultSettings.json);
|
||||
if (settings.source === 'json') {
|
||||
value = settingsValues.valueFromJson(settings.json);
|
||||
} else if (settings.source === 'form') {
|
||||
value = settingsValues.valueFromForm(settings.form);
|
||||
const switchToForm = (json) => {
|
||||
try {
|
||||
validator.validate(JSON.parse(json));
|
||||
// AllowdOps filters operations, this is dirty dependency
|
||||
let form = settingsValues.formFromJson(json, KeymapsForm.AllowdOps);
|
||||
return {
|
||||
type: actions.SETTING_SWITCH_TO_FORM,
|
||||
form,
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
type: actions.SETTING_SHOW_ERROR,
|
||||
error: e.toString(),
|
||||
json,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const switchToJson = (form) => {
|
||||
let json = settingsValues.jsonFromForm(form);
|
||||
return {
|
||||
type: actions.SETTING_SWITCH_TO_JSON,
|
||||
json,
|
||||
};
|
||||
};
|
||||
|
||||
const set = (settings) => {
|
||||
return {
|
||||
type: actions.SETTING_SET_SETTINGS,
|
||||
source: settings.source,
|
||||
json: settings.json,
|
||||
form: settings.form,
|
||||
value,
|
||||
};
|
||||
};
|
||||
|
||||
export { load, save };
|
||||
export { load, save, switchToForm, switchToJson };
|
||||
|
|
Reference in a new issue