@fc-components/monaco-editor 0.1.5 → 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.
@@ -1,7 +1,7 @@
1
- import React, { forwardRef, useRef, useEffect, useImperativeHandle } from 'react';
1
+ import React, { useRef, useEffect } from 'react';
2
2
  import MonacoEditor from 'react-monaco-editor';
3
3
  import * as monaco from 'monaco-editor';
4
- import { languages, editor, KeyMod, KeyCode, Range, MarkerSeverity } from 'monaco-editor';
4
+ import { languages, Range, editor, KeyMod, KeyCode, MarkerSeverity } from 'monaco-editor';
5
5
  import { promLanguageDefinition } from 'monaco-promql';
6
6
  import { parser, LabelMatchers, VectorSelector, PromQL, FunctionCallBody, StringLiteral, UnquotedLabelMatcher, BinaryExpr, NumberDurationLiteralInDurationContext, MatrixSelector, GroupingLabels, AggregateModifier, AggregateExpr, Identifier, LabelName, MatchOp, EqlSingle, EqlRegex, Neq, NeqRegex } from '@fc-components/lezer-metricsql';
7
7
  import { v4 } from 'uuid';
@@ -1984,13 +1984,12 @@ var getStyles = function getStyles(placeholder) {
1984
1984
  })
1985
1985
  };
1986
1986
  };
