Skip to content

Commit a10feb2

Browse files
Merge pull request #142 from RedisInsight/bugfix/RI-1981_Use_Enter_or_Space_exit_snippet_mode
* #RI-1981 - Using enter or space user is still inside of printing command
2 parents ff87281 + 44c89aa commit a10feb2

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { KeyboardShortcut } from 'uiSrc/components'
2727
import { ThemeContext } from 'uiSrc/contexts/themeContext'
2828
import { appRedisCommandsSelector } from 'uiSrc/slices/app/redis-commands'
29+
import { IEditorMount, ISnippetController } from 'uiSrc/pages/workbench/interfaces'
2930

3031
import styles from './styles.module.scss'
3132

@@ -38,15 +39,11 @@ export interface Props {
3839
onKeyDown?: (e: React.KeyboardEvent, script: string) => void;
3940
}
4041

41-
interface IEditorMount {
42-
editor: monacoEditor.editor.IStandaloneCodeEditor
43-
monaco: typeof monacoEditor
44-
}
45-
4642
let decorations: string[] = []
4743

4844
const Query = (props: Props) => {
4945
const { query = '', setQuery, onKeyDown, onSubmit, setQueryEl } = props
46+
let contribution: Nullable<ISnippetController> = null
5047

5148
const {
5249
commandsArray: REDIS_COMMANDS_ARRAY,
@@ -60,6 +57,7 @@ const Query = (props: Props) => {
6057
useEffect(() =>
6158
// componentWillUnmount
6259
() => {
60+
contribution?.dispose?.()
6361
disposeCompletionItemProvider()
6462
disposeSignatureHelpProvider()
6563
},
@@ -124,6 +122,21 @@ const Query = (props: Props) => {
124122
) {
125123
onTriggerParameterHints()
126124
}
125+
126+
if (e.keyCode === monaco.KeyCode.Enter || e.keyCode === monaco.KeyCode.Space) {
127+
onExitSnippetMode()
128+
}
129+
}
130+
131+
const onExitSnippetMode = () => {
132+
if (!monacoObjects.current) return
133+
const { editor } = monacoObjects?.current
134+
135+
if (contribution?.isInSnippet?.()) {
136+
const { lineNumber = 0, column = 0 } = editor?.getPosition() ?? {}
137+
editor.setSelection(new monaco.Selection(lineNumber, column, lineNumber, column))
138+
contribution?.cancel?.()
139+
}
127140
}
128141

129142
const editorDidMount = (
@@ -132,6 +145,10 @@ const Query = (props: Props) => {
132145
) => {
133146
monacoObjects.current = { editor, monaco }
134147

148+
// hack for exit from snippet mode after click Enter until no answer from monaco authors
149+
// https://github.com/microsoft/monaco-editor/issues/2756
150+
contribution = editor.getContribution<ISnippetController>('snippetController2')
151+
135152
editor.focus()
136153
setQueryEl(editor)
137154

@@ -179,11 +196,6 @@ const Query = (props: Props) => {
179196
showIcons: false,
180197
},
181198
lineNumbersMinChars: 4
182-
// fontFamily: 'Inconsolata',
183-
// fontSize: 16,
184-
// minimap: {
185-
// enabled: false,
186-
// },
187199
}
188200

189201
return (

redisinsight/ui/src/pages/workbench/interfaces.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api'
12
import { Maybe } from 'uiSrc/utils'
23
import { IHistoryObject } from 'uiSrc/services/queryHistory'
34
import { CommandExecutionStatus } from 'uiSrc/slices/interfaces/cli'
@@ -11,3 +12,14 @@ export interface WBHistoryObject extends IHistoryObject {
1112
loading?: boolean;
1213
time?: number
1314
}
15+
16+
export interface IEditorMount {
17+
editor: monacoEditor.editor.IStandaloneCodeEditor
18+
monaco: typeof monacoEditor
19+
}
20+
21+
export interface ISnippetController extends monacoEditor.editor.IEditorContribution {
22+
isInSnippet: () => boolean
23+
finish: () => boolean
24+
cancel: () => boolean
25+
}

0 commit comments

Comments
 (0)