@@ -16,7 +16,7 @@ class JsonPathPreviewer extends Component {
1616 evalJsonPath ( json , jsonPath ) {
1717 try {
1818 return jp . paths ( json , jsonPath ) ;
19- } catch ( e ) {
19+ } catch ( e ) {
2020 return [ ] ;
2121 }
2222 }
@@ -25,22 +25,22 @@ class JsonPathPreviewer extends Component {
2525 if ( Array . isArray ( jsonAsObject ) ) {
2626 const doesTraversingPathMatch = paths . filter ( oneOfPathsToRetrieve => oneOfPathsToRetrieve . join ( ',' ) === traversedPath . join ( ',' ) ) . length > 0 ;
2727 const isALengthPathMatchingThisCollection = paths . filter ( oneOfPathsToRetrieve => oneOfPathsToRetrieve . join ( ',' ) === [ ...traversedPath , 'length' ] . join ( ',' ) ) . length > 0 ;
28- return `${ carriageReturnTag + indentationIncrementationTag } ${ doesTraversingPathMatch ? highlightingTags . start : '' } [${ carriageReturnTag + indentationIncrementationTag } ${ jsonAsObject . map ( ( item , index ) =>
28+ return `${ carriageReturnTag + indentationIncrementationTag } ${ doesTraversingPathMatch ? highlightingTags . start : '' } [${ carriageReturnTag + indentationIncrementationTag } ${ jsonAsObject . map ( ( item , index ) =>
2929 this . tagPartOfJsonToHighlight ( item , paths , [ ...traversedPath , index ] )
3030 ) . join ( ',' + carriageReturnTag ) } ${ indentationDecrementationTag + carriageReturnTag } ]${
31- doesTraversingPathMatch ? highlightingTags . end : ''
32- } ${ isALengthPathMatchingThisCollection ? highlightingTags . start + '.length = ' + jsonAsObject . length + highlightingTags . end : '' } ${ indentationDecrementationTag } `;
31+ doesTraversingPathMatch ? highlightingTags . end : ''
32+ } ${ isALengthPathMatchingThisCollection ? highlightingTags . start + '.length = ' + jsonAsObject . length + highlightingTags . end : '' } ${ indentationDecrementationTag } `;
3333 }
3434
3535 if ( typeof jsonAsObject === 'object' ) {
3636 const doesTraversingPathMatch = paths . filter ( oneOfPathsToRetrieve => oneOfPathsToRetrieve . join ( ',' ) === traversedPath . join ( ',' ) ) . length > 0 ;
37- return `${ doesTraversingPathMatch ? highlightingTags . start : '' } {${ carriageReturnTag + indentationIncrementationTag } ${ Object . keys ( jsonAsObject ) . map ( key =>
37+ return `${ doesTraversingPathMatch ? highlightingTags . start : '' } {${ carriageReturnTag + indentationIncrementationTag } ${ Object . keys ( jsonAsObject ) . map ( key =>
3838 `"${ key } ": ${ this . tagPartOfJsonToHighlight ( jsonAsObject [ key ] , paths , [ ...traversedPath , key ] ) } `
3939 ) . join ( ',' + carriageReturnTag ) } ${ indentationDecrementationTag + carriageReturnTag } }${ doesTraversingPathMatch ? highlightingTags . end : '' } `;
4040 }
41-
41+
4242 const doesTraversingPathMatch = paths . filter ( oneOfPathsToRetrieve => oneOfPathsToRetrieve . join ( ',' ) === traversedPath . join ( ',' ) ) . length > 0 ;
43-
43+
4444 if ( typeof jsonAsObject === 'number' ) {
4545 return `${ doesTraversingPathMatch ? highlightingTags . start : '' } ${ jsonAsObject } ${ doesTraversingPathMatch ? highlightingTags . end : '' } ` ;
4646 } else {
@@ -50,14 +50,15 @@ class JsonPathPreviewer extends Component {
5050
5151 convertTaggedJsonAsReactComponent ( taggedJSON ) {
5252 let increments = 0 ;
53- let highlightBlock = false ;
53+ let isBlockHighlighted = false ;
5454 return taggedJSON . split ( carriageReturnTag ) . map ( line => {
5555 if ( line . includes ( indentationIncrementationTag ) ) increments ++ ;
56- if ( line . includes ( highlightingTags . start + '[' ) || line . includes ( highlightingTags . start + '{' ) ) highlightBlock = true ;
56+ if ( line . includes ( highlightingTags . start + '[' ) || line . includes ( highlightingTags . start + '{' ) ) isBlockHighlighted = true ;
57+ // const isLineSelectable = line.includes(':');
5758 const toReturn = < React . Fragment >
58- < p className = { highlightBlock ? 'highlighted' : '' } >
59+ < p className = { ( isBlockHighlighted ? 'highlighted ' : '' ) /*+ (isLineSelectable ? 'selectable' : '')*/ } >
5960 { Array ( increments ) . fill ( < React . Fragment > </ React . Fragment > ) }
60- { line . replace ( new RegExp ( indentationIncrementationTag , 'g' ) , '' ) . replace ( new RegExp ( indentationDecrementationTag , 'g' ) , '' )
61+ { line . replace ( new RegExp ( indentationIncrementationTag , 'g' ) , '' ) . replace ( new RegExp ( indentationDecrementationTag , 'g' ) , '' )
6162 . split ( highlightingTags . start ) . map ( jsonPart => {
6263 const parts = jsonPart . split ( highlightingTags . end ) ;
6364 if ( parts . length === 2 ) {
@@ -68,20 +69,22 @@ class JsonPathPreviewer extends Component {
6869 </ p >
6970 </ React . Fragment > ;
7071 if ( line . includes ( indentationDecrementationTag ) ) increments -- ;
71- if ( line . includes ( ']' + highlightingTags . end ) || line . includes ( '}' + highlightingTags . end ) ) highlightBlock = false ;
72+ if ( line . includes ( ']' + highlightingTags . end ) || line . includes ( '}' + highlightingTags . end ) ) isBlockHighlighted = false ;
7273 return toReturn ;
7374 } ) ;
7475 }
7576
7677 render ( ) {
77- const { json, jsonPath} = this . props ;
78+ const { json, jsonPath } = this . props ;
7879
7980 const pathsEvaluated = this . evalJsonPath ( json , jsonPath ) ;
8081
8182 return (
82- < code >
83- { this . convertTaggedJsonAsReactComponent ( this . tagPartOfJsonToHighlight ( json , pathsEvaluated ) ) }
84- </ code >
83+ < div className = 'react-json-path-editor-jsoneditor-container' >
84+ < code >
85+ { this . convertTaggedJsonAsReactComponent ( this . tagPartOfJsonToHighlight ( json , pathsEvaluated ) ) }
86+ </ code >
87+ </ div >
8588 ) ;
8689 }
8790}
0 commit comments