2017-02-28 21:32:23 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2017-02-27 21:00:13 +00:00
|
|
|
import Editor from '../codemirror/Editor';
|
|
|
|
import {DEBOUNCE_MILLIS} from '../../../common/constants';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-02-28 21:32:23 +00:00
|
|
|
class EnvironmentEditor extends PureComponent {
|
2017-02-27 21:00:13 +00:00
|
|
|
_handleChange = () => this.props.didChange();
|
2016-11-29 22:28:55 +00:00
|
|
|
|
|
|
|
_setEditorRef = n => this._editor = n;
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
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,
|
2017-02-27 21:00:13 +00:00
|
|
|
render,
|
2017-02-26 23:47:45 +00:00
|
|
|
lineWrapping,
|
|
|
|
...props
|
|
|
|
} = this.props;
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Editor
|
2016-11-29 22:28:55 +00:00
|
|
|
ref={this._setEditorRef}
|
2017-03-01 21:15:56 +00:00
|
|
|
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}
|
2016-11-29 22:28:55 +00:00
|
|
|
onChange={this._handleChange}
|
2017-02-27 21:00:13 +00:00
|
|
|
debounceMillis={DEBOUNCE_MILLIS * 6}
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue={JSON.stringify(environment)}
|
2017-02-27 21:00:13 +00:00
|
|
|
render={render}
|
2016-08-15 17:04:36 +00:00
|
|
|
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,
|
2017-02-27 21:00:13 +00:00
|
|
|
render: PropTypes.func.isRequired,
|
2017-02-26 23:47:45 +00:00
|
|
|
lineWrapping: PropTypes.bool.isRequired,
|
2016-08-15 17:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default EnvironmentEditor;
|