diff --git a/src/migration.test.ts b/src/migration.test.ts new file mode 100644 index 0000000..18f85d4 --- /dev/null +++ b/src/migration.test.ts @@ -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 = { + 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, + }); + }); + }); +}); diff --git a/src/migration.ts b/src/migration.ts new file mode 100644 index 0000000..5ad12bc --- /dev/null +++ b/src/migration.ts @@ -0,0 +1,32 @@ +import { PanelModel } from '@grafana/data'; + +import { EditorMode } from './constants'; +import { PanelOptions } from './types'; + +/** + * Outdated Panel Options + */ +interface OutdatedPanelOptions extends Omit { + /** + * Editor Mode introduced in 5.2.0 + */ + editorMode?: EditorMode; +} + +/** + * Get Migrated Options + * + * @param panel + */ +export const getMigratedOptions = (panel: PanelModel): PanelOptions => { + const { ...options } = panel.options; + + /** + * Set code editor mode if not defined + */ + if (!options.editorMode) { + options.editorMode = EditorMode.CODE; + } + + return options; +}; diff --git a/src/module.ts b/src/module.ts index 8b8eaf1..5a7398a 100644 --- a/src/module.ts +++ b/src/module.ts @@ -13,6 +13,7 @@ import { Theme, THEME_OPTIONS, } from './constants'; +import { getMigratedOptions } from './migration'; import { PanelOptions } from './types'; /** @@ -20,6 +21,7 @@ import { PanelOptions } from './types'; */ export const plugin = new PanelPlugin(EchartsPanel) .setNoPadding() + .setMigrationHandler(getMigratedOptions) .setPanelOptions((builder) => { const isCodeEditor = (config: PanelOptions) => config.editorMode !== EditorMode.VISUAL; const isVisualEditor = (config: PanelOptions) => config.editorMode === EditorMode.VISUAL;