@fc-components/monaco-editor 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -17
- package/dist/monaco-editor.cjs.development.js +45 -29
- package/dist/monaco-editor.cjs.development.js.map +1 -1
- package/dist/monaco-editor.cjs.production.min.js +1 -1
- package/dist/monaco-editor.cjs.production.min.js.map +1 -1
- package/dist/monaco-editor.esm.js +45 -29
- package/dist/monaco-editor.esm.js.map +1 -1
- package/dist/promql/completion/DataProvider.d.ts +1 -0
- package/dist/promql/index.d.ts +2 -0
- package/dist/promql/types.d.ts +1 -0
- package/package.json +4 -1
- package/src/promql/completion/DataProvider.ts +2 -0
- package/src/promql/completion/completions.ts +10 -1
- package/src/promql/index.tsx +29 -25
- package/src/promql/types.ts +1 -0
- package/src/promql/util.ts +1 -1
|
@@ -13,6 +13,7 @@ export declare class DataProvider {
|
|
|
13
13
|
metrics: string[];
|
|
14
14
|
labelKeys: string[];
|
|
15
15
|
metricsMetadata?: PromMetricsMetadata;
|
|
16
|
+
durationVariablesCompletion: boolean;
|
|
16
17
|
constructor(params: DataProviderParams);
|
|
17
18
|
getVariablesNames(): string[];
|
|
18
19
|
setVariablesNames(variablesNames: string[]): void;
|
package/dist/promql/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ interface PromQLEditorProps {
|
|
|
5
5
|
theme?: 'light' | 'dark';
|
|
6
6
|
value?: string;
|
|
7
7
|
placeholder?: string;
|
|
8
|
+
enableAutocomplete?: boolean;
|
|
9
|
+
durationVariablesCompletion?: boolean;
|
|
8
10
|
interpolateString?: (query: string) => string;
|
|
9
11
|
onChange?: (value: string) => void;
|
|
10
12
|
onShiftEnter?: (value: string) => void;
|
package/dist/promql/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -30,6 +30,7 @@ export class DataProvider {
|
|
|
30
30
|
metrics: string[];
|
|
31
31
|
labelKeys: string[];
|
|
32
32
|
metricsMetadata?: PromMetricsMetadata;
|
|
33
|
+
durationVariablesCompletion: boolean;
|
|
33
34
|
|
|
34
35
|
constructor(params: DataProviderParams) {
|
|
35
36
|
this.inputInRange = '';
|
|
@@ -44,6 +45,7 @@ export class DataProvider {
|
|
|
44
45
|
if (params.variablesNames) {
|
|
45
46
|
this.variablesNames = [...params.variablesNames];
|
|
46
47
|
}
|
|
48
|
+
this.durationVariablesCompletion = params.durationVariablesCompletion ?? true;
|
|
47
49
|
if (params.request) {
|
|
48
50
|
this.customRequest = params.request;
|
|
49
51
|
}
|
|
@@ -61,7 +61,13 @@ async function getAllFunctionsAndMetricNamesCompletions(dataProvider: DataProvid
|
|
|
61
61
|
return [...FUNCTION_COMPLETIONS, ...metricNames];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
const DURATION_COMPLETIONS: Completion[] = ['
|
|
64
|
+
const DURATION_COMPLETIONS: Completion[] = ['1m', '5m', '10m', '30m', '1h', '1d'].map((text) => ({
|
|
65
|
+
type: 'DURATION',
|
|
66
|
+
label: text,
|
|
67
|
+
insertText: text,
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
const DURATION_VARIABLES_COMPLETIONS: Completion[] = ['$__interval', '$__range', '$__rate_interval'].map((text) => ({
|
|
65
71
|
type: 'DURATION',
|
|
66
72
|
label: text,
|
|
67
73
|
insertText: text,
|
|
@@ -165,6 +171,9 @@ async function getLabelValuesForMetricCompletions(
|
|
|
165
171
|
export function getCompletions(situation: Situation, dataProvider: DataProvider): Promise<Completion[]> {
|
|
166
172
|
switch (situation.type) {
|
|
167
173
|
case 'IN_DURATION':
|
|
174
|
+
if (dataProvider.durationVariablesCompletion) {
|
|
175
|
+
return Promise.resolve(DURATION_VARIABLES_COMPLETIONS.concat(DURATION_COMPLETIONS));
|
|
176
|
+
}
|
|
168
177
|
return Promise.resolve(DURATION_COMPLETIONS);
|
|
169
178
|
case 'IN_FUNCTION':
|
|
170
179
|
return getAllFunctionsAndMetricNamesCompletions(dataProvider);
|
package/src/promql/index.tsx
CHANGED
|
@@ -18,6 +18,8 @@ interface PromQLEditorProps {
|
|
|
18
18
|
theme?: 'light' | 'dark';
|
|
19
19
|
value?: string;
|
|
20
20
|
placeholder?: string;
|
|
21
|
+
enableAutocomplete?: boolean;
|
|
22
|
+
durationVariablesCompletion?: boolean;
|
|
21
23
|
interpolateString?: (query: string) => string;
|
|
22
24
|
onChange?: (value: string) => void;
|
|
23
25
|
onShiftEnter?: (value: string) => void;
|
|
@@ -68,7 +70,7 @@ const getStyles = (placeholder?: string) => {
|
|
|
68
70
|
|
|
69
71
|
export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
|
|
70
72
|
const id = uuidv4();
|
|
71
|
-
const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, onChange, onShiftEnter, onBlur } = props;
|
|
73
|
+
const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur } = props;
|
|
72
74
|
const autocompleteDisposeFun = useRef<(() => void) | null>(null);
|
|
73
75
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
74
76
|
const dataProviderRef = useRef<DataProvider | null>(null);
|
|
@@ -106,33 +108,36 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
|
|
|
106
108
|
isEditorFocused.set(true);
|
|
107
109
|
});
|
|
108
110
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
111
|
+
if (enableAutocomplete) {
|
|
112
|
+
const dataProvider = new DataProvider({
|
|
113
|
+
url: props.url,
|
|
114
|
+
lookbackInterval: props.lookbackInterval,
|
|
115
|
+
variablesNames: props.variablesNames,
|
|
116
|
+
durationVariablesCompletion: props.durationVariablesCompletion,
|
|
117
|
+
request: props.request,
|
|
118
|
+
httpMethod: props.httpMethod,
|
|
119
|
+
apiPrefix: props.apiPrefix,
|
|
120
|
+
httpErrorHandler: props.httpErrorHandler,
|
|
121
|
+
});
|
|
122
|
+
dataProviderRef.current = dataProvider;
|
|
123
|
+
dataProvider.start();
|
|
120
124
|
|
|
121
|
-
|
|
125
|
+
const completionProvider = getCompletionProvider(monaco, dataProvider);
|
|
122
126
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
127
|
+
const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
|
|
128
|
+
...completionProvider,
|
|
129
|
+
provideCompletionItems: (model, position, context, token) => {
|
|
130
|
+
if (editor.getModel()?.id !== model.id) {
|
|
131
|
+
return { suggestions: [] };
|
|
132
|
+
}
|
|
133
|
+
return completionProvider.provideCompletionItems(model, position, context, token);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
132
136
|
|
|
133
|
-
|
|
137
|
+
const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
|
|
134
138
|
|
|
135
|
-
|
|
139
|
+
autocompleteDisposeFun.current = dispose;
|
|
140
|
+
}
|
|
136
141
|
|
|
137
142
|
const updateElementHeight = () => {
|
|
138
143
|
const containerDiv = containerRef.current;
|
|
@@ -158,7 +163,6 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
|
|
|
158
163
|
editor.addCommand(
|
|
159
164
|
monaco.KeyMod.Shift | monaco.KeyCode.Enter,
|
|
160
165
|
() => {
|
|
161
|
-
console.log(22222);
|
|
162
166
|
onShiftEnter?.(editor.getValue());
|
|
163
167
|
},
|
|
164
168
|
'isEditorFocused' + id,
|
package/src/promql/types.ts
CHANGED
package/src/promql/util.ts
CHANGED
|
@@ -25,5 +25,5 @@ export function makeSelector(metricName: string, labels?: Label[], labelName?: s
|
|
|
25
25
|
return `${label.name}${label.op}"${escapeLabelValueInExactSelector(label.value)}"`;
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
return
|
|
28
|
+
return `${metricName}{${allLabelTexts.join(',')}}`;
|
|
29
29
|
}
|