insomnia/app/components/CodeEditor.js

172 lines
4.7 KiB
JavaScript
Raw Normal View History

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-16 20:02:47 +00:00
import 'codemirror/mode/css/css';
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';
// CSS Themes
2016-03-16 20:02:47 +00:00
import 'codemirror/theme/material.css'
import 'codemirror/theme/railscasts.css'
import 'codemirror/theme/tomorrow-night-bright.css'
2016-03-16 23:34:25 +00:00
import 'codemirror/theme/tomorrow-night-eighties.css'
2016-03-16 20:02:47 +00:00
import 'codemirror/theme/ambiance.css'
import 'codemirror/theme/mbo.css'
import 'codemirror/theme/pastel-on-dark.css'
2016-03-16 23:34:25 +00:00
import 'codemirror/theme/seti.css'
import 'codemirror/theme/monokai.css'
// App styles
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,
foldGutter: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
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-16 20:02:47 +00:00
this.codeMirror.on('change', this.codemirrorValueChanged.bind(this));
this.codeMirror.on('focus', this.focusChanged.bind(this, true));
this.codeMirror.on('blur', this.focusChanged.bind(this, false));
2016-03-20 23:20:00 +00:00
this.codeMirror.on('paste', this.codemirrorValueChanged.bind(this));
2016-03-16 20:02:47 +00:00
this._currentCodemirrorValue = this.props.defaultValue || this.props.value || '';
2016-03-20 20:42:27 +00:00
this.codemirrorSetValue(this._currentCodemirrorValue);
2016-03-20 04:00:40 +00:00
this.codemirrorSetOptions(this.props.options);
}
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();
}
}
componentWillReceiveProps (nextProps) {
if (this.codeMirror && nextProps.value !== undefined && this._currentCodemirrorValue !== nextProps.value) {
2016-03-20 20:42:27 +00:00
this.codemirrorSetValue(nextProps.value);
2016-03-16 20:02:47 +00:00
}
2016-03-20 20:42:27 +00:00
// Reset any options that may have changed
this.codemirrorSetOptions(nextProps.options);
2016-03-16 20:02:47 +00:00
}
focus () {
if (this.codeMirror) {
this.codeMirror.focus();
}
}
focusChanged (focused) {
2016-03-20 20:42:27 +00:00
this.setState({isFocused: focused});
2016-03-16 20:02:47 +00:00
this.props.onFocusChange && this.props.onFocusChange(focused);
}
2016-03-20 20:42:27 +00:00
/**
* Set options on the CodeMirror editor while also sanitizing them
* @param options
*/
2016-03-20 04:00:40 +00:00
codemirrorSetOptions (options) {
if (options.mode === 'json') {
options.mode = 'application/json';
}
if (options.mode === 'application/json') {
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-20 04:00:40 +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);
}
/**
* Set the CodeMirror value without triggering the onChange event
* @param code the code to set in the editor
*/
codemirrorSetValue (code) {
this._ignoreNextChange = true;
this.codeMirror.setValue(code);
2016-03-16 20:02:47 +00:00
}
render () {
return (
2016-03-20 04:00:40 +00:00
<div className={`editor ${this.props.className || ''}`}>
<textarea name={this.props.path}
ref='textarea'
defaultValue={this.props.value}
autoComplete='off'></textarea>
</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-03-20 04:00:40 +00:00
className: PropTypes.any,
debounceMillis: PropTypes.number
2016-03-16 20:02:47 +00:00
};
export default Editor;