mirror of
https://github.com/grafana/grafana.git
synced 2025-02-10 15:45:43 -06:00
Fixes #15635 Importing ansicolor from node_modules or vendored as is failed because there is no ES5 transpiler configured for ES6 libs. - remove ansicolor from package.json - add ansicolor to public/vendor/ansicolor and port to typescript - adapt all ansicolor imports - add `public/vendor` as a path to be considered by ts compiler
75 lines
1.5 KiB
TypeScript
75 lines
1.5 KiB
TypeScript
import React, { PureComponent } from 'react';
|
|
import ansicolor from 'vendor/ansicolor/ansicolor';
|
|
|
|
interface Style {
|
|
[key: string]: string;
|
|
}
|
|
|
|
interface ParsedChunk {
|
|
style: Style;
|
|
text: string;
|
|
}
|
|
|
|
function convertCSSToStyle(css: string): Style {
|
|
return css.split(/;\s*/).reduce((accumulated, line) => {
|
|
const match = line.match(/([^:\s]+)\s*:\s*(.+)/);
|
|
|
|
if (match && match[1] && match[2]) {
|
|
const key = match[1].replace(/-(a-z)/g, (_, character) => character.toUpperCase());
|
|
accumulated[key] = match[2];
|
|
}
|
|
|
|
return accumulated;
|
|
}, {});
|
|
}
|
|
|
|
interface Props {
|
|
value: string;
|
|
}
|
|
|
|
interface State {
|
|
chunks: ParsedChunk[];
|
|
prevValue: string;
|
|
}
|
|
|
|
export class LogMessageAnsi extends PureComponent<Props, State> {
|
|
state = {
|
|
chunks: [],
|
|
prevValue: '',
|
|
};
|
|
|
|
static getDerivedStateFromProps(props: Props, state: State) {
|
|
if (props.value === state.prevValue) {
|
|
return null;
|
|
}
|
|
|
|
const parsed = ansicolor.parse(props.value);
|
|
|
|
return {
|
|
chunks: parsed.spans.map(span => {
|
|
return span.css
|
|
? {
|
|
style: convertCSSToStyle(span.css),
|
|
text: span.text,
|
|
}
|
|
: { text: span.text };
|
|
}),
|
|
prevValue: props.value,
|
|
};
|
|
}
|
|
|
|
render() {
|
|
const { chunks } = this.state;
|
|
|
|
return chunks.map((chunk, index) =>
|
|
chunk.style ? (
|
|
<span key={index} style={chunk.style}>
|
|
{chunk.text}
|
|
</span>
|
|
) : (
|
|
chunk.text
|
|
)
|
|
);
|
|
}
|
|
}
|