Skip to content

Commit 832cf5d

Browse files
committed
fix: new ReactMonacoEditor not support old grafana version.
1 parent c3549d8 commit 832cf5d

File tree

7 files changed

+113
-21
lines changed

7 files changed

+113
-21
lines changed

dist/module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/module.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
],
3939
"screenshots": [],
4040
"version": "2.29",
41-
"updated": "2023-06-28"
41+
"updated": "2023-06-29"
4242
},
4343
"dependencies": {
4444
"grafanaDependency": ">=7.0.0",

src/QueryEditor.tsx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import { QueryEditorProps } from '@grafana/data';
77

88
import { SLSDataSource } from './datasource';
99
import { defaultQuery, SLSDataSourceOptions, SLSQuery } from './types';
10-
import { xColInfoSeries, yColInfoSeries } from './const';
10+
import { version, xColInfoSeries, yColInfoSeries } from './const';
1111
import MonacoQueryField from './SLS-monaco-editor/MonacoQueryField';
12+
import MonacoQueryFieldOld from 'SLS-monaco-editor/MonacoQueryFieldOld';
1213

1314
// const { FormField } = LegacyForms;
1415

1516
type Props = QueryEditorProps<SLSDataSource, SLSQuery, SLSDataSourceOptions>;
1617

1718
export class SLSQueryEditor extends PureComponent<Props> {
19+
1820
onQueryTextChange = (event: ChangeEvent<HTMLInputElement>) => {
1921
const { onChange, query } = this.props;
2022
onChange({ ...query, query: event.target.value });
@@ -56,22 +58,36 @@ export class SLSQueryEditor extends PureComponent<Props> {
5658
<InlineFormLabel width={6} className="query-keyword">
5759
Query
5860
</InlineFormLabel>
59-
{/* <input
60-
className="gf-form-input"
61-
value={query}
62-
onChange={this.onQueryTextChange}
63-
onBlur={this.onQueryTextChange}
64-
style={{ fontFamily: 'Menlo, monospace' }}
65-
></input> */}
66-
<MonacoQueryField
67-
onChange={this.onQueryTextChangeString}
68-
onRunQuery={this.onQueryTextChangeWithRunQuery}
69-
onBlur={this.onQueryTextChangeString}
70-
initialValue={query ?? ''}
71-
languageProvider={{} as any}
72-
history={[]}
73-
placeholder={''}
74-
/>
61+
{version === '' || version.startsWith('8.0') || version.startsWith('8.1') || version.startsWith('8.2') || version.startsWith('7') ? (
62+
// <input
63+
// className="gf-form-input"
64+
// value={query}
65+
// onChange={this.onQueryTextChange}
66+
// onBlur={this.onQueryTextChange}
67+
// style={{ fontFamily: 'Menlo, monospace' }}
68+
// ></input>
69+
<div style={{ width: '100%' }}>
70+
<MonacoQueryFieldOld
71+
onChange={this.onQueryTextChangeString}
72+
onRunQuery={this.onQueryTextChangeWithRunQuery}
73+
onBlur={this.onQueryTextChangeString}
74+
initialValue={query ?? ''}
75+
languageProvider={{} as any}
76+
history={[]}
77+
placeholder={''}
78+
/>
79+
</div>
80+
) : (
81+
<MonacoQueryField
82+
onChange={this.onQueryTextChangeString}
83+
onRunQuery={this.onQueryTextChangeWithRunQuery}
84+
onBlur={this.onQueryTextChangeString}
85+
initialValue={query ?? ''}
86+
languageProvider={{} as any}
87+
history={[]}
88+
placeholder={''}
89+
/>
90+
)}
7591
</div>
7692
<div className="gf-form-inline" style={{ lineHeight: '32px', verticalAlign: 'center' }}>
7793
<InlineField label={'ycol'} labelWidth={12}>
@@ -105,7 +121,7 @@ export class SLSQueryEditor extends PureComponent<Props> {
105121
prefix={<Icon name="x" />}
106122
value={xcol}
107123
onChange={this.onXChange}
108-
label="xcol(time)"
124+
label="xcol"
109125
suffix={
110126
<Tooltip
111127
content={
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Button, CodeEditor, Monaco, MonacoEditor } from '@grafana/ui';
2+
import React, { useCallback, useRef, useState } from 'react';
3+
import { Props } from './MonacoQueryFieldProps';
4+
5+
export class Deferred<T = any> {
6+
resolve?: (reason?: T | PromiseLike<T>) => void;
7+
reject?: (reason?: any) => void;
8+
promise: Promise<T>;
9+
10+
constructor() {
11+
this.resolve = undefined;
12+
this.reject = undefined;
13+
14+
this.promise = new Promise((resolve, reject) => {
15+
this.resolve = resolve as any;
16+
this.reject = reject;
17+
});
18+
Object.freeze(this);
19+
}
20+
}
21+
22+
interface MonacoPromise {
23+
editor: MonacoEditor;
24+
monaco: Monaco;
25+
}
26+
27+
const QueryField: React.FC<Props> = ({ initialValue: query, onChange }) => {
28+
const monacoPromiseRef = useRef<Deferred<MonacoPromise>>();
29+
const [queryHeight, setQueryHeight] = useState<number>(30);
30+
const handleEditorMount = useCallback((editor: MonacoEditor, monaco: Monaco) => {
31+
monacoPromiseRef.current?.resolve?.({ editor, monaco });
32+
}, []);
33+
34+
return (
35+
<div style={{ width: '100%' }}>
36+
<div style={{ width: 'calc(100% - 65px)', display: 'inline-block', fontSize: '14px' }}>
37+
<CodeEditor
38+
value={query}
39+
language="sql"
40+
height={queryHeight}
41+
width="100%"
42+
showMiniMap={false}
43+
onBlur={onChange}
44+
onSave={onChange}
45+
onEditorDidMount={handleEditorMount}
46+
monacoOptions={{ contextmenu: true, codeLens: true, overviewRulerBorder: true }}
47+
/>
48+
</div>
49+
<div style={{ width: '60px', height: '100%', display: 'inline-block', float: 'right' }}>
50+
<Button
51+
icon="angle-up"
52+
variant="secondary"
53+
size="sm"
54+
disabled={queryHeight === 30}
55+
onClick={() => {
56+
setQueryHeight(queryHeight - 30);
57+
}}
58+
/>
59+
<Button
60+
icon="angle-down"
61+
variant="secondary"
62+
size="sm"
63+
disabled={queryHeight === 120}
64+
onClick={() => {
65+
setQueryHeight(queryHeight + 30);
66+
}}
67+
/>
68+
</div>
69+
</div>
70+
);
71+
};
72+
73+
export default QueryField;

src/const.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ export const yColInfoSeries = [
5353
value: '[col1#:#col2] Applicable to bar and SLS metricStore, col1 is the aggregation column, col2 is other columns',
5454
},
5555
];
56+
57+
export const version = (window as any)?.grafanaBootData?.settings?.buildInfo?.version ?? '';

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "@grafana/toolkit/src/config/tsconfig.plugin.json",
33
"include": ["src", "types"],
44
"compilerOptions": {
5+
"jsx": "react",
56
"rootDir": "./src",
67
"baseUrl": "./src",
78
"typeRoots": ["./node_modules/@types"]

0 commit comments

Comments
 (0)