@fc-components/monaco-editor 0.1.6 → 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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.6",
6
+ "version": "0.1.7",
7
7
  "license": "MIT",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/monaco-editor.esm.js",
@@ -70,24 +70,15 @@ const getStyles = (placeholder?: string) => {
70
70
 
71
71
  export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
72
72
  const id = uuidv4();
73
- const {
74
- size = 'middle',
75
- theme = 'light',
76
- variablesNames,
77
- value,
78
- placeholder,
79
- interpolateString,
80
- enableAutocomplete = true,
81
- onChange,
82
- onShiftEnter,
83
- onBlur,
84
- editorDidMount,
85
- } = props;
73
+ const { size = 'middle', theme = 'light', value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur, editorDidMount } = props;
86
74
  const autocompleteDisposeFun = useRef<(() => void) | null>(null);
87
75
  const containerRef = useRef<HTMLDivElement>(null);
88
76
  const dataProviderRef = useRef<DataProvider | null>(null);
77
+ const editorRef = useRef<monacoTypes.editor.IStandaloneCodeEditor | null>(null);
89
78
  const styles = getStyles(placeholder);
90
79
  const handleEditorDidMount = (editor: monacoTypes.editor.IStandaloneCodeEditor) => {
80
+ editorRef.current = editor;
81
+
91
82
  monaco.editor.defineTheme('n9e-dark', {
92
83
  base: 'vs-dark',
93
84
  inherit: true,
@@ -108,37 +99,7 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
108
99
  isEditorFocused.set(true);
109
100
  });
110
101
 
111
- if (enableAutocomplete) {
112
- const dataProvider = new DataProvider({
113
- url: props.url,
114
- lookbackInterval: props.lookbackInterval,
115
- variablesNames: props.variablesNames,
116
- durationVariablesCompletion: props.durationVariablesCompletion,
117
- request: props.request,
118
- httpMethod: props.httpMethod,
119
- apiPrefix: props.apiPrefix,
120
- httpErrorHandler: props.httpErrorHandler,
121
- });
122
- dataProviderRef.current = dataProvider;
123
- dataProvider.start();
124
-
125
- const completionProvider = getCompletionProvider(monaco, dataProvider);
126
-
127
- const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
128
- ...completionProvider,
129
- provideCompletionItems: (model, position, context, token) => {
130
- if (editor.getModel()?.id !== model.id) {
131
- return { suggestions: [] };
132
- }
133
- return completionProvider.provideCompletionItems(model, position, context, token);
134
- },
135
- };
136
-
137
- const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
138
-
139
- autocompleteDisposeFun.current = dispose;
140
- }
141
-
102
+ // set the height of the editor container
142
103
  const updateElementHeight = () => {
143
104
  const containerDiv = containerRef.current;
144
105
  if (containerDiv !== null) {
@@ -168,34 +129,6 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
168
129
  'isEditorFocused' + id,
169
130
  );
170
131
 
171
- if (placeholder) {
172
- const placeholderDecorators = [
173
- {
174
- range: new monaco.Range(1, 1, 1, 1),
175
- options: {
176
- className: styles.placeholder,
177
- isWholeLine: true,
178
- },
179
- },
180
- ];
181
-
182
- let decorators: string[] = [];
183
-
184
- const checkDecorators: () => void = () => {
185
- const model = editor.getModel();
186
-
187
- if (!model) {
188
- return;
189
- }
190
-
191
- const newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
192
- decorators = model.deltaDecorations(decorators, newDecorators);
193
- };
194
-
195
- checkDecorators();
196
- editor.onDidChangeModelContent(checkDecorators);
197
- }
198
-
199
132
  editor.onDidChangeModelContent(() => {
200
133
  const model = editor.getModel();
201
134
  if (model) {
@@ -232,15 +165,98 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
232
165
 
233
166
  return () => {
234
167
  autocompleteDisposeFun.current?.();
168
+ dataProviderRef.current = null;
235
169
  };
236
170
  }, []);
237
171
 
238
172
  useEffect(() => {
239
- const dataProvider = dataProviderRef.current;
240
- if (dataProvider) {
241
- dataProvider.setVariablesNames(variablesNames || []);
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;
242
180
  }
243
- }, [JSON.stringify(variablesNames)]);
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
+ ]);
244
260
 
245
261
  return (
246
262
  <div className={'ant-input' + (size ? ` ${SIZE_MAP[size].className}` : '')}>