Skip to content

Commit

Permalink
Add context to non-visual mode (#245)
Browse files Browse the repository at this point in the history
* ft Add context to non-visual mode

* Update CHANGELOG.md

* Add code editor suggestions

* Add panel options migration for editor mode

---------

Co-authored-by: Mikhail Volkov <[email protected]>
Co-authored-by: asimonok <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent b3b3b68 commit 81fac77
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 37 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Change Log

## 5.2.0 (IN PROGRESS)
## 5.2.0 (2024-02-05)

### Features / Enhancements

- Updated README and documentation (#214)
- Add visual editor for working with data sources (#211)
- Update ESLint configuration and refactoring (#237)
- Update dependencies and Actions (#238)
- Add context parameter to non-visual mode (#245)

## 5.1.0 (2023-08-11)

Expand Down
2 changes: 1 addition & 1 deletion src/components/EchartsEditor/EchartsEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const EchartsEditor: React.FC<Props> = ({ value, onChange, context, item
});

if (item.id === Editor.VISUALCODE) {
return [...CODE_EDITOR_SUGGESTIONS, ...VISUAL_CODE_EDITOR_SUGGESTIONS, ...suggestions];
return [...VISUAL_CODE_EDITOR_SUGGESTIONS, ...suggestions];
}

return [...CODE_EDITOR_SUGGESTIONS, ...suggestions];
Expand Down
1 change: 1 addition & 0 deletions src/components/EchartsPanel/EchartsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const EchartsPanel: React.FC<Props> = ({ options, data, width, height, re
'locationService',
'notifySuccess',
'notifyError',
'context',
options.getOption
);

Expand Down
131 changes: 96 additions & 35 deletions src/constants/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,89 @@ export const FORMAT_OPTIONS = [
{ value: Format.NONE, label: 'Disabled' },
];

/**
* Editor Base Context Suggestions
*/
const EDITOR_BASE_CONTEXT_SUGGESTIONS: CodeEditorSuggestionItem[] = [
{
label: 'context',
kind: CodeEditorSuggestionItemKind.Constant,
detail: 'All passed possible properties and methods.',
},

/**
* Panel
*/
{
label: 'context.panel',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Panel instance properties.',
},
{
label: 'context.panel.data',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Panel data.',
},
{
label: 'context.panel.chart',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'ECharts instance.',
},

/**
* Grafana
*/
{
label: 'context.grafana',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Grafana properties and methods.',
},
{
label: 'context.grafana.theme',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Grafana theme.',
},
{
label: 'context.grafana.replaceVariables',
kind: CodeEditorSuggestionItemKind.Method,
detail: 'Interpolate variables.',
},
{
label: 'context.grafana.eventBus',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Panels events.',
},
{
label: 'context.grafana.locationService',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Location service.',
},
{
label: 'context.grafana.notifySuccess',
kind: CodeEditorSuggestionItemKind.Method,
detail: 'Show success notification.',
},
{
label: 'context.grafana.notifyError',
kind: CodeEditorSuggestionItemKind.Method,
detail: 'Show error notification.',
},

/**
* Echarts
*/
{
label: 'context.echarts',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'ECharts library.',
},
{
label: 'context.ecStat',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'A statistical and data mining tool.',
},
];

/**
* Suggestions
*/
Expand Down Expand Up @@ -103,47 +186,25 @@ export const CODE_EDITOR_SUGGESTIONS: CodeEditorSuggestionItem[] = [
kind: CodeEditorSuggestionItemKind.Method,
detail: 'Display error notification.',
},
{
label: 'context',
kind: CodeEditorSuggestionItemKind.Constant,
detail: 'All passed possible properties and methods.',
},
{
label: 'context.panel',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Panel instance properties.',
},
{
label: 'context.panel.data',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Panel data.',
},
{
label: 'context.panel.chart',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'ECharts instance.',
},
{
label: 'context.grafana',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'Grafana properties and methods.',
},
{
label: 'context.echarts',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'ECharts library.',
},
{
label: 'context.ecStat',
kind: CodeEditorSuggestionItemKind.Property,
detail: 'A statistical and data mining tool.',
},

/**
* Context
*/
...EDITOR_BASE_CONTEXT_SUGGESTIONS,
];

/**
* Visual Code Editor Suggestions
*/
export const VISUAL_CODE_EDITOR_SUGGESTIONS: CodeEditorSuggestionItem[] = [
/**
* Context
*/
...EDITOR_BASE_CONTEXT_SUGGESTIONS,

/**
* Visual Editor
*/
{
label: 'context.editor',
kind: CodeEditorSuggestionItemKind.Property,
Expand Down
29 changes: 29 additions & 0 deletions src/migration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { EditorMode } from './constants';
import { getMigratedOptions } from './migration';
import { PanelOptions } from './types';

describe('Migrations', () => {
it('Should return panel options', () => {
const options: Partial<PanelOptions> = {
editorMode: EditorMode.VISUAL,
};

expect(
getMigratedOptions({
options: options,
} as any)
).toEqual(options);
});

describe('5.2.0', () => {
it('Should set code to editor mode if not defined', () => {
expect(
getMigratedOptions({
options: {},
} as any)
).toEqual({
editorMode: EditorMode.CODE,
});
});
});
});
32 changes: 32 additions & 0 deletions src/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PanelModel } from '@grafana/data';

import { EditorMode } from './constants';
import { PanelOptions } from './types';

/**
* Outdated Panel Options
*/
interface OutdatedPanelOptions extends Omit<PanelOptions, 'editorMode'> {
/**
* Editor Mode introduced in 5.2.0
*/
editorMode?: EditorMode;
}

/**
* Get Migrated Options
*
* @param panel
*/
export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): PanelOptions => {
const { ...options } = panel.options;

/**
* Set code editor mode if not defined
*/
if (!options.editorMode) {
options.editorMode = EditorMode.CODE;
}

return options;
};
2 changes: 2 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import {
Theme,
THEME_OPTIONS,
} from './constants';
import { getMigratedOptions } from './migration';
import { PanelOptions } from './types';

/**
* Panel Plugin
*/
export const plugin = new PanelPlugin<PanelOptions>(EchartsPanel)
.setNoPadding()
.setMigrationHandler(getMigratedOptions)
.setPanelOptions((builder) => {
const isCodeEditor = (config: PanelOptions) => config.editorMode !== EditorMode.VISUAL;
const isVisualEditor = (config: PanelOptions) => config.editorMode === EditorMode.VISUAL;
Expand Down

0 comments on commit 81fac77

Please sign in to comment.