1987
- var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
1987
+ function PromQLEditor(props) {
1988
1988
  var id = v4();
1989
1989
  var _props$size = props.size,
1990
1990
  size = _props$size === void 0 ? 'middle' : _props$size,
1991
1991
  _props$theme = props.theme,
1992
1992
  theme = _props$theme === void 0 ? 'light' : _props$theme,
1993
- variablesNames = props.variablesNames,
1994
1993
  value = props.value,
1995
1994
  placeholder = props.placeholder,
1996
1995
  interpolateString = props.interpolateString,
@@ -1998,7 +1997,8 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
1998
1997
  enableAutocomplete = _props$enableAutocomp === void 0 ? true : _props$enableAutocomp,
1999
1998
  onChange = props.onChange,
2000
1999
  onShiftEnter = props.onShiftEnter,
2001
- onBlur = props.onBlur;
2000
+ onBlur = props.onBlur,
2001
+ editorDidMount = props.editorDidMount;
2002
2002
  var autocompleteDisposeFun = useRef(null);
2003
2003
  var containerRef = useRef(null);
2004
2004
  var dataProviderRef = useRef(null);
@@ -2024,35 +2024,7 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
2024
2024
  editor$1.onDidFocusEditorText(function () {
2025
2025
  isEditorFocused.set(true);
2026
2026
  });
2027
- if (enableAutocomplete) {
2028
- var dataProvider = new DataProvider({
2029
- url: props.url,
2030
- lookbackInterval: props.lookbackInterval,
2031
- variablesNames: props.variablesNames,
2032
- durationVariablesCompletion: props.durationVariablesCompletion,
2033
- request: props.request,
2034
- httpMethod: props.httpMethod,
2035
- apiPrefix: props.apiPrefix,
2036
- httpErrorHandler: props.httpErrorHandler
2037
- });
2038
- dataProviderRef.current = dataProvider;
2039
- dataProvider.start();
2040
- var completionProvider = getCompletionProvider(monaco, dataProvider);
2041
- var filteringCompletionProvider = _extends({}, completionProvider, {
2042
- provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2043
- var _editor$getModel;
2044
- if (((_editor$getModel = editor$1.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2045
- return {
2046
- suggestions: []
2047
- };
2048
- }
2049
- return completionProvider.provideCompletionItems(model, position, context, token);
2050
- }
2051
- });
2052
- var _monaco$languages$reg = languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2053
- dispose = _monaco$languages$reg.dispose;
2054
- autocompleteDisposeFun.current = dispose;
2055
- }
2027
+ // set the height of the editor container
2056
2028
  var updateElementHeight = function updateElementHeight() {
2057
2029
  var containerDiv = containerRef.current;
2058
2030
  if (containerDiv !== null) {
@@ -2077,26 +2049,6 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
2077
2049
  editor$1.addCommand(KeyMod.Shift | KeyCode.Enter, function () {
2078
2050
  onShiftEnter == null || onShiftEnter(editor$1.getValue());
2079
2051
  }, 'isEditorFocused' + id);
2080
- if (placeholder) {
2081
- var placeholderDecorators = [{
2082
- range: new Range(1, 1, 1, 1),
2083
- options: {
2084
- className: styles.placeholder,
2085
- isWholeLine: true
2086
- }
2087
- }];
2088
- var decorators = [];
2089
- var checkDecorators = function checkDecorators() {
2090
- var model = editor$1.getModel();
2091
- if (!model) {
2092
- return;
2093
- }
2094
- var newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
2095
- decorators = model.deltaDecorations(decorators, newDecorators);
2096
- };
2097
- checkDecorators();
2098
- editor$1.onDidChangeModelContent(checkDecorators);
2099
- }
2100
2052
  editor$1.onDidChangeModelContent(function () {
2101
2053
  var model = editor$1.getModel();
2102
2054
  if (model) {
@@ -2113,13 +2065,8 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
2113
2065
  editor.setModelMarkers(model, 'owner', markers);
2114
2066
  }
2115
2067
  });
2068
+ editorDidMount == null || editorDidMount(editor$1);
2116
2069
  };
2117
- useEffect(function () {
2118
- var dataProvider = dataProviderRef.current;
2119
- if (dataProvider) {
2120
- dataProvider.setVariablesNames(variablesNames || []);
2121
- }
2122
- }, [JSON.stringify(variablesNames)]);
2123
2070
  useEffect(function () {
2124
2071
  // 注册 PromQL 语言
2125
2072
  var aliases = promLanguageDefinition.aliases,
@@ -2137,15 +2084,76 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
2137
2084
  languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration);
2138
2085
  return function () {
2139
2086
  autocompleteDisposeFun.current == null || autocompleteDisposeFun.current();
2087
+ dataProviderRef.current = null;
2140
2088
  };
2141
2089
  }, []);
2142
- useImperativeHandle(ref, function () {
2143
- return {
2144
- getEditor: function getEditor() {
2145
- return editorRef.current;
2146
- }
2147
- };
2148
- });
2090
+ useEffect(function () {
2091
+ var editor = editorRef.current;
2092
+ if (!editor) return;
2093
+ // clean up previous autocomplete provider
2094
+ if (autocompleteDisposeFun.current) {
2095
+ autocompleteDisposeFun.current();
2096
+ autocompleteDisposeFun.current = null;
2097
+ }
2098
+ dataProviderRef.current = null;
2099
+ // If autocomplete is enabled, set up a new autocomplete provider
2100
+ if (enableAutocomplete) {
2101
+ var dataProvider = new DataProvider({
2102
+ url: props.url,
2103
+ lookbackInterval: props.lookbackInterval,
2104
+ variablesNames: props.variablesNames,
2105
+ durationVariablesCompletion: props.durationVariablesCompletion,
2106
+ request: props.request,
2107
+ httpMethod: props.httpMethod,
2108
+ apiPrefix: props.apiPrefix,
2109
+ httpErrorHandler: props.httpErrorHandler
2110
+ });
2111
+ dataProviderRef.current = dataProvider;
2112
+ dataProvider.start();
2113
+ var completionProvider = getCompletionProvider(monaco, dataProvider);
2114
+ var filteringCompletionProvider = _extends({}, completionProvider, {
2115
+ provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2116
+ var _editor$getModel;
2117
+ if (((_editor$getModel = editor.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2118
+ return {
2119
+ suggestions: []
2120
+ };
2121
+ }
2122
+ return completionProvider.provideCompletionItems(model, position, context, token);
2123
+ }
2124
+ });
2125
+ var _monaco$languages$reg = languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2126
+ dispose = _monaco$languages$reg.dispose;
2127
+ autocompleteDisposeFun.current = dispose;
2128
+ }
2129
+ // clean up previous placeholder decorations
2130
+ var model = editor.getModel();
2131
+ if (model) {
2132
+ model.deltaDecorations(model.getAllDecorations().map(function (d) {
2133
+ return d.id;
2134
+ }), []);
2135
+ }
2136
+ if (placeholder) {
2137
+ var placeholderDecorators = [{
2138
+ range: new Range(1, 1, 1, 1),
2139
+ options: {
2140
+ className: styles.placeholder,
2141
+ isWholeLine: true
2142
+ }
2143
+ }];
2144
+ var decorators = [];
2145
+ var checkDecorators = function checkDecorators() {
2146
+ var model = editor.getModel();
2147
+ if (!model) {
2148
+ return;
2149
+ }
2150
+ var newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
2151
+ decorators = model.deltaDecorations(decorators, newDecorators);
2152
+ };
2153
+ checkDecorators();
2154
+ editor.onDidChangeModelContent(checkDecorators);
2155
+ }
2156
+ }, [enableAutocomplete, props.url, props.lookbackInterval, JSON.stringify(props.variablesNames), props.durationVariablesCompletion, props.httpMethod, props.apiPrefix, placeholder]);
2149
2157
  return React.createElement("div", {
2150
2158
  className: 'ant-input' + (size ? " " + SIZE_MAP[size].className : '')
2151
2159
  }, React.createElement("div", {
@@ -2191,7 +2199,7 @@ var index = /*#__PURE__*/forwardRef(function PromQLEditor(props, ref) {
2191
2199
  wordWrap: 'on'
2192
2200
  }
2193
2201
  })));
2194
- });
2202
+ }
2195
2203
 
2196
- export { index as PromQLMonacoEditor };
2204
+ export { PromQLEditor as PromQLMonacoEditor };
2197
2205
  //# sourceMappingURL=monaco-editor.esm.js.map