Skip to content

Commit 2cb4bf4

Browse files
authored
Merge pull request #421 from RedisInsight/bugfix/RI-2576_RI-2579
#RI-2576 - add indents for dedicated editor
2 parents 450ce6c + 92d742a commit 2cb4bf4

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

redisinsight/ui/src/components/query/DedicatedEditor/DedicatedEditor.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react'
1+
import React, { useEffect, useRef, useState } from 'react'
22
import { compact, findIndex } from 'lodash'
33
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api'
44
import MonacoEditor, { monaco } from 'react-monaco-editor'
@@ -29,7 +29,7 @@ import { getCypherMonarchTokensProvider } from 'uiSrc/utils/monaco/cypher/monarc
2929
import styles from './styles.module.scss'
3030

3131
export interface Props {
32-
value: string
32+
query: string
3333
lang: string
3434
onSubmit: (query?: string) => void
3535
onCancel: () => void
@@ -47,11 +47,14 @@ const langs: MonacoSyntaxLang = {
4747
}
4848
}
4949
let decorations: string[] = []
50+
const notCommandRegEx = /^\s|\/\//
5051

5152
const DedicatedEditor = (props: Props) => {
52-
const { width, value = '', lang, onCancel, onSubmit } = props
53+
const { width, query = '', lang, onCancel, onSubmit } = props
5354
const selectedLang = langs[lang]
5455
let contribution: Nullable<ISnippetController> = null
56+
57+
const [value, setValue] = useState<string>(query)
5558
const monacoObjects = useRef<Nullable<IEditorMount>>(null)
5659
let disposeCompletionItemProvider = () => {}
5760

@@ -67,7 +70,6 @@ const DedicatedEditor = (props: Props) => {
6770
if (!monacoObjects.current) return
6871
const commands = value.split('\n')
6972
const { monaco, editor } = monacoObjects.current
70-
const notCommandRegEx = /^\s|\/\//
7173

7274
const newDecorations = compact(commands.map((command, index) => {
7375
if (!command || notCommandRegEx.test(command)) return null
@@ -78,10 +80,7 @@ const DedicatedEditor = (props: Props) => {
7880
)
7981
}))
8082

81-
decorations = editor.deltaDecorations(
82-
decorations,
83-
newDecorations
84-
)
83+
decorations = editor.deltaDecorations(decorations, newDecorations)
8584
}, [value])
8685

8786
const handleKeyDown = (e: React.KeyboardEvent) => {
@@ -92,7 +91,11 @@ const DedicatedEditor = (props: Props) => {
9291

9392
const handleSubmit = () => {
9493
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 || '')
9699
}
97100

98101
const onKeyDownMonaco = (e: monacoEditor.IKeyboardEvent) => {
@@ -194,6 +197,7 @@ const DedicatedEditor = (props: Props) => {
194197
<MonacoEditor
195198
language={selectedLang.id || MonacoLanguage.Cypher}
196199
value={value}
200+
onChange={setValue}
197201
options={options}
198202
className={`${lang}-editor`}
199203
editorDidMount={editorDidMount}

redisinsight/ui/src/components/query/Query/Query.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ const Query = (props: Props) => {
457457
{isDedicatedEditorOpen && (
458458
<DedicatedEditor
459459
lang={syntaxCommand.current.lang}
460-
value={selectedArg.current.replace(aroundQuotesRegExp, '')}
460+
query={selectedArg.current.replace(aroundQuotesRegExp, '')}
461461
onSubmit={updateArgFromDedicatedEditor}
462462
onCancel={onCancelDedicatedEditor}
463463
width={input?.current?.scrollWidth || 300}

0 commit comments

Comments
 (0)