-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCypherEditor.tsx
458 lines (417 loc) · 12.4 KB
/
CypherEditor.tsx
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import {
Annotation,
Compartment,
EditorState,
Extension,
} from '@codemirror/state';
import {
EditorView,
KeyBinding,
keymap,
lineNumbers,
placeholder,
ViewUpdate,
} from '@codemirror/view';
import type { DbSchema } from '@neo4j-cypher/language-support';
import debounce from 'lodash.debounce';
import { Component, createRef } from 'react';
import {
replaceHistory,
replMode as historyNavigation,
} from './historyNavigation';
import { cypher, CypherConfig } from './lang-cypher/langCypher';
import { cleanupWorkers } from './lang-cypher/syntaxValidation';
import { basicNeo4jSetup } from './neo4jSetup';
import { getThemeExtension } from './themes';
type DomEventHandlers = Parameters<typeof EditorView.domEventHandlers>[0];
export interface CypherEditorProps {
/**
* The prompt to show on single line editors
*/
prompt?: string;
/**
* Custom keybindings to add to the editor.
* See https://codemirror.net/6/docs/ref/#keymap.of
*/
extraKeybindings?: KeyBinding[];
/**
* Callback on query execution, triggered via ctrl/cmd + Enter.
* If provided, will enable "repl-mode", which turns on navigating editor history
*
* @param cmd - the editor value when ctrl/cmd + enter was pressed
* @returns void
*/
onExecute?: (cmd: string) => void;
/**
* The editor history navigable via up/down arrow keys. Order newest to oldest.
* Add to this list with the `onExecute` callback for REPL style history.
*/
history?: string[];
/**
* When set to `true` the editor will use the background color of the parent element.
*
* @default false
*/
overrideThemeBackgroundColor?: boolean;
/**
* Whether the editor should take focus on mount.
* Will move the cursor to the end of the query when provided with an initial value.
*
* @default false
*/
autofocus?: boolean;
/**
* Where to place the cursor in the query. Cannot be enabled at the same time than autofocus
*/
offset?: number;
/**
* Whether the editor should wrap lines.
*
* @default false
*/
lineWrap?: boolean;
/**
* Whether the editor should perform syntax validation.
*
* @default true
*/
lint?: boolean;
/**
* The schema to use for autocompletion and linting.
*
* @type {DbSchema}
*/
schema?: DbSchema;
/**
* The current value of the editor.
*/
value?: string;
/**
* Extra css classnames to add to the editor container.
*/
className?: string;
/**
* Set the built in theme or provide a custom theme.
*
* `light` / `dark` / `Extension`
* @default light
*/
theme?: 'light' | 'dark' | Extension;
/**
* Callback when the editor value changes.
* @param {string} value - the current editor value
* @param {ViewUpdate} viewUpdate - the view update from codemirror
*/
onChange?(value: string, viewUpdate: ViewUpdate): void;
/**
* Map of event handlers to add to the editor.
*
* Note that the props are compared by reference, meaning object defined inline
* will cause the editor to re-render (much like the style prop does in this example:
* <div style={{}} />
*
* Memoize the object if you want/need to avoid this.
*
* @example
* // listen to blur events
* <CypherEditor domEventHandlers={{blur: () => console.log("blur event fired")}} />
*/
domEventHandlers?: DomEventHandlers;
/**
* Placeholder text to display when the editor is empty.
*/
placeholder?: string;
/**
* Whether the editor should show line numbers.
*
* @default true
*/
lineNumbers?: boolean;
/**
* Whether the editor is read-only.
*
* @default false
*/
readonly?: boolean;
}
const executeKeybinding = (onExecute?: (cmd: string) => void) =>
onExecute
? [
{
key: 'Ctrl-Enter',
mac: 'Mod-Enter',
preventDefault: true,
run: (view: EditorView) => {
const doc = view.state.doc.toString();
if (doc.trim() !== '') {
onExecute(doc);
}
return true;
},
},
]
: [];
const themeCompartment = new Compartment();
const keyBindingCompartment = new Compartment();
const lineNumbersCompartment = new Compartment();
const readOnlyCompartment = new Compartment();
const placeholderCompartment = new Compartment();
const domEventHandlerCompartment = new Compartment();
const formatLineNumber =
(prompt?: string) => (a: number, state: EditorState) => {
if (state.doc.lines === 1 && prompt !== undefined) {
return prompt;
}
return a.toString();
};
type CypherEditorState = { cypherSupportEnabled: boolean };
const ExternalEdit = Annotation.define<boolean>();
export class CypherEditor extends Component<
CypherEditorProps,
CypherEditorState
> {
/**
* The codemirror editor container.
*/
editorContainer: React.RefObject<HTMLDivElement> = createRef();
/**
* The codemirror editor state.
*/
editorState: React.MutableRefObject<EditorState> = createRef();
/**
* The codemirror editor view.
*/
editorView: React.MutableRefObject<EditorView> = createRef();
private schemaRef: React.MutableRefObject<CypherConfig> = createRef();
/**
* Focus the editor
*/
focus() {
this.editorView.current?.focus();
}
/**
* Move the cursor to the supplied position.
* For example, to move the cursor to the end of the editor, use `value.length`
*/
updateCursorPosition(position: number) {
this.editorView.current?.dispatch({
selection: { anchor: position, head: position },
});
}
/**
* Externally set the editor value and focus the editor.
*/
setValueAndFocus(value = '') {
const currentCmValue = this.editorView.current.state?.doc.toString() ?? '';
this.editorView.current.dispatch({
changes: {
from: 0,
to: currentCmValue.length,
insert: value,
},
selection: { anchor: value.length, head: value.length },
});
this.editorView.current?.focus();
}
static defaultProps: CypherEditorProps = {
lint: true,
schema: {},
overrideThemeBackgroundColor: false,
lineWrap: false,
extraKeybindings: [],
history: [],
theme: 'light',
lineNumbers: true,
};
private debouncedOnChange = this.props.onChange
? debounce(this.props.onChange, 200)
: undefined;
componentDidMount(): void {
const {
theme,
extraKeybindings,
lineWrap,
overrideThemeBackgroundColor,
schema,
lint,
onExecute,
} = this.props;
this.schemaRef.current = {
schema,
lint,
useLightVersion: false,
setUseLightVersion: (newVal) => {
if (this.schemaRef.current !== undefined) {
this.schemaRef.current.useLightVersion = newVal;
}
},
};
const themeExtension = getThemeExtension(
theme,
overrideThemeBackgroundColor,
);
const changeListener = this.debouncedOnChange
? [
EditorView.updateListener.of((upt: ViewUpdate) => {
const wasUserEdit = !upt.transactions.some((tr) =>
tr.annotation(ExternalEdit),
);
if (upt.docChanged && wasUserEdit) {
const doc = upt.state.doc;
const value = doc.toString();
this.debouncedOnChange(value, upt);
}
}),
]
: [];
this.editorState.current = EditorState.create({
extensions: [
keyBindingCompartment.of(
keymap.of([...executeKeybinding(onExecute), ...extraKeybindings]),
),
historyNavigation(this.props),
basicNeo4jSetup(),
themeCompartment.of(themeExtension),
changeListener,
cypher(this.schemaRef.current),
lineWrap ? EditorView.lineWrapping : [],
lineNumbersCompartment.of(
this.props.lineNumbers
? lineNumbers({ formatNumber: formatLineNumber(this.props.prompt) })
: [],
),
readOnlyCompartment.of(EditorState.readOnly.of(this.props.readonly)),
placeholderCompartment.of(
this.props.placeholder ? placeholder(this.props.placeholder) : [],
),
domEventHandlerCompartment.of(
this.props.domEventHandlers
? EditorView.domEventHandlers(this.props.domEventHandlers)
: [],
),
],
doc: this.props.value,
});
this.editorView.current = new EditorView({
state: this.editorState.current,
parent: this.editorContainer.current,
});
if (this.props.autofocus) {
this.focus();
if (this.props.value) {
this.updateCursorPosition(this.props.value.length);
}
} else if (this.props.offset) {
this.updateCursorPosition(this.props.offset);
}
}
componentDidUpdate(prevProps: CypherEditorProps): void {
if (!this.editorView.current) {
return;
}
// Handle externally set value
const currentCmValue = this.editorView.current.state?.doc.toString() ?? '';
if (this.props.value !== undefined && currentCmValue !== this.props.value) {
this.editorView.current.dispatch({
changes: {
from: 0,
to: currentCmValue.length,
insert: this.props.value ?? '',
},
annotations: [ExternalEdit.of(true)],
});
}
// Handle theme change
const didChangeTheme =
prevProps.theme !== this.props.theme ||
prevProps.overrideThemeBackgroundColor !==
this.props.overrideThemeBackgroundColor;
if (didChangeTheme) {
this.editorView.current.dispatch({
effects: themeCompartment.reconfigure(
getThemeExtension(
this.props.theme,
this.props.overrideThemeBackgroundColor,
),
),
});
}
if (prevProps.lineNumbers !== this.props.lineNumbers) {
this.editorView.current.dispatch({
effects: lineNumbersCompartment.reconfigure(
this.props.lineNumbers
? lineNumbers({ formatNumber: formatLineNumber(this.props.prompt) })
: [],
),
});
}
if (prevProps.readonly !== this.props.readonly) {
this.editorView.current.dispatch({
effects: readOnlyCompartment.reconfigure(
EditorState.readOnly.of(this.props.readonly),
),
});
}
if (prevProps.placeholder !== this.props.placeholder) {
this.editorView.current.dispatch({
effects: placeholderCompartment.reconfigure(
this.props.placeholder ? placeholder(this.props.placeholder) : [],
),
});
}
if (
prevProps.extraKeybindings !== this.props.extraKeybindings ||
prevProps.onExecute !== this.props.onExecute
) {
this.editorView.current.dispatch({
effects: keyBindingCompartment.reconfigure(
keymap.of([
...executeKeybinding(this.props.onExecute),
...this.props.extraKeybindings,
]),
),
});
}
if (prevProps.domEventHandlers !== this.props.domEventHandlers) {
this.editorView.current.dispatch({
effects: domEventHandlerCompartment.reconfigure(
this.props.domEventHandlers
? EditorView.domEventHandlers(this.props.domEventHandlers)
: [],
),
});
}
// This component rerenders on every keystroke and comparing the
// full lists of editor strings on every render could be expensive.
const didChangeHistoryEstimate =
prevProps.history?.length !== this.props.history?.length ||
prevProps.history?.[0] !== this.props.history?.[0];
if (didChangeHistoryEstimate) {
this.editorView.current.dispatch({
effects: replaceHistory.of(this.props.history ?? []),
});
}
/*
The cypher configuration is a mutable object that is passed to the cypher language extension.
Much like how the schema based completions work in the official sql language extension.
https://github.com/codemirror/lang-sql/blob/4b7b2564dff7cdb1a15f8ccd08142f2cc8a0006f/src/sql.ts#L178C17-L178C18
*/
this.schemaRef.current.schema = this.props.schema;
this.schemaRef.current.lint = this.props.lint;
}
componentWillUnmount(): void {
this.editorView.current?.destroy();
cleanupWorkers();
}
render(): React.ReactNode {
const { className, theme } = this.props;
const themeClass =
typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';
return (
<div
ref={this.editorContainer}
className={`${themeClass}${className ? ` ${className}` : ''}`}
/>
);
}
}