insomnia/app/components/base/Editor.js

220 lines
5.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-07-19 22:28:29 +00:00
import classnames from 'classnames';
2016-03-16 23:34:25 +00:00
import {DEBOUNCE_MILLIS} from '../../lib/constants';
2016-03-16 23:34:25 +00:00
// Modes
2016-06-20 06:05:40 +00:00
import '../../../node_modules/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/dialog/dialog'
import 'codemirror/addon/dialog/dialog.css'
2016-06-20 06:05:40 +00:00
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/display/autorefresh'
2016-06-20 06:05:40 +00:00
import 'codemirror/addon/search/search'
import 'codemirror/addon/search/searchcursor'
import 'codemirror/addon/search/matchesonscrollbar'
import 'codemirror/addon/search/matchesonscrollbar.css'
import 'codemirror/addon/fold/foldgutter'
import 'codemirror/addon/fold/foldgutter.css'
import 'codemirror/addon/display/placeholder'
import 'codemirror/addon/lint/lint'
import 'codemirror/addon/lint/json-lint'
import 'codemirror/addon/lint/lint.css'
2016-03-22 05:01:58 +00:00
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 20:42:27 +00:00
const BASE_CODEMIRROR_OPTIONS = {
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',
autoRefresh: {delay: 5000}, // Necessary to show up in the env modal first launch
2016-06-20 06:31:32 +00:00
lineWrapping: true,
2016-06-18 22:57:23 +00:00
lint: true,
2016-04-15 02:13:49 +00:00
tabSize: 4,
2016-06-18 22:57:23 +00:00
indentUnit: 4,
2016-07-14 22:48:56 +00:00
indentWithTabs: true,
2016-03-22 05:01:58 +00:00
gutters: [
'CodeMirror-linenumbers',
'CodeMirror-foldgutter',
'CodeMirror-lint-markers'
],
cursorScrollMargin: 12, // NOTE: This is px
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 {
2016-07-14 22:48:56 +00:00
constructor () {
2016-03-16 20:02:47 +00:00
super();
2016-03-20 20:42:27 +00:00
this.state = {isFocused: false}
2016-03-16 20:02:47 +00:00
}
2016-07-14 22:48:56 +00:00
componentDidMount () {
2016-04-29 01:00:12 +00:00
const {value} = this.props;
2016-04-29 05:58:37 +00:00
2016-03-16 20:02:47 +00:00
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-07-14 22:48:56 +00:00
if (!this.codeMirror.getOption('indentWithTabs')) {
this.codeMirror.setOption('extraKeys', {
Tab: cm => {
var spaces = Array(this.codeMirror.getOption('indentUnit') + 1).join(' ');
cm.replaceSelection(spaces);
}
});
}
this._currentCodemirrorValue = value || '';
2016-03-22 05:01:58 +00:00
this._codemirrorSetValue(this._currentCodemirrorValue);
2016-04-29 01:00:12 +00:00
this._codemirrorSetOptions();
2016-03-20 04:00:40 +00:00
}
2016-07-14 22:48:56 +00:00
componentWillUnmount () {
2016-03-16 20:02:47 +00:00
// todo: is there a lighter-weight way to remove the cm instance?
if (this.codeMirror) {
this.codeMirror.toTextArea();
}
}
2016-03-22 05:01:58 +00:00
/**
* Focus the cursor to the editor
*/
2016-07-14 22:48:56 +00:00
focus () {
2016-03-16 20:02:47 +00:00
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
*/
2016-07-14 22:48:56 +00:00
_codemirrorSetOptions () {
2016-04-10 02:58:48 +00:00
// Clone first so we can modify it
2016-04-29 01:00:12 +00:00
let options = {
placeholder: this.props.placeholder || '',
mode: this.props.mode || 'text/plain',
2016-04-30 05:01:57 +00:00
readOnly: this.props.readOnly || false,
lineWrapping: !!this.props.lineWrapping
2016-04-29 01:00:12 +00:00
};
2016-04-11 00:40:14 +00:00
// Strip of charset if there is one
2016-04-15 02:13:49 +00:00
options.mode = options.mode ? options.mode.split(';')[0] : 'text/plain';
2016-04-17 22:46:17 +00:00
2016-03-20 04:00:40 +00:00
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-07-14 22:48:56 +00:00
_codemirrorValueChanged (doc) {
2016-07-29 00:24:05 +00:00
// Don't trigger change event if we're ignoring changes
if (this._ignoreNextChange || !this.props.onChange) {
this._ignoreNextChange = false;
return;
}
// Debounce URL changes so we don't update the app so much
clearTimeout(this._timeout);
this._timeout = setTimeout(() => {
// Update our cached value
var newValue = doc.getValue();
this._currentCodemirrorValue = newValue;
this.props.onChange(newValue);
}, DEBOUNCE_MILLIS)
2016-03-20 20:42:27 +00:00
}
/**
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-07-14 22:48:56 +00:00
_codemirrorSetValue (code) {
2016-03-20 20:42:27 +00:00
this._ignoreNextChange = true;
2016-04-17 22:46:17 +00:00
2016-04-10 02:58:48 +00:00
if (this.props.prettify) {
try {
code = JSON.stringify(JSON.parse(code), null, '\t');
} catch (e) {
// That's Ok, just leave it
// TODO: support more than just JSON prettifying
2016-04-10 02:58:48 +00:00
}
}
2016-04-17 22:46:17 +00:00
2016-03-20 20:42:27 +00:00
this.codeMirror.setValue(code);
2016-03-16 20:02:47 +00:00
}
2016-07-14 22:48:56 +00:00
render () {
const {value, readOnly, fontSize, lightTheme} = this.props;
2016-07-19 22:28:29 +00:00
const classes = classnames(
2016-04-17 22:46:17 +00:00
'editor',
2016-03-24 05:26:04 +00:00
this.props.className,
{
'editor--readonly': readOnly,
'editor--light-theme': !!lightTheme,
'editor--dark-theme': !lightTheme
}
2016-07-19 22:28:29 +00:00
);
2016-04-29 05:58:37 +00:00
2016-03-16 20:02:47 +00:00
return (
2016-07-19 22:28:29 +00:00
<div className={classes} style={{fontSize: `${fontSize || 12}px`}}>
2016-04-06 04:21:42 +00:00
<textarea
ref='textarea'
2016-04-29 05:58:37 +00:00
defaultValue={value}
readOnly={readOnly}
2016-04-06 04:21:42 +00:00
autoComplete='off'>
</textarea>
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,
2016-04-29 01:00:12 +00:00
mode: PropTypes.string,
placeholder: PropTypes.string,
2016-07-19 22:28:29 +00:00
lineWrapping: PropTypes.bool,
fontSize: PropTypes.number,
2016-03-16 20:02:47 +00:00
value: PropTypes.string,
2016-04-10 02:58:48 +00:00
prettify: PropTypes.bool,
className: PropTypes.any,
lightTheme: PropTypes.bool
2016-03-16 20:02:47 +00:00
};
export default Editor;