mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Move xss and sanitize packages to grafana-data
* Move text, url and location utils to grafana-data
* Move grafana config types to grafana-data
* Move field display value proxy to grafana-data
* Fix
* Move data links built in vars to grafana-data
* Attach links supplier to when applying field overrides
* Prep tests
* Use links suppliers attached via field overrides
* locationUtil dependencies type
* Move sanitize-url declaration to grafana-data
* Revert "Move sanitize-url declaration to grafana-data"
This reverts commit 11db9f5e55
.
* Fix typo
* fix ts vol1
* Remove import from runtime in data.... Make TS happy at the same time ;)
* Lovely TS, please shut up
* Lovely TS, please shut up vol2
* fix tests
* Fixes
* minor refactor
* Attach get links to FieldDisplayValue for seamless usage
* Update packages/grafana-data/src/field/fieldOverrides.ts
* Make storybook build
82 lines
1.9 KiB
TypeScript
82 lines
1.9 KiB
TypeScript
// Libraries
|
|
import React, { PureComponent } from 'react';
|
|
import { debounce } from 'lodash';
|
|
import { PanelProps, renderMarkdown, textUtil } from '@grafana/data';
|
|
// Utils
|
|
import config from 'app/core/config';
|
|
// Types
|
|
import { TextOptions } from './types';
|
|
|
|
interface Props extends PanelProps<TextOptions> {}
|
|
interface State {
|
|
html: string;
|
|
}
|
|
|
|
export class TextPanel extends PureComponent<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props);
|
|
|
|
this.state = {
|
|
html: this.processContent(props.options),
|
|
};
|
|
}
|
|
|
|
updateHTML = debounce(() => {
|
|
const html = this.processContent(this.props.options);
|
|
if (html !== this.state.html) {
|
|
this.setState({ html });
|
|
}
|
|
}, 150);
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
// Since any change could be referenced in a template variable,
|
|
// This needs to process everytime (with debounce)
|
|
this.updateHTML();
|
|
}
|
|
|
|
prepareHTML(html: string): string {
|
|
const { replaceVariables } = this.props;
|
|
|
|
html = replaceVariables(html, {}, 'html');
|
|
|
|
return config.disableSanitizeHtml ? html : textUtil.sanitize(html);
|
|
}
|
|
|
|
prepareText(content: string): string {
|
|
return this.prepareHTML(
|
|
content
|
|
.replace(/&/g, '&')
|
|
.replace(/>/g, '>')
|
|
.replace(/</g, '<')
|
|
.replace(/\n/g, '<br/>')
|
|
);
|
|
}
|
|
|
|
prepareMarkdown(content: string): string {
|
|
return this.prepareHTML(renderMarkdown(content));
|
|
}
|
|
|
|
processContent(options: TextOptions): string {
|
|
const { mode, content } = options;
|
|
|
|
if (!content) {
|
|
return '';
|
|
}
|
|
|
|
if (mode === 'markdown') {
|
|
return this.prepareMarkdown(content);
|
|
}
|
|
if (mode === 'html') {
|
|
return this.prepareHTML(content);
|
|
}
|
|
|
|
return this.prepareText(content);
|
|
}
|
|
|
|
render() {
|
|
const { html } = this.state;
|
|
|
|
return <div className="markdown-html panel-text-content" dangerouslySetInnerHTML={{ __html: html }} />;
|
|
}
|
|
}
|