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';
|
2017-02-01 03:01:27 +00:00
|
|
|
import jq from 'jsonpath';
|
2016-09-15 03:46:23 +00:00
|
|
|
import vkBeautify from 'vkbeautify';
|
2016-09-13 21:18:22 +00:00
|
|
|
import {DOMParser} from 'xmldom';
|
|
|
|
import xpath from 'xpath';
|
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';
|
2016-11-10 07:34:26 +00:00
|
|
|
import 'codemirror/mode/markdown/markdown';
|
2016-08-31 05:37:21 +00:00
|
|
|
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/search/search';
|
|
|
|
import 'codemirror/addon/search/searchcursor';
|
|
|
|
import 'codemirror/addon/edit/matchbrackets';
|
2016-12-22 07:24:28 +00:00
|
|
|
import 'codemirror/addon/edit/closebrackets';
|
2016-08-31 05:37:21 +00:00
|
|
|
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';
|
2017-02-08 00:31:48 +00:00
|
|
|
import 'codemirror/addon/mode/overlay';
|
2017-01-24 22:18:11 +00:00
|
|
|
import 'codemirror/keymap/vim';
|
|
|
|
import 'codemirror/keymap/emacs';
|
|
|
|
import 'codemirror/keymap/sublime';
|
2016-08-31 05:37:21 +00:00
|
|
|
import '../../css/components/editor.less';
|
2016-11-07 20:24:38 +00:00
|
|
|
import {showModal} from '../modals/index';
|
2016-09-08 22:33:03 +00:00
|
|
|
import AlertModal from '../modals/AlertModal';
|
|
|
|
import Link from '../base/Link';
|
2016-11-10 21:03:12 +00:00
|
|
|
import * as misc from '../../../common/misc';
|
2016-11-23 19:33:24 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-01-27 01:00:27 +00:00
|
|
|
// Make jsonlint available to the jsonlint plugin
|
|
|
|
import {parser as jsonlint} from 'jsonlint';
|
2017-02-07 17:13:59 +00:00
|
|
|
import {prettifyJson} from '../../../common/prettify';
|
2017-01-27 01:00:27 +00:00
|
|
|
global.jsonlint = jsonlint;
|
2016-07-06 20:18:26 +00:00
|
|
|
|
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',
|
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-12-22 07:24:28 +00:00
|
|
|
autoCloseBrackets: true,
|
2016-06-18 22:57:23 +00:00
|
|
|
indentUnit: 4,
|
2016-07-14 22:48:56 +00:00
|
|
|
indentWithTabs: true,
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap: 'default',
|
2016-03-22 05:01:58 +00:00
|
|
|
gutters: [
|
|
|
|
'CodeMirror-linenumbers',
|
|
|
|
'CodeMirror-foldgutter',
|
|
|
|
'CodeMirror-lint-markers'
|
|
|
|
],
|
2016-07-29 16:29:14 +00:00
|
|
|
cursorScrollMargin: 12, // NOTE: This is px
|
2016-03-20 20:42:27 +00:00
|
|
|
extraKeys: {
|
2016-09-13 02:09:35 +00:00
|
|
|
'Ctrl-Q': function (cm) {
|
2016-03-20 20:42:27 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
selectAll () {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.setSelection(
|
|
|
|
{line: 0, ch: 0},
|
|
|
|
{line: this.codeMirror.lineCount(), ch: 0}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue () {
|
|
|
|
return this.codeMirror.getValue();
|
|
|
|
}
|
|
|
|
|
2016-11-29 21:28:22 +00:00
|
|
|
_handleInitTextarea = textarea => {
|
2016-08-31 05:37:21 +00:00
|
|
|
if (!textarea) {
|
|
|
|
// Not mounted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.codeMirror) {
|
|
|
|
// Already initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {value} = this.props;
|
|
|
|
|
2017-02-08 00:31:48 +00:00
|
|
|
// Add overlay to editor to make all links clickable
|
2017-02-13 08:12:02 +00:00
|
|
|
CodeMirror.defineMode('master', (config, parserConfig) => {
|
2017-02-08 00:31:48 +00:00
|
|
|
const baseMode = CodeMirror.getMode(config, parserConfig.baseMode || 'text/plain');
|
|
|
|
|
|
|
|
// Only add the click mode if we have links to click
|
2017-02-13 08:12:02 +00:00
|
|
|
const highlightLinks = !!this.props.onClickLink;
|
|
|
|
const highlightNunjucks = !this.props.readOnly;
|
|
|
|
|
|
|
|
const regexUrl = /^(https?:\/\/)?([\da-z.\-]+)\.([a-z.]{2,6})([\/\w .\-]*)*\/?/;
|
|
|
|
const regexVariable = /^{{[ |a-zA-Z0-9_\-+,'"\\()\[\]]+}}/;
|
|
|
|
const regexTag = /^{%[ |a-zA-Z0-9_\-+,'"\\()\[\]]+%}/;
|
|
|
|
const regexComment = /^{#[^#]+#}/;
|
2017-02-08 00:31:48 +00:00
|
|
|
|
|
|
|
const overlay = {
|
|
|
|
token: function (stream, state) {
|
2017-02-13 08:12:02 +00:00
|
|
|
if (highlightLinks && stream.match(regexUrl, true)) {
|
2017-02-08 00:31:48 +00:00
|
|
|
return 'clickable';
|
|
|
|
}
|
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
if (highlightNunjucks && stream.match(regexVariable, true)) {
|
|
|
|
return 'variable-3';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highlightNunjucks && stream.match(regexTag, true)) {
|
|
|
|
return 'variable-3';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highlightNunjucks && stream.match(regexComment, true)) {
|
|
|
|
return 'comment';
|
|
|
|
}
|
|
|
|
|
|
|
|
while (stream.next() != null) {
|
|
|
|
if (stream.match(regexUrl, false)) break;
|
|
|
|
if (stream.match(regexVariable, false)) break;
|
|
|
|
if (stream.match(regexTag, false)) break;
|
|
|
|
if (stream.match(regexComment, false)) break;
|
2017-02-08 00:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return CodeMirror.overlayMode(baseMode, overlay, true);
|
|
|
|
});
|
|
|
|
|
2016-08-31 05:37:21 +00:00
|
|
|
this.codeMirror = CodeMirror.fromTextArea(textarea, BASE_CODEMIRROR_OPTIONS);
|
2016-11-10 21:03:12 +00:00
|
|
|
this.codeMirror.on('change', misc.debounce(this._codemirrorValueChanged.bind(this)));
|
|
|
|
this.codeMirror.on('paste', misc.debounce(this._codemirrorValueChanged.bind(this)));
|
2016-08-31 05:37:21 +00:00
|
|
|
if (!this.codeMirror.getOption('indentWithTabs')) {
|
|
|
|
this.codeMirror.setOption('extraKeys', {
|
|
|
|
Tab: cm => {
|
2017-02-08 00:31:48 +00:00
|
|
|
const spaces = Array(this.codeMirror.getOption('indentUnit') + 1).join(' ');
|
2016-08-31 05:37:21 +00:00
|
|
|
cm.replaceSelection(spaces);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:04:25 +00:00
|
|
|
// Do this a bit later so we don't block the render process
|
2017-02-08 00:31:48 +00:00
|
|
|
setTimeout(() => this._codemirrorSetValue(value || ''), 50);
|
2016-08-31 05:37:21 +00:00
|
|
|
|
|
|
|
this._codemirrorSetOptions();
|
2016-11-29 21:28:22 +00:00
|
|
|
};
|
2016-08-31 05:37:21 +00:00
|
|
|
|
2017-02-08 00:31:48 +00:00
|
|
|
_handleEditorClick = e => {
|
|
|
|
if (!this.props.onClickLink) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.target.className.indexOf('cm-clickable') >= 0) {
|
|
|
|
this.props.onClickLink(e.target.innerHTML);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-08 22:04:25 +00:00
|
|
|
_isJSON (mode) {
|
|
|
|
if (!mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode.indexOf('json') !== -1
|
|
|
|
}
|
|
|
|
|
2016-09-10 01:51:49 +00:00
|
|
|
_isXML (mode) {
|
|
|
|
if (!mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode.indexOf('xml') !== -1
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:35:49 +00:00
|
|
|
_handleBeautify () {
|
2016-11-23 19:33:24 +00:00
|
|
|
trackEvent('Request', 'Beautify');
|
2016-09-13 17:35:49 +00:00
|
|
|
this._prettify(this.codeMirror.getValue());
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:13:59 +00:00
|
|
|
_prettify (code) {
|
2016-09-13 17:29:09 +00:00
|
|
|
if (this._isXML(this.props.mode)) {
|
2017-02-07 17:13:59 +00:00
|
|
|
code = this._prettifyXML(code);
|
2016-09-13 17:29:09 +00:00
|
|
|
} else {
|
2017-02-07 17:13:59 +00:00
|
|
|
code = this._prettifyJSON(code);
|
2016-09-13 17:29:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
this.codeMirror.setValue(code);
|
2016-09-13 17:29:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:13:59 +00:00
|
|
|
_prettifyJSON (code) {
|
2016-09-10 01:51:49 +00:00
|
|
|
try {
|
2017-02-07 17:13:59 +00:00
|
|
|
let jsonString = code;
|
2016-09-10 01:51:49 +00:00
|
|
|
|
|
|
|
if (this.props.updateFilter && this.state.filter) {
|
2017-02-07 17:13:59 +00:00
|
|
|
let obj = JSON.parse(code);
|
2017-02-01 03:01:27 +00:00
|
|
|
try {
|
2017-02-07 17:13:59 +00:00
|
|
|
jsonString = JSON.stringify(jq.query(obj, this.state.filter));
|
2017-02-01 03:01:27 +00:00
|
|
|
} catch (err) {
|
2017-02-07 17:13:59 +00:00
|
|
|
jsonString = '[]';
|
2017-02-01 03:01:27 +00:00
|
|
|
}
|
2016-09-10 01:51:49 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:13:59 +00:00
|
|
|
return prettifyJson(jsonString, '\t');
|
2016-09-10 01:51:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
// That's Ok, just leave it
|
2016-10-02 20:57:00 +00:00
|
|
|
return code;
|
2016-09-10 01:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:13:59 +00:00
|
|
|
_prettifyXML (code) {
|
2016-09-13 21:18:22 +00:00
|
|
|
if (this.props.updateFilter && this.state.filter) {
|
|
|
|
try {
|
|
|
|
const dom = new DOMParser().parseFromString(code);
|
|
|
|
const nodes = xpath.select(this.state.filter, dom);
|
|
|
|
const inner = nodes.map(n => n.toString()).join('\n');
|
|
|
|
code = `<result>${inner}</result>`
|
|
|
|
} catch (e) {
|
|
|
|
// Failed to parse filter (that's ok)
|
|
|
|
code = `<result></result>`
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 01:51:49 +00:00
|
|
|
|
2016-09-28 21:17:57 +00:00
|
|
|
try {
|
2016-10-02 20:57:00 +00:00
|
|
|
return vkBeautify.xml(code, '\t');
|
2016-09-28 21:17:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Failed to parse so just return original
|
2016-10-02 20:57:00 +00:00
|
|
|
return code;
|
2016-09-28 21:17:57 +00:00
|
|
|
}
|
2016-09-10 01:51:49 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2017-02-08 00:31:48 +00:00
|
|
|
const normalizedMode = this.props.mode ? this.props.mode.split(';')[0] : 'text/plain';
|
|
|
|
|
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 || '',
|
2017-02-08 00:31:48 +00:00
|
|
|
mode: {
|
2017-02-13 08:12:02 +00:00
|
|
|
name: 'master',
|
2017-02-08 00:31:48 +00:00
|
|
|
baseMode: normalizedMode,
|
|
|
|
},
|
2017-01-24 22:18:11 +00:00
|
|
|
lineWrapping: this.props.lineWrapping,
|
|
|
|
keyMap: this.props.keyMap || 'default',
|
2016-09-13 02:09:35 +00:00
|
|
|
matchBrackets: !readOnly,
|
2016-09-02 17:05:21 +00:00
|
|
|
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-03-20 04:00:40 +00:00
|
|
|
Object.keys(options).map(key => {
|
2017-02-08 01:52:05 +00:00
|
|
|
// Don't set the option if it hasn't changed
|
|
|
|
if (options[key] === this.codeMirror.options[key]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since mode is an object, let's compare the inner baseMode instead
|
|
|
|
if (key === 'mode' && options.mode.baseMode === this.codeMirror.options.mode.baseMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:00:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:48:49 +00:00
|
|
|
this.props.onChange(doc.getValue());
|
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-09-13 17:29:09 +00:00
|
|
|
if (this.props.autoPrettify && this._canPrettify()) {
|
|
|
|
this._prettify(code);
|
2016-09-10 01:51:49 +00:00
|
|
|
} else {
|
2016-12-01 18:48:49 +00:00
|
|
|
this.codeMirror.setValue(code || '');
|
2016-04-10 02:58:48 +00:00
|
|
|
}
|
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-11-10 21:03:12 +00:00
|
|
|
}, 400);
|
2016-11-23 23:29:31 +00:00
|
|
|
|
|
|
|
// So we don't track on every keystroke, give analytics a longer timeout
|
|
|
|
clearTimeout(this._analyticsTimeout);
|
|
|
|
const json = this._isJSON(this.props.mode);
|
|
|
|
this._analyticsTimeout = setTimeout(() => {
|
|
|
|
trackEvent(
|
|
|
|
'Response',
|
|
|
|
`Filter ${json ? 'JSONPath' : 'XPath'}`,
|
|
|
|
`${filter ? 'Change' : 'Clear'}`
|
|
|
|
);
|
|
|
|
}, 2000);
|
2016-09-08 22:04:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 17:29:09 +00:00
|
|
|
_canPrettify () {
|
|
|
|
const {mode} = this.props;
|
|
|
|
return this._isJSON(mode) || this._isXML(mode);
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:33:03 +00:00
|
|
|
_showFilterHelp () {
|
2016-09-10 01:51:49 +00:00
|
|
|
const json = this._isJSON(this.props.mode);
|
|
|
|
const link = json ? (
|
2017-01-27 01:00:27 +00:00
|
|
|
<Link href="http://goessner.net/articles/JsonPath/">
|
|
|
|
JSONPath
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<Link href="https://www.w3.org/TR/xpath/">
|
|
|
|
XPath
|
|
|
|
</Link>
|
|
|
|
);
|
2016-09-10 01:51:49 +00:00
|
|
|
|
2016-11-23 23:29:31 +00:00
|
|
|
trackEvent('Response', `Filter ${json ? 'JSONPath' : 'XPath'}`, 'Help');
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
showModal(AlertModal, {
|
|
|
|
title: 'Response Filtering Help',
|
2016-09-08 22:33:03 +00:00
|
|
|
message: (
|
|
|
|
<div>
|
|
|
|
<p>
|
2016-09-10 01:51:49 +00:00
|
|
|
Use {link} to filter the response body. Here are some examples that
|
|
|
|
you might use on a book store API.
|
2016-09-08 22:33:03 +00:00
|
|
|
</p>
|
|
|
|
<table className="pad-top-sm">
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
2016-11-23 23:42:10 +00:00
|
|
|
<td>
|
|
|
|
<code className="selectable">
|
|
|
|
{json ? '$.store.books[*].title' : '/store/books/title'}
|
|
|
|
</code>
|
2016-09-09 00:32:36 +00:00
|
|
|
</td>
|
2016-09-08 22:33:03 +00:00
|
|
|
<td>Get titles of all books in the store</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2016-11-23 23:42:10 +00:00
|
|
|
<td>
|
|
|
|
<code className="selectable">
|
|
|
|
{json ? '$.store.books[?(@.price < 10)].title' : '/store/books[price < 10]'}
|
|
|
|
</code>
|
|
|
|
</td>
|
2016-11-16 17:18:39 +00:00
|
|
|
<td>Get books costing less than $10</td>
|
2016-09-08 22:33:03 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
2016-11-23 23:42:10 +00:00
|
|
|
<td>
|
|
|
|
<code className="selectable">
|
|
|
|
{json ? '$.store.books[-1:]' : '/store/books[last()]'}
|
|
|
|
</code>
|
|
|
|
</td>
|
2016-09-08 22:33:03 +00:00
|
|
|
<td>Get the last book in the store</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
2016-11-23 23:42:10 +00:00
|
|
|
<td>
|
|
|
|
<code className="selectable">
|
|
|
|
{json ? '$.store.books.length' : 'count(/store/books)'}
|
|
|
|
</code>
|
|
|
|
</td>
|
2016-09-08 22:33:03 +00:00
|
|
|
<td>Get the number of books in the store</td>
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-29 20:37:23 +00:00
|
|
|
componentDidUpdate () {
|
2016-10-02 20:57:00 +00:00
|
|
|
// Don't don it sync because it might block the UI
|
|
|
|
setTimeout(() => {
|
|
|
|
this._codemirrorSetOptions();
|
|
|
|
}, 50);
|
2016-07-29 20:37:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 22:48:56 +00:00
|
|
|
render () {
|
2017-02-13 08:12:02 +00:00
|
|
|
const {readOnly, fontSize, 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,
|
2017-02-13 08:12:02 +00:00
|
|
|
{'editor--readonly': readOnly}
|
2016-07-19 22:28:29 +00:00
|
|
|
);
|
2016-04-29 05:58:37 +00:00
|
|
|
|
2016-09-13 17:29:09 +00:00
|
|
|
const toolbarChildren = [];
|
2016-09-10 01:51:49 +00:00
|
|
|
if (this.props.updateFilter && (this._isJSON(mode) || this._isXML(mode))) {
|
2016-09-13 17:29:09 +00:00
|
|
|
toolbarChildren.push(
|
|
|
|
<input
|
|
|
|
key="filter"
|
|
|
|
type="text"
|
|
|
|
title="Filter response body"
|
|
|
|
defaultValue={filter || ''}
|
|
|
|
placeholder={this._isJSON(mode) ? '$.store.books[*].author' : '/store/books/author'}
|
|
|
|
onChange={e => this._handleFilterChange(e.target.value)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
toolbarChildren.push(
|
|
|
|
<button key="help"
|
|
|
|
className="btn btn--compact"
|
|
|
|
onClick={() => this._showFilterHelp()}>
|
|
|
|
<i className="fa fa-question-circle"></i>
|
|
|
|
</button>
|
2016-09-08 22:04:25 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:29:09 +00:00
|
|
|
if (this.props.manualPrettify && this._canPrettify()) {
|
|
|
|
let contentTypeName = '';
|
|
|
|
if (this._isJSON(mode)) {
|
|
|
|
contentTypeName = 'JSON'
|
|
|
|
} else if (this._isXML(mode)) {
|
|
|
|
contentTypeName = 'XML'
|
|
|
|
}
|
|
|
|
|
|
|
|
toolbarChildren.push(
|
|
|
|
<button key="prettify"
|
|
|
|
className="btn btn--compact"
|
|
|
|
title="Auto-format request body whitespace"
|
2016-09-13 17:35:49 +00:00
|
|
|
onClick={() => this._handleBeautify()}>
|
2016-09-13 17:29:09 +00:00
|
|
|
Beautify {contentTypeName}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
let toolbar = null;
|
|
|
|
if (toolbarChildren.length) {
|
|
|
|
toolbar = <div className="editor__toolbar">{toolbarChildren}</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`}}>
|
2017-02-08 00:31:48 +00:00
|
|
|
<div className="editor__container" onClick={this._handleEditorClick}>
|
2016-11-16 17:18:39 +00:00
|
|
|
<textarea
|
2016-11-29 21:28:22 +00:00
|
|
|
ref={this._handleInitTextarea}
|
2016-11-16 17:18:39 +00:00
|
|
|
defaultValue=" "
|
|
|
|
readOnly={readOnly}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
2016-09-20 21:17:01 +00:00
|
|
|
</div>
|
2016-09-13 17:29:09 +00:00
|
|
|
{toolbar}
|
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,
|
2017-02-08 00:31:48 +00:00
|
|
|
onClickLink: PropTypes.func,
|
2017-02-13 08:12:02 +00:00
|
|
|
render: PropTypes.func,
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap: PropTypes.string,
|
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-09-13 17:29:09 +00:00
|
|
|
autoPrettify: PropTypes.bool,
|
|
|
|
manualPrettify: PropTypes.bool,
|
2016-07-22 21:35:49 +00:00
|
|
|
className: PropTypes.any,
|
2016-09-09 00:32:36 +00:00
|
|
|
updateFilter: PropTypes.func,
|
2017-02-13 08:12:02 +00:00
|
|
|
readOnly: PropTypes.bool,
|
2016-09-09 00:32:36 +00:00
|
|
|
filter: PropTypes.string
|
2016-03-16 20:02:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Editor;
|