@fc-components/monaco-editor 0.1.3 → 0.1.5
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 +53 -30
- package/dist/monaco-editor.cjs.development.js +43 -26
- 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 +44 -27
- package/dist/monaco-editor.esm.js.map +1 -1
- package/dist/promql/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/promql/index.tsx +44 -28
package/dist/promql/index.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ interface PromQLEditorProps {
|
|
|
12
12
|
onShiftEnter?: (value: string) => void;
|
|
13
13
|
onBlur?: (value: string) => void;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
export
|
|
15
|
+
declare const _default: React.ForwardRefExoticComponent<PromQLEditorProps & DataProviderParams & React.RefAttributes<unknown>>;
|
|
16
|
+
export default _default;
|
package/package.json
CHANGED
package/src/promql/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect, useRef } from 'react';
|
|
1
|
+
import React, { forwardRef, useEffect, useRef, useImperativeHandle } 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';
|
|
@@ -27,7 +27,6 @@ interface PromQLEditorProps {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const PROMQL_LANG_ID = promLanguageDefinition.id;
|
|
30
|
-
const EDITOR_HEIGHT_OFFSET = 2;
|
|
31
30
|
const SIZE_MAP: Record<
|
|
32
31
|
string,
|
|
33
32
|
{
|
|
@@ -53,8 +52,8 @@ const SIZE_MAP: Record<
|
|
|
53
52
|
},
|
|
54
53
|
};
|
|
55
54
|
const themeMap: Record<string, string> = {
|
|
56
|
-
light: '
|
|
57
|
-
dark: '
|
|
55
|
+
light: 'light',
|
|
56
|
+
dark: 'n9e-dark',
|
|
58
57
|
};
|
|
59
58
|
|
|
60
59
|
const getStyles = (placeholder?: string) => {
|
|
@@ -68,36 +67,26 @@ const getStyles = (placeholder?: string) => {
|
|
|
68
67
|
};
|
|
69
68
|
};
|
|
70
69
|
|
|
71
|
-
export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
|
|
70
|
+
export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataProviderParams, ref) {
|
|
72
71
|
const id = uuidv4();
|
|
73
72
|
const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur } = props;
|
|
74
73
|
const autocompleteDisposeFun = useRef<(() => void) | null>(null);
|
|
75
74
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
76
75
|
const dataProviderRef = useRef<DataProvider | null>(null);
|
|
76
|
+
const editorRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
|
|
77
77
|
const styles = getStyles(placeholder);
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
78
|
+
const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
|
|
79
|
+
editorRef.current = editor;
|
|
80
|
+
monaco.editor.defineTheme('n9e-dark', {
|
|
81
|
+
base: 'vs-dark',
|
|
82
|
+
inherit: true,
|
|
83
|
+
rules: [],
|
|
84
|
+
colors: {
|
|
85
|
+
'editor.background': '#00000000',
|
|
86
|
+
focusBorder: '#00000000',
|
|
87
|
+
},
|
|
87
88
|
});
|
|
88
89
|
|
|
89
|
-
// 设置语法高亮
|
|
90
|
-
monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language as any);
|
|
91
|
-
|
|
92
|
-
// 设置语言配置
|
|
93
|
-
monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration as any);
|
|
94
|
-
|
|
95
|
-
return () => {
|
|
96
|
-
autocompleteDisposeFun.current?.();
|
|
97
|
-
};
|
|
98
|
-
}, []);
|
|
99
|
-
|
|
100
|
-
const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
|
|
101
90
|
const isEditorFocused = editor.createContextKey<boolean>('isEditorFocused' + id, false);
|
|
102
91
|
// we setup on-blur
|
|
103
92
|
editor.onDidBlurEditorWidget(() => {
|
|
@@ -143,7 +132,7 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
|
|
|
143
132
|
const containerDiv = containerRef.current;
|
|
144
133
|
if (containerDiv !== null) {
|
|
145
134
|
const pixelHeight = editor.getContentHeight();
|
|
146
|
-
containerDiv.style.height = `${pixelHeight
|
|
135
|
+
containerDiv.style.height = `${pixelHeight}px`;
|
|
147
136
|
containerDiv.style.width = '100%';
|
|
148
137
|
const pixelWidth = containerDiv.clientWidth;
|
|
149
138
|
editor.layout({ width: pixelWidth, height: pixelHeight });
|
|
@@ -219,6 +208,33 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
|
|
|
219
208
|
}
|
|
220
209
|
}, [JSON.stringify(variablesNames)]);
|
|
221
210
|
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
// 注册 PromQL 语言
|
|
213
|
+
const { aliases, extensions, mimetypes } = promLanguageDefinition;
|
|
214
|
+
monaco.languages.register({
|
|
215
|
+
id: PROMQL_LANG_ID,
|
|
216
|
+
aliases,
|
|
217
|
+
extensions,
|
|
218
|
+
mimetypes,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// 设置语法高亮
|
|
222
|
+
monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language as any);
|
|
223
|
+
|
|
224
|
+
// 设置语言配置
|
|
225
|
+
monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration as any);
|
|
226
|
+
|
|
227
|
+
return () => {
|
|
228
|
+
autocompleteDisposeFun.current?.();
|
|
229
|
+
};
|
|
230
|
+
}, []);
|
|
231
|
+
|
|
232
|
+
useImperativeHandle(ref, () => ({
|
|
233
|
+
getEditor: () => {
|
|
234
|
+
return editorRef.current;
|
|
235
|
+
},
|
|
236
|
+
}));
|
|
237
|
+
|
|
222
238
|
return (
|
|
223
239
|
<div className={'ant-input' + (size ? ` ${SIZE_MAP[size].className}` : '')}>
|
|
224
240
|
<div ref={containerRef}>
|
|
@@ -264,4 +280,4 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
|
|
|
264
280
|
</div>
|
|
265
281
|
</div>
|
|
266
282
|
);
|
|
267
|
-
}
|
|
283
|
+
});
|