Skip to content

Commit

Permalink
Add option to enabled/disable wrapping lines in paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
asimonok committed Nov 15, 2023
1 parent 0015994 commit 5f1b245
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
13 changes: 11 additions & 2 deletions src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,17 @@ export const Text: React.FC<Props> = ({ options, frame, timeRange, timeZone, rep
*/
const getHtml = useCallback(
(data: any, content: string) =>
generateHtml({ data, content, helpers: options.helpers, timeRange, timeZone, replaceVariables, eventBus }),
[eventBus, options.helpers, replaceVariables, timeRange, timeZone]
generateHtml({
data,
content,
helpers: options.helpers,
timeRange,
timeZone,
replaceVariables,
eventBus,
options,
}),
[eventBus, replaceVariables, timeRange, timeZone, options]
);

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/constants/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const DefaultOptions: PanelOptions = {
helpers: '',
status: '',
styles: '',
wrap: true,
};
8 changes: 8 additions & 0 deletions src/constants/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export const EditorsOptions = [
{ value: EditorType.HELPERS, label: 'JavaScript Code' },
{ value: EditorType.STYLES, label: 'Styles' },
];

/**
* Wrap Options
*/
export const WrapOptions = [
{ value: true, label: 'Enabled' },
{ value: false, label: 'Disabled' },
];
5 changes: 4 additions & 1 deletion src/helpers/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { config, locationService } from '@grafana/runtime';
import { TimeZone } from '@grafana/schema';
import { registerHelpers } from './handlebars';
import { replaceVariablesHelper } from './variable';
import { PanelOptions } from '../types';

/**
* Helpers
Expand All @@ -23,6 +24,7 @@ export const generateHtml = ({
timeZone,
replaceVariables,
eventBus,
options,
}: {
data: Record<string, any>;
content: string;
Expand All @@ -31,6 +33,7 @@ export const generateHtml = ({
timeZone: TimeZone;
replaceVariables: InterpolateFunction;
eventBus: EventBus;
options: PanelOptions;
}): { html: string; unsubscribe?: unknown } => {
/**
* Variable
Expand Down Expand Up @@ -93,7 +96,7 @@ export const generateHtml = ({
/**
* Render Markdown
*/
const html = md.render(markdown);
const html = options.wrap ? md.render(markdown) : md.renderInline(markdown);

/**
* Skip sanitizing if disabled in Grafana
Expand Down
19 changes: 18 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Field, FieldConfigProperty, FieldType, PanelPlugin } from '@grafana/data';
import { config } from '@grafana/runtime';
import { HelpersEditor, ResourcesEditor, StylesEditor, TextEditor, TextPanel } from './components';
import { CodeLanguageOptions, DefaultOptions, EditorsOptions, EveryRowOptions, FormatOptions } from './constants';
import {
CodeLanguageOptions,
DefaultOptions,
EditorsOptions,
EveryRowOptions,
FormatOptions,
WrapOptions,
} from './constants';
import { EditorType, PanelOptions } from './types';

/**
Expand Down Expand Up @@ -110,6 +117,16 @@ export const plugin = new PanelPlugin<PanelOptions>(TextPanel)
* Content
*/
builder
.addRadio({
path: 'wrap',
name: 'Wrap automatically in paragraphs',
description: 'If disabled, result will NOT be wrapped into <p> tags',
defaultValue: DefaultOptions.wrap,
settings: {
options: WrapOptions,
},
category: ['Content'],
})
.addCustomEditor({
id: 'content',
path: 'content',
Expand Down
7 changes: 7 additions & 0 deletions src/types/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,11 @@ export interface PanelOptions {
* @type {Resource[]}
*/
externalScripts: Resource[];

/**
* Wrap
*
* @type {boolean}
*/
wrap: boolean;
}

0 comments on commit 5f1b245

Please sign in to comment.