@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.
@@ -1996,7 +1996,6 @@ function PromQLEditor(props) {
1996
1996
  size = _props$size === void 0 ? 'middle' : _props$size,
1997
1997
  _props$theme = props.theme,
1998
1998
  theme = _props$theme === void 0 ? 'light' : _props$theme,
1999
- variablesNames = props.variablesNames,
2000
1999
  value = props.value,
2001
2000
  placeholder = props.placeholder,
2002
2001
  interpolateString = props.interpolateString,
@@ -2009,8 +2008,10 @@ function PromQLEditor(props) {
2009
2008
  var autocompleteDisposeFun = React.useRef(null);
2010
2009
  var containerRef = React.useRef(null);
2011
2010
  var dataProviderRef = React.useRef(null);
2011
+ var editorRef = React.useRef(null);
2012
2012
  var styles = getStyles(placeholder);
2013
2013
  var handleEditorDidMount = function handleEditorDidMount(editor) {
2014
+ editorRef.current = editor;
2014
2015
  monaco.editor.defineTheme('n9e-dark', {
2015
2016
  base: 'vs-dark',
2016
2017
  inherit: true,
@@ -2029,35 +2030,7 @@ function PromQLEditor(props) {
2029
2030
  editor.onDidFocusEditorText(function () {
2030
2031
  isEditorFocused.set(true);
2031
2032
  });
2032
- if (enableAutocomplete) {
2033
- var dataProvider = new DataProvider({
2034
- url: props.url,
2035
- lookbackInterval: props.lookbackInterval,
2036
- variablesNames: props.variablesNames,
2037
- durationVariablesCompletion: props.durationVariablesCompletion,
2038
- request: props.request,
2039
- httpMethod: props.httpMethod,
2040
- apiPrefix: props.apiPrefix,
2041
- httpErrorHandler: props.httpErrorHandler
2042
- });
2043
- dataProviderRef.current = dataProvider;
2044
- dataProvider.start();
2045
- var completionProvider = getCompletionProvider(monaco, dataProvider);
2046
- var filteringCompletionProvider = _extends({}, completionProvider, {
2047
- provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2048
- var _editor$getModel;
2049
- if (((_editor$getModel = editor.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2050
- return {
2051
- suggestions: []
2052
- };
2053
- }
2054
- return completionProvider.provideCompletionItems(model, position, context, token);
2055
- }
2056
- });
2057
- var _monaco$languages$reg = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2058
- dispose = _monaco$languages$reg.dispose;
2059
- autocompleteDisposeFun.current = dispose;
2060
- }
2033
+ // set the height of the editor container
2061
2034
  var updateElementHeight = function updateElementHeight() {
2062
2035
  var containerDiv = containerRef.current;
2063
2036
  if (containerDiv !== null) {
@@ -2082,26 +2055,6 @@ function PromQLEditor(props) {
2082
2055
  editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Enter, function () {
2083
2056
  onShiftEnter == null || onShiftEnter(editor.getValue());
2084
2057
  }, 'isEditorFocused' + id);
2085
- if (placeholder) {
2086
- var placeholderDecorators = [{
2087
- range: new monaco.Range(1, 1, 1, 1),
2088
- options: {
2089
- className: styles.placeholder,
2090
- isWholeLine: true
2091
- }
2092
- }];
2093
- var decorators = [];
2094
- var checkDecorators = function checkDecorators() {
2095
- var model = editor.getModel();
2096
- if (!model) {
2097
- return;
2098
- }
2099
- var newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
2100
- decorators = model.deltaDecorations(decorators, newDecorators);
2101
- };
2102
- checkDecorators();
2103
- editor.onDidChangeModelContent(checkDecorators);
2104
- }
2105
2058
  editor.onDidChangeModelContent(function () {
2106
2059
  var model = editor.getModel();
2107
2060
  if (model) {
@@ -2137,14 +2090,76 @@ function PromQLEditor(props) {
2137
2090
  monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration);
2138
2091
  return function () {
2139
2092
  autocompleteDisposeFun.current == null || autocompleteDisposeFun.current();
2093
+ dataProviderRef.current = null;
2140
2094
  };
2141
2095
  }, []);
2142
2096
  React.useEffect(function () {
2143
- var dataProvider = dataProviderRef.current;
2144
- if (dataProvider) {
2145
- dataProvider.setVariablesNames(variablesNames || []);
2097
+ var editor = editorRef.current;
2098
+ if (!editor) return;
2099
+ // clean up previous autocomplete provider
2100
+ if (autocompleteDisposeFun.current) {
2101
+ autocompleteDisposeFun.current();
2102
+ autocompleteDisposeFun.current = null;
2103
+ }
2104
+ dataProviderRef.current = null;
2105
+ // If autocomplete is enabled, set up a new autocomplete provider
2106
+ if (enableAutocomplete) {
2107
+ var dataProvider = new DataProvider({
2108
+ url: props.url,
2109
+ lookbackInterval: props.lookbackInterval,
2110
+ variablesNames: props.variablesNames,
2111
+ durationVariablesCompletion: props.durationVariablesCompletion,
2112
+ request: props.request,
2113
+ httpMethod: props.httpMethod,
2114
+ apiPrefix: props.apiPrefix,
2115
+ httpErrorHandler: props.httpErrorHandler
2116
+ });
2117
+ dataProviderRef.current = dataProvider;
2118
+ dataProvider.start();
2119
+ var completionProvider = getCompletionProvider(monaco, dataProvider);
2120
+ var filteringCompletionProvider = _extends({}, completionProvider, {
2121
+ provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2122
+ var _editor$getModel;
2123
+ if (((_editor$getModel = editor.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2124
+ return {
2125
+ suggestions: []
2126
+ };
2127
+ }
2128
+ return completionProvider.provideCompletionItems(model, position, context, token);
2129
+ }
2130
+ });
2131
+ var _monaco$languages$reg = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2132
+ dispose = _monaco$languages$reg.dispose;
2133
+ autocompleteDisposeFun.current = dispose;
2134
+ }
2135
+ // clean up previous placeholder decorations
2136
+ var model = editor.getModel();
2137
+ if (model) {
2138
+ model.deltaDecorations(model.getAllDecorations().map(function (d) {
2139
+ return d.id;
2140
+ }), []);
2141
+ }
2142
+ if (placeholder) {
2143
+ var placeholderDecorators = [{
2144
+ range: new monaco.Range(1, 1, 1, 1),
2145
+ options: {
2146
+ className: styles.placeholder,
2147
+ isWholeLine: true
2148
+ }
2149
+ }];
2150
+ var decorators = [];
2151
+ var checkDecorators = function checkDecorators() {
2152
+ var model = editor.getModel();
2153
+ if (!model) {
2154
+ return;
2155
+ }
2156
+ var newDecorators = model.getValueLength() === 0 ? placeholderDecorators : [];
2157
+ decorators = model.deltaDecorations(decorators, newDecorators);
2158
+ };
2159
+ checkDecorators();
2160
+ editor.onDidChangeModelContent(checkDecorators);
2146
2161
  }
2147
- }, [JSON.stringify(variablesNames)]);
2162
+ }, [enableAutocomplete, props.url, props.lookbackInterval, JSON.stringify(props.variablesNames), props.durationVariablesCompletion, props.httpMethod, props.apiPrefix, placeholder]);
2148
2163
  return React__default.createElement("div", {
2149
2164
  className: 'ant-input' + (size ? " " + SIZE_MAP[size].className : '')
2150
2165
  }, React__default.createElement("div", {