@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.
@@ -13,6 +13,7 @@ export declare class DataProvider {
13
13
  metrics: string[];
14
14
  labelKeys: string[];
15
15
  metricsMetadata?: PromMetricsMetadata;
16
+ durationVariablesCompletion: boolean;
16
17
  constructor(params: DataProviderParams);
17
18
  getVariablesNames(): string[];
18
19
  setVariablesNames(variablesNames: string[]): void;
@@ -5,6 +5,8 @@ interface PromQLEditorProps {
5
5
  theme?: 'light' | 'dark';
6
6
  value?: string;
7
7
  placeholder?: string;
8
+ enableAutocomplete?: boolean;
9
+ durationVariablesCompletion?: boolean;
8
10
  interpolateString?: (query: string) => string;
9
11
  onChange?: (value: string) => void;
10
12
  onShiftEnter?: (value: string) => void;
@@ -22,6 +22,7 @@ export interface DataProviderParams {
22
22
  url?: string;
23
23
  lookbackInterval?: number;
24
24
  variablesNames?: string[];
25
+ durationVariablesCompletion?: boolean;
25
26
  request: FetchFn;
26
27
  httpMethod?: 'POST' | 'GET';
27
28
  apiPrefix?: string;
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "@fc-components/monaco-editor",
3
- "version": "0.1.1",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "0.1.3",
4
7
  "license": "MIT",
5
8
  "main": "dist/index.js",
6
9
  "module": "dist/monaco-editor.esm.js",
@@ -30,6 +30,7 @@ export class DataProvider {
30
30
  metrics: string[];
31
31
  labelKeys: string[];
32
32
  metricsMetadata?: PromMetricsMetadata;
33
+ durationVariablesCompletion: boolean;
33
34
 
34
35
  constructor(params: DataProviderParams) {
35
36
  this.inputInRange = '';
@@ -44,6 +45,7 @@ export class DataProvider {
44
45
  if (params.variablesNames) {
45
46
  this.variablesNames = [...params.variablesNames];
46
47
  }
48
+ this.durationVariablesCompletion = params.durationVariablesCompletion ?? true;
47
49
  if (params.request) {
48
50
  this.customRequest = params.request;
49
51
  }
@@ -61,7 +61,13 @@ async function getAllFunctionsAndMetricNamesCompletions(dataProvider: DataProvid
61
61
  return [...FUNCTION_COMPLETIONS, ...metricNames];
62
62
  }
63
63
 
64
- const DURATION_COMPLETIONS: Completion[] = ['$__interval', '$__range', '$__rate_interval', '1m', '5m', '10m', '30m', '1h', '1d'].map((text) => ({
64
+ const DURATION_COMPLETIONS: Completion[] = ['1m', '5m', '10m', '30m', '1h', '1d'].map((text) => ({
65
+ type: 'DURATION',
66
+ label: text,
67
+ insertText: text,
68
+ }));
69
+
70
+ const DURATION_VARIABLES_COMPLETIONS: Completion[] = ['$__interval', '$__range', '$__rate_interval'].map((text) => ({
65
71
  type: 'DURATION',
66
72
  label: text,
67
73
  insertText: text,
@@ -165,6 +171,9 @@ async function getLabelValuesForMetricCompletions(
165
171
  export function getCompletions(situation: Situation, dataProvider: DataProvider): Promise<Completion[]> {
166
172
  switch (situation.type) {
167
173
  case 'IN_DURATION':
174
+ if (dataProvider.durationVariablesCompletion) {
175
+ return Promise.resolve(DURATION_VARIABLES_COMPLETIONS.concat(DURATION_COMPLETIONS));
176
+ }
168
177
  return Promise.resolve(DURATION_COMPLETIONS);
169
178
  case 'IN_FUNCTION':
170
179
  return getAllFunctionsAndMetricNamesCompletions(dataProvider);
@@ -18,6 +18,8 @@ interface PromQLEditorProps {
18
18
  theme?: 'light' | 'dark';
19
19
  value?: string;
20
20
  placeholder?: string;
21
+ enableAutocomplete?: boolean;
22
+ durationVariablesCompletion?: boolean;
21
23
  interpolateString?: (query: string) => string;
22
24
  onChange?: (value: string) => void;
23
25
  onShiftEnter?: (value: string) => void;
@@ -68,7 +70,7 @@ const getStyles = (placeholder?: string) => {
68
70
 
69
71
  export default function PromQLEditor(props: PromQLEditorProps & DataProviderParams) {
70
72
  const id = uuidv4();
71
- const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, onChange, onShiftEnter, onBlur } = props;
73
+ const { size = 'middle', theme = 'light', variablesNames, value, placeholder, interpolateString, enableAutocomplete = true, onChange, onShiftEnter, onBlur } = props;
72
74
  const autocompleteDisposeFun = useRef<(() => void) | null>(null);
73
75
  const containerRef = useRef<HTMLDivElement>(null);
74
76
  const dataProviderRef = useRef<DataProvider | null>(null);
@@ -106,33 +108,36 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
106
108
  isEditorFocused.set(true);
107
109
  });
108
110
 
109
- const dataProvider = new DataProvider({
110
- url: props.url,
111
- lookbackInterval: props.lookbackInterval,
112
- variablesNames: props.variablesNames,
113
- request: props.request,
114
- httpMethod: props.httpMethod,
115
- apiPrefix: props.apiPrefix,
116
- httpErrorHandler: props.httpErrorHandler,
117
- });
118
- dataProviderRef.current = dataProvider;
119
- dataProvider.start();
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();
120
124
 
121
- const completionProvider = getCompletionProvider(monaco, dataProvider);
125
+ const completionProvider = getCompletionProvider(monaco, dataProvider);
122
126
 
123
- const filteringCompletionProvider: monacoTypes.languages.CompletionItemProvider = {
124
- ...completionProvider,
125
- provideCompletionItems: (model, position, context, token) => {
126
- if (editor.getModel()?.id !== model.id) {
127
- return { suggestions: [] };
128
- }
129
- return completionProvider.provideCompletionItems(model, position, context, token);
130
- },
131
- };
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
+ };
132
136
 
133
- const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
137
+ const { dispose } = monaco.languages.registerCompletionItemProvider(PROMQL_LANG_ID, filteringCompletionProvider);
134
138
 
135
- autocompleteDisposeFun.current = dispose;
139
+ autocompleteDisposeFun.current = dispose;
140
+ }
136
141
 
137
142
  const updateElementHeight = () => {
138
143
  const containerDiv = containerRef.current;
@@ -158,7 +163,6 @@ export default function PromQLEditor(props: PromQLEditorProps & DataProviderPara
158
163
  editor.addCommand(
159
164
  monaco.KeyMod.Shift | monaco.KeyCode.Enter,
160
165
  () => {
161
- console.log(22222);
162
166
  onShiftEnter?.(editor.getValue());
163
167
  },
164
168
  'isEditorFocused' + id,
@@ -28,6 +28,7 @@ export interface DataProviderParams {
28
28
  url?: string;
29
29
  lookbackInterval?: number;
30
30
  variablesNames?: string[];
31
+ durationVariablesCompletion?: boolean;
31
32
  request: FetchFn;
32
33
  httpMethod?: 'POST' | 'GET';
33
34
  apiPrefix?: string;
@@ -25,5 +25,5 @@ export function makeSelector(metricName: string, labels?: Label[], labelName?: s
25
25
  return `${label.name}${label.op}"${escapeLabelValueInExactSelector(label.value)}"`;
26
26
  });
27
27
 
28
- return `{${allLabelTexts.join(',')}}`;
28
+ return `${metricName}{${allLabelTexts.join(',')}}`;
29
29
  }