2016-03-16 20:02:47 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
import {getDOMNode} from 'react-dom';
|
|
|
|
import CodeMirror from 'codemirror';
|
2016-03-16 23:34:25 +00:00
|
|
|
|
|
|
|
// Modes
|
2016-03-22 05:01:58 +00:00
|
|
|
import '../../../node_modules/codemirror/mode/css/css';
|
2016-03-16 20:02:47 +00:00
|
|
|
import 'codemirror/mode/htmlmixed/htmlmixed';
|
|
|
|
import 'codemirror/mode/javascript/javascript';
|
2016-03-16 23:34:25 +00:00
|
|
|
|
|
|
|
// CSS
|
|
|
|
import 'codemirror/lib/codemirror.css'
|
|
|
|
|
|
|
|
// Plugins
|
|
|
|
import 'codemirror/addon/fold/foldcode';
|
|
|
|
import 'codemirror/addon/fold/brace-fold';
|
|
|
|
import 'codemirror/addon/fold/comment-fold';
|
|
|
|
import 'codemirror/addon/fold/indent-fold';
|
|
|
|
import 'codemirror/addon/fold/xml-fold';
|
|
|
|
|
|
|
|
import 'codemirror/addon/fold/foldgutter';
|
|
|
|
import 'codemirror/addon/fold/foldgutter.css';
|
|
|
|
|
2016-03-23 18:34:39 +00:00
|
|
|
import 'codemirror/addon/display/placeholder';
|
|
|
|
|
2016-03-22 05:01:58 +00:00
|
|
|
// TODO: Figure out how to lint (json-lint doesn't build in webpack environment)
|
|
|
|
// import 'codemirror/addon/lint/lint';
|
|
|
|
// import 'codemirror/addon/lint/json-lint';
|
|
|
|
// import 'codemirror/addon/lint/html-lint';
|
|
|
|
// import 'codemirror/addon/lint/lint.css';
|
|
|
|
// import * as jsonlint from 'jsonlint';
|
|
|
|
// import * as htmlhint from 'htmlhint';
|
|
|
|
|
|
|
|
// window.jsonlint = jsonlint;
|
|
|
|
// window.htmlhint = htmlhint;
|
|
|
|
|
2016-03-16 23:34:25 +00:00
|
|
|
// CSS Themes
|
|
|
|
import 'codemirror/theme/monokai.css'
|
2016-04-10 02:58:48 +00:00
|
|
|
import 'codemirror/theme/neat.css'
|
2016-03-16 23:34:25 +00:00
|
|
|
|
|
|
|
// App styles
|
2016-03-22 05:01:58 +00:00
|
|
|
import '../../css/components/editor.scss';
|
2016-03-16 20:02:47 +00:00
|
|
|
|
2016-03-20 23:27:46 +00:00
|
|
|
const DEFAULT_DEBOUNCE_MILLIS = 500;
|
2016-03-20 20:42:27 +00:00
|
|
|
|
|
|
|
const BASE_CODEMIRROR_OPTIONS = {
|
|
|
|
theme: 'monokai',
|
|
|
|
lineNumbers: true,
|
2016-03-23 18:34:39 +00:00
|
|
|
placeholder: 'Start Typing...',
|
2016-03-20 20:42:27 +00:00
|
|
|
foldGutter: true,
|
2016-03-23 05:58:16 +00:00
|
|
|
height: 'auto',
|
2016-03-22 05:01:58 +00:00
|
|
|
gutters: [
|
|
|
|
'CodeMirror-linenumbers',
|
|
|
|
'CodeMirror-foldgutter',
|
|
|
|
'CodeMirror-lint-markers'
|
|
|
|
],
|
2016-03-21 05:47:49 +00:00
|
|
|
cursorScrollMargin: 80,
|
2016-03-20 20:42:27 +00:00
|
|
|
extraKeys: {
|
|
|
|
"Ctrl-Q": function (cm) {
|
|
|
|
cm.foldCode(cm.getCursor());
|
|
|
|
}
|
2016-03-21 05:47:49 +00:00
|
|
|
}
|
2016-03-20 20:42:27 +00:00
|
|
|
};
|
|
|
|
|
2016-03-16 20:02:47 +00:00
|
|
|
class Editor extends Component {
|
|
|
|
constructor () {
|
|
|
|
super();
|
2016-03-20 20:42:27 +00:00
|
|
|
this.state = {isFocused: false}
|
2016-03-16 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
var textareaNode = this.refs.textarea;
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
this.codeMirror = CodeMirror.fromTextArea(textareaNode, BASE_CODEMIRROR_OPTIONS);
|
2016-03-22 05:01:58 +00:00
|
|
|
this.codeMirror.on('change', this._codemirrorValueChanged.bind(this));
|
|
|
|
this.codeMirror.on('paste', this._codemirrorValueChanged.bind(this));
|
2016-04-09 21:08:55 +00:00
|
|
|
this._currentCodemirrorValue = this.props.value || '';
|
2016-03-22 05:01:58 +00:00
|
|
|
this._codemirrorSetValue(this._currentCodemirrorValue);
|
|
|
|
this._codemirrorSetOptions(this.props.options);
|
2016-03-20 04:00:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 20:02:47 +00:00
|
|
|
componentWillUnmount () {
|
|
|
|
// todo: is there a lighter-weight way to remove the cm instance?
|
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.toTextArea();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 02:58:48 +00:00
|
|
|
componentDidUpdate () {
|
2016-03-22 05:01:58 +00:00
|
|
|
// Don't update if no CodeMirror instance
|
|
|
|
if (!this.codeMirror) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't update if no value passed
|
2016-04-10 02:58:48 +00:00
|
|
|
if (this.props.value === undefined) {
|
2016-03-22 05:01:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't update if same value passed again
|
2016-04-10 02:58:48 +00:00
|
|
|
if (this._currentCodemirrorValue === this.props.value) {
|
2016-03-22 05:01:58 +00:00
|
|
|
return;
|
2016-03-16 20:02:47 +00:00
|
|
|
}
|
2016-03-20 20:42:27 +00:00
|
|
|
|
|
|
|
// Reset any options that may have changed
|
2016-04-10 02:58:48 +00:00
|
|
|
this._codemirrorSetOptions(this.props.options);
|
|
|
|
|
|
|
|
// Set the new value
|
|
|
|
this._codemirrorSetValue(this.props.value);
|
2016-03-16 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 05:01:58 +00:00
|
|
|
/**
|
|
|
|
* Focus the cursor to the editor
|
|
|
|
*/
|
2016-03-16 20:02:47 +00:00
|
|
|
focus () {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
/**
|
2016-03-22 05:01:58 +00:00
|
|
|
* Sets options on the CodeMirror editor while also sanitizing them
|
2016-03-20 20:42:27 +00:00
|
|
|
* @param options
|
|
|
|
*/
|
2016-03-22 05:01:58 +00:00
|
|
|
_codemirrorSetOptions (options) {
|
2016-04-10 02:58:48 +00:00
|
|
|
// Clone first so we can modify it
|
|
|
|
options = Object.assign({}, options);
|
|
|
|
|
2016-03-20 04:00:40 +00:00
|
|
|
if (options.mode === 'json') {
|
|
|
|
options.mode = 'application/json';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.mode === 'application/json') {
|
2016-03-22 05:01:58 +00:00
|
|
|
// ld+json looks better because keys are a different color
|
2016-03-20 04:00:40 +00:00
|
|
|
options.mode = 'application/ld+json';
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(options).map(key => {
|
|
|
|
this.codeMirror.setOption(key, options[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
/**
|
|
|
|
* Wrapper function to add extra behaviour to our onChange event
|
|
|
|
* @param doc CodeMirror document
|
|
|
|
*/
|
2016-03-22 05:01:58 +00:00
|
|
|
_codemirrorValueChanged (doc) {
|
2016-03-20 20:42:27 +00:00
|
|
|
// Update our cached value
|
|
|
|
var newValue = doc.getValue();
|
|
|
|
this._currentCodemirrorValue = newValue;
|
|
|
|
|
|
|
|
// Don't trigger change event if we're ignoring changes
|
|
|
|
if (this._ignoreNextChange || !this.props.onChange) {
|
2016-03-20 04:47:43 +00:00
|
|
|
this._ignoreNextChange = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
// Do the debounce in a closure so the callback doesn't change while we're waiting
|
|
|
|
const debounceMillis = this.props.debounceMillis || DEFAULT_DEBOUNCE_MILLIS;
|
|
|
|
((v, cb, millis) => {
|
|
|
|
clearTimeout(this._timeout);
|
|
|
|
this._timeout = setTimeout(() => cb(v), millis);
|
|
|
|
})(newValue, this.props.onChange, debounceMillis);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-22 05:01:58 +00:00
|
|
|
* Sets the CodeMirror value without triggering the onChange event
|
2016-03-20 20:42:27 +00:00
|
|
|
* @param code the code to set in the editor
|
|
|
|
*/
|
2016-03-22 05:01:58 +00:00
|
|
|
_codemirrorSetValue (code) {
|
2016-03-20 20:42:27 +00:00
|
|
|
this._ignoreNextChange = true;
|
2016-04-10 02:58:48 +00:00
|
|
|
|
|
|
|
if (this.props.prettify) {
|
|
|
|
if (this.props.options.mode === 'application/json') {
|
|
|
|
try {
|
|
|
|
code = JSON.stringify(JSON.parse(code), null, 4);
|
|
|
|
} catch (e) { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
this.codeMirror.setValue(code);
|
2016-03-16 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2016-04-09 21:08:55 +00:00
|
|
|
const options = this.props.options || {};
|
2016-03-24 05:26:04 +00:00
|
|
|
const classes = [
|
2016-04-06 04:21:42 +00:00
|
|
|
'editor-wrapper',
|
2016-03-24 05:26:04 +00:00
|
|
|
this.props.className,
|
2016-04-09 21:08:55 +00:00
|
|
|
options.readOnly ? 'editor-wrapper--readonly' : ''
|
2016-03-24 05:26:04 +00:00
|
|
|
];
|
2016-04-06 04:21:42 +00:00
|
|
|
|
2016-03-16 20:02:47 +00:00
|
|
|
return (
|
2016-03-24 05:26:04 +00:00
|
|
|
<div className={classes.join(' ')}>
|
2016-04-06 04:21:42 +00:00
|
|
|
<div className="editor">
|
|
|
|
<textarea
|
|
|
|
name={this.props.path}
|
|
|
|
ref='textarea'
|
|
|
|
defaultValue={this.props.value}
|
2016-04-09 21:08:55 +00:00
|
|
|
readOnly={options.readOnly}
|
2016-04-06 04:21:42 +00:00
|
|
|
autoComplete='off'>
|
|
|
|
</textarea>
|
|
|
|
</div>
|
2016-03-20 04:00:40 +00:00
|
|
|
</div>
|
2016-03-16 20:02:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Editor.propTypes = {
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onFocusChange: PropTypes.func,
|
|
|
|
options: PropTypes.object,
|
|
|
|
path: PropTypes.string,
|
|
|
|
value: PropTypes.string,
|
2016-04-10 02:58:48 +00:00
|
|
|
prettify: PropTypes.bool,
|
2016-03-20 04:00:40 +00:00
|
|
|
className: PropTypes.any,
|
|
|
|
debounceMillis: PropTypes.number
|
2016-03-16 20:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Editor;
|