2017-02-28 21:32:23 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2017-04-14 21:47:15 +00:00
|
|
|
import CodeEditor from '../codemirror/code-editor';
|
2017-02-27 21:00:13 +00:00
|
|
|
import {DEBOUNCE_MILLIS} from '../../../common/constants';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class EnvironmentEditor extends PureComponent {
|
2017-05-25 18:31:36 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
|
|
|
warning: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleChange () {
|
2017-05-25 18:31:36 +00:00
|
|
|
let error = null;
|
|
|
|
let warning = null;
|
|
|
|
let value = null;
|
|
|
|
|
|
|
|
// Check for JSON parse errors
|
|
|
|
try {
|
|
|
|
value = this.getValue();
|
|
|
|
} catch (err) {
|
|
|
|
error = err.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid key names
|
|
|
|
if (value) {
|
|
|
|
for (const key of Object.keys(value)) {
|
|
|
|
if (!key.match(/^[a-zA-Z_$][0-9a-zA-Z_$]*$/)) {
|
|
|
|
warning = `"${key}" must only contain letters, numbers, and underscores`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.error !== error || this.state.warning !== warning) {
|
|
|
|
this.setState({error, warning});
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
this.props.didChange();
|
|
|
|
}
|
2016-11-29 22:28:55 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_setEditorRef (n) {
|
|
|
|
this._editor = n;
|
|
|
|
}
|
2016-11-29 22:28:55 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
getValue () {
|
|
|
|
return JSON.parse(this._editor.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid () {
|
2017-05-25 18:31:36 +00:00
|
|
|
return !this.state.error;
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-02-26 23:47:45 +00:00
|
|
|
const {
|
|
|
|
environment,
|
|
|
|
editorFontSize,
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize,
|
2017-02-26 23:47:45 +00:00
|
|
|
editorKeyMap,
|
2017-02-27 21:00:13 +00:00
|
|
|
render,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext,
|
2017-02-26 23:47:45 +00:00
|
|
|
lineWrapping,
|
|
|
|
...props
|
|
|
|
} = this.props;
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-05-25 18:31:36 +00:00
|
|
|
const {error, warning} = this.state;
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
return (
|
2017-05-25 18:31:36 +00:00
|
|
|
<div className="environment-editor">
|
|
|
|
<CodeEditor
|
|
|
|
ref={this._setEditorRef}
|
|
|
|
autoPrettify
|
|
|
|
fontSize={editorFontSize}
|
|
|
|
indentSize={editorIndentSize}
|
|
|
|
lineWrapping={lineWrapping}
|
|
|
|
keyMap={editorKeyMap}
|
|
|
|
onChange={this._handleChange}
|
|
|
|
debounceMillis={DEBOUNCE_MILLIS * 6}
|
|
|
|
defaultValue={JSON.stringify(environment)}
|
|
|
|
render={render}
|
|
|
|
getRenderContext={getRenderContext}
|
|
|
|
mode="application/json"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
{error && <p className="notice error margin">{error}</p>}
|
|
|
|
{(!error && warning) && <p className="notice warning margin">{warning}</p>}
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EnvironmentEditor.propTypes = {
|
|
|
|
environment: PropTypes.object.isRequired,
|
2017-01-23 22:41:31 +00:00
|
|
|
didChange: PropTypes.func.isRequired,
|
|
|
|
editorFontSize: PropTypes.number.isRequired,
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize: 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-03-12 00:31:23 +00:00
|
|
|
getRenderContext: PropTypes.func.isRequired,
|
2017-03-03 20:09:08 +00:00
|
|
|
lineWrapping: PropTypes.bool.isRequired
|
2016-08-15 17:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default EnvironmentEditor;
|