Skip to content

Commit 7961718

Browse files
authored
Merge pull request #16 from grafana/ismail/fix-slate-issues
Fix slate package related issues
2 parents af8bdc0 + b3c7c3e commit 7961718

File tree

7 files changed

+298
-1127
lines changed

7 files changed

+298
-1127
lines changed

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
"@types/jest": "29.5.5",
3838
"@types/lodash": "4.14.199",
3939
"@types/node": "20.8.2",
40+
"@types/ol-ext": "npm:@siedlerchr/types-ol-ext@3.2.0",
4041
"@types/pluralize": "0.0.30",
4142
"@types/prismjs": "1.26.0",
4243
"@types/react": "18.2.15",
4344
"@types/react-beautiful-dnd": "13.1.4",
4445
"@types/react-dom": "18.2.7",
4546
"@types/react-highlight-words": "0.16.5",
4647
"@types/react-redux": "7.1.25",
48+
"@types/react-window": "1.8.7",
4749
"@types/semver": "7.5.0",
4850
"@types/slate": "0.47.11",
4951
"@types/slate-plain-serializer": "0.7.2",
@@ -90,12 +92,12 @@
9092
"dependencies": {
9193
"@emotion/css": "11.11.2",
9294
"@grafana/aws-sdk": "0.1.3",
93-
"@grafana/data": "10.2.0-nightly.6184bf21",
95+
"@grafana/data": "10.2.0-nightly.e365200c",
9496
"@grafana/experimental": "1.7.3",
9597
"@grafana/faro-web-sdk": "1.2.1",
96-
"@grafana/runtime": "10.2.0-nightly.6184bf21",
97-
"@grafana/schema": "10.2.0-nightly.6184bf21",
98-
"@grafana/ui": "10.2.0-nightly.6184bf21",
98+
"@grafana/runtime": "10.2.0-nightly.e365200c",
99+
"@grafana/schema": "10.2.0-nightly.e365200c",
100+
"@grafana/ui": "10.2.0-nightly.e365200c",
99101
"@leeoniya/ufuzzy": "1.0.8",
100102
"@lezer/common": "1.1.0",
101103
"@lezer/highlight": "1.1.3",
@@ -119,9 +121,9 @@
119121
"react-window": "1.8.9",
120122
"rxjs": "7.8.1",
121123
"semver": "7.5.4",
122-
"slate": "0.94.1",
124+
"slate": "0.47.9",
123125
"slate-plain-serializer": "0.7.13",
124-
"slate-react": "0.99.0",
126+
"slate-react": "0.22.10",
125127
"uuid": "^9.0.1",
126128
"whatwg-fetch": "3.6.2"
127129
},
@@ -130,5 +132,9 @@
130132
"prettier@3.0.0": {
131133
"unplugged": true
132134
}
135+
},
136+
"resolutions": {
137+
"@types/slate": "0.47.11",
138+
"slate-dev-environment@^0.2.2": "patch:slate-dev-environment@npm:0.2.5#.yarn/patches/slate-dev-environment-npm-0.2.5-9aeb7da7b5.patch"
133139
}
134140
}

src/components/PromQueryField.tsx

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
11
import { cx } from '@emotion/css';
22
import { isDataFrame, QueryEditorProps, QueryHint, TimeRange, toLegacyResponseData } from '@grafana/data';
33
import { reportInteraction } from '@grafana/runtime';
4-
import {
5-
BracesPlugin,
6-
clearButtonStyles,
7-
DOMUtil,
8-
Icon,
9-
SlatePrism,
10-
SuggestionsState,
11-
Themeable2,
12-
TypeaheadInput,
13-
TypeaheadOutput,
14-
withTheme2,
15-
} from '@grafana/ui';
16-
import { LocalStorageValueProvider } from 'app/core/components/LocalStorageValueProvider';
4+
import { clearButtonStyles, Icon, Themeable2, withTheme2 } from '@grafana/ui';
5+
import React, { ReactNode } from 'react';
6+
7+
import { PrometheusDatasource } from '../datasource';
8+
import { LocalStorageValueProvider } from '../gcopypaste/app/core/components/LocalStorageValueProvider';
179
import {
1810
CancelablePromise,
1911
isCancelablePromiseRejection,
2012
makePromiseCancelable,
21-
} from 'app/core/utils/CancelablePromise';
22-
import { LanguageMap, languages as prismLanguages } from 'prismjs';
23-
import React, { ReactNode } from 'react';
24-
import { Plugin } from 'slate';
25-
import { Editor } from 'slate-react';
26-
27-
import { PrometheusDatasource } from '../datasource';
13+
} from '../gcopypaste/app/core/utils/CancelablePromise';
2814
import { roundMsToMin } from '../language_utils';
2915
import { PromOptions, PromQuery } from '../types';
3016

3117
import { PrometheusMetricsBrowser } from './PrometheusMetricsBrowser';
3218
import { MonacoQueryFieldWrapper } from './monaco-query-field/MonacoQueryFieldWrapper';
3319

