insomnia/app/components/base/Editor.js

337 lines
9.1 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-09-08 22:33:03 +00:00
import JSONPath from 'jsonpath-plus';
import {DEBOUNCE_MILLIS} from '../../lib/constants';
2016-08-31 05:37:21 +00:00
import 'codemirror/mode/css/css';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/go/go';
import 'codemirror/mode/shell/shell';
import 'codemirror/mode/clike/clike';
import 'codemirror/mode/mllike/mllike';
import 'codemirror/mode/php/php';
import 'codemirror/mode/python/python';
import 'codemirror/mode/ruby/ruby';
import 'codemirror/mode/swift/swift';
import 'codemirror/lib/codemirror.css';
import 'codemirror/addon/dialog/dialog';
import 'codemirror/addon/dialog/dialog.css';
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';
import 'codemirror/addon/search/search';
import 'codemirror/addon/search/searchcursor';
import 'codemirror/addon/edit/matchbrackets';
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';
import '../../css/components/editor.less';
2016-09-08 22:33:03 +00:00
import {getModal} from '../modals/index';
import AlertModal from '../modals/AlertModal';
import Link from '../base/Link';
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: 250}, // 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-07-29 20:37:23 +00:00
matchBrackets: true,
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-09-09 00:32:36 +00:00
constructor (props) {
super(props);
2016-09-08 22:04:25 +00:00
this.state = {
2016-09-09 00:32:36 +00:00
filter: props.filter || ''
2016-09-08 22:04:25 +00:00
};
this._originalCode = '';
2016-03-16 20:02:47 +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();
}
}
selectAll () {
if (this.codeMirror) {
this.codeMirror.setSelection(
{line: 0, ch: 0},
{line: this.codeMirror.lineCount(), ch: 0}
);
}
}
getValue () {
return this.codeMirror.getValue();
}
2016-08-31 05:37:21 +00:00
_initEditor (textarea) {
if (!textarea) {
// Not mounted
return;
}
if (this.codeMirror) {
// Already initialized
return;
}
const {value} = this.props;
this.codeMirror = CodeMirror.fromTextArea(textarea, BASE_CODEMIRROR_OPTIONS);
this.codeMirror.on('change', this._codemirrorValueChanged.bind(this));
this.codeMirror.on('paste', this._codemirrorValueChanged.bind(this));
if (!this.codeMirror.getOption('indentWithTabs')) {
this.codeMirror.setOption('extraKeys', {
Tab: cm => {
var spaces = Array(this.codeMirror.getOption('indentUnit') + 1).join(' ');
cm.replaceSelection(spaces);
}
});
}
2016-09-08 22:04:25 +00:00
// Do this a bit later so we don't block the render process
2016-08-31 05:37:21 +00:00
setTimeout(() => {
this._codemirrorSetValue(value || '');
}, 50);
this._codemirrorSetOptions();
}
2016-09-08 22:04:25 +00:00
_isJSON (mode) {
if (!mode) {
return false;
}
return mode.indexOf('json') !== -1
}
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-09-02 17:05:21 +00:00
const readOnly = this.props.readOnly || false;
2016-04-29 01:00:12 +00:00
let options = {
2016-09-02 17:05:21 +00:00
readOnly,
2016-04-29 01:00:12 +00:00
placeholder: this.props.placeholder || '',
mode: this.props.mode || 'text/plain',
2016-09-02 17:05:21 +00:00
lineWrapping: !!this.props.lineWrapping,
lint: !readOnly
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-09-08 22:04:25 +00:00
if (this._isJSON(options.mode)) {
// set LD JSON because it highlights the keys a different color
options.mode = {name: 'javascript', jsonld: true}
2016-03-20 04:00:40 +00:00
}
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.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-09-08 22:04:25 +00:00
this._originalCode = 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 {
2016-09-08 22:04:25 +00:00
let obj = JSON.parse(code);
2016-09-09 00:32:36 +00:00
if (this.props.updateFilter && this.state.filter) {
obj = JSONPath({json: obj, path: this.state.filter});
2016-09-08 22:04:25 +00:00
}
code = JSON.stringify(obj, 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-09-08 22:04:25 +00:00
_handleFilterChange (filter) {
clearTimeout(this._filterTimeout);
this._filterTimeout = setTimeout(() => {
this.setState({filter});
this._codemirrorSetValue(this._originalCode);
2016-09-09 00:32:36 +00:00
if (this.props.updateFilter) {
this.props.updateFilter(filter);
}
2016-09-08 22:04:25 +00:00
}, DEBOUNCE_MILLIS);
}
2016-09-08 22:33:03 +00:00
_showFilterHelp () {
getModal(AlertModal).show({
headerName: 'Response Filtering Help',
message: (
<div>
<p>
Use <Link href="http://schier.co">JSONPath</Link> to filter the
response body. Here are some examples that you might use on a
book store API.
</p>
<table className="pad-top-sm">
<tbody>
<tr>
2016-09-09 00:32:36 +00:00
<td><code className="selectable">$.store.books[*].title</code>
</td>
2016-09-08 22:33:03 +00:00
<td>Get titles of all books in the store</td>
</tr>
<tr>
2016-09-09 00:32:36 +00:00
<td><code className="selectable">$.store.books[?(@.price &lt;
10)].title</code></td>
2016-09-08 22:33:03 +00:00
<td>Get books costing more than $10</td>
</tr>
<tr>
<td><code className="selectable">$.store.books[-1:]</code></td>
<td>Get the last book in the store</td>
</tr>
<tr>
<td><code className="selectable">$.store.books.length</code></td>
<td>Get the number of books in the store</td>
</tr>
</tbody>
</table>
</div>
)
})
}
2016-07-29 20:37:23 +00:00
componentDidUpdate () {
this._codemirrorSetOptions();
}
2016-07-14 22:48:56 +00:00
render () {
2016-09-09 00:32:36 +00:00
const {readOnly, fontSize, lightTheme, mode, filter} = 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-09-09 00:32:36 +00:00
let filterElement = null;
if (this.props.updateFilter && this._isJSON(mode)) {
filterElement = (
2016-09-08 22:04:25 +00:00
<div className="editor__filter">
<div className="form-control form-control--outlined">
<input
type="text"
2016-09-09 00:32:36 +00:00
defaultValue={filter || ''}
2016-09-08 22:04:25 +00:00
placeholder="$.store.book[*].author"
onChange={e => this._handleFilterChange(e.target.value)}
/>
</div>
2016-09-08 22:33:03 +00:00
<button className="btn btn--compact"
onClick={() => this._showFilterHelp()}>
2016-09-08 22:04:25 +00:00
<i className="fa fa-question-circle"></i>
</button>
</div>
)
}
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-09-08 22:04:25 +00:00
<textarea
ref={n => this._initEditor(n)}
readOnly={readOnly}
autoComplete='off'>
</textarea>
2016-09-09 00:32:36 +00:00
{filterElement}
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,
2016-09-08 22:04:25 +00:00
lightTheme: PropTypes.bool,
2016-09-09 00:32:36 +00:00
updateFilter: PropTypes.func,
filter: PropTypes.string
2016-03-16 20:02:47 +00:00
};
export default Editor;