Refactor value renderer

This commit is contained in:
Thomas Nordquist
2019-07-17 15:18:27 +02:00
parent c86659f61c
commit d651fc7a6c
4 changed files with 36 additions and 43 deletions

View File

@@ -8,7 +8,7 @@ import { isPlottable, lineChangeStyle, trimNewlineRight } from './util'
import { JsonPropertyLocation, literalsMappedByLines } from '../../../../../backend/src/JsonAstParser' import { JsonPropertyLocation, literalsMappedByLines } from '../../../../../backend/src/JsonAstParser'
import { selectTextWithCtrlA } from '../../../utils/handleTextSelectWithCtrlA' import { selectTextWithCtrlA } from '../../../utils/handleTextSelectWithCtrlA'
import { style } from './style' import { style } from './style'
import { withStyles } from '@material-ui/core' import { withStyles, Typography } from '@material-ui/core'
import 'prismjs/components/prism-json' import 'prismjs/components/prism-json'
interface Props { interface Props {
@@ -17,6 +17,7 @@ interface Props {
current: string current: string
nameOfCompareMessage: string nameOfCompareMessage: string
language?: 'json' language?: 'json'
title?: string
classes: any classes: any
} }
@@ -88,7 +89,8 @@ class CodeDiff extends React.PureComponent<Props, State> {
const code = this.renderStyledCodeLines(changes) const code = this.renderStyledCodeLines(changes)
return ( return (
<div> <div style={{ marginTop: '8px' }}>
{this.props.title ? <Typography className={this.props.classes.title}>{this.props.title}</Typography> : null}
<div tabIndex={0} onKeyDown={this.handleCtrlA} className={this.props.classes.codeWrapper}> <div tabIndex={0} onKeyDown={this.handleCtrlA} className={this.props.classes.codeWrapper}>
<Gutters <Gutters
className={this.props.classes.gutters} className={this.props.classes.gutters}

View File

@@ -11,6 +11,11 @@ export const style = (theme: Theme) => {
} }
return { return {
title: {
font: codeBaseStyle.font,
paddingLeft: '4px',
backgroundColor: codeBlockColors.gutters,
},
line: { line: {
lineHeight: 'normal' as 'normal', lineHeight: 'normal' as 'normal',
paddingLeft: '4px', paddingLeft: '4px',
@@ -22,7 +27,6 @@ export const style = (theme: Theme) => {
maxHeight: '15em', maxHeight: '15em',
overflow: 'auto', overflow: 'auto',
backgroundColor: `${codeBlockColors.background}`, backgroundColor: `${codeBlockColors.background}`,
margin: '8px 0 0 0',
}, },
gutters: { gutters: {
...codeBaseStyle, ...codeBaseStyle,

View File

@@ -91,7 +91,7 @@ function ValuePanel(props: Props) {
<span>Value {copyValue}</span> <span>Value {copyValue}</span>
<span style={{ width: '100%' }}> <span style={{ width: '100%' }}>
{renderViewOptions()} {renderViewOptions()}
<div style={{ marginBottom: '-8px' }}> <div style={{ marginBottom: '-8px', marginTop: '8px' }}>
<React.Suspense fallback={<div>Loading...</div>}> <React.Suspense fallback={<div>Loading...</div>}>
<RenderedValue node={node} compareMessage={compareMessage} /> <RenderedValue node={node} compareMessage={compareMessage} />
</React.Suspense> </React.Suspense>

View File

@@ -25,18 +25,34 @@ class ValueRenderer extends React.Component<Props, State> {
this.state = { width: 0 } this.state = { width: 0 }
} }
private renderDiff(current: string = '', previous: string = '', language?: 'json') { private renderDiff(current: string = '', previous: string = '', title?: string, language?: 'json') {
return ( return (
<CodeDiff <CodeDiff
treeNode={this.props.treeNode} treeNode={this.props.treeNode}
previous={previous} previous={previous}
current={current} current={current}
title={title}
language={language} language={language}
nameOfCompareMessage={this.props.compareWith ? 'selected' : 'previous'} nameOfCompareMessage={this.props.compareWith ? 'selected' : 'previous'}
/> />
) )
} }
private convertMessage(msg?: Base64Message): string | undefined {
if (!msg) {
return
}
const str = Base64Message.toUnicodeString(msg)
try {
JSON.parse(str)
} catch (error) {
return str
}
return this.messageToPrettyJson(str)
}
private messageToPrettyJson(str: string): string | undefined { private messageToPrettyJson(str: string): string | undefined {
try { try {
const json = JSON.parse(str) const json = JSON.parse(str)
@@ -46,21 +62,11 @@ class ValueRenderer extends React.Component<Props, State> {
} }
} }
private updateWidth = (width: number) => {
if (width !== this.state.width) {
this.setState({ width })
}
}
private renderRawValue(value: string, compare: string) {
return this.renderDiff(value, compare)
}
private renderRawMode(message: q.Message, compare?: q.Message) { private renderRawMode(message: q.Message, compare?: q.Message) {
if (!message.value) { if (!message.value) {
return return
} }
const value = Base64Message.toUnicodeString(message.value) const value = this.convertMessage(message.value)
if (!compare) { if (!compare) {
return this.renderDiff(value, value) return this.renderDiff(value, value)
} }
@@ -68,14 +74,12 @@ class ValueRenderer extends React.Component<Props, State> {
if (!compare.value) { if (!compare.value) {
return return
} }
const compareStr = Base64Message.toUnicodeString(compare.value) const compareStr = this.convertMessage(compare.value)
return ( return (
<div> <div>
<Typography>selected</Typography>
{this.renderDiff(compareStr, compareStr)}
<Typography>current</Typography>
{this.renderDiff(value, value)} {this.renderDiff(value, value)}
{this.renderDiff(compareStr, compareStr, 'selected')}
</div> </div>
) )
} }
@@ -88,39 +92,22 @@ class ValueRenderer extends React.Component<Props, State> {
const { message, treeNode, compareWith, renderMode } = this.props const { message, treeNode, compareWith, renderMode } = this.props
const previousMessages = treeNode.messageHistory.toArray() const previousMessages = treeNode.messageHistory.toArray()
const previousMessage = previousMessages[previousMessages.length - 2] const previousMessage = previousMessages[previousMessages.length - 2]
let compareMessage = compareWith || previousMessage || message const compareMessage = compareWith || previousMessage || message
if (renderMode === 'raw') { if (renderMode === 'raw') {
return this.renderRawMode(message, compareWith) return this.renderRawMode(message, compareWith)
compareMessage = message
} }
if (!message.value) { if (!message.value) {
return null return null
} }
const compareValue = compareMessage.value || message.value const compareValue = compareMessage.value || message.value
const str = Base64Message.toUnicodeString(message.value) const current = this.convertMessage(message.value)
const compareStr = Base64Message.toUnicodeString(compareValue) const compare = this.convertMessage(compareValue)
let json const language = current && compare ? 'json' : undefined
try {
json = JSON.parse(str)
} catch (error) {
return this.renderRawValue(str, compareStr)
}
if (typeof json === 'string') { return this.renderDiff(current, compare, undefined, language)
return this.renderRawValue(str, compareStr)
} else if (typeof json === 'number') {
return this.renderRawValue(str, compareStr)
} else if (typeof json === 'boolean') {
return this.renderRawValue(str, compareStr)
} else {
const current = this.messageToPrettyJson(str) || str
const compare = this.messageToPrettyJson(compareStr) || compareStr
const language = current && compare ? 'json' : undefined
return this.renderDiff(current, compare, language)
}
} }
} }