Skip to content

Commit

Permalink
Enable enter for autocompletions in the command palette KCL input (#4896
Browse files Browse the repository at this point in the history
)

* Enable enter for autocompletions in the command palette KCL input

* Oops I commented out code for the variable name input
Thanks for the catch @pierremtb

---------

Co-authored-by: Pierre Jacquier <pierrejacquier39@gmail.com>
  • Loading branch information
franknoirot and pierremtb authored Jan 2, 2025
1 parent 579ab23 commit 9ae025d
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/components/CommandBar/CommandBarKclInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Completion } from '@codemirror/autocomplete'
import { EditorView, ViewUpdate } from '@codemirror/view'
import {
closeBrackets,
closeBracketsKeymap,
Completion,
completionKeymap,
completionStatus,
} from '@codemirror/autocomplete'
import { EditorView, keymap, ViewUpdate } from '@codemirror/view'
import { CustomIcon } from 'components/CustomIcon'
import { useCommandsContext } from 'hooks/useCommandsContext'
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
Expand Down Expand Up @@ -95,6 +101,7 @@ function CommandBarKclInput({
label: v.key,
detail: String(roundOff(v.value as number)),
}))
const varMentionsExtension = varMentions(varMentionData)

const { setContainer } = useCodeMirror({
container: editorRef.current,
Expand All @@ -112,23 +119,40 @@ function CommandBarKclInput({
? getSystemTheme()
: settings.context.app.theme.current,
extensions: [
EditorView.domEventHandlers({
keydown: (event) => {
if (event.key === 'Backspace' && value === '') {
event.preventDefault()
stepBack()
} else if (event.key === 'Enter') {
event.preventDefault()
handleSubmit()
}
},
}),
varMentions(varMentionData),
varMentionsExtension,
EditorView.updateListener.of((vu: ViewUpdate) => {
if (vu.docChanged) {
setValue(vu.state.doc.toString())
}
}),
closeBrackets(),
keymap.of([
...closeBracketsKeymap,
...completionKeymap,
{
key: 'Enter',
run: (editor) => {
// Only submit if there is no completion active
if (completionStatus(editor.state) === null) {
handleSubmit()
return true
} else {
return false
}
},
},
{
key: 'Backspace',
run: (editor) => {
// Only step back if the editor is empty
if (editor.state.doc.toString() === '') {
stepBack()
return true
}
return false
},
},
]),
],
})

Expand Down Expand Up @@ -227,7 +251,7 @@ function CommandBarKclInput({
}
}}
onKeyUp={(e) => {
if (e.key === 'Enter') {
if (e.key === 'Enter' && canSubmit) {
handleSubmit()
}
}}
Expand Down

0 comments on commit 9ae025d

Please sign in to comment.