forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryEditor.jsx
More file actions
330 lines (296 loc) · 11.4 KB
/
QueryEditor.jsx
File metadata and controls
330 lines (296 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import React from 'react';
import PropTypes from 'prop-types';
import Tooltip from 'antd/lib/tooltip';
import { react2angular } from 'react2angular';
import AceEditor from 'react-ace';
import ace from 'brace';
import toastr from 'angular-toastr';
import 'brace/ext/language_tools';
import 'brace/mode/json';
import 'brace/mode/python';
import 'brace/mode/sql';
import 'brace/mode/yaml';
import 'brace/theme/textmate';
import 'brace/ext/searchbox';
import { Query } from '@/services/query';
import { QuerySnippet } from '@/services/query-snippet';
import { KeyboardShortcuts } from '@/services/keyboard-shortcuts';
import localOptions from '@/lib/localOptions';
import AutocompleteToggle from '@/components/AutocompleteToggle';
import keywordBuilder from './keywordBuilder';
import { DataSource, Schema } from './proptypes';
import './QueryEditor.css';
const langTools = ace.acequire('ace/ext/language_tools');
const snippetsModule = ace.acequire('ace/snippets');
// By default Ace will try to load snippet files for the different modes and fail.
// We don't need them, so we use these placeholders until we define our own.
function defineDummySnippets(mode) {
ace.define(`ace/snippets/${mode}`, ['require', 'exports', 'module'], (require, exports) => {
exports.snippetText = '';
exports.scope = mode;
});
}
defineDummySnippets('python');
defineDummySnippets('sql');
defineDummySnippets('json');
defineDummySnippets('yaml');
class QueryEditor extends React.Component {
static propTypes = {
queryText: PropTypes.string.isRequired,
schema: Schema, // eslint-disable-line react/no-unused-prop-types
addNewParameter: PropTypes.func.isRequired,
dataSources: PropTypes.arrayOf(DataSource),
dataSource: DataSource,
canEdit: PropTypes.bool.isRequired,
isDirty: PropTypes.bool.isRequired,
isQueryOwner: PropTypes.bool.isRequired,
updateDataSource: PropTypes.func.isRequired,
canExecuteQuery: PropTypes.func.isRequired,
executeQuery: PropTypes.func.isRequired,
queryExecuting: PropTypes.bool.isRequired,
saveQuery: PropTypes.func.isRequired,
updateQuery: PropTypes.func.isRequired,
updateSelectedQuery: PropTypes.func.isRequired,
listenForResize: PropTypes.func.isRequired,
listenForEditorCommand: PropTypes.func.isRequired,
};
static defaultProps = {
schema: null,
dataSource: {},
dataSources: [],
};
constructor(props) {
super(props);
this.refEditor = React.createRef();
this.state = {
schema: null, // eslint-disable-line react/no-unused-state
keywords: {
table: [],
column: [],
tableColumn: [],
},
autocompleteQuery: localOptions.get('liveAutocomplete', true),
liveAutocompleteDisabled: false,
// XXX temporary while interfacing with angular
queryText: props.queryText,
selectedQueryText: null,
};
const schemaCompleter = {
identifierRegexps: [/[a-zA-Z_0-9.\-\u00A2-\uFFFF]/],
getCompletions: (state, session, pos, prefix, callback) => {
const tableKeywords = this.state.keywords.table;
const columnKeywords = this.state.keywords.column;
const tableColumnKeywords = this.state.keywords.tableColumn;
if (prefix.length === 0 || tableKeywords.length === 0) {
callback(null, []);
return;
}
if (prefix[prefix.length - 1] === '.') {
const tableName = prefix.substring(0, prefix.length - 1);
callback(null, tableKeywords.concat(tableColumnKeywords[tableName]));
return;
}
callback(null, tableKeywords.concat(columnKeywords));
},
};
langTools.setCompleters([
langTools.snippetCompleter,
langTools.keyWordCompleter,
langTools.textCompleter,
schemaCompleter,
]);
}
static getDerivedStateFromProps(nextProps, prevState) {
if (!nextProps.schema) {
return {
keywords: {
table: [],
column: [],
tableColumn: [],
},
liveAutocompleteDisabled: false,
};
} else if (nextProps.schema !== prevState.schema) {
const tokensCount = nextProps.schema.reduce((totalLength, table) => totalLength + table.columns.length, 0);
return {
schema: nextProps.schema,
keywords: keywordBuilder.buildKeywordsFromSchema(nextProps.schema),
liveAutocompleteDisabled: tokensCount > 5000,
};
}
return null;
}
onLoad = (editor) => {
// Release Cmd/Ctrl+L to the browser
editor.commands.bindKey('Cmd+L', null);
editor.commands.bindKey('Ctrl+P', null);
editor.commands.bindKey('Ctrl+L', null);
// Ignore Ctrl+P to open new parameter dialog
editor.commands.bindKey({ win: 'Ctrl+P', mac: null }, null);
// Lineup only mac
editor.commands.bindKey({ win: null, mac: 'Ctrl+P' }, 'golineup');
// Reset Completer in case dot is pressed
editor.commands.on('afterExec', (e) => {
if (e.command.name === 'insertstring' && e.args === '.'
&& editor.completer) {
editor.completer.showPopup(editor);
}
});
QuerySnippet.query((snippets) => {
const snippetManager = snippetsModule.snippetManager;
const m = {
snippetText: '',
};
m.snippets = snippetManager.parseSnippetFile(m.snippetText);
snippets.forEach((snippet) => {
m.snippets.push(snippet.getSnippet());
});
snippetManager.register(m.snippets || [], m.scope);
});
editor.focus();
this.props.listenForResize(() => editor.resize());
this.props.listenForEditorCommand((e, command, ...args) => {
switch (command) {
case 'focus': {
editor.focus();
break;
}
case 'paste': {
const [text] = args;
editor.session.doc.replace(editor.selection.getRange(), text);
const range = editor.selection.getRange();
this.props.updateQuery(editor.session.getValue());
editor.selection.setRange(range);
break;
}
default:
break;
}
});
};
updateSelectedQuery = (selection) => {
const { editor } = this.refEditor.current;
const doc = editor.getSession().doc;
const rawSelectedQueryText = doc.getTextRange(selection.getRange());
const selectedQueryText = (rawSelectedQueryText.length > 1) ? rawSelectedQueryText : null;
this.setState({ selectedQueryText });
this.props.updateSelectedQuery(selectedQueryText);
};
updateQuery = (queryText) => {
this.props.updateQuery(queryText);
this.setState({ queryText });
};
formatQuery = () => {
Query.format(this.props.dataSource.syntax || 'sql', this.props.queryText)
.then(this.updateQuery)
.catch(error => toastr.error(error));
};
toggleAutocomplete = (state) => {
this.setState({ autocompleteQuery: state });
localOptions.set('liveAutocomplete', state);
};
componentDidUpdate = () => {
// ANGULAR_REMOVE_ME Work-around for a resizing issue, see https://github.com/getredash/redash/issues/3353
const { editor } = this.refEditor.current;
editor.resize();
};
render() {
const modKey = KeyboardShortcuts.modKey;
const isExecuteDisabled = this.props.queryExecuting || !this.props.canExecuteQuery();
return (
<section style={{ height: '100%' }} data-test="QueryEditor">
<div className="container p-15 m-b-10" style={{ height: '100%' }}>
<div data-executing={this.props.queryExecuting} style={{ height: 'calc(100% - 40px)', marginBottom: '0px' }} className="editor__container">
<AceEditor
ref={this.refEditor}
theme="textmate"
mode={this.props.dataSource.syntax || 'sql'}
value={this.state.queryText}
editorProps={{ $blockScrolling: Infinity }}
width="100%"
height="100%"
setOptions={{
behavioursEnabled: true,
enableSnippets: true,
enableBasicAutocompletion: true,
enableLiveAutocompletion: !this.state.liveAutocompleteDisabled && this.state.autocompleteQuery,
autoScrollEditorIntoView: true,
}}
showPrintMargin={false}
wrapEnabled={false}
onLoad={this.onLoad}
onPaste={this.onPaste}
onChange={this.updateQuery}
onSelectionChange={this.updateSelectedQuery}
/>
</div>
<div className="editor__control">
<div className="form-inline d-flex">
<Tooltip
placement="top"
title={<span>Add New Parameter (<i>{modKey} + P</i>)</span>}
>
<button type="button" className="btn btn-default m-r-5" onClick={this.props.addNewParameter}>
{{ }}
</button>
</Tooltip>
<Tooltip placement="top" title="Format Query">
<button type="button" className="btn btn-default m-r-5" onClick={this.formatQuery}>
<span className="zmdi zmdi-format-indent-increase" />
</button>
</Tooltip>
<AutocompleteToggle
state={this.state.autocompleteQuery}
onToggle={this.toggleAutocomplete}
disabled={this.state.liveAutocompleteDisabled}
/>
<select
className="form-control datasource-small flex-fill w-100"
onChange={this.props.updateDataSource}
disabled={!this.props.isQueryOwner}
>
{this.props.dataSources.map(ds => (
<option label={ds.name} value={ds.id} key={`ds-option-${ds.id}`}>
{ds.name}
</option>
))}
</select>
{this.props.canEdit ? (
<Tooltip placement="top" title={modKey + ' + S'}>
<button type="button" className="btn btn-default m-l-5" onClick={this.props.saveQuery} title="Save">
<span className="fa fa-floppy-o" />
<span className="hidden-xs m-l-5">Save</span>
{this.props.isDirty ? '*' : null}
</button>
</Tooltip>
) : null}
<Tooltip placement="top" title={modKey + ' + Enter'}>
{/*
Tooltip wraps disabled buttons with `<span>` and moves all styles
and classes to that `<span>`. There is a piece of CSS that fixes
button appearance, but also wwe need to add `disabled` class to
disabled buttons so it will be assigned to wrapper and make it
looking properly
*/}
<button
type="button"
className={'btn btn-primary m-l-5' + (isExecuteDisabled ? ' disabled' : '')}
disabled={isExecuteDisabled}
onClick={this.props.executeQuery}
data-test="ExecuteButton"
>
<span className="zmdi zmdi-play" />
<span className="hidden-xs m-l-5">{ (this.state.selectedQueryText == null) ? 'Execute' : 'Execute Selected' }</span>
</button>
</Tooltip>
</div>
</div>
</div>
</section>
);
}
}
export default function init(ngModule) {
ngModule.component('queryEditor', react2angular(QueryEditor));
}
init.init = true;