@fc-components/monaco-editor 0.1.1 → 0.1.3

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/README.md CHANGED
@@ -5,23 +5,36 @@ import { PromQLMonacoEditor } from '@fc-components/monaco-editor';
5
5
 
6
6
  const App = () => {
7
7
  return (
8
- <div>
9
- <PromQLMonacoEditor
10
- height="500px"
11
- apiPrefix="/api/n9e/proxy/1/api/v1"
12
- fetchFn={(resource, options: any) => {
13
- const params = options.body?.toString();
14
- const search = params ? `?${params}` : '';
15
- return fetch(resource + search, {
16
- method: 'Get',
17
- headers: new Headers({
18
- Authorization: `Bearer ${localStorage.getItem('access_token') ||
19
- ''}`,
20
- }),
21
- });
22
- }}
23
- />
24
- </div>
8
+ <PromQLMonacoEditor
9
+ placeholder='Enter your PromQL query here...'
10
+ variablesNames={['$job', '$instance']} // Example variable names
11
+ apiPrefix='/api/n9e/proxy/1/api/v1'
12
+ request={(resource, options) => {
13
+ const params = options?.body?.toString();
14
+ const search = params ? `?${params}` : '';
15
+ return fetch(resource + search, {
16
+ method: 'Get',
17
+ headers: new Headers({
18
+ Authorization: `Bearer ${localStorage.getItem('access_token') || ''}`,
19
+ }),
20
+ });
21
+ }}
22
+ enableAutocomplete={true} // Enable completion
23
+ durationVariablesCompletion={false} // Disable duration variables completion. eg. $__interval, $__range, $__rate_interval
24
+ interpolateString={(query) => {
25
+ // Interpolate variables in the query string
26
+ return query.replace(/\$__interval/g, '10s');
27
+ }}
28
+ onShiftEnter={(value) => {
29
+ console.log('Shift+Enter pressed, current value:', value);
30
+ }}
31
+ onBlur={(value) => {
32
+ console.log('Editor lost focus, current value:', value);
33
+ }}
34
+ onChange={(value) => {
35
+ console.log('Value changed:', value);
36
+ }}
37
+ />
25
38
  );
26
39
  };
27
40
 
@@ -862,7 +862,7 @@ function makeSelector(metricName, labels, labelName) {
862
862
  var allLabelTexts = allLabels.map(function (label) {
863
863
  return "" + label.name + label.op + "\"" + escapeLabelValueInExactSelector(label.value) + "\"";
864
864
  });
865
- return "{" + allLabelTexts.join(',') + "}";
865
+ return metricName + "{" + allLabelTexts.join(',') + "}";
866
866
  }
867
867
 
868
868
  var DEFAULT_SERIES_LIMIT = '40000';
@@ -872,7 +872,8 @@ var serviceUnavailable = 503;
872
872
  var CODE_MODE_SUGGESTIONS_INCOMPLETE_EVENT = 'codeModeSuggestionsIncomplete';
873
873
  var DataProvider = /*#__PURE__*/function () {
874
874
  function DataProvider(params) {
875
- var _this = this;
875
+ var _this = this,
876
+ _params$durationVaria;
876
877
  this.lookbackInterval = 60 * 60 * 1000 * 12; // 12 hours
877
878
  this.variablesNames = [];
878
879
  this.httpMethod = 'GET';
@@ -1018,6 +1019,7 @@ var DataProvider = /*#__PURE__*/function () {
1018
1019
  if (params.variablesNames) {
1019
1020
  this.variablesNames = [].concat(params.variablesNames);
1020
1021
  }
1022
+ this.durationVariablesCompletion = (_params$durationVaria = params.durationVariablesCompletion) != null ? _params$durationVaria : true;
1021
1023
  if (params.request) {
1022
1024
  this.customRequest = params.request;
1023
1025
  }
@@ -1542,7 +1544,14 @@ function _getAllFunctionsAndMetricNamesCompletions() {
1542
1544
  }));
1543
1545
  return _getAllFunctionsAndMetricNamesCompletions.apply(this, arguments);
1544
1546
  }
