Refactor value rendering

This commit is contained in:
Thomas Nordquist
2019-02-25 17:07:29 +01:00
parent 6b859d31e8
commit b9c0a66948
2 changed files with 19 additions and 28 deletions

View File

@@ -8,26 +8,25 @@ import 'prismjs/themes/prism-tomorrow.css'
interface Props {
previous: string
current: string
language?: 'json'
classes: any
}
interface State {
}
class CodeDiff extends React.Component<Props, State> {
class CodeDiff extends React.Component<Props, {}> {
constructor(props: Props) {
super(props)
}
public render() {
const changes = diff.diffLines(this.props.previous, this.props.current)
const styledLines = Prism.highlight(this.props.current, Prism.languages.json).split('\n')
let lineNumber = 0
const code = changes.map((change, key) => {
const hasStyledCode = change.removed !== true
const changedLines = change.count || 0
if (hasStyledCode) {
if (hasStyledCode && this.props.language === 'json') {
const html = styledLines.slice(lineNumber, lineNumber + changedLines).join('\n')
lineNumber += changedLines
@@ -37,7 +36,11 @@ class CodeDiff extends React.Component<Props, State> {
return <div key={key}><span className={this.cssClassForChange(change)}>{change.value}</span></div>
})
return <pre style={{ maxHeight: '200px' }} className="language-json">{code}</pre>
return (
<pre className={`language-json ${this.props.classes.codeBlock}`}>
{code}
</pre>
)
}
private cssClassForChange(change: diff.Change) {
@@ -49,7 +52,7 @@ class CodeDiff extends React.Component<Props, State> {
return this.props.classes.deletion
}
return this.props.classes.code
return this.props.classes.noChange
}
}
@@ -58,10 +61,12 @@ const style = (theme: Theme) => {
width: '100%',
}
return {
code: {
codeBlock: {
fontSize: '12px',
maxHeight: '200px',
},
noChange: {
...baseStyle,
// backgroundColor: theme.palette.background.paper,
// color: theme.palette.text.primary,
},
deletion: {
...baseStyle,