From e8d399ef94fecc623c1eff5414f266f74a13a016 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Fri, 30 Oct 2020 13:14:14 +0530 Subject: [PATCH] TextPanel: Fixes problems where text panel would show old content (#28643) (#28687) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 16a1d2f744f8a47a74cac1369902ca5f73d57a43) Co-authored-by: Torkel Ödegaard --- .../text/textPanelMigrationHandler.test.ts | 18 +++++++++++++++++ .../panel/text/textPanelMigrationHandler.ts | 20 +++++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/panel/text/textPanelMigrationHandler.test.ts b/public/app/plugins/panel/text/textPanelMigrationHandler.test.ts index e61c01178c2..009443a6caa 100644 --- a/public/app/plugins/panel/text/textPanelMigrationHandler.test.ts +++ b/public/app/plugins/panel/text/textPanelMigrationHandler.test.ts @@ -8,12 +8,30 @@ describe('textPanelMigrationHandler', () => { const panel: any = { content: 'Hello World', mode: 'html', + options: {}, }; const result = textPanelMigrationHandler(panel); expect(result.content).toEqual('Hello World'); expect(result.mode).toEqual('html'); + expect(panel.content).toBeUndefined(); + expect(panel.mode).toBeUndefined(); + }); + }); + + describe('when invoked and previous version 7.1 or later', () => { + it('then not migrate options', () => { + const panel: any = { + content: 'Hello World', + mode: 'html', + options: { content: 'New content' }, + pluginVersion: '7.1.0', + }; + + const result = textPanelMigrationHandler(panel); + + expect(result.content).toEqual('New content'); }); }); diff --git a/public/app/plugins/panel/text/textPanelMigrationHandler.ts b/public/app/plugins/panel/text/textPanelMigrationHandler.ts index eb30abd086f..7c87f5fd1fe 100644 --- a/public/app/plugins/panel/text/textPanelMigrationHandler.ts +++ b/public/app/plugins/panel/text/textPanelMigrationHandler.ts @@ -2,19 +2,27 @@ import { PanelModel } from '@grafana/data'; import { TextMode, TextOptions } from './types'; export const textPanelMigrationHandler = (panel: PanelModel): Partial => { + const previousVersion = parseFloat(panel.pluginVersion || '6.1'); + let options = panel.options; + // Migrates old Angular based text panel props to new props if (panel.hasOwnProperty('content') && panel.hasOwnProperty('mode')) { - const oldTextPanel: { content: string; mode: string } = (panel as unknown) as any; + const oldTextPanel: any = panel as any; const content = oldTextPanel.content; - const mode = (oldTextPanel.mode as unknown) as TextMode; + const mode = oldTextPanel.mode as TextMode; - return { content, mode }; + delete oldTextPanel.content; + delete oldTextPanel.mode; + + if (previousVersion < 7.1) { + options = { content, mode }; + } } // The 'text' mode has been removed so we need to update any panels still using it to markdown - if (panel.options.mode !== 'html' && panel.options.mode !== 'markdown') { - return { content: panel.options.content, mode: 'markdown' }; + if (options.mode !== 'html' && options.mode !== 'markdown') { + options = { ...options, mode: 'markdown' }; } - return panel.options; + return options; };