11import React , { Component } from 'react' ;
2- import JSONEditor from 'jsoneditor' ;
32import jp from 'jsonpath/jsonpath.min' ;
43import ejs from 'easy-json-schema' ;
54import { getSuggestions } from './suggestionBuilder' ;
@@ -8,6 +7,7 @@ import SuggestionList from './SuggestionList';
87
98import 'jsoneditor/dist/jsoneditor.min.css' ;
109import './editor.css' ;
10+ import JsonPathPreviewer from './JsonPathPreviewer' ;
1111
1212const defaultJsonToFilter = {
1313 'store' : {
@@ -51,7 +51,6 @@ class Editor extends Component {
5151 constructor ( props ) {
5252 super ( props ) ;
5353
54- this . jsonEditorRef = React . createRef ( ) ;
5554 this . onJsonChange = this . onJsonChange . bind ( this ) ;
5655 this . onSelectSuggestion = this . onSelectSuggestion . bind ( this ) ;
5756 this . onCaretChanged = this . onCaretChanged . bind ( this ) ;
@@ -64,10 +63,7 @@ class Editor extends Component {
6463 }
6564
6665 componentDidMount ( ) {
67- this . jsonEditor = new JSONEditor ( this . jsonEditorRef . current , { modes : [ 'tree' , 'code' ] , search : false , change : this . onJsonChange } ) ;
68- this . jsonEditor . set ( this . state . jsonToFilter ) ;
6966 this . initSchema ( this . props ) ;
70- this . jsonEditor . expandAll ( ) ;
7167 this . evalPathAndSuggestions ( this . props ) ;
7268 this . props . input . addEventListener ( 'keyup' , this . onCaretChanged ) ;
7369 this . props . input . addEventListener ( 'click' , this . onCaretChanged ) ;
@@ -90,7 +86,6 @@ class Editor extends Component {
9086 initSchema ( props ) {
9187 if ( props . jsonSchema ) {
9288 this . setState ( { jsonSchema : props . jsonSchema } ) ;
93- this . jsonEditor . setSchema ( props . jsonSchema ) ;
9489 } else {
9590 const schema = ejs ( this . state . jsonToFilter ) ;
9691 this . setState ( { jsonSchema : schema } ) ;
@@ -104,14 +99,10 @@ class Editor extends Component {
10499 this . setState ( { jsonToFilter : newProps . json } , ( ) => {
105100 resolve ( ) ;
106101 } ) ;
107- this . jsonEditor . set ( newProps . json ) ;
108102 } ) ;
109103
110104 promises . push ( jsonToFilterStatePromise ) ;
111105 }
112- if ( newProps . jsonSchema ) {
113- this . jsonEditor . setSchema ( newProps . jsonSchema ) ;
114- }
115106
116107 // ensure state modifications to jsonToFilter did propagate
117108 if ( promises . length > 0 ) {
@@ -134,20 +125,11 @@ class Editor extends Component {
134125 ) ;
135126
136127 this . setState ( { suggestions } ) ;
137-
138- try {
139- const filteredJson = jp . query ( this . state . jsonToFilter , props . jsonpath ) ;
140- this . jsonEditor . set ( filteredJson ) ;
141- this . jsonEditor . expandAll ( ) ;
142- } catch ( e ) {
143- this . jsonEditor . set ( this . state . jsonToFilter ) ;
144- this . jsonEditor . expandAll ( ) ;
145- }
146128 }
147129 }
148130
149131 onJsonChange ( ) {
150- this . setState ( { jsonToFilter : this . jsonEditor . get ( ) } , ( ) => this . initSchema ( this . props ) ) ;
132+ this . initSchema ( this . props ) ;
151133 }
152134
153135 onSelectSuggestion ( suggestion ) {
@@ -174,15 +156,20 @@ class Editor extends Component {
174156 left : this . props . position . x ,
175157 } ;
176158
159+ const { jsonpath} = this . props ;
160+ const { jsonToFilter, suggestions} = this . state ;
161+
177162 return < div className = 'react-json-path-editor-container' style = { style } onMouseEnter = { this . props . onMouseEnter } onMouseLeave = { this . props . onMouseLeave } >
178163 < div className = 'react-json-path-editor-intellisense' >
179164 < SuggestionList
180- suggestions = { this . state . suggestions }
165+ suggestions = { suggestions }
181166 onSelectSuggestion = { this . onSelectSuggestion }
182167 // onSelectSuggestionToSimulate={}
183168 />
184169 </ div >
185- < div className = 'react-json-path-editor-jsoneditor-container' ref = { this . jsonEditorRef } > </ div >
170+ < div className = 'react-json-path-editor-jsoneditor-container' >
171+ < JsonPathPreviewer json = { jsonToFilter } jsonPath = { jsonpath } />
172+ </ div >
186173 </ div > ;
187174 }
188175}
0 commit comments