Handle invalid json
- fix style
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import * as diff from 'diff'
|
||||
import * as React from 'react'
|
||||
import ShowChart from '@material-ui/icons/ShowChart'
|
||||
import { JsonPropertyLocation, literalsMappedByLines } from '../../../../../backend/src/JsonAstParser'
|
||||
import { JsonPropertyLocation } from '../../../../../backend/src/JsonAstParser'
|
||||
import { lineChangeStyle, trimNewlineRight } from './util'
|
||||
import { Theme } from '@material-ui/core'
|
||||
import { withStyles } from '@material-ui/styles'
|
||||
@@ -15,21 +15,39 @@ interface Props {
|
||||
const style = (theme: Theme) => {
|
||||
return {
|
||||
gutterLine: {
|
||||
display: 'flex' as 'flex',
|
||||
textAlign: 'right' as 'right',
|
||||
paddingRight: theme.spacing(0.5),
|
||||
height: '16px',
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function tokensForLine(change: diff.Change, line: number, literalPositions: Array<JsonPropertyLocation>) {
|
||||
let diagram = literalPositions[line] ? <ShowChart style={{ height: '16px' }} /> : ''
|
||||
|
||||
if (change.added) {
|
||||
return [diagram, '+']
|
||||
} else if (change.removed) {
|
||||
return '-'
|
||||
} else {
|
||||
return [diagram, ' ']
|
||||
}
|
||||
}
|
||||
|
||||
function Gutters(props: Props) {
|
||||
let currentLine = -1
|
||||
const gutters = props.changes.map((change, key) => {
|
||||
return trimNewlineRight(change.value)
|
||||
.split('\n')
|
||||
.map((_, idx) => (
|
||||
<div key={`${key}-${idx}`} style={lineChangeStyle(change)} className={props.classes.gutterLine}>
|
||||
{change.added ? '+' : null}{change.removed ? '-' : null}{!change.added && !change.removed ? ' ' : null}
|
||||
</div>
|
||||
))
|
||||
.map((_, idx) => {
|
||||
currentLine = !change.removed ? currentLine + 1 : currentLine
|
||||
return (
|
||||
<div key={`${key}-${idx}`} style={lineChangeStyle(change)} className={props.classes.gutterLine}>
|
||||
{tokensForLine(change, currentLine, props.literalPositions)}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
}).reduce((a, b) => a.concat(b), [])
|
||||
|
||||
return <div>{gutters}</div>
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as Prism from 'prismjs'
|
||||
import * as React from 'react'
|
||||
import DiffCount from './DiffCount'
|
||||
import { CodeBlockColors, CodeBlockColorsBraceMonokai } from '../CodeBlockColors'
|
||||
import { literalsMappedByLines, parseJson } from '../../../../../backend/src/JsonAstParser'
|
||||
import { literalsMappedByLines } from '../../../../../backend/src/JsonAstParser'
|
||||
import { selectTextWithCtrlA } from '../../../utils/handleTextSelectWithCtrlA'
|
||||
import { Theme, withStyles } from '@material-ui/core'
|
||||
import 'prismjs/components/prism-json'
|
||||
@@ -28,7 +28,7 @@ class CodeDiff extends React.Component<Props, {}> {
|
||||
public render() {
|
||||
const changes = diff.diffLines(this.props.previous, this.props.current)
|
||||
const styledLines = Prism.highlight(this.props.current, Prism.languages.json, 'json').split('\n')
|
||||
const literalPositions = literalsMappedByLines(this.props.current)
|
||||
const literalPositions = literalsMappedByLines(this.props.current) || []
|
||||
|
||||
let lineNumber = 0
|
||||
const code = changes.map((change, key) => {
|
||||
@@ -78,6 +78,7 @@ const style = (theme: Theme) => {
|
||||
lineHeight: 'normal' as 'normal',
|
||||
paddingLeft: '4px',
|
||||
width: '100%',
|
||||
height: '16px',
|
||||
},
|
||||
codeWrapper: {
|
||||
maxHeight: '15em',
|
||||
|
||||
Reference in New Issue
Block a user