1
- import React , { useState } from 'react' ;
1
+ import React from 'react' ;
2
2
import { CoreApp , QueryEditorProps } from '@grafana/data' ;
3
- import { CodeEditor } from '@grafana/ui' ;
3
+ import { CodeEditor , monacoTypes } from '@grafana/ui' ;
4
4
import { Datasource } from 'data/CHDatasource' ;
5
5
import { registerSQL , Range , Fetcher } from './sqlProvider' ;
6
6
import { CHConfig } from 'types/config' ;
7
7
import { CHQuery , EditorType , CHSqlQuery } from 'types/sql' ;
8
8
import { styles } from 'styles' ;
9
9
import { fetchSuggestions , Schema } from './suggestions' ;
10
- import { selectors } from 'selectors' ;
11
10
import { validate } from 'data/validate' ;
12
11
import { mapQueryTypeToGrafanaFormat } from 'data/utils' ;
13
12
import { QueryType } from 'types/queryBuilder' ;
@@ -16,22 +15,24 @@ import { pluginVersion } from 'utils/version';
16
15
17
16
type SqlEditorProps = QueryEditorProps < Datasource , CHQuery , CHConfig > ;
18
17
19
- interface Expand {
20
- height : string ;
21
- icon : 'plus' | 'minus' ;
22
- on : boolean ;
18
+ function setupAutoSize ( editor : monacoTypes . editor . IStandaloneCodeEditor ) {
19
+ const container = editor . getDomNode ( ) ;
20
+ const updateHeight = ( ) => {
21
+ if ( container ) {
22
+ const contentHeight = Math . max ( 100 , Math . min ( 1000 , editor . getContentHeight ( ) ) ) ;
23
+ const width = parseInt ( container . style . width , 10 ) ;
24
+ container . style . width = `${ width } px` ;
25
+ container . style . height = `${ contentHeight } px` ;
26
+ editor . layout ( { width, height : contentHeight } ) ;
27
+ }
28
+ } ;
29
+ editor . onDidContentSizeChange ( updateHeight ) ;
30
+ updateHeight ( ) ;
23
31
}
24
32
25
33
export const SqlEditor = ( props : SqlEditorProps ) => {
26
- const defaultHeight = '150px' ;
27
34
const { app, query, onChange, datasource } = props ;
28
35
const sqlQuery = query as CHSqlQuery ;
29
- const [ codeEditor , setCodeEditor ] = useState < any > ( ) ;
30
- const [ expand , setExpand ] = useState < Expand > ( {
31
- height : defaultHeight ,
32
- icon : 'plus' ,
33
- on : sqlQuery . expand || false ,
34
- } ) ;
35
36
const queryType = sqlQuery . queryType || QueryType . Table ;
36
37
37
38
const saveChanges = ( changes : Partial < CHSqlQuery > ) => {
@@ -40,31 +41,8 @@ export const SqlEditor = (props: SqlEditorProps) => {
40
41
pluginVersion,
41
42
editorType : EditorType . SQL ,
42
43
format : mapQueryTypeToGrafanaFormat ( changes . queryType || queryType ) ,
43
- ...changes
44
+ ...changes ,
44
45
} ) ;
45
- }
46
-
47
- const updateExpand = ( expand : Expand ) => {
48
- setExpand ( expand ) ;
49
- saveChanges ( { expand : expand . on } ) ;
50
- }
51
-
52
- const onToggleExpand = ( ) => {
53
- const on = ! expand . on ;
54
- const icon = on ? 'minus' : 'plus' ;
55
-
56
- if ( ! codeEditor ) {
57
- return ;
58
- }
59
- if ( on ) {
60
- codeEditor . expanded = true ;
61
- const height = getEditorHeight ( codeEditor ) ;
62
- updateExpand ( { height : `${ height } px` , on, icon } ) ;
63
- return ;
64
- }
65
-
66
- codeEditor . expanded = false ;
67
- updateExpand ( { height : defaultHeight , icon, on } ) ;
68
46
} ;
69
47
70
48
const schema : Schema = {
@@ -101,60 +79,35 @@ export const SqlEditor = (props: SqlEditorProps) => {
101
79
102
80
const handleMount = ( editor : any ) => {
103
81
const me = registerSQL ( 'chSql' , editor , getSuggestions ) ;
104
- editor . expanded = ( query as CHSqlQuery ) . expand ;
105
- editor . onDidChangeModelDecorations ( ( a : any ) => {
106
- if ( editor . expanded ) {
107
- const height = getEditorHeight ( editor ) ;
108
- updateExpand ( { height : `${ height } px` , on : true , icon : 'minus' } ) ;
109
- }
110
- } ) ;
82
+ setupAutoSize ( editor ) ;
111
83
editor . onKeyUp ( ( e : any ) => {
112
84
if ( datasource . settings . jsonData . validateSql ) {
113
85
const sql = editor . getValue ( ) ;
114
86
validateSql ( sql , editor . getModel ( ) , me ) ;
115
87
}
116
88
} ) ;
117
- setCodeEditor ( editor ) ;
118
89
} ;
119
90
120
91
return (
121
92
< >
122
93
{ /* Only show in explore view where panel can't be manually selected. Dashboard view lets you change the panel. */ }
123
- { app === CoreApp . Explore &&
94
+ { app === CoreApp . Explore && (
124
95
< div className = { 'gf-form ' + styles . QueryEditor . queryType } >
125
- < QueryTypeSwitcher queryType = { queryType } onChange = { queryType => saveChanges ( { queryType } ) } sqlEditor />
96
+ < QueryTypeSwitcher queryType = { queryType } onChange = { ( queryType ) => saveChanges ( { queryType } ) } sqlEditor />
126
97
</ div >
127
- }
98
+ ) }
128
99
< div className = { styles . Common . wrapper } >
129
- < a
130
- onClick = { ( ) => onToggleExpand ( ) }
131
- className = { styles . Common . expand }
132
- data-testid = { selectors . components . QueryEditor . CodeEditor . Expand }
133
- >
134
- < i className = { `fa fa-${ expand . icon } ` } > </ i >
135
- </ a >
136
100
< CodeEditor
137
101
aria-label = "SQL Editor"
138
- height = { expand . height }
139
102
language = "sql"
140
103
value = { query . rawSql }
141
- onSave = { sql => saveChanges ( { rawSql : sql } ) }
104
+ onSave = { ( sql ) => saveChanges ( { rawSql : sql } ) }
142
105
showMiniMap = { false }
143
106
showLineNumbers = { true }
144
- onBlur = { sql => saveChanges ( { rawSql : sql } ) }
107
+ onBlur = { ( sql ) => saveChanges ( { rawSql : sql } ) }
145
108
onEditorDidMount = { ( editor : any ) => handleMount ( editor ) }
146
109
/>
147
110
</ div >
148
111
</ >
149
112
) ;
150
113
} ;
151
-
152
- const getEditorHeight = ( editor : any ) : number | undefined => {
153
- const editorElement = editor . getDomNode ( ) ;
154
- if ( ! editorElement ) {
155
- return ;
156
- }
157
-
158
- const lineCount = editor . getModel ( ) ?. getLineCount ( ) || 1 ;
159
- return editor . getTopForLineNumber ( lineCount + 1 ) + 40 ;
160
- } ;
0 commit comments