grafana/public/app/plugins/panel/text/TextPanel.tsx

110 lines
2.7 KiB
TypeScript
Raw Normal View History

// Libraries
import { css, cx } from '@emotion/css';
import DangerouslySetHtmlContent from 'dangerously-set-html-content';
import { debounce } from 'lodash';
import React, { PureComponent } from 'react';
import { PanelProps, renderTextPanelMarkdown, textUtil } from '@grafana/data';
// Utils
import { CustomScrollbar, stylesFactory } from '@grafana/ui';
2019-02-23 23:53:20 -06:00
import config from 'app/core/config';
2019-02-23 23:53:20 -06:00
// Types
import { PanelOptions, TextMode } from './models.gen';
2019-02-23 23:53:20 -06:00
export interface Props extends PanelProps<PanelOptions> {}
2019-02-23 23:53:20 -06:00
interface State {
html: string;
}
2019-03-06 17:54:19 -06:00
export class TextPanel extends PureComponent<Props, State> {
constructor(props: Props) {
2019-02-23 23:53:20 -06:00
super(props);
this.state = {
2019-03-05 13:14:22 -06:00
html: this.processContent(props.options),
2019-02-23 23:53:20 -06:00
};
}
updateHTML = debounce(() => {
const html = this.processContent(this.props.options);
if (html !== this.state.html) {
this.setState({ html });
}
2019-03-06 17:54:19 -06:00
}, 150);
2019-02-23 23:53:20 -06:00
componentDidUpdate(prevProps: Props) {
// Since any change could be referenced in a template variable,
// This needs to process every time (with debounce)
2019-02-23 23:53:20 -06:00
this.updateHTML();
}
prepareHTML(html: string): string {
const result = this.interpolateString(html);
return config.disableSanitizeHtml ? result : this.sanitizeString(result);
2019-02-23 23:53:20 -06:00
}
prepareMarkdown(content: string): string {
// Always interpolate variables before converting to markdown
// because `marked` replaces '{' and '}' in URLs with '%7B' and '%7D'
// See https://marked.js.org/demo
let result = this.interpolateString(content);
if (config.disableSanitizeHtml) {
result = renderTextPanelMarkdown(result, {
noSanitize: true,
});
return result;
}
result = renderTextPanelMarkdown(result);
return this.sanitizeString(result);
2019-02-23 23:53:20 -06:00
}
interpolateString(content: string): string {
const { replaceVariables } = this.props;
return replaceVariables(content, {}, 'html');
}
sanitizeString(content: string): string {
return textUtil.sanitizeTextPanelContent(content);
2019-02-23 23:53:20 -06:00
}
processContent(options: PanelOptions): string {
2019-02-23 23:53:20 -06:00
const { mode, content } = options;
if (!content) {
return '';
}
if (mode === TextMode.HTML) {
return this.prepareHTML(content);
2019-02-23 23:53:20 -06:00
}
return this.prepareMarkdown(content);
2019-02-23 23:53:20 -06:00
}
render() {
const { html } = this.state;
const styles = getStyles();
return (
<CustomScrollbar autoHeightMin="100%">
<DangerouslySetHtmlContent
html={html}
className={cx('markdown-html', styles.content)}
data-testid="TextPanel-converted-content"
/>
</CustomScrollbar>
);
2019-02-23 23:53:20 -06:00
}
}
const getStyles = stylesFactory(() => {
return {
content: css`
height: 100%;
`,
};
});