diff options
Diffstat (limited to 'src/settings')
-rw-r--r-- | src/settings/actions/setting.js | 16 | ||||
-rw-r--r-- | src/settings/components/index.jsx | 91 | ||||
-rw-r--r-- | src/settings/components/setting.js | 45 | ||||
-rw-r--r-- | src/settings/components/site.scss (renamed from src/settings/site.scss) | 2 | ||||
-rw-r--r-- | src/settings/index.html | 10 | ||||
-rw-r--r-- | src/settings/index.js | 15 | ||||
-rw-r--r-- | src/settings/index.jsx | 18 | ||||
-rw-r--r-- | src/settings/reducers/setting.js | 12 |
8 files changed, 129 insertions, 80 deletions
diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index 697bcf0..c1b27c8 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -3,9 +3,9 @@ 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 browser.storage.local.get('settings').then(({ settings }) => { + if (settings) { + return set(settings); } return set(DefaultSettings); }, console.error); @@ -13,10 +13,12 @@ const load = () => { const save = (settings) => { return browser.storage.local.set({ - settings + settings, }).then(() => { return browser.runtime.sendMessage({ type: messages.SETTINGS_RELOAD + }).then(() => { + return set(settings); }); }); }; @@ -24,8 +26,10 @@ const save = (settings) => { const set = (settings) => { return { type: actions.SETTING_SET_SETTINGS, - settings, + source: settings.source, + json: settings.json, + value: JSON.parse(settings.json), }; }; -export { load, save, set }; +export { load, save }; diff --git a/src/settings/components/index.jsx b/src/settings/components/index.jsx new file mode 100644 index 0000000..4418942 --- /dev/null +++ b/src/settings/components/index.jsx @@ -0,0 +1,91 @@ +import './site.scss'; +import React from 'react'; +import PropTypes from 'prop-types'; +import * as settingActions from 'settings/actions/setting'; +import * as validator from 'shared/validators/setting'; + +class SettingsComponent extends React.Component { + constructor(props, context) { + super(props, context); + + this.state = { + settings: { + json: '', + } + }; + this.context.store.subscribe(this.stateChanged.bind(this)); + } + + componentDidMount() { + this.context.store.dispatch(settingActions.load()); + } + + stateChanged() { + let settings = this.context.store.getState(); + this.setState({ + settings: { + source: settings.source, + json: settings.json, + } + }); + } + + render() { + return ( + <div> + <h1>Configure Vim-Vixen</h1> + <form className='vimvixen-settings-form'> + + <p>Load settings from:</p> + <input type='radio' id='setting-source-json' + name='source' + value='json' + onChange={this.bindAndSave.bind(this)} + checked={this.state.settings.source === 'json'} /> + <label htmlFor='settings-source-json'>JSON</label> + + <textarea name='json' spellCheck='false' + onInput={this.validate.bind(this)} + onChange={this.bindValue.bind(this)} + onBlur={this.bindAndSave.bind(this)} + value={this.state.settings.json} /> + </form> + </div> + ); + } + + validate(e) { + try { + let settings = JSON.parse(e.target.value); + validator.validate(settings); + e.target.setCustomValidity(''); + } catch (err) { + e.target.setCustomValidity(err.message); + } + } + + bindValue(e) { + let nextSettings = Object.assign({}, this.state.settings); + nextSettings[e.target.name] = e.target.value; + + this.setState({ settings: nextSettings }); + } + + bindAndSave(e) { + this.bindValue(e); + + try { + let json = this.state.settings.json; + validator.validate(JSON.parse(json)); + this.context.store.dispatch(settingActions.save(this.state.settings)); + } catch (err) { + // error already shown + } + } +} + +SettingsComponent.contextTypes = { + store: PropTypes.any, +}; + +export default SettingsComponent; diff --git a/src/settings/components/setting.js b/src/settings/components/setting.js deleted file mode 100644 index 14482a3..0000000 --- a/src/settings/components/setting.js +++ /dev/null @@ -1,45 +0,0 @@ -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; - } -} diff --git a/src/settings/site.scss b/src/settings/components/site.scss index 5707c8a..fae9c39 100644 --- a/src/settings/site.scss +++ b/src/settings/components/site.scss @@ -1,5 +1,5 @@ .vimvixen-settings-form { - textarea[name=plain-json] { + textarea[name=json] { font-family: monospace; width: 100%; min-height: 64ex; diff --git a/src/settings/index.html b/src/settings/index.html index 99d6c6b..6fe00df 100644 --- a/src/settings/index.html +++ b/src/settings/index.html @@ -4,15 +4,7 @@ <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> + <div id='vimvixen-settings'></div> <script src='settings.js'></script> </body> </html> diff --git a/src/settings/index.js b/src/settings/index.js deleted file mode 100644 index c8d6cc4..0000000 --- a/src/settings/index.js +++ /dev/null @@ -1,15 +0,0 @@ -import './site.scss'; -import SettingComponent from 'settings/components/setting'; -import settingReducer from 'settings/reducers/setting'; -import { createStore } from 'shared/store'; - -const store = createStore(settingReducer); -let settingComponent = null; - -store.subscribe(() => { - settingComponent.update(); -}); - -document.addEventListener('DOMContentLoaded', () => { - settingComponent = new SettingComponent(document.body, store); -}); diff --git a/src/settings/index.jsx b/src/settings/index.jsx new file mode 100644 index 0000000..7516fb7 --- /dev/null +++ b/src/settings/index.jsx @@ -0,0 +1,18 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import SettingsComponent from './components'; +import reducer from 'settings/reducers/setting'; +import Provider from 'shared/store/provider'; +import { createStore } from 'shared/store'; + +const store = createStore(reducer); + +document.addEventListener('DOMContentLoaded', () => { + let wrapper = document.getElementById('vimvixen-settings'); + ReactDOM.render( + <Provider store={store}> + <SettingsComponent /> + </Provider>, + wrapper + ); +}); diff --git a/src/settings/reducers/setting.js b/src/settings/reducers/setting.js index f7d9242..a61c09f 100644 --- a/src/settings/reducers/setting.js +++ b/src/settings/reducers/setting.js @@ -1,15 +1,19 @@ import actions from 'settings/actions'; const defaultState = { - settings: {} + source: '', + 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 { + source: action.source, + json: action.json, + value: action.value, + }; default: return state; } |