@fc-components/monaco-editor 0.1.5 → 0.1.7
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/dist/monaco-editor.cjs.development.js +75 -67
- 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 +77 -69
- package/dist/monaco-editor.esm.js.map +1 -1
- package/dist/promql/index.d.ts +4 -2
- package/package.json +1 -1
- package/src/promql/index.tsx +98 -75
package/dist/promql/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import type * as monacoTypes from 'monaco-editor/esm/vs/editor/editor.api';
|
|
2
3
|
import type { DataProviderParams } from './types';
|
|
3
4
|
interface PromQLEditorProps {
|
|
4
5
|
size?: 'small' | 'middle' | 'large';
|
|
@@ -11,6 +12,7 @@ interface PromQLEditorProps {
|
|
|
11
12
|
onChange?: (value: string) => void;
|
|
12
13
|
onShiftEnter?: (value: string) => void;
|
|
13
14
|
onBlur?: (value: string) => void;
|
|
15
|
+
editorDidMount?: (editor: monacoTypes.editor.IStandaloneCodeEditor) => void;
|
|
14
16
|
}
|
|
15
|
-
|
|
16
|
-
export
|
|
17
|
+
export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams): React.JSX.Element;
|
|
18
|
+
export {};
|
package/package.json
CHANGED
package/src/promql/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
2
|
import MonacoEditor from 'react-monaco-editor';
|
|
3
3
|
import * as monaco from 'monaco-editor';
|
|
4
4
|
import { promLanguageDefinition } from 'monaco-promql';
|
|
@@ -24,6 +24,7 @@ interface PromQLEditorProps {
|
|
|
24
24
|
onChange?: (value: string) => void;
|
|
25
25
|
onShiftEnter?: (value: string) => void;
|
|
26
26
|
onBlur?: (value: string) => void;
|
|
27
|
+
editorDidMount?: (editor: monacoTypes.editor.IStandaloneCodeEditor) => void;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
const PROMQL_LANG_ID = promLanguageDefinition.id;
|
|
@@ -67,9 +68,9 @@ const getStyles = (placeholder?: string) => {
|
|
|
67
68
|
};
|
|
68
69
|
};
|
|
69
70
|
|
|
70
|
-
export default
|
|
71
|
+
export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
|
|
71
72
|
const id = uuidv4();
|
|
72
|
-
const { size = 'middle', theme = 'light',
|
|
73
|
+
const { size = 'middle', theme = 'light', value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur, editorDidMount } = props;
|
|
73
74
|
const autocompleteDisposeFun = useRef<(() => void) | null>(null);
|
|
74
75
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
75
76
|
const dataProviderRef = useRef<DataProvider | null>(null);
|
|
@@ -77,6 +78,7 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
77
78
|
const styles = getStyles(placeholder);
|
|
78
79
|
const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
|
|
79
80
|
editorRef.current = editor;
|
|
81
|
+
|
|
80
82
|
monaco.editor.defineTheme('n9e-dark', {
|
|
81
83
|
base: 'vs-dark',
|
|
82
84
|
inherit: true,
|
|
@@ -97,37 +99,7 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
97
99
|
isEditorFocused.set(true);
|
|
98
100
|
});
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
const dataProvider = new DataProvider({
|
|
102
|
-
url: props.url,
|
|
103
|
-
lookbackInterval: props.lookbackInterval,
|
|
104
|
-
variablesNames: props.variablesNames,
|
|
105
|
-
durationVariablesCompletion: props.durationVariablesCompletion,
|
|
106
|
-
request: props.request,
|
|
107
|
-
httpMethod: props.httpMethod,
|
|
108
|
-
apiPrefix: props.apiPrefix,
|
|
109
|
-
httpErrorHandler: props.httpErrorHandler,
|
|
110
|
-
});
|
|
111
|
-
dataProviderRef.current = dataProvider;
|
|
112
|
-
dataProvider.start();
|
|
113
|
-
|
|
114
|
-
const completionProvider = getCompletionProvider(monaco, dataProvider);
|
|
115
|
-
|
|
116
|
-
const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
|
|
117
|
-
...completionProvider,
|
|
118
|
-
provideCompletionItems: (model, position, context, token) => {
|
|
119
|
-
if (editor.getModel()?.id !== model.id) {
|
|
120
|
-
return { suggestions: [] };
|
|
121
|
-
}
|
|
122
|
-
return completionProvider.provideCompletionItems(model, position, context, token);
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
|
|
127
|
-
|
|
128
|
-
autocompleteDisposeFun.current = dispose;
|
|
129
|
-
}
|
|
130
|
-
|
|
102
|
+
// set the height of the editor container
|
|
131
103
|
const updateElementHeight = () => {
|
|
132
104
|
const containerDiv = containerRef.current;
|
|
133
105
|
if (containerDiv !== null) {
|
|
@@ -157,34 +129,6 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
157
129
|
'isEditorFocused' + id,
|
|
158
130
|
);
|
|
159
131
|
|
|
160
|
-
if (placeholder) {
|
|
161
|
-
const placeholderDecorators = [
|
|
162
|
-
{
|
|
163
|
-
range: new monaco.Range(1, 1, 1, 1),
|
|
164
|
-
options: {
|
|
165
|
-
className: styles.placeholder,
|
|
166
|
-
isWholeLine: true,
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
];
|
|
170
|
-
|
|
171
|
-
let decorators: string[] = [];
|
|
172
|
-
|
|
173
|
-
const checkDecorators: () => void = () => {
|
|
174
|
-
const model = editor.getModel();
|
|
175
|
-
|
|
176
|
-
if (!model) {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
|
|
181
|
-
decorators = model.deltaDecorations(decorators, newDecorators);
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
checkDecorators();
|
|
185
|
-
editor.onDidChangeModelContent(checkDecorators);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
132
|
editor.onDidChangeModelContent(() => {
|
|
189
133
|
const model = editor.getModel();
|
|
190
134
|
if (model) {
|
|
@@ -199,14 +143,9 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
199
143
|
monaco.editor.setModelMarkers(model, 'owner', markers);
|
|
200
144
|
}
|
|
201
145
|
});
|
|
202
|
-
};
|
|
203
146
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
if (dataProvider) {
|
|
207
|
-
dataProvider.setVariablesNames(variablesNames || []);
|
|
208
|
-
}
|
|
209
|
-
}, [JSON.stringify(variablesNames)]);
|
|
147
|
+
editorDidMount?.(editor);
|
|
148
|
+
};
|
|
210
149
|
|
|
211
150
|
useEffect(() => {
|
|
212
151
|
// 注册 PromQL 语言
|
|
@@ -226,14 +165,98 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
226
165
|
|
|
227
166
|
return () => {
|
|
228
167
|
autocompleteDisposeFun.current?.();
|
|
168
|
+
dataProviderRef.current = null;
|
|
229
169
|
};
|
|
230
170
|
}, []);
|
|
231
171
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
const editor = editorRef.current;
|
|
174
|
+
if (!editor) return;
|
|
175
|
+
|
|
176
|
+
// clean up previous autocomplete provider
|
|
177
|
+
if (autocompleteDisposeFun.current) {
|
|
178
|
+
autocompleteDisposeFun.current();
|
|
179
|
+
autocompleteDisposeFun.current = null;
|
|
180
|
+
}
|
|
181
|
+
dataProviderRef.current = null;
|
|
182
|
+
|
|
183
|
+
// If autocomplete is enabled, set up a new autocomplete provider
|
|
184
|
+
if (enableAutocomplete) {
|
|
185
|
+
const dataProvider = new DataProvider({
|
|
186
|
+
url: props.url,
|
|
187
|
+
lookbackInterval: props.lookbackInterval,
|
|
188
|
+
variablesNames: props.variablesNames,
|
|
189
|
+
durationVariablesCompletion: props.durationVariablesCompletion,
|
|
190
|
+
request: props.request,
|
|
191
|
+
httpMethod: props.httpMethod,
|
|
192
|
+
apiPrefix: props.apiPrefix,
|
|
193
|
+
httpErrorHandler: props.httpErrorHandler,
|
|
194
|
+
});
|
|
195
|
+
dataProviderRef.current = dataProvider;
|
|
196
|
+
dataProvider.start();
|
|
197
|
+
|
|
198
|
+
const completionProvider = getCompletionProvider(monaco, dataProvider);
|
|
199
|
+
|
|
200
|
+
const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
|
|
201
|
+
...completionProvider,
|
|
202
|
+
provideCompletionItems: (model, position, context, token) => {
|
|
203
|
+
if (editor.getModel()?.id !== model.id) {
|
|
204
|
+
return { suggestions: [] };
|
|
205
|
+
}
|
|
206
|
+
return completionProvider.provideCompletionItems(model, position, context, token);
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
|
|
211
|
+
autocompleteDisposeFun.current = dispose;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// clean up previous placeholder decorations
|
|
215
|
+
const model = editor.getModel();
|
|
216
|
+
if (model) {
|
|
217
|
+
model.deltaDecorations(
|
|
218
|
+
model.getAllDecorations().map((d) => d.id),
|
|
219
|
+
[],
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (placeholder) {
|
|
224
|
+
const placeholderDecorators = [
|
|
225
|
+
{
|
|
226
|
+
range: new monaco.Range(1, 1, 1, 1),
|
|
227
|
+
options: {
|
|
228
|
+
className: styles.placeholder,
|
|
229
|
+
isWholeLine: true,
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
];
|
|
233
|
+
|
|
234
|
+
let decorators: string[] = [];
|
|
235
|
+
|
|
236
|
+
const checkDecorators: () => void = () => {
|
|
237
|
+
const model = editor.getModel();
|
|
238
|
+
|
|
239
|
+
if (!model) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
|
|
244
|
+
decorators = model.deltaDecorations(decorators, newDecorators);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
checkDecorators();
|
|
248
|
+
editor.onDidChangeModelContent(checkDecorators);
|
|
249
|
+
}
|
|
250
|
+
}, [
|
|
251
|
+
enableAutocomplete,
|
|
252
|
+
props.url,
|
|
253
|
+
props.lookbackInterval,
|
|
254
|
+
JSON.stringify(props.variablesNames),
|
|
255
|
+
props.durationVariablesCompletion,
|
|
256
|
+
props.httpMethod,
|
|
257
|
+
props.apiPrefix,
|
|
258
|
+
placeholder,
|
|
259
|
+
]);
|
|
237
260
|
|
|
238
261
|
return (
|
|
239
262
|
<div className={'ant-input' + (size ? ` ${SIZE_MAP[size].className}` : '')}>
|
|
@@ -280,4 +303,4 @@ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataP
|
|
|
280
303
|
</div>
|
|
281
304
|
</div>
|
|
282
305
|
);
|
|
283
|
-
}
|
|
306
|
+
}
|