diff options
Diffstat (limited to 'src/settings/components')
-rw-r--r-- | src/settings/components/index.jsx | 78 | ||||
-rw-r--r-- | src/settings/components/setting.js | 46 | ||||
-rw-r--r-- | src/settings/components/site.scss | 8 |
3 files changed, 86 insertions, 46 deletions
diff --git a/src/settings/components/index.jsx b/src/settings/components/index.jsx new file mode 100644 index 0000000..e57b09d --- /dev/null +++ b/src/settings/components/index.jsx @@ -0,0 +1,78 @@ +import './site.scss'; +import React from 'react'; +import PropTypes from 'prop-types'; +import * as settingActions from 'settings/actions/setting'; +import { validate } 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 }); + } + + render() { + return ( + <div> + <h1>Configure Vim-Vixen</h1> + + <form id='vimvixen-settings-form' className='vimvixen-settings-form' + onSubmit={this.saveSettings.bind(this)}> + <label htmlFor='load-from-json'>Load from JSON:</label> + <textarea name='plain-json' spellCheck='false' + onInput={this.onPlainJsonChanged.bind(this)} + onChange={this.bindValue.bind(this)} + value={this.state.settings.json} /> + <button type='submit'>Save</button> + </form> + </div> + ); + } + + saveSettings(e) { + let settings = { + json: e.target.elements['plain-json'].value, + }; + this.context.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); + } + } + + bindValue(e) { + let next = Object.assign({}, this.state, { + settings: { + 'json': e.target.value, + } + }); + this.setState(next); + } +} + +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 cf1538f..0000000 --- a/src/settings/components/setting.js +++ /dev/null @@ -1,46 +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/components/site.scss b/src/settings/components/site.scss new file mode 100644 index 0000000..5707c8a --- /dev/null +++ b/src/settings/components/site.scss @@ -0,0 +1,8 @@ +.vimvixen-settings-form { + textarea[name=plain-json] { + font-family: monospace; + width: 100%; + min-height: 64ex; + resize: vertical; + } +} |