34-
export const RECORDING_RULES_GROUP = '__recording_rules__';
3520
const LAST_USED_LABELS_KEY = 'grafana.datasources.prometheus.browser.labels';
3621

3722
function getChooserText(metricsLookupDisabled: boolean, hasSyntax: boolean, hasMetrics: boolean) {
@@ -50,33 +35,6 @@ function getChooserText(metricsLookupDisabled: boolean, hasSyntax: boolean, hasM
5035
return 'Metrics browser';
5136
}
5237

53-
export function willApplySuggestion(suggestion: string, { typeaheadContext, typeaheadText }: SuggestionsState): string {
54-
// Modify suggestion based on context
55-
switch (typeaheadContext) {
56-
case 'context-labels': {
57-
const nextChar = DOMUtil.getNextCharacter();
58-
if (!nextChar || nextChar === '}' || nextChar === ',') {
59-
suggestion += '=';
60-
}
61-
break;
62-
}
63-
64-
case 'context-label-values': {
65-
// Always add quotes and remove existing ones instead
66-
if (!typeaheadText.match(/^(!?=~?"|")/)) {
67-
suggestion = `"${suggestion}`;
68-
}
69-
if (DOMUtil.getNextCharacter() !== '"') {
70-
suggestion = `${suggestion}"`;
71-
}
72-
break;
73-
}
74-
75-
default:
76-
}
77-
return suggestion;
78-
}
79-
8038
interface PromQueryFieldProps extends QueryEditorProps<PrometheusDatasource, PromQuery, PromOptions>, Themeable2 {
8139
ExtraFieldElement?: ReactNode;
8240
'data-testid'?: string;
@@ -89,23 +47,11 @@ interface PromQueryFieldState {
8947
}
9048

9149
class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryFieldState> {
92-
plugins: Array<Plugin<Editor>>;
9350
declare languageProviderInitializationPromise: CancelablePromise<any>;
9451

9552
constructor(props: PromQueryFieldProps, context: React.Context<any>) {
9653
super(props, context);
9754

98-
this.plugins = [
99-
BracesPlugin(),
100-
SlatePrism(
101-
{
102-
onlyIn: (node: any) => node.type === 'code_block',
103-
getSyntax: (node: any) => 'promql',
104-
},
105-
{ ...(prismLanguages as LanguageMap), promql: this.props.datasource.languageProvider.syntax }
106-
),
107-
];
108-
10955
this.state = {
11056
labelBrowserVisible: false,
11157
syntaxLoaded: false,
@@ -254,26 +200,6 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
254200
this.setState({ syntaxLoaded: true });
255201
};
256202

257-
onTypeahead = async (typeahead: TypeaheadInput): Promise<TypeaheadOutput> => {
258-
const {
259-
datasource: { languageProvider },
260-
} = this.props;
261-
262-
if (!languageProvider) {
263-
return { suggestions: [] };
264-
}
265-
266-
const { history } = this.props;
267-
const { prefix, text, value, wrapperClasses, labelKey } = typeahead;
268-
269-
const result = await languageProvider.provideCompletionItems(
270-
{ text, value, prefix, wrapperClasses, labelKey },
271-
{ history }
272-
);
273-
274-
return result;
275-
};
276-
277203
render() {
278204
const {
279205
datasource,

src/datasource.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ import {
3131
FetchError,
3232
FetchResponse,
3333
getBackendSrv,
34+
getTemplateSrv,
3435
isFetchError,
36+
TemplateSrv,
3537
toDataQueryResponse,
3638
} from '@grafana/runtime';
37-
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
3839
import { cloneDeep, defaults } from 'lodash';
3940
import { forkJoin, lastValueFrom, merge, Observable, of, OperatorFunction, pipe, throwError } from 'rxjs';
4041
import { catchError, filter, map, tap } from 'rxjs/operators';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://github.com/facebook/react/issues/5465
2+
3+
export interface CancelablePromise<T> {
4+
promise: Promise<T>;
5+
cancel: () => void;
6+
}
7+
8+
export interface CancelablePromiseRejection {
9+
isCanceled: boolean;
10+
}
11+
12+
export function isCancelablePromiseRejection(promise: unknown): promise is CancelablePromiseRejection {
13+
return typeof promise === 'object' && promise !== null && 'isCanceled' in promise;
14+
}
15+
16+
export const makePromiseCancelable = <T>(promise: Promise<T>): CancelablePromise<T> => {
17+
let hasCanceled_ = false;
18+
19+
const wrappedPromise = new Promise<T>((resolve, reject) => {
20+
const canceledPromiseRejection: CancelablePromiseRejection = { isCanceled: true };
21+
promise.then((val) => (hasCanceled_ ? reject(canceledPromiseRejection) : resolve(val)));
22+
promise.catch((error) => (hasCanceled_ ? reject(canceledPromiseRejection) : reject(error)));
23+
});
24+
25+
return {
26+
promise: wrappedPromise,
27+
cancel() {
28+
hasCanceled_ = true;
29+
},
30+
};
31+
};

0 commit comments

Comments
 (0)