Skip to content

Commit

Permalink
Add panel options migration for editor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed Feb 5, 2024
1 parent 19ff7da commit 685a0d6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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 685a0d6

Please sign in to comment.