separate settings
This commit is contained in:
parent
541449b1fc
commit
58123210ab
13 changed files with 17 additions and 34 deletions
4
src/settings/actions/index.js
Normal file
4
src/settings/actions/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
export default {
|
||||
// Settings
|
||||
SETTING_SET_SETTINGS: 'setting.set.settings',
|
||||
};
|
31
src/settings/actions/setting.js
Normal file
31
src/settings/actions/setting.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
import actions from 'settings/actions';
|
||||
import messages from 'shared/messages';
|
||||
import DefaultSettings from 'shared/default-settings';
|
||||
|
||||
const load = () => {
|
||||
return browser.storage.local.get('settings').then((value) => {
|
||||
if (value.settings) {
|
||||
return set(value.settings);
|
||||
}
|
||||
return set(DefaultSettings);
|
||||
}, console.error);
|
||||
};
|
||||
|
||||
const save = (settings) => {
|
||||
return browser.storage.local.set({
|
||||
settings
|
||||
}).then(() => {
|
||||
return browser.runtime.sendMessage({
|
||||
type: messages.SETTINGS_RELOAD
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const set = (settings) => {
|
||||
return {
|
||||
type: actions.SETTING_SET_SETTINGS,
|
||||
settings,
|
||||
};
|
||||
};
|
||||
|
||||
export { load, save, set };
|
45
src/settings/components/setting.js
Normal file
45
src/settings/components/setting.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
import * as settingActions from 'settings/actions/setting';
|
||||
import { validate } from 'shared/validators/setting';
|
||||
|
||||
export default class SettingComponent {
|
||||
constructor(wrapper, store) {
|
||||
this.wrapper = wrapper;
|
||||
this.store = store;
|
||||
|
||||
let doc = wrapper.ownerDocument;
|
||||
let form = doc.getElementById('vimvixen-settings-form');
|
||||
form.addEventListener('submit', this.onSubmit.bind(this));
|
||||
|
||||
let plainJson = form.elements['plain-json'];
|
||||
plainJson.addEventListener('input', this.onPlainJsonChanged.bind(this));
|
||||
|
||||
store.dispatch(settingActions.load());
|
||||
}
|
||||
|
||||
onSubmit(e) {
|
||||
let settings = {
|
||||
json: e.target.elements['plain-json'].value,
|
||||
};
|
||||
this.store.dispatch(settingActions.save(settings));
|
||||
e.preventDefault();
|
||||
}
|
||||
|
||||
onPlainJsonChanged(e) {
|
||||
try {
|
||||
let settings = JSON.parse(e.target.value);
|
||||
validate(settings);
|
||||
e.target.setCustomValidity('');
|
||||
} catch (err) {
|
||||
e.target.setCustomValidity(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
update() {
|
||||
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;
|
||||
}
|
||||
}
|
18
src/settings/index.html
Normal file
18
src/settings/index.html
Normal file
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Configure</h1>
|
||||
|
||||
<h2>Home page</h2>
|
||||
<form id='vimvixen-settings-form' class='vimvixen-settings-form'>
|
||||
<label for='load-from-json'>Load from JSON:</label>
|
||||
<textarea name='plain-json' spellcheck='false'></textarea>
|
||||
|
||||
<button type='submit'>Save</button>
|
||||
</form>
|
||||
<script src='settings.js'></script>
|
||||
</body>
|
||||
</html>
|
15
src/settings/index.js
Normal file
15
src/settings/index.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import './site.scss';
|
||||
import SettingComponent from 'settings/components/setting';
|
||||
import settingReducer from 'settings/reducers/setting';
|
||||
import { createStore } from 'store';
|
||||
|
||||
const store = createStore(settingReducer);
|
||||
let settingComponent = null;
|
||||
|
||||
store.subscribe(() => {
|
||||
settingComponent.update();
|
||||
});
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
settingComponent = new SettingComponent(document.body, store);
|
||||
});
|
17
src/settings/reducers/setting.js
Normal file
17
src/settings/reducers/setting.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
import actions from 'settings/actions';
|
||||
|
||||
const defaultState = {
|
||||
settings: {}
|
||||
};
|
||||
|
||||
export default function reducer(state = defaultState, action = {}) {
|
||||
switch (action.type) {
|
||||
case actions.SETTING_SET_SETTINGS:
|
||||
return Object.assign({}, state, {
|
||||
settings: action.settings,
|
||||
});
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
8
src/settings/site.scss
Normal file
8
src/settings/site.scss
Normal file
|
@ -0,0 +1,8 @@
|
|||
.vimvixen-settings-form {
|
||||
textarea[name=plain-json] {
|
||||
font-family: monospace;
|
||||
width: 100%;
|
||||
min-height: 64ex;
|
||||
resize: vertical;
|
||||
}
|
||||
}
|
Reference in a new issue