@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.
@@ -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.3",
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';
@@ -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: 'vs-light',
57
- dark: 'vs-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
- useEffect(() => {
80
- // 注册 PromQL 语言
81
- const { aliases, extensions, mimetypes } = promLanguageDefinition;
82
- monaco.languages.register({
83
- id: PROMQL_LANG_ID,
84
- aliases,
85
- extensions,
86
- mimetypes,
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 + EDITOR_HEIGHT_OFFSET}px`;
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
+ });