2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-03-16 20:02:47 +00:00
|
|
|
import CodeMirror from 'codemirror';
|
2016-07-19 22:28:29 +00:00
|
|
|
import classnames from 'classnames';
|
2017-06-09 01:10:12 +00:00
|
|
|
import clone from 'clone';
|
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-11-07 20:24:38 +00:00
|
|
|
import {showModal} from '../modals/index';
|
2017-03-12 00:31:23 +00:00
|
|
|
import FilterHelpModal from '../modals/filter-help-modal';
|
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-02-07 17:13:59 +00:00
|
|
|
import {prettifyJson} from '../../../common/prettify';
|
2017-02-27 21:00:13 +00:00
|
|
|
import {DEBOUNCE_MILLIS} from '../../../common/constants';
|
2017-03-12 00:31:23 +00:00
|
|
|
import './base-imports';
|
2017-05-23 22:05:31 +00:00
|
|
|
import {getTagDefinitions} from '../../../templating/index';
|
2017-06-08 02:42:16 +00:00
|
|
|
import Dropdown from '../base/dropdown/dropdown';
|
|
|
|
import DropdownButton from '../base/dropdown/dropdown-button';
|
|
|
|
import DropdownItem from '../base/dropdown/dropdown-item';
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
const TAB_KEY = 9;
|
2017-03-29 23:09:28 +00:00
|
|
|
const TAB_SIZE = 4;
|
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',
|
2017-03-09 06:23:23 +00:00
|
|
|
autoRefresh: 500,
|
2016-06-20 06:31:32 +00:00
|
|
|
lineWrapping: true,
|
2017-02-27 21:00:13 +00:00
|
|
|
scrollbarStyle: 'native',
|
2016-06-18 22:57:23 +00:00
|
|
|
lint: true,
|
2016-07-29 20:37:23 +00:00
|
|
|
matchBrackets: true,
|
2016-12-22 07:24:28 +00:00
|
|
|
autoCloseBrackets: true,
|
2017-03-29 23:09:28 +00:00
|
|
|
tabSize: TAB_SIZE,
|
|
|
|
indentUnit: TAB_SIZE,
|
2017-03-12 00:31:23 +00:00
|
|
|
hintOptions: null,
|
2017-02-27 21:00:13 +00:00
|
|
|
dragDrop: true,
|
|
|
|
viewportMargin: 30, // default 10
|
|
|
|
selectionPointer: 'default',
|
|
|
|
styleActiveLine: true,
|
2016-07-14 22:48:56 +00:00
|
|
|
indentWithTabs: true,
|
2017-03-03 21:10:35 +00:00
|
|
|
showCursorWhenSelecting: false,
|
2017-02-27 21:00:13 +00:00
|
|
|
cursorScrollMargin: 12, // NOTE: This is px
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap: 'default',
|
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
|
|
|
};
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-03-09 06:23:23 +00:00
|
|
|
class CodeEditor extends PureComponent {
|
2016-09-09 00:32:36 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
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
|
|
|
};
|
2017-03-03 01:44:07 +00:00
|
|
|
|
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
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.toTextArea();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
componentDidUpdate () {
|
|
|
|
this._codemirrorSetOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
// Update if any properties changed, except value. We ignore value.
|
|
|
|
for (const key of Object.keys(nextProps)) {
|
|
|
|
if (key === 'defaultValue') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props[key] !== nextProps[key]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
selectAll () {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.setSelection(
|
|
|
|
{line: 0, ch: 0},
|
|
|
|
{line: this.codeMirror.lineCount(), ch: 0}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-14 22:48:56 +00:00
|
|
|
focus () {
|
2016-03-16 20:02:47 +00:00
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
setCursor (ch, line = 0) {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
if (!this.hasFocus()) {
|
|
|
|
this.focus();
|
|
|
|
}
|
|
|
|
this.codeMirror.setCursor({line, ch});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelection (chStart, chEnd, line = 0) {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.setSelection(
|
|
|
|
{line, ch: chStart},
|
|
|
|
{line, ch: chEnd}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
getSelectionStart () {
|
|
|
|
const selections = this.codeMirror.listSelections();
|
|
|
|
if (selections.length) {
|
|
|
|
return selections[0].anchor.ch;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectionEnd () {
|
|
|
|
const selections = this.codeMirror.listSelections();
|
|
|
|
if (selections.length) {
|
|
|
|
return selections[0].head.ch;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
focusEnd () {
|
2016-08-15 17:04:36 +00:00
|
|
|
if (this.codeMirror) {
|
2017-03-01 21:15:56 +00:00
|
|
|
if (!this.hasFocus()) {
|
|
|
|
this.focus();
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
const doc = this.codeMirror.getDoc();
|
|
|
|
doc.setCursor(doc.lineCount(), 0);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 03:09:04 +00:00
|
|
|
hasFocus () {
|
|
|
|
if (this.codeMirror) {
|
|
|
|
return this.codeMirror.hasFocus();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 07:38:48 +00:00
|
|
|
setAttribute (name, value) {
|
|
|
|
this.codeMirror.getTextArea().parentNode.setAttribute(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAttribute (name) {
|
|
|
|
this.codeMirror.getTextArea().parentNode.removeAttribute(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAttribute (name) {
|
|
|
|
this.codeMirror.getTextArea().parentNode.getAttribute(name);
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:10:35 +00:00
|
|
|
clearSelection () {
|
2017-03-12 00:31:23 +00:00
|
|
|
// Never do this if dropdown is open
|
|
|
|
if (this.codeMirror.isHintDropdownActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-03 21:10:35 +00:00
|
|
|
if (this.codeMirror) {
|
|
|
|
this.codeMirror.setSelection(
|
|
|
|
{line: -1, ch: -1},
|
|
|
|
{line: -1, ch: -1}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
getValue () {
|
2017-03-01 21:15:56 +00:00
|
|
|
if (this.codeMirror) {
|
|
|
|
return this.codeMirror.getValue();
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 02:42:16 +00:00
|
|
|
_setFilterInputRef (n) {
|
|
|
|
this._filterInput = n;
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleInitTextarea (textarea) {
|
2016-08-31 05:37:21 +00:00
|
|
|
if (!textarea) {
|
|
|
|
// Not mounted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.codeMirror) {
|
|
|
|
// Already initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
const {defaultValue, debounceMillis: ms} = this.props;
|
2017-02-27 21:00:13 +00:00
|
|
|
this.codeMirror = CodeMirror.fromTextArea(textarea, BASE_CODEMIRROR_OPTIONS);
|
2016-08-31 05:37:21 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
// Set default listeners
|
2017-02-28 21:32:23 +00:00
|
|
|
const debounceMillis = typeof ms === 'number' ? ms : DEBOUNCE_MILLIS;
|
|
|
|
this.codeMirror.on('changes', misc.debounce(this._codemirrorValueChanged, debounceMillis));
|
2017-02-27 21:00:13 +00:00
|
|
|
this.codeMirror.on('beforeChange', this._codemirrorValueBeforeChange);
|
|
|
|
this.codeMirror.on('keydown', this._codemirrorKeyDown);
|
|
|
|
this.codeMirror.on('focus', this._codemirrorFocus);
|
|
|
|
this.codeMirror.on('blur', this._codemirrorBlur);
|
2017-04-04 23:06:43 +00:00
|
|
|
this.codeMirror.on('paste', this._codemirrorPaste);
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
this.codeMirror.setCursor({line: -1, ch: -1});
|
2017-03-01 21:15:56 +00:00
|
|
|
|
2016-08-31 05:37:21 +00:00
|
|
|
if (!this.codeMirror.getOption('indentWithTabs')) {
|
|
|
|
this.codeMirror.setOption('extraKeys', {
|
|
|
|
Tab: cm => {
|
2017-04-04 23:06:43 +00:00
|
|
|
const spaces = (new Array(this.codeMirror.getOption('indentUnit') + 1)).join(' ');
|
2016-08-31 05:37:21 +00:00
|
|
|
cm.replaceSelection(spaces);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:10:23 +00:00
|
|
|
// Set editor options
|
|
|
|
this._codemirrorSetOptions();
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
const setup = () => {
|
2017-02-28 21:32:23 +00:00
|
|
|
// Actually set the value
|
2017-03-01 21:15:56 +00:00
|
|
|
this._codemirrorSetValue(defaultValue || '');
|
2017-02-28 21:32:23 +00:00
|
|
|
|
|
|
|
// Setup nunjucks listeners
|
|
|
|
if (this.props.render) {
|
|
|
|
this.codeMirror.enableNunjucksTags(this.props.render);
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
|
|
|
// Make URLs clickable
|
|
|
|
if (this.props.onClickLink) {
|
|
|
|
this.codeMirror.makeLinksClickable(this.props.onClickLink);
|
|
|
|
}
|
2017-04-07 18:10:15 +00:00
|
|
|
|
|
|
|
// HACK: Refresh because sometimes it renders too early and the scroll doesn't
|
|
|
|
// quite fit.
|
|
|
|
setTimeout(() => {
|
|
|
|
this.codeMirror.refresh();
|
|
|
|
}, 100);
|
2017-03-01 21:15:56 +00:00
|
|
|
};
|
2017-02-28 21:32:23 +00:00
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
// Do this a bit later for big values so we don't block the render process
|
|
|
|
if (defaultValue && defaultValue.length > 10000) {
|
|
|
|
setTimeout(setup, 100);
|
|
|
|
} else {
|
|
|
|
setup();
|
|
|
|
}
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2017-02-08 00:31:48 +00:00
|
|
|
|
2016-09-08 22:04:25 +00:00
|
|
|
_isJSON (mode) {
|
|
|
|
if (!mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return mode.indexOf('json') !== -1;
|
2016-09-08 22:04:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 01:51:49 +00:00
|
|
|
_isXML (mode) {
|
|
|
|
if (!mode) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
return mode.indexOf('xml') !== -1;
|
2016-09-10 01:51:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +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-03-03 01:44:07 +00:00
|
|
|
}
|
2016-09-13 17:35:49 +00:00
|
|
|
|
2017-02-07 17:13:59 +00:00
|
|
|
_prettify (code) {
|
2017-02-27 21:00:13 +00:00
|
|
|
this._codemirrorSetValue(code, true);
|
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');
|
2017-03-03 20:09:08 +00:00
|
|
|
code = `<result>${inner}</result>`;
|
2016-09-13 21:18:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Failed to parse filter (that's ok)
|
2017-03-03 20:09:08 +00:00
|
|
|
code = `<result></result>`;
|
2016-09-13 21:18:22 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2017-03-03 01:44:07 +00:00
|
|
|
_codemirrorSetOptions () {
|
2017-02-27 21:00:13 +00:00
|
|
|
const {
|
|
|
|
mode: rawMode,
|
|
|
|
readOnly,
|
|
|
|
hideLineNumbers,
|
|
|
|
keyMap,
|
|
|
|
lineWrapping,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext,
|
|
|
|
getAutocompleteConstants,
|
2017-03-28 22:45:23 +00:00
|
|
|
hideGutters,
|
2017-02-28 21:32:23 +00:00
|
|
|
tabIndex,
|
2017-02-27 21:00:13 +00:00
|
|
|
placeholder,
|
|
|
|
noMatchBrackets,
|
2017-02-28 06:26:24 +00:00
|
|
|
noDragDrop,
|
2017-02-27 21:00:13 +00:00
|
|
|
hideScrollbars,
|
2017-03-01 21:15:56 +00:00
|
|
|
noStyleActiveLine,
|
2017-04-14 21:47:15 +00:00
|
|
|
noLint,
|
2017-06-01 22:58:09 +00:00
|
|
|
indentSize,
|
|
|
|
dynamicHeight
|
2017-02-27 21:00:13 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
let mode;
|
2017-03-03 01:44:07 +00:00
|
|
|
if (this.props.render) {
|
2017-02-27 21:00:13 +00:00
|
|
|
mode = {name: 'nunjucks', baseMode: this._normalizeMode(rawMode)};
|
2017-03-03 01:44:07 +00:00
|
|
|
} else {
|
|
|
|
// foo bar baz
|
|
|
|
mode = this._normalizeMode(rawMode);
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
2017-02-08 00:31:48 +00:00
|
|
|
|
2016-04-29 01:00:12 +00:00
|
|
|
let options = {
|
2017-03-03 01:44:07 +00:00
|
|
|
readOnly: !!readOnly,
|
2017-02-27 21:00:13 +00:00
|
|
|
placeholder: placeholder || '',
|
|
|
|
mode: mode,
|
2017-02-28 21:32:23 +00:00
|
|
|
tabIndex: typeof tabIndex === 'number' ? tabIndex : null,
|
2017-02-28 06:26:24 +00:00
|
|
|
dragDrop: !noDragDrop,
|
2017-02-27 21:00:13 +00:00
|
|
|
scrollbarStyle: hideScrollbars ? 'null' : 'native',
|
2017-03-01 21:15:56 +00:00
|
|
|
styleActiveLine: !noStyleActiveLine,
|
2017-03-28 22:45:23 +00:00
|
|
|
lineNumbers: !hideGutters && !hideLineNumbers,
|
|
|
|
foldGutter: !hideGutters && !hideLineNumbers,
|
2017-02-27 21:00:13 +00:00
|
|
|
lineWrapping: lineWrapping,
|
|
|
|
matchBrackets: !noMatchBrackets,
|
2017-03-01 21:15:56 +00:00
|
|
|
lint: !noLint && !readOnly,
|
2017-03-03 20:09:08 +00:00
|
|
|
gutters: []
|
2016-04-29 01:00:12 +00:00
|
|
|
};
|
2016-04-11 00:40:14 +00:00
|
|
|
|
2017-05-03 17:48:23 +00:00
|
|
|
// Only set keyMap if we're not read-only. This is so things like
|
|
|
|
// ctrl-a work on read-only mode.
|
|
|
|
if (!readOnly && keyMap) {
|
|
|
|
options.keyMap = keyMap;
|
|
|
|
}
|
|
|
|
|
2017-04-14 21:47:15 +00:00
|
|
|
if (indentSize) {
|
|
|
|
options.tabSize = indentSize;
|
|
|
|
options.indentUnit = indentSize;
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!hideGutters && options.lineNumbers) {
|
2017-03-01 21:15:56 +00:00
|
|
|
options.gutters.push('CodeMirror-linenumbers');
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!hideGutters && options.foldGutter) {
|
2017-03-01 21:15:56 +00:00
|
|
|
options.gutters.push('CodeMirror-foldgutter');
|
|
|
|
}
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!hideGutters && options.lint) {
|
2017-03-01 21:15:56 +00:00
|
|
|
options.gutters.push('CodeMirror-lint-markers');
|
|
|
|
}
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
// Setup the hint options
|
2017-03-30 18:52:07 +00:00
|
|
|
if (getRenderContext || getAutocompleteConstants) {
|
|
|
|
let getVariables = null;
|
2017-03-31 21:59:12 +00:00
|
|
|
let getTags = null;
|
2017-03-30 18:52:07 +00:00
|
|
|
if (getRenderContext) {
|
|
|
|
getVariables = async () => {
|
|
|
|
const context = await getRenderContext();
|
|
|
|
const variables = context ? context.keys : [];
|
|
|
|
return variables || [];
|
|
|
|
};
|
2017-03-31 21:59:12 +00:00
|
|
|
|
|
|
|
// Only allow tags if we have variables too
|
2017-06-09 01:10:12 +00:00
|
|
|
getTags = () => {
|
|
|
|
const expandedTags = [];
|
|
|
|
for (const tagDef of getTagDefinitions()) {
|
|
|
|
if (tagDef.args[0].type !== 'enum') {
|
|
|
|
expandedTags.push(tagDef);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const option of tagDef.args[0].options) {
|
|
|
|
const optionName = misc.fnOrString(option.displayName, tagDef.args) || option.name;
|
|
|
|
const newDef = clone(tagDef);
|
|
|
|
newDef.displayName = `${tagDef.displayName} ⇒ ${optionName}`;
|
|
|
|
newDef.args[0].defaultValue = option.value;
|
|
|
|
expandedTags.push(newDef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return expandedTags;
|
|
|
|
};
|
2017-03-30 18:52:07 +00:00
|
|
|
}
|
2017-03-12 00:31:23 +00:00
|
|
|
options.environmentAutocomplete = {
|
2017-03-30 18:52:07 +00:00
|
|
|
getVariables,
|
2017-03-31 21:59:12 +00:00
|
|
|
getTags,
|
2017-03-12 00:31:23 +00:00
|
|
|
getConstants: getAutocompleteConstants
|
|
|
|
};
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-06-01 22:58:09 +00:00
|
|
|
if (dynamicHeight) {
|
|
|
|
options.viewportMargin = Infinity;
|
|
|
|
}
|
|
|
|
|
2016-04-11 00:40:14 +00:00
|
|
|
// Strip of charset if there is one
|
2017-03-12 00:31:23 +00:00
|
|
|
const cm = this.codeMirror;
|
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
|
2017-02-27 21:00:13 +00:00
|
|
|
if (options[key] === cm.options[key]) {
|
2017-02-08 01:52:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
cm.setOption(key, options[key]);
|
2016-03-20 04:00:40 +00:00
|
|
|
});
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2016-03-20 04:00:40 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
_normalizeMode (mode) {
|
|
|
|
const mimeType = mode ? mode.split(';')[0] : 'text/plain';
|
|
|
|
|
|
|
|
if (this._isJSON(mimeType)) {
|
|
|
|
return 'application/json';
|
|
|
|
} else if (this._isXML(mimeType)) {
|
|
|
|
return 'application/xml';
|
|
|
|
} else {
|
|
|
|
return mimeType;
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
async _codemirrorKeyDown (doc, e) {
|
2017-02-27 21:00:13 +00:00
|
|
|
// Use default tab behaviour if we're told
|
|
|
|
if (this.props.defaultTabBehavior && e.keyCode === TAB_KEY) {
|
|
|
|
e.codemirrorIgnore = true;
|
|
|
|
}
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
if (this.props.onKeyDown && !doc.isHintDropdownActive()) {
|
2017-02-27 21:00:13 +00:00
|
|
|
this.props.onKeyDown(e, doc.getValue());
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_codemirrorFocus (doc, e) {
|
2017-02-27 21:00:13 +00:00
|
|
|
if (this.props.onFocus) {
|
|
|
|
this.props.onFocus(e);
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
_codemirrorBlur (doc, e) {
|
2017-02-27 21:00:13 +00:00
|
|
|
if (this.props.onBlur) {
|
|
|
|
this.props.onBlur(e);
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_codemirrorValueBeforeChange (doc, change) {
|
2017-02-27 21:00:13 +00:00
|
|
|
// If we're in single-line mode, merge all changed lines into one
|
2017-03-28 22:45:23 +00:00
|
|
|
if (this.props.singleLine && change.text && change.text.length > 1) {
|
2017-02-27 21:00:13 +00:00
|
|
|
const text = change.text
|
|
|
|
.join('') // join all changed lines into one
|
|
|
|
.replace(/\n/g, ' '); // Convert all whitespace to spaces
|
2017-04-12 04:00:57 +00:00
|
|
|
change.update(change.from, change.to, [text]);
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-04-04 23:06:43 +00:00
|
|
|
_codemirrorPaste (cm, e) {
|
|
|
|
if (this.props.onPaste) {
|
|
|
|
this.props.onPaste(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 20:42:27 +00:00
|
|
|
/**
|
|
|
|
* Wrapper function to add extra behaviour to our onChange event
|
|
|
|
*/
|
2017-03-03 01:44:07 +00:00
|
|
|
_codemirrorValueChanged () {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
const value = this.codeMirror.getDoc().getValue();
|
|
|
|
this.props.onChange(value);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
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
|
2017-02-27 21:00:13 +00:00
|
|
|
* @param forcePrettify
|
2016-03-20 20:42:27 +00:00
|
|
|
*/
|
2017-02-27 21:00:13 +00:00
|
|
|
_codemirrorSetValue (code, forcePrettify = false) {
|
2016-09-08 22:04:25 +00:00
|
|
|
this._originalCode = code;
|
2016-04-17 22:46:17 +00:00
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
// Don't ignore changes from prettify
|
|
|
|
if (!forcePrettify) {
|
|
|
|
this._ignoreNextChange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shouldPrettify = forcePrettify || this.props.autoPrettify;
|
|
|
|
|
|
|
|
if (shouldPrettify && this._canPrettify()) {
|
|
|
|
if (this._isXML(this.props.mode)) {
|
|
|
|
code = this._prettifyXML(code);
|
|
|
|
} else {
|
|
|
|
code = this._prettifyJSON(code);
|
|
|
|
}
|
2016-04-10 02:58:48 +00:00
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
|
|
this.codeMirror.setValue(code || '');
|
2016-03-16 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 02:42:16 +00:00
|
|
|
_handleFilterHistorySelect (filter) {
|
|
|
|
this._filterInput.value = filter;
|
|
|
|
this._setFilter(filter);
|
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleFilterChange (e) {
|
2017-06-08 02:42:16 +00:00
|
|
|
this._setFilter(e.target.value);
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-06-08 02:42:16 +00:00
|
|
|
_setFilter (filter) {
|
2016-09-08 22:04:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
}, 200);
|
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);
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
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 () {
|
2017-03-12 00:31:23 +00:00
|
|
|
const isJson = this._isJSON(this.props.mode);
|
|
|
|
showModal(FilterHelpModal, isJson);
|
|
|
|
trackEvent('Response', `Filter ${isJson ? 'JSONPath' : 'XPath'}`, 'Help');
|
2016-09-08 22:33:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 22:48:56 +00:00
|
|
|
render () {
|
2017-03-28 22:45:23 +00:00
|
|
|
const {
|
|
|
|
id,
|
|
|
|
readOnly,
|
|
|
|
fontSize,
|
|
|
|
mode,
|
|
|
|
filter,
|
2017-06-08 02:42:16 +00:00
|
|
|
filterHistory,
|
2017-03-28 22:45:23 +00:00
|
|
|
onMouseLeave,
|
|
|
|
onClick,
|
2017-06-01 13:32:55 +00:00
|
|
|
className,
|
2017-06-01 22:58:09 +00:00
|
|
|
dynamicHeight,
|
2017-03-28 22:45:23 +00:00
|
|
|
style
|
|
|
|
} = this.props;
|
2016-07-19 22:28:29 +00:00
|
|
|
|
2017-06-01 13:32:55 +00:00
|
|
|
const classes = classnames(className, {
|
2017-03-28 22:45:23 +00:00
|
|
|
'editor': true,
|
2017-06-01 22:58:09 +00:00
|
|
|
'editor--dynamic-height': dynamicHeight,
|
2017-03-28 22:45:23 +00:00
|
|
|
'editor--readonly': readOnly
|
|
|
|
});
|
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
|
2017-06-08 02:42:16 +00:00
|
|
|
ref={this._setFilterInputRef}
|
2016-09-13 17:29:09 +00:00
|
|
|
key="filter"
|
|
|
|
type="text"
|
|
|
|
title="Filter response body"
|
|
|
|
defaultValue={filter || ''}
|
|
|
|
placeholder={this._isJSON(mode) ? '$.store.books[*].author' : '/store/books/author'}
|
2017-02-27 21:00:13 +00:00
|
|
|
onChange={this._handleFilterChange}
|
2016-09-13 17:29:09 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-06-08 02:42:16 +00:00
|
|
|
|
2017-06-08 02:53:46 +00:00
|
|
|
if (filterHistory && filterHistory.length) {
|
2017-06-08 02:42:16 +00:00
|
|
|
toolbarChildren.push(
|
|
|
|
<Dropdown key="history" className="tall" right>
|
|
|
|
<DropdownButton className="btn btn--compact">
|
|
|
|
<i className="fa fa-clock-o"/>
|
|
|
|
</DropdownButton>
|
|
|
|
{filterHistory.reverse().map(filter => (
|
|
|
|
<DropdownItem key={filter} value={filter} onClick={this._handleFilterHistorySelect}>
|
|
|
|
{filter}
|
|
|
|
</DropdownItem>
|
|
|
|
))}
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-13 17:29:09 +00:00
|
|
|
toolbarChildren.push(
|
2017-06-08 02:42:16 +00:00
|
|
|
<button key="help" className="btn btn--compact" onClick={this._showFilterHelp}>
|
2017-03-28 22:45:23 +00:00
|
|
|
<i className="fa fa-question-circle"/>
|
2016-09-13 17:29:09 +00:00
|
|
|
</button>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
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)) {
|
2017-03-03 20:09:08 +00:00
|
|
|
contentTypeName = 'JSON';
|
2016-09-13 17:29:09 +00:00
|
|
|
} else if (this._isXML(mode)) {
|
2017-03-03 20:09:08 +00:00
|
|
|
contentTypeName = 'XML';
|
2016-09-13 17:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toolbarChildren.push(
|
|
|
|
<button key="prettify"
|
|
|
|
className="btn btn--compact"
|
|
|
|
title="Auto-format request body whitespace"
|
2017-02-27 21:00:13 +00:00
|
|
|
onClick={this._handleBeautify}>
|
2016-09-13 17:29:09 +00:00
|
|
|
Beautify {contentTypeName}
|
|
|
|
</button>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-09-13 17:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let toolbar = null;
|
|
|
|
if (toolbarChildren.length) {
|
|
|
|
toolbar = <div className="editor__toolbar">{toolbarChildren}</div>;
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:15:56 +00:00
|
|
|
const styles = {};
|
|
|
|
if (fontSize) {
|
2017-03-03 20:09:08 +00:00
|
|
|
styles.fontSize = `${fontSize}px`;
|
2017-03-01 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 20:02:47 +00:00
|
|
|
return (
|
2017-03-28 22:45:23 +00:00
|
|
|
<div className={classes} style={style}>
|
2017-06-01 13:32:55 +00:00
|
|
|
<div className={classnames('editor__container', 'input', className)}
|
2017-03-09 06:23:23 +00:00
|
|
|
style={styles}
|
|
|
|
onClick={onClick}
|
|
|
|
onMouseLeave={onMouseLeave}>
|
2017-03-01 00:10:23 +00:00
|
|
|
<textarea
|
2017-03-23 22:10:42 +00:00
|
|
|
id={id}
|
2017-03-01 00:10:23 +00:00
|
|
|
ref={this._handleInitTextarea}
|
|
|
|
style={{display: 'none'}}
|
|
|
|
defaultValue=" "
|
|
|
|
readOnly={readOnly}
|
2017-03-01 21:15:56 +00:00
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
CodeEditor.propTypes = {
|
2016-03-16 20:02:47 +00:00
|
|
|
onChange: PropTypes.func,
|
2017-02-27 21:00:13 +00:00
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
2017-02-08 00:31:48 +00:00
|
|
|
onClickLink: PropTypes.func,
|
2017-03-08 05:52:17 +00:00
|
|
|
onKeyDown: PropTypes.func,
|
2017-03-08 00:56:51 +00:00
|
|
|
onMouseLeave: PropTypes.func,
|
2017-03-09 06:23:23 +00:00
|
|
|
onClick: PropTypes.func,
|
2017-04-04 23:06:43 +00:00
|
|
|
onPaste: PropTypes.func,
|
2017-02-13 08:12:02 +00:00
|
|
|
render: PropTypes.func,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext: PropTypes.func,
|
|
|
|
getAutocompleteConstants: PropTypes.func,
|
2017-01-24 22:18:11 +00:00
|
|
|
keyMap: PropTypes.string,
|
2016-04-29 01:00:12 +00:00
|
|
|
mode: PropTypes.string,
|
2017-03-23 22:10:42 +00:00
|
|
|
id: PropTypes.string,
|
2016-04-29 01:00:12 +00:00
|
|
|
placeholder: PropTypes.string,
|
2016-07-19 22:28:29 +00:00
|
|
|
lineWrapping: PropTypes.bool,
|
2017-02-27 21:00:13 +00:00
|
|
|
hideLineNumbers: PropTypes.bool,
|
2017-03-28 22:45:23 +00:00
|
|
|
hideGutters: PropTypes.bool,
|
2017-02-27 21:00:13 +00:00
|
|
|
noMatchBrackets: PropTypes.bool,
|
|
|
|
hideScrollbars: PropTypes.bool,
|
2016-07-19 22:28:29 +00:00
|
|
|
fontSize: PropTypes.number,
|
2017-04-14 21:47:15 +00:00
|
|
|
indentSize: PropTypes.number,
|
2017-03-01 21:15:56 +00:00
|
|
|
defaultValue: PropTypes.string,
|
2017-02-28 21:32:23 +00:00
|
|
|
tabIndex: PropTypes.number,
|
2016-09-13 17:29:09 +00:00
|
|
|
autoPrettify: PropTypes.bool,
|
|
|
|
manualPrettify: PropTypes.bool,
|
2017-03-01 21:15:56 +00:00
|
|
|
noLint: PropTypes.bool,
|
2017-02-28 06:26:24 +00:00
|
|
|
noDragDrop: PropTypes.bool,
|
2017-03-01 21:15:56 +00:00
|
|
|
noStyleActiveLine: PropTypes.bool,
|
2016-07-22 21:35:49 +00:00
|
|
|
className: PropTypes.any,
|
2017-03-28 22:45:23 +00:00
|
|
|
style: PropTypes.object,
|
2016-09-09 00:32:36 +00:00
|
|
|
updateFilter: PropTypes.func,
|
2017-02-27 21:00:13 +00:00
|
|
|
defaultTabBehavior: PropTypes.bool,
|
2017-02-13 08:12:02 +00:00
|
|
|
readOnly: PropTypes.bool,
|
2017-02-27 21:00:13 +00:00
|
|
|
filter: PropTypes.string,
|
2017-06-08 02:53:46 +00:00
|
|
|
filterHistory: PropTypes.arrayOf(PropTypes.string.isRequired),
|
2017-02-27 21:00:13 +00:00
|
|
|
singleLine: PropTypes.bool,
|
2017-06-01 22:58:09 +00:00
|
|
|
debounceMillis: PropTypes.number,
|
|
|
|
dynamicHeight: PropTypes.bool
|
2016-03-16 20:02:47 +00:00
|
|
|
};
|
|
|
|
|
2017-03-09 06:23:23 +00:00
|
|
|
export default CodeEditor;
|