|
1 | | -import { FC, useCallback } from 'react'; |
| 1 | +import { FC, useCallback, useRef } from 'react'; |
2 | 2 | import Editor, { EditorProps } from '@monaco-editor/react'; |
3 | 3 | import type * as Monaco from 'monaco-editor'; |
| 4 | +import { tsquery } from '@phenomnomnominal/tsquery'; |
| 5 | +import { sanitizeQuery } from './engine'; |
4 | 6 |
|
5 | | -const QueryEditor: FC<Omit<EditorProps, 'defaultLanguage' | 'theme' | 'options' | 'onMount'>> = (props) => { |
| 7 | +const MONACO_MODEL_MARKER_OWNER = 'tsquery'; |
| 8 | + |
| 9 | +const QueryEditor: FC<Omit<EditorProps, 'defaultLanguage' | 'theme' | 'options' | 'onMount'>> = ({ |
| 10 | + onChange, |
| 11 | + ...rest |
| 12 | +}) => { |
| 13 | + const monacoRef = useRef<typeof Monaco>(); |
| 14 | + const editorRef = useRef<Monaco.editor.IStandaloneCodeEditor>(); |
6 | 15 | const handleQueryMount = useCallback((editor: Monaco.editor.IStandaloneCodeEditor, monaco: typeof Monaco) => { |
| 16 | + monacoRef.current = monaco; |
| 17 | + editorRef.current = editor; |
7 | 18 | monaco.languages.css.cssDefaults.setOptions({ |
8 | 19 | validate: false, |
9 | 20 | }); |
10 | 21 | }, []); |
11 | 22 |
|
| 23 | + const handleQueryChange = useCallback( |
| 24 | + (value: string | undefined, event: Monaco.editor.IModelContentChangedEvent) => { |
| 25 | + const monaco = monacoRef.current; |
| 26 | + const editor = editorRef.current; |
| 27 | + const model = editor?.getModel(); |
| 28 | + if (monaco && model && typeof value === 'string') { |
| 29 | + const sanitizedQuery = sanitizeQuery(value); |
| 30 | + const error = getSyntaxError(sanitizedQuery); |
| 31 | + if (!error) { |
| 32 | + monaco.editor.setModelMarkers(model, MONACO_MODEL_MARKER_OWNER, []); |
| 33 | + } else { |
| 34 | + if (model) { |
| 35 | + monaco.editor.setModelMarkers(model, MONACO_MODEL_MARKER_OWNER, [ |
| 36 | + { |
| 37 | + ...getFullRange(model), |
| 38 | + severity: monaco.MarkerSeverity.Error, |
| 39 | + message: error.message, |
| 40 | + }, |
| 41 | + ]); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (onChange) { |
| 47 | + onChange(value, event); |
| 48 | + } |
| 49 | + }, |
| 50 | + [onChange], |
| 51 | + ); |
| 52 | + |
12 | 53 | return ( |
13 | 54 | <Editor |
14 | 55 | defaultLanguage="css" |
15 | 56 | theme="vs-dark" |
16 | 57 | onMount={handleQueryMount} |
| 58 | + onChange={handleQueryChange} |
17 | 59 | options={{ |
18 | 60 | minimap: { |
19 | 61 | enabled: false, |
20 | 62 | }, |
21 | 63 | }} |
22 | | - {...props} |
| 64 | + {...rest} |
23 | 65 | /> |
24 | 66 | ); |
25 | 67 | }; |
26 | 68 |
|
27 | 69 | export default QueryEditor; |
| 70 | + |
| 71 | +function getSyntaxError(query: string): Error | null { |
| 72 | + try { |
| 73 | + tsquery.parse(query); |
| 74 | + return null; |
| 75 | + } catch (error) { |
| 76 | + if (error instanceof SyntaxError) { |
| 77 | + return error; |
| 78 | + } |
| 79 | + throw error; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function getFullRange(model: Monaco.editor.ITextModel): { |
| 84 | + startLineNumber: number; |
| 85 | + startColumn: number; |
| 86 | + endLineNumber: number; |
| 87 | + endColumn: number; |
| 88 | +} { |
| 89 | + const lastLine = model.getLineCount(); |
| 90 | + return { |
| 91 | + startLineNumber: 0, |
| 92 | + startColumn: 0, |
| 93 | + endLineNumber: lastLine, |
| 94 | + endColumn: model.getLineMaxColumn(lastLine), |
| 95 | + }; |
| 96 | +} |
0 commit comments