@ -1,7 +1,6 @@
// Core Grafana history https://github.com/grafana/grafana/blob/v11.0.0-preview/public/app/plugins/datasource/prometheus/components/monaco-query-field/MonacoQueryField.tsx
import { css } from '@emotion/css' ;
import { parser } from '@prometheus-io/lezer-promql' ;
import { debounce } from 'lodash' ;
import { promLanguageDefinition } from 'monaco-promql' ;
import { useEffect , useRef } from 'react' ;
import { useLatest } from 'react-use' ;
@ -106,13 +105,12 @@ const MonacoQueryField = (props: Props) => {
// we need only one instance of `overrideServices` during the lifetime of the react component
const overrideServicesRef = useRef ( getOverrideServices ( ) ) ;
const containerRef = useRef < HTMLDivElement > ( null ) ;
const { languageProvider , history , onBlur , onRunQuery , initialValue , placeholder , onChange , datasource } = props ;
const { languageProvider , history , onBlur , onRunQuery , initialValue , placeholder , datasource } = props ;
const lpRef = useLatest ( languageProvider ) ;
const historyRef = useLatest ( history ) ;
const onRunQueryRef = useLatest ( onRunQuery ) ;
const onBlurRef = useLatest ( onBlur ) ;
const onChangeRef = useLatest ( onChange ) ;
const autocompleteDisposeFun = useRef < ( ( ) = > void ) | null > ( null ) ;
@ -134,6 +132,8 @@ const MonacoQueryField = (props: Props) => {
ref = { containerRef }
>
< ReactMonacoEditor
// see https://github.com/suren-atoyan/monaco-react/issues/365
saveViewState
overrideServices = { overrideServicesRef . current }
options = { options }
language = "promql"
@ -201,23 +201,6 @@ const MonacoQueryField = (props: Props) => {
editor . onDidContentSizeChange ( updateElementHeight ) ;
updateElementHeight ( ) ;
// Whenever the editor changes, lets save the last value so the next query for this editor will be up-to-date.
// This change is being introduced to fix a bug where you can submit a query via shift+enter:
// If you clicked into another field and haven't un-blurred the active field,
// then the query that is run will be stale, as the reference is only updated
// with the value of the last blurred input.
// This can run quite slowly, so we're debouncing this which should accomplish two things
// 1. Should prevent this function from blocking the current call stack by pushing into the web API callback queue
// 2. Should prevent a bunch of duplicates of this function being called as the user is typing
const updateCurrentEditorValue = debounce ( ( ) = > {
const editorValue = editor . getValue ( ) ;
onChangeRef . current ( editorValue ) ;
} , lpRef . current . datasource . getDebounceTimeInMilliseconds ( ) ) ;
editor . getModel ( ) ? . onDidChangeContent ( ( ) = > {
updateCurrentEditorValue ( ) ;
} ) ;
// handle: shift + enter
// FIXME: maybe move this functionality into CodeEditor?
editor . addCommand (
@ -235,9 +218,8 @@ const MonacoQueryField = (props: Props) => {
command : null ,
} ) ;
/ * S o m e t h i n g i n t h i s c o n f i g u r a t i o n o f m o n a c o d o e s n ' t b u b b l e u p [ m o d ] + K , w h i c h t h e
command palette uses . Pass the event out of monaco manually
* /
// Something in this configuration of monaco doesn't bubble up [mod]+K,
// which the command palette uses. Pass the event out of monaco manually
editor . addCommand ( monaco . KeyMod . CtrlCmd | monaco . KeyCode . KeyK , function ( ) {
global . dispatchEvent ( new KeyboardEvent ( 'keydown' , { key : 'k' , metaKey : true } ) ) ;
} ) ;