1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __assign = (this && this.__assign) || function () {
|
16 | __assign = Object.assign || function(t) {
|
17 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
18 | s = arguments[i];
|
19 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
20 | t[p] = s[p];
|
21 | }
|
22 | return t;
|
23 | };
|
24 | return __assign.apply(this, arguments);
|
25 | };
|
26 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
27 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
28 | };
|
29 | Object.defineProperty(exports, "__esModule", { value: true });
|
30 | exports.QueryEditor = void 0;
|
31 | var react_1 = __importDefault(require("react"));
|
32 | var markdown_it_1 = __importDefault(require("markdown-it"));
|
33 | var normalizeWhitespace_1 = require("../utility/normalizeWhitespace");
|
34 | var onHasCompletion_1 = __importDefault(require("../utility/onHasCompletion"));
|
35 | var commonKeys_1 = __importDefault(require("../utility/commonKeys"));
|
36 | var md = new markdown_it_1.default();
|
37 | var AUTO_COMPLETE_AFTER_KEY = /^[a-zA-Z0-9_@(]$/;
|
38 | var QueryEditor = (function (_super) {
|
39 | __extends(QueryEditor, _super);
|
40 | function QueryEditor(props) {
|
41 | var _this = _super.call(this, props) || this;
|
42 | _this.editor = null;
|
43 | _this.ignoreChangeEvent = false;
|
44 | _this._node = null;
|
45 | _this._onKeyUp = function (_cm, event) {
|
46 | if (AUTO_COMPLETE_AFTER_KEY.test(event.key) && _this.editor) {
|
47 | _this.editor.execCommand('autocomplete');
|
48 | }
|
49 | };
|
50 | _this._onEdit = function () {
|
51 | if (!_this.ignoreChangeEvent && _this.editor) {
|
52 | _this.cachedValue = _this.editor.getValue();
|
53 | if (_this.props.onEdit) {
|
54 | _this.props.onEdit(_this.cachedValue);
|
55 | }
|
56 | }
|
57 | };
|
58 | _this._onHasCompletion = function (cm, data) {
|
59 | onHasCompletion_1.default(cm, data, _this.props.onHintInformationRender);
|
60 | };
|
61 | _this.cachedValue = props.value || '';
|
62 | return _this;
|
63 | }
|
64 | QueryEditor.prototype.componentDidMount = function () {
|
65 | var _this = this;
|
66 | var _a, _b, _c;
|
67 | var CodeMirror = require('codemirror');
|
68 | require('codemirror/addon/hint/show-hint');
|
69 | require('codemirror/addon/comment/comment');
|
70 | require('codemirror/addon/edit/matchbrackets');
|
71 | require('codemirror/addon/edit/closebrackets');
|
72 | require('codemirror/addon/fold/foldgutter');
|
73 | require('codemirror/addon/fold/brace-fold');
|
74 | require('codemirror/addon/search/search');
|
75 | require('codemirror/addon/search/searchcursor');
|
76 | require('codemirror/addon/search/jump-to-line');
|
77 | require('codemirror/addon/dialog/dialog');
|
78 | require('codemirror/addon/lint/lint');
|
79 | require('codemirror/keymap/sublime');
|
80 | require('codemirror-graphql/hint');
|
81 | require('codemirror-graphql/lint');
|
82 | require('codemirror-graphql/info');
|
83 | require('codemirror-graphql/jump');
|
84 | require('codemirror-graphql/mode');
|
85 | var editor = (this.editor = CodeMirror(this._node, {
|
86 | value: this.props.value || '',
|
87 | lineNumbers: true,
|
88 | tabSize: 2,
|
89 | mode: 'graphql',
|
90 | theme: this.props.editorTheme || 'graphiql',
|
91 | keyMap: 'sublime',
|
92 | autoCloseBrackets: true,
|
93 | matchBrackets: true,
|
94 | showCursorWhenSelecting: true,
|
95 | readOnly: this.props.readOnly ? 'nocursor' : false,
|
96 | foldGutter: {
|
97 | minFoldSize: 4,
|
98 | },
|
99 | lint: {
|
100 | schema: this.props.schema,
|
101 | validationRules: (_a = this.props.validationRules) !== null && _a !== void 0 ? _a : null,
|
102 | externalFragments: (_b = this.props) === null || _b === void 0 ? void 0 : _b.externalFragments,
|
103 | },
|
104 | hintOptions: {
|
105 | schema: this.props.schema,
|
106 | closeOnUnfocus: false,
|
107 | completeSingle: false,
|
108 | container: this._node,
|
109 | externalFragments: (_c = this.props) === null || _c === void 0 ? void 0 : _c.externalFragments,
|
110 | },
|
111 | info: {
|
112 | schema: this.props.schema,
|
113 | renderDescription: function (text) { return md.render(text); },
|
114 | onClick: function (reference) {
|
115 | return _this.props.onClickReference && _this.props.onClickReference(reference);
|
116 | },
|
117 | },
|
118 | jump: {
|
119 | schema: this.props.schema,
|
120 | onClick: function (reference) {
|
121 | return _this.props.onClickReference && _this.props.onClickReference(reference);
|
122 | },
|
123 | },
|
124 | gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
|
125 | extraKeys: __assign(__assign({ 'Cmd-Space': function () {
|
126 | return editor.showHint({ completeSingle: true, container: _this._node });
|
127 | }, 'Ctrl-Space': function () {
|
128 | return editor.showHint({ completeSingle: true, container: _this._node });
|
129 | }, 'Alt-Space': function () {
|
130 | return editor.showHint({ completeSingle: true, container: _this._node });
|
131 | }, 'Shift-Space': function () {
|
132 | return editor.showHint({ completeSingle: true, container: _this._node });
|
133 | }, 'Shift-Alt-Space': function () {
|
134 | return editor.showHint({ completeSingle: true, container: _this._node });
|
135 | }, 'Cmd-Enter': function () {
|
136 | if (_this.props.onRunQuery) {
|
137 | _this.props.onRunQuery();
|
138 | }
|
139 | }, 'Ctrl-Enter': function () {
|
140 | if (_this.props.onRunQuery) {
|
141 | _this.props.onRunQuery();
|
142 | }
|
143 | }, 'Shift-Ctrl-C': function () {
|
144 | if (_this.props.onCopyQuery) {
|
145 | _this.props.onCopyQuery();
|
146 | }
|
147 | }, 'Shift-Ctrl-P': function () {
|
148 | if (_this.props.onPrettifyQuery) {
|
149 | _this.props.onPrettifyQuery();
|
150 | }
|
151 | }, 'Shift-Ctrl-F': function () {
|
152 | if (_this.props.onPrettifyQuery) {
|
153 | _this.props.onPrettifyQuery();
|
154 | }
|
155 | }, 'Shift-Ctrl-M': function () {
|
156 | if (_this.props.onMergeQuery) {
|
157 | _this.props.onMergeQuery();
|
158 | }
|
159 | } }, commonKeys_1.default), { 'Cmd-S': function () {
|
160 | if (_this.props.onRunQuery) {
|
161 | }
|
162 | }, 'Ctrl-S': function () {
|
163 | if (_this.props.onRunQuery) {
|
164 | }
|
165 | } }),
|
166 | }));
|
167 | if (editor) {
|
168 | editor.on('change', this._onEdit);
|
169 | editor.on('keyup', this._onKeyUp);
|
170 | editor.on('hasCompletion', this._onHasCompletion);
|
171 | editor.on('beforeChange', this._onBeforeChange);
|
172 | }
|
173 | };
|
174 | QueryEditor.prototype.componentDidUpdate = function (prevProps) {
|
175 | var CodeMirror = require('codemirror');
|
176 | this.ignoreChangeEvent = true;
|
177 | if (this.props.schema !== prevProps.schema && this.editor) {
|
178 | this.editor.options.lint.schema = this.props.schema;
|
179 | this.editor.options.hintOptions.schema = this.props.schema;
|
180 | this.editor.options.info.schema = this.props.schema;
|
181 | this.editor.options.jump.schema = this.props.schema;
|
182 | CodeMirror.signal(this.editor, 'change', this.editor);
|
183 | }
|
184 | if (this.props.value !== prevProps.value &&
|
185 | this.props.value !== this.cachedValue &&
|
186 | this.editor) {
|
187 | this.cachedValue = this.props.value;
|
188 | this.editor.setValue(this.props.value);
|
189 | }
|
190 | this.ignoreChangeEvent = false;
|
191 | };
|
192 | QueryEditor.prototype.componentWillUnmount = function () {
|
193 | if (this.editor) {
|
194 | this.editor.off('change', this._onEdit);
|
195 | this.editor.off('keyup', this._onKeyUp);
|
196 | this.editor.off('hasCompletion', this._onHasCompletion);
|
197 | this.editor = null;
|
198 | }
|
199 | };
|
200 | QueryEditor.prototype.render = function () {
|
201 | var _this = this;
|
202 | return (react_1.default.createElement("section", { className: "query-editor", "aria-label": "Query Editor", ref: function (node) {
|
203 | _this._node = node;
|
204 | } }));
|
205 | };
|
206 | QueryEditor.prototype.getCodeMirror = function () {
|
207 | return this.editor;
|
208 | };
|
209 | QueryEditor.prototype.getClientHeight = function () {
|
210 | return this._node && this._node.clientHeight;
|
211 | };
|
212 | QueryEditor.prototype._onBeforeChange = function (_instance, change) {
|
213 | if (change.origin === 'paste') {
|
214 | var text = change.text.map(normalizeWhitespace_1.normalizeWhitespace);
|
215 | change.update(change.from, change.to, text);
|
216 | }
|
217 | };
|
218 | return QueryEditor;
|
219 | }(react_1.default.Component));
|
220 | exports.QueryEditor = QueryEditor;
|
221 |
|
\ | No newline at end of file |