Skip to content

Commit da85657

Browse files
authored
Merge branch 'main' into forward-http-headers-from-grafana-option
2 parents 2f69b38 + 409f765 commit da85657

File tree

2 files changed

+23
-89
lines changed

2 files changed

+23
-89
lines changed

src/components/SqlEditor.test.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React from 'react';
2-
import { render, screen, waitFor } from '@testing-library/react';
3-
import userEvent from '@testing-library/user-event';
2+
import { render, screen } from '@testing-library/react';
43
import '@testing-library/jest-dom';
54
import { SqlEditor } from './SqlEditor';
65
import * as ui from '@grafana/ui';
76
import { mockDatasource } from '__mocks__/datasource';
87
import { EditorType } from 'types/sql';
9-
import { Components } from 'selectors';
108

119
jest.mock('@grafana/ui', () => ({
1210
...jest.requireActual<typeof ui>('@grafana/ui'),
@@ -35,21 +33,4 @@ describe('SQL Editor', () => {
3533
);
3634
expect(screen.queryByText(rawSql)).toBeInTheDocument();
3735
});
38-
it('Should Expand Query', async () => {
39-
const onChange = jest.fn();
40-
const onRunQuery = jest.fn();
41-
const result = await waitFor(() =>
42-
render(
43-
<SqlEditor
44-
query={{ pluginVersion: '', rawSql: 'test', refId: 'A', editorType: EditorType.SQL }}
45-
onChange={onChange}
46-
onRunQuery={onRunQuery}
47-
datasource={mockDatasource}
48-
/>
49-
));
50-
51-
expect(result.queryByText('test')).toBeInTheDocument();
52-
await userEvent.click(result.getByTestId(Components.QueryEditor.CodeEditor.Expand));
53-
expect(onChange).toHaveBeenCalledTimes(0); // TODO: codeEditor isn't mounting and wont call onChange
54-
});
5536
});

src/components/SqlEditor.tsx

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22
import { CoreApp, QueryEditorProps } from '@grafana/data';
3-
import { CodeEditor } from '@grafana/ui';
3+
import { CodeEditor, monacoTypes } from '@grafana/ui';
44
import { Datasource } from 'data/CHDatasource';
55
import { registerSQL, Range, Fetcher } from './sqlProvider';
66
import { CHConfig } from 'types/config';
77
import { CHQuery, EditorType, CHSqlQuery } from 'types/sql';
88
import { styles } from 'styles';
99
import { fetchSuggestions, Schema } from './suggestions';
10-
import { selectors } from 'selectors';
1110
import { validate } from 'data/validate';
1211
import { mapQueryTypeToGrafanaFormat } from 'data/utils';
1312
import { QueryType } from 'types/queryBuilder';
@@ -16,22 +15,24 @@ import { pluginVersion } from 'utils/version';
1615

1716
type SqlEditorProps = QueryEditorProps<Datasource, CHQuery, CHConfig>;
1817

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();
2331
}
2432

2533
export const SqlEditor = (props: SqlEditorProps) => {
26-
const defaultHeight = '150px';
2734
const { app, query, onChange, datasource } = props;
2835
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-
});
3536
const queryType = sqlQuery.queryType || QueryType.Table;
3637

3738
const saveChanges = (changes: Partial<CHSqlQuery>) => {
@@ -40,31 +41,8 @@ export const SqlEditor = (props: SqlEditorProps) => {
4041
pluginVersion,
4142
editorType: EditorType.SQL,
4243
format: mapQueryTypeToGrafanaFormat(changes.queryType || queryType),
43-
...changes
44+
...changes,
4445
});
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 });
6846
};
6947

7048
const schema: Schema = {
@@ -101,60 +79,35 @@ export const SqlEditor = (props: SqlEditorProps) => {
10179

10280
const handleMount = (editor: any) => {
10381
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);
11183
editor.onKeyUp((e: any) => {
11284
if (datasource.settings.jsonData.validateSql) {
11385
const sql = editor.getValue();
11486
validateSql(sql, editor.getModel(), me);
11587
}
11688
});
117-
setCodeEditor(editor);
11889
};
11990

12091
return (
12192
<>
12293
{/* 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 && (
12495
<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 />
12697
</div>
127-
}
98+
)}
12899
<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>
136100
<CodeEditor
137101
aria-label="SQL Editor"
138-
height={expand.height}
139102
language="sql"
140103
value={query.rawSql}
141-
onSave={sql => saveChanges({ rawSql: sql })}
104+
onSave={(sql) => saveChanges({ rawSql: sql })}
142105
showMiniMap={false}
143106
showLineNumbers={true}
144-
onBlur={sql => saveChanges({ rawSql: sql })}
107+
onBlur={(sql) => saveChanges({ rawSql: sql })}
145108
onEditorDidMount={(editor: any) => handleMount(editor)}
146109
/>
147110
</div>
148111
</>
149112
);
150113
};
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

Comments
 (0)