feat: adding in some editor events for monaco (#15854)

pull/15857/head
Alex Boatwright 2019-11-12 10:03:07 -08:00 committed by GitHub
parent 291f7b3e19
commit 118721a61d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -6,17 +6,51 @@ import MonacoEditor from 'react-monaco-editor'
import {tokenizeFlux} from 'src/external/monaco.fluxLang'
import {addFluxTheme} from 'src/external/monaco.fluxTheme'
import {addSnippets} from 'src/external/monaco.fluxCompletions'
import {OnChangeScript} from 'src/types/flux'
interface Position {
line: number
ch: number
}
interface Props {
script: string
onChangeScript: OnChangeScript
onSubmitScript?: () => void
onCursorChange?: (position: Position) => void
}
const FluxEditorMonaco: FC<Props> = ({script}) => {
const FluxEditorMonaco: FC<Props> = props => {
const editorWillMount = monaco => {
tokenizeFlux(monaco)
addFluxTheme(monaco)
addSnippets(monaco)
}
const editorDidMount = editor => {
editor.onDidChangeCursorPosition(evt => {
const {position} = evt
const {onCursorChange} = props
const pos = {
line: position.lineNumber,
ch: position.column,
}
if (onCursorChange) {
onCursorChange(pos)
}
})
editor.onKeyUp(evt => {
const {ctrlKey, code} = evt
const {onSubmitScript} = props
if (ctrlKey && code === 'Enter') {
if (onSubmitScript) {
onSubmitScript()
}
}
})
}
const {script, onChangeScript} = props
return (
<div className="time-machine-editor" data-testid="flux-editor">
@ -26,7 +60,9 @@ const FluxEditorMonaco: FC<Props> = ({script}) => {
language="flux"
theme="vs-light"
value={script}
onChange={onChangeScript}
editorWillMount={editorWillMount}
editorDidMount={editorDidMount}
/>
</div>
)

View File

@ -58,7 +58,14 @@ class TimeMachineFluxEditor extends PureComponent<Props, State> {
if (ENABLE_MONACO) {
const FluxMonacoEditor = require('src/shared/components/FluxMonacoEditor')
.default
return <FluxMonacoEditor script={activeQueryText} />
return (
<FluxMonacoEditor
script={activeQueryText}
onChangeScript={onSetActiveQueryText}
onSubmitScript={onSubmitQueries}
onCursorChange={this.handleCursorPosition}
/>
)
}
return (
<FluxEditor