@fc-components/monaco-editor 0.1.4 → 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.
@@ -12,5 +12,5 @@ interface PromQLEditorProps {
12
12
  onShiftEnter?: (value: string) => void;
13
13
  onBlur?: (value: string) => void;
14
14
  }
15
- export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams): React.JSX.Element;
16
- export {};
15
+ declare const _default: React.ForwardRefExoticComponent<PromQLEditorProps & DataProviderParams & React.RefAttributes<unknown>>;
16
+ export default _default;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.4",
6
+ "version": "0.1.5",
7
7
  "license": "MIT",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/monaco-editor.esm.js",
@@ -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';
@@ -67,36 +67,16 @@ const getStyles = (placeholder?: string) => {
67
67
  };
68
68
  };
69
69
 
70
- export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
70
+ export default forwardRef(function PromQLEditor(props: PromQLEditorProps & DataProviderParams, ref) {
71
71
  const id = uuidv4();
72
72
  const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur } = props;
73
73
  const autocompleteDisposeFun = useRef<(() => void) | null>(null);
74
74
  const containerRef = useRef<HTMLDivElement>(null);
75
75
  const dataProviderRef = useRef<DataProvider | null>(null);
76
+ const editorRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
76
77
  const styles = getStyles(placeholder);
77
-
78
- useEffect(() => {
79
- // 注册 PromQL 语言
80
- const { aliases, extensions, mimetypes } = promLanguageDefinition;
81
- monaco.languages.register({
82
- id: PROMQL_LANG_ID,
83
- aliases,
84
- extensions,
85
- mimetypes,
86
- });
87
-
88
- // 设置语法高亮
89
- monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language as any);
90
-
91
- // 设置语言配置
92
- monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration as any);
93
-
94
- return () => {
95
- autocompleteDisposeFun.current?.();
96
- };
97
- }, []);
98
-
99
78
  const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
79
+ editorRef.current = editor;
100
80
  monaco.editor.defineTheme('n9e-dark', {
101
81
  base: 'vs-dark',
102
82
  inherit: true,
@@ -228,6 +208,33 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
228
208
  }
229
209
  }, [JSON.stringify(variablesNames)]);
230
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
+
231
238
  return (
232
239
  <div className={'ant-input' + (size ? ` ${SIZE_MAP[size].className}` : '')}>
233
240
  <div ref={containerRef}>
@@ -273,4 +280,4 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
273
280
  </div>
274
281
  </div>
275
282
  );
276
- }
283
+ });