1545
- var DURATION_COMPLETIONS = /*#__PURE__*/['$__interval', '$__range', '$__rate_interval', '1m', '5m', '10m', '30m', '1h', '1d'].map(function (text) {
1547
+ var DURATION_COMPLETIONS = /*#__PURE__*/['1m', '5m', '10m', '30m', '1h', '1d'].map(function (text) {
1548
+ return {
1549
+ type: 'DURATION',
1550
+ label: text,
1551
+ insertText: text
1552
+ };
1553
+ });
1554
+ var DURATION_VARIABLES_COMPLETIONS = /*#__PURE__*/['$__interval', '$__range', '$__rate_interval'].map(function (text) {
1546
1555
  return {
1547
1556
  type: 'DURATION',
1548
1557
  label: text,
@@ -1770,6 +1779,9 @@ function _getLabelValuesForMetricCompletions() {
1770
1779
  function getCompletions(situation, dataProvider) {
1771
1780
  switch (situation.type) {
1772
1781
  case 'IN_DURATION':
1782
+ if (dataProvider.durationVariablesCompletion) {
1783
+ return Promise.resolve(DURATION_VARIABLES_COMPLETIONS.concat(DURATION_COMPLETIONS));
1784
+ }
1773
1785
  return Promise.resolve(DURATION_COMPLETIONS);
1774
1786
  case 'IN_FUNCTION':
1775
1787
  return getAllFunctionsAndMetricNamesCompletions(dataProvider);
@@ -1989,6 +2001,8 @@ function PromQLEditor(props) {
1989
2001
  value = props.value,
1990
2002
  placeholder = props.placeholder,
1991
2003
  interpolateString = props.interpolateString,
2004
+ _props$enableAutocomp = props.enableAutocomplete,
2005
+ enableAutocomplete = _props$enableAutocomp === void 0 ? true : _props$enableAutocomp,
1992
2006
  onChange = props.onChange,
1993
2007
  onShiftEnter = props.onShiftEnter,
1994
2008
  onBlur = props.onBlur;
@@ -2025,32 +2039,35 @@ function PromQLEditor(props) {
2025
2039
  editor.onDidFocusEditorText(function () {
2026
2040
  isEditorFocused.set(true);
2027
2041
  });
2028
- var dataProvider = new DataProvider({
2029
- url: props.url,
2030
- lookbackInterval: props.lookbackInterval,
2031
- variablesNames: props.variablesNames,
2032
- request: props.request,
2033
- httpMethod: props.httpMethod,
2034
- apiPrefix: props.apiPrefix,
2035
- httpErrorHandler: props.httpErrorHandler
2036
- });
2037
- dataProviderRef.current = dataProvider;
2038
- dataProvider.start();
2039
- var completionProvider = getCompletionProvider(monaco, dataProvider);
2040
- var filteringCompletionProvider = _extends({}, completionProvider, {
2041
- provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2042
- var _editor$getModel;
2043
- if (((_editor$getModel = editor.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2044
- return {
2045
- suggestions: []
2046
- };
2042
+ if (enableAutocomplete) {
2043
+ var dataProvider = new DataProvider({
2044
+ url: props.url,
2045
+ lookbackInterval: props.lookbackInterval,
2046
+ variablesNames: props.variablesNames,
2047
+ durationVariablesCompletion: props.durationVariablesCompletion,
2048
+ request: props.request,
2049
+ httpMethod: props.httpMethod,
2050
+ apiPrefix: props.apiPrefix,
2051
+ httpErrorHandler: props.httpErrorHandler
2052
+ });
2053
+ dataProviderRef.current = dataProvider;
2054
+ dataProvider.start();
2055
+ var completionProvider = getCompletionProvider(monaco, dataProvider);
2056
+ var filteringCompletionProvider = _extends({}, completionProvider, {
2057
+ provideCompletionItems: function provideCompletionItems(model, position, context, token) {
2058
+ var _editor$getModel;
2059
+ if (((_editor$getModel = editor.getModel()) == null ? void 0 : _editor$getModel.id) !== model.id) {
2060
+ return {
2061
+ suggestions: []
2062
+ };
2063
+ }
2064
+ return completionProvider.provideCompletionItems(model, position, context, token);
2047
2065
  }
2048
- return completionProvider.provideCompletionItems(model, position, context, token);
2049
- }
2050
- });
2051
- var _monaco$languages$reg = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2052
- dispose = _monaco$languages$reg.dispose;
2053
- autocompleteDisposeFun.current = dispose;
2066
+ });
2067
+ var _monaco$languages$reg = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider),
2068
+ dispose = _monaco$languages$reg.dispose;
2069
+ autocompleteDisposeFun.current = dispose;
2070
+ }
2054
2071
  var updateElementHeight = function updateElementHeight() {
2055
2072
  var containerDiv = containerRef.current;
2056
2073
  if (containerDiv !== null) {
@@ -2073,7 +2090,6 @@ function PromQLEditor(props) {
2073
2090
  });
2074
2091
  // handle: shift + enter
2075
2092
  editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Enter, function () {
2076
- console.log(22222);
2077
2093
  onShiftEnter == null || onShiftEnter(editor.getValue());
2078
2094
  }, 'isEditorFocused' + id);
2079
2095
  if (placeholder) {