1
- import React , { useEffect , useRef } from 'react'
1
+ import React , { useEffect , useRef , useState } from 'react'
2
2
import { compact , findIndex } from 'lodash'
3
3
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api'
4
4
import MonacoEditor , { monaco } from 'react-monaco-editor'
@@ -29,7 +29,7 @@ import { getCypherMonarchTokensProvider } from 'uiSrc/utils/monaco/cypher/monarc
29
29
import styles from './styles.module.scss'
30
30
31
31
export interface Props {
32
- value : string
32
+ query : string
33
33
lang : string
34
34
onSubmit : ( query ?: string ) => void
35
35
onCancel : ( ) => void
@@ -47,11 +47,14 @@ const langs: MonacoSyntaxLang = {
47
47
}
48
48
}
49
49
let decorations : string [ ] = [ ]
50
+ const notCommandRegEx = / ^ \s | \/ \/ /
50
51
51
52
const DedicatedEditor = ( props : Props ) => {
52
- const { width, value = '' , lang, onCancel, onSubmit } = props
53
+ const { width, query = '' , lang, onCancel, onSubmit } = props
53
54
const selectedLang = langs [ lang ]
54
55
let contribution : Nullable < ISnippetController > = null
56
+
57
+ const [ value , setValue ] = useState < string > ( query )
55
58
const monacoObjects = useRef < Nullable < IEditorMount > > ( null )
56
59
let disposeCompletionItemProvider = ( ) => { }
57
60
@@ -67,7 +70,6 @@ const DedicatedEditor = (props: Props) => {
67
70
if ( ! monacoObjects . current ) return
68
71
const commands = value . split ( '\n' )
69
72
const { monaco, editor } = monacoObjects . current
70
- const notCommandRegEx = / ^ \s | \/ \/ /
71
73
72
74
const newDecorations = compact ( commands . map ( ( command , index ) => {
73
75
if ( ! command || notCommandRegEx . test ( command ) ) return null
@@ -78,10 +80,7 @@ const DedicatedEditor = (props: Props) => {
78
80
)
79
81
} ) )
80
82
81
- decorations = editor . deltaDecorations (
82
- decorations ,
83
- newDecorations
84
- )
83
+ decorations = editor . deltaDecorations ( decorations , newDecorations )
85
84
} , [ value ] )
86
85
87
86
const handleKeyDown = ( e : React . KeyboardEvent ) => {
@@ -92,7 +91,11 @@ const DedicatedEditor = (props: Props) => {
92
91
93
92
const handleSubmit = ( ) => {
94
93
const { editor } = monacoObjects ?. current || { }
95
- onSubmit ( editor ?. getValue ( ) || '' )
94
+ const val = editor ?. getValue ( )
95
+ . split ( '\n' )
96
+ . map ( ( line : string , i : number ) => ( i > 0 && ! notCommandRegEx . test ( line ) ) ? `\t${ line } ` : line )
97
+ . join ( '\n' )
98
+ onSubmit ( val || '' )
96
99
}
97
100
98
101
const onKeyDownMonaco = ( e : monacoEditor . IKeyboardEvent ) => {
@@ -194,6 +197,7 @@ const DedicatedEditor = (props: Props) => {
194
197
< MonacoEditor
195
198
language = { selectedLang . id || MonacoLanguage . Cypher }
196
199
value = { value }
200
+ onChange = { setValue }
197
201
options = { options }
198
202
className = { `${ lang } -editor` }
199
203
editorDidMount = { editorDidMount }
0 commit comments