@fc-components/monaco-editor 0.1.1
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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/monaco-editor.cjs.development.js +2170 -0
- package/dist/monaco-editor.cjs.development.js.map +1 -0
- package/dist/monaco-editor.cjs.production.min.js +2 -0
- package/dist/monaco-editor.cjs.production.min.js.map +1 -0
- package/dist/monaco-editor.esm.js +2164 -0
- package/dist/monaco-editor.esm.js.map +1 -0
- package/dist/promql/completion/DataProvider.d.ts +44 -0
- package/dist/promql/completion/completions.d.ts +13 -0
- package/dist/promql/completion/getCompletionProvider.d.ts +6 -0
- package/dist/promql/completion/situation.d.ts +25 -0
- package/dist/promql/index.d.ts +14 -0
- package/dist/promql/promql.d.ts +60 -0
- package/dist/promql/types.d.ts +30 -0
- package/dist/promql/util.d.ts +6 -0
- package/dist/promql/validation.d.ts +11 -0
- package/package.json +56 -0
- package/src/index.tsx +3 -0
- package/src/promql/NOTICE.md +3 -0
- package/src/promql/completion/DataProvider.ts +252 -0
- package/src/promql/completion/completions.ts +188 -0
- package/src/promql/completion/getCompletionProvider.ts +96 -0
- package/src/promql/completion/situation.ts +491 -0
- package/src/promql/index.tsx +263 -0
- package/src/promql/promql.ts +912 -0
- package/src/promql/types.ts +35 -0
- package/src/promql/util.ts +29 -0
- package/src/promql/validation.ts +93 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
|
+
import MonacoEditor from 'react-monaco-editor';
|
|
3
|
+
import * as monaco from 'monaco-editor';
|
|
4
|
+
import { promLanguageDefinition } from 'monaco-promql';
|
|
5
|
+
import type * as monacoTypes from 'monaco-editor/esm/vs/editor/editor.api';
|
|
6
|
+
import { parser } from '@fc-components/lezer-metricsql';
|
|
7
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
8
|
+
import { css } from '@emotion/css';
|
|
9
|
+
|
|
10
|
+
import { language, languageConfiguration } from './promql';
|
|
11
|
+
import { DataProvider } from './completion/DataProvider';
|
|
12
|
+
import { getCompletionProvider } from './completion/getCompletionProvider';
|
|
13
|
+
import type { DataProviderParams } from './types';
|
|
14
|
+
import { validateQuery } from './validation';
|
|
15
|
+
|
|
16
|
+
interface PromQLEditorProps {
|
|
17
|
+
size?: 'small' | 'middle' | 'large';
|
|
18
|
+
theme?: 'light' | 'dark';
|
|
19
|
+
value?: string;
|
|
20
|
+
placeholder?: string;
|
|
21
|
+
interpolateString?: (query: string) => string;
|
|
22
|
+
onChange?: (value: string) => void;
|
|
23
|
+
onShiftEnter?: (value: string) => void;
|
|
24
|
+
onBlur?: (value: string) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PROMQL_LANG_ID = promLanguageDefinition.id;
|
|
28
|
+
const EDITOR_HEIGHT_OFFSET = 2;
|
|
29
|
+
const SIZE_MAP: Record<
|
|
30
|
+
string,
|
|
31
|
+
{
|
|
32
|
+
className: string;
|
|
33
|
+
top: number;
|
|
34
|
+
bottom: number;
|
|
35
|
+
}
|
|
36
|
+
> = {
|
|
37
|
+
small: {
|
|
38
|
+
className: 'ant-input-sm',
|
|
39
|
+
top: 1,
|
|
40
|
+
bottom: 1,
|
|
41
|
+
},
|
|
42
|
+
middle: {
|
|
43
|
+
className: 'ant-input-md',
|
|
44
|
+
top: 1,
|
|
45
|
+
bottom: 1,
|
|
46
|
+
},
|
|
47
|
+
large: {
|
|
48
|
+
className: 'ant-input-lg',
|
|
49
|
+
top: 3,
|
|
50
|
+
bottom: 2,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const themeMap: Record<string, string> = {
|
|
54
|
+
light: 'vs-light',
|
|
55
|
+
dark: 'vs-dark',
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const getStyles = (placeholder?: string) => {
|
|
59
|
+
return {
|
|
60
|
+
placeholder: css({
|
|
61
|
+
'::after': {
|
|
62
|
+
content: `'${placeholder}'`,
|
|
63
|
+
opacity: 0.6,
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
|
|
70
|
+
const id = uuidv4();
|
|
71
|
+
const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, onChange, onShiftEnter, onBlur } = props;
|
|
72
|
+
const autocompleteDisposeFun = useRef<(() => void) | null>(null);
|
|
73
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
74
|
+
const dataProviderRef = useRef<DataProvider | null>(null);
|
|
75
|
+
const styles = getStyles(placeholder);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
// 注册 PromQL 语言
|
|
79
|
+
const { aliases, extensions, mimetypes } = promLanguageDefinition;
|
|
80
|
+
monaco.languages.register({
|
|
81
|
+
id: PROMQL_LANG_ID,
|
|
82
|
+
aliases,
|
|
83
|
+
extensions,
|
|
84
|
+
mimetypes,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// 设置语法高亮
|
|
88
|
+
monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language as any);
|
|
89
|
+
|
|
90
|
+
// 设置语言配置
|
|
91
|
+
monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration as any);
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
autocompleteDisposeFun.current?.();
|
|
95
|
+
};
|
|
96
|
+
}, []);
|
|
97
|
+
|
|
98
|
+
const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
|
|
99
|
+
const isEditorFocused = editor.createContextKey<boolean>('isEditorFocused' + id, false);
|
|
100
|
+
// we setup on-blur
|
|
101
|
+
editor.onDidBlurEditorWidget(() => {
|
|
102
|
+
isEditorFocused.set(false);
|
|
103
|
+
onBlur?.(editor.getValue());
|
|
104
|
+
});
|
|
105
|
+
editor.onDidFocusEditorText(() => {
|
|
106
|
+
isEditorFocused.set(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const dataProvider = new DataProvider({
|
|
110
|
+
url: props.url,
|
|
111
|
+
lookbackInterval: props.lookbackInterval,
|
|
112
|
+
variablesNames: props.variablesNames,
|
|
113
|
+
request: props.request,
|
|
114
|
+
httpMethod: props.httpMethod,
|
|
115
|
+
apiPrefix: props.apiPrefix,
|
|
116
|
+
httpErrorHandler: props.httpErrorHandler,
|
|
117
|
+
});
|
|
118
|
+
dataProviderRef.current = dataProvider;
|
|
119
|
+
dataProvider.start();
|
|
120
|
+
|
|
121
|
+
const completionProvider = getCompletionProvider(monaco, dataProvider);
|
|
122
|
+
|
|
123
|
+
const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
|
|
124
|
+
...completionProvider,
|
|
125
|
+
provideCompletionItems: (model, position, context, token) => {
|
|
126
|
+
if (editor.getModel()?.id !== model.id) {
|
|
127
|
+
return { suggestions: [] };
|
|
128
|
+
}
|
|
129
|
+
return completionProvider.provideCompletionItems(model, position, context, token);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
|
|
134
|
+
|
|
135
|
+
autocompleteDisposeFun.current = dispose;
|
|
136
|
+
|
|
137
|
+
const updateElementHeight = () => {
|
|
138
|
+
const containerDiv = containerRef.current;
|
|
139
|
+
if (containerDiv !== null) {
|
|
140
|
+
const pixelHeight = editor.getContentHeight();
|
|
141
|
+
containerDiv.style.height = `${pixelHeight + EDITOR_HEIGHT_OFFSET}px`;
|
|
142
|
+
containerDiv.style.width = '100%';
|
|
143
|
+
const pixelWidth = containerDiv.clientWidth;
|
|
144
|
+
editor.layout({ width: pixelWidth, height: pixelHeight });
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
editor.onDidContentSizeChange(updateElementHeight);
|
|
149
|
+
updateElementHeight();
|
|
150
|
+
|
|
151
|
+
// Fixes Monaco capturing the search key binding and displaying a useless search box within the Editor.
|
|
152
|
+
monaco.editor.addKeybindingRule({
|
|
153
|
+
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyF,
|
|
154
|
+
command: null,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// handle: shift + enter
|
|
158
|
+
editor.addCommand(
|
|
159
|
+
monaco.KeyMod.Shift | monaco.KeyCode.Enter,
|
|
160
|
+
() => {
|
|
161
|
+
console.log(22222);
|
|
162
|
+
onShiftEnter?.(editor.getValue());
|
|
163
|
+
},
|
|
164
|
+
'isEditorFocused' + id,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
if (placeholder) {
|
|
168
|
+
const placeholderDecorators = [
|
|
169
|
+
{
|
|
170
|
+
range: new monaco.Range(1, 1, 1, 1),
|
|
171
|
+
options: {
|
|
172
|
+
className: styles.placeholder,
|
|
173
|
+
isWholeLine: true,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
];
|
|
177
|
+
|
|
178
|
+
let decorators: string[] = [];
|
|
179
|
+
|
|
180
|
+
const checkDecorators: () => void = () => {
|
|
181
|
+
const model = editor.getModel();
|
|
182
|
+
|
|
183
|
+
if (!model) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
|
|
188
|
+
decorators = model.deltaDecorations(decorators, newDecorators);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
checkDecorators();
|
|
192
|
+
editor.onDidChangeModelContent(checkDecorators);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
editor.onDidChangeModelContent(() => {
|
|
196
|
+
const model = editor.getModel();
|
|
197
|
+
if (model) {
|
|
198
|
+
const query = model.getValue();
|
|
199
|
+
const errors = validateQuery(query, interpolateString ? interpolateString(query) : query, model.getLinesContent(), parser) || [];
|
|
200
|
+
const markers = errors.map(({ error, ...boundary }) => ({
|
|
201
|
+
message: `${error ? `Error parsing "${error}"` : 'Parse error'}. The query appears to be incorrect and could fail to be executed.`,
|
|
202
|
+
severity: monaco.MarkerSeverity.Error,
|
|
203
|
+
...boundary,
|
|
204
|
+
}));
|
|
205
|
+
|
|
206
|
+
monaco.editor.setModelMarkers(model, 'owner', markers);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
const dataProvider = dataProviderRef.current;
|
|
213
|
+
if (dataProvider) {
|
|
214
|
+
dataProvider.setVariablesNames(variablesNames || []);
|
|
215
|
+
}
|
|
216
|
+
}, [JSON.stringify(variablesNames)]);
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<div className={'ant-input' + (size ? ` ${SIZE_MAP[size].className}` : '')}>
|
|
220
|
+
<div ref={containerRef}>
|
|
221
|
+
<MonacoEditor
|
|
222
|
+
width='100%'
|
|
223
|
+
height='100%'
|
|
224
|
+
language='promql'
|
|
225
|
+
theme={themeMap[theme]}
|
|
226
|
+
value={value}
|
|
227
|
+
onChange={onChange}
|
|
228
|
+
editorDidMount={handleEditorDidMount}
|
|
229
|
+
options={{
|
|
230
|
+
codeLens: false,
|
|
231
|
+
contextmenu: false,
|
|
232
|
+
fixedOverflowWidgets: true,
|
|
233
|
+
folding: false,
|
|
234
|
+
fontSize: 12,
|
|
235
|
+
lineDecorationsWidth: 0,
|
|
236
|
+
lineNumbers: 'off',
|
|
237
|
+
minimap: { enabled: false },
|
|
238
|
+
overviewRulerBorder: false,
|
|
239
|
+
overviewRulerLanes: 0,
|
|
240
|
+
padding: {
|
|
241
|
+
top: SIZE_MAP[size].top,
|
|
242
|
+
bottom: SIZE_MAP[size].bottom,
|
|
243
|
+
},
|
|
244
|
+
renderLineHighlight: 'none',
|
|
245
|
+
scrollbar: {
|
|
246
|
+
vertical: 'hidden',
|
|
247
|
+
verticalScrollbarSize: 8,
|
|
248
|
+
horizontal: 'hidden',
|
|
249
|
+
horizontalScrollbarSize: 0,
|
|
250
|
+
alwaysConsumeMouseWheel: false,
|
|
251
|
+
},
|
|
252
|
+
scrollBeyondLastLine: false,
|
|
253
|
+
suggest: {
|
|
254
|
+
showWords: false,
|
|
255
|
+
},
|
|
256
|
+
suggestFontSize: 12,
|
|
257
|
+
wordWrap: 'on',
|
|
258
|
+
}}
|
|
259
|
+
/>
|
|
260
|
+
</div>
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
}
|