insomnia/app/ui/components/editors/EnvironmentEditor.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, PureComponent} from 'react';
import Editor from '../codemirror/Editor';
import {DEBOUNCE_MILLIS} from '../../../common/constants';
class EnvironmentEditor extends PureComponent {
_handleChange = () => this.props.didChange();
_setEditorRef = n => this._editor = n;
getValue () {
return JSON.parse(this._editor.getValue());
}
isValid () {
try {
return this.getValue() !== undefined;
} catch (e) {
// Failed to parse JSON
return false;
}
}
render () {
2017-02-26 23:47:45 +00:00
const {
environment,
editorFontSize,
editorKeyMap,
render,
2017-02-26 23:47:45 +00:00
lineWrapping,
...props
} = this.props;
return (
<Editor
ref={this._setEditorRef}
autoPrettify
2017-01-23 22:41:31 +00:00
fontSize={editorFontSize}
2017-02-26 23:47:45 +00:00
lineWrapping={lineWrapping}
2017-01-24 22:18:11 +00:00
keyMap={editorKeyMap}
onChange={this._handleChange}
debounceMillis={DEBOUNCE_MILLIS * 6}
defaultValue={JSON.stringify(environment)}
render={render}
mode="application/json"
{...props}
/>
)
}
}
EnvironmentEditor.propTypes = {
environment: PropTypes.object.isRequired,
2017-01-23 22:41:31 +00:00
didChange: PropTypes.func.isRequired,
editorFontSize: PropTypes.number.isRequired,
2017-01-24 22:18:11 +00:00
editorKeyMap: PropTypes.string.isRequired,
render: PropTypes.func.isRequired,
2017-02-26 23:47:45 +00:00
lineWrapping: PropTypes.bool.isRequired,
};
export default EnvironmentEditor;