2017-06-09 07:26:25 +03:00
|
|
|
import 'codemirror/lib/codemirror.css';
|
|
|
|
import 'codemirror/theme/eclipse.css';
|
|
|
|
import 'codemirror/addon/fold/foldgutter.css';
|
|
|
|
import 'codemirror/addon/lint/lint.css';
|
|
|
|
import 'codemirror/mode/yaml/yaml';
|
|
|
|
import 'codemirror/mode/javascript/javascript';
|
2017-07-05 16:33:16 +03:00
|
|
|
import 'codemirror/mode/properties/properties';
|
2017-06-09 07:26:25 +03:00
|
|
|
import 'codemirror/addon/edit/closebrackets';
|
|
|
|
import 'codemirror/addon/edit/matchbrackets';
|
|
|
|
import 'codemirror/addon/fold/foldcode';
|
|
|
|
import 'codemirror/addon/fold/foldgutter';
|
|
|
|
import 'codemirror/addon/fold/brace-fold';
|
|
|
|
import 'codemirror/addon/fold/indent-fold';
|
|
|
|
import 'codemirror/addon/fold/comment-fold';
|
|
|
|
import 'codemirror/addon/hint/show-hint';
|
|
|
|
import 'codemirror/addon/selection/active-line';
|
|
|
|
import 'codemirror/addon/edit/closetag';
|
|
|
|
|
2017-06-09 03:54:25 +03:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import ReactCodeMirror from 'react-codemirror';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
2017-06-09 07:22:49 +03:00
|
|
|
const options = {
|
|
|
|
theme: 'eclipse',
|
|
|
|
indentUnit: 2,
|
|
|
|
smartIndent: true,
|
|
|
|
tabSize: 2,
|
|
|
|
indentWithTabs: false,
|
|
|
|
electricChars: true,
|
|
|
|
lineNumbers: true,
|
|
|
|
inputStyle: 'contenteditable',
|
|
|
|
lint: true,
|
|
|
|
autoCloseBrackets: true,
|
|
|
|
styleActiveLine: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
lineWrapping: true,
|
|
|
|
foldGutter: true,
|
|
|
|
autoCloseTags: true,
|
|
|
|
viewportMargin: Infinity,
|
|
|
|
gutters: [
|
|
|
|
'CodeMirror-lint-markers',
|
|
|
|
'CodeMirror-foldgutter',
|
|
|
|
'CodeMirror-linenumbers'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2017-06-09 03:54:25 +03:00
|
|
|
class ManifestEditor extends Component {
|
2017-06-09 07:22:49 +03:00
|
|
|
constructor() {
|
2017-06-09 03:54:25 +03:00
|
|
|
super();
|
|
|
|
|
|
|
|
this._refs = {};
|
|
|
|
}
|
|
|
|
ref(name) {
|
|
|
|
return ref => {
|
|
|
|
this._refs[name] = ref;
|
|
|
|
};
|
|
|
|
}
|
2017-06-09 07:26:25 +03:00
|
|
|
options({ mode, readOnly }) {
|
2017-06-09 03:54:25 +03:00
|
|
|
const modes = {
|
|
|
|
json: {
|
|
|
|
name: 'javascript',
|
|
|
|
json: true
|
|
|
|
},
|
2017-07-05 16:33:16 +03:00
|
|
|
yaml: 'yaml',
|
|
|
|
ini: 'properties'
|
2017-06-09 03:54:25 +03:00
|
|
|
};
|
|
|
|
|
2017-06-09 07:22:49 +03:00
|
|
|
return Object.assign({}, options, {
|
2017-06-09 07:26:25 +03:00
|
|
|
mode: modes[mode.toLowerCase()],
|
|
|
|
readOnly
|
2017-06-09 07:22:49 +03:00
|
|
|
});
|
2017-06-09 03:54:25 +03:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ReactCodeMirror
|
|
|
|
ref={this.ref('cm')}
|
|
|
|
value={this.props.value || this.props.defaultValue}
|
|
|
|
onChange={this.props.onChange}
|
|
|
|
onFocusChange={this.props.onFocusChange}
|
|
|
|
options={this.options(this.props)}
|
|
|
|
autoSave={this.props.autoSave}
|
|
|
|
preserveScrollPosition={this.props.preserveScrollPosition}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ManifestEditor.defaultProps = {
|
|
|
|
mode: 'json',
|
|
|
|
defaultValue: '',
|
|
|
|
onChange: () => null,
|
|
|
|
onFocusChange: () => null,
|
|
|
|
autoSave: true,
|
2017-06-09 07:26:25 +03:00
|
|
|
preserveScrollPosition: true,
|
|
|
|
readOnly: false
|
2017-06-09 03:54:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
ManifestEditor.propTypes = {
|
2017-07-19 13:31:16 +03:00
|
|
|
mode: PropTypes.oneOf(['json', 'yaml', 'ini']),
|
2017-06-09 03:54:25 +03:00
|
|
|
value: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onFocusChange: PropTypes.func,
|
|
|
|
autoSave: PropTypes.bool,
|
2017-06-09 07:26:25 +03:00
|
|
|
preserveScrollPosition: PropTypes.bool,
|
|
|
|
readOnly: PropTypes.bool
|
2017-06-09 03:54:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ManifestEditor;
|