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' ;
65import { getInputSelection , setCaretPosition , insertAtCursor } from './getInputSelection' ;
76import SuggestionList from './SuggestionList' ;
87
9- import 'jsoneditor/dist/jsoneditor.min.css' ;
108import './editor.css' ;
9+ import JsonPathPreviewer from './JsonPathPreviewer' ;
1110
1211const defaultJsonToFilter = {
1312 'store' : {
@@ -51,8 +50,6 @@ class Editor extends Component {
5150 constructor ( props ) {
5251 super ( props ) ;
5352
54- this . jsonEditorRef = React . createRef ( ) ;
55- this . onJsonChange = this . onJsonChange . bind ( this ) ;
5653 this . onSelectSuggestion = this . onSelectSuggestion . bind ( this ) ;
5754 this . onCaretChanged = this . onCaretChanged . bind ( this ) ;
5855
@@ -64,10 +61,7 @@ class Editor extends Component {
6461 }
6562
6663 componentDidMount ( ) {
67- this . jsonEditor = new JSONEditor ( this . jsonEditorRef . current , { modes : [ 'tree' , 'code' ] , search : false , change : this . onJsonChange } ) ;
68- this . jsonEditor . set ( this . state . jsonToFilter ) ;
6964 this . initSchema ( this . props ) ;
70- this . jsonEditor . expandAll ( ) ;
7165 this . evalPathAndSuggestions ( this . props ) ;
7266 this . props . input . addEventListener ( 'keyup' , this . onCaretChanged ) ;
7367 this . props . input . addEventListener ( 'click' , this . onCaretChanged ) ;
@@ -90,7 +84,6 @@ class Editor extends Component {
9084 initSchema ( props ) {
9185 if ( props . jsonSchema ) {
9286 this . setState ( { jsonSchema : props . jsonSchema } ) ;
93- this . jsonEditor . setSchema ( props . jsonSchema ) ;
9487 } else {
9588 const schema = ejs ( this . state . jsonToFilter ) ;
9689 this . setState ( { jsonSchema : schema } ) ;
@@ -104,14 +97,10 @@ class Editor extends Component {
10497 this . setState ( { jsonToFilter : newProps . json } , ( ) => {
10598 resolve ( ) ;
10699 } ) ;
107- this . jsonEditor . set ( newProps . json ) ;
108100 } ) ;
109101
110102 promises . push ( jsonToFilterStatePromise ) ;
111103 }
112- if ( newProps . jsonSchema ) {
113- this . jsonEditor . setSchema ( newProps . jsonSchema ) ;
114- }
115104
116105 // ensure state modifications to jsonToFilter did propagate
117106 if ( promises . length > 0 ) {
@@ -134,22 +123,9 @@ class Editor extends Component {
134123 ) ;
135124
136125 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- }
146126 }
147127 }
148128
149- onJsonChange ( ) {
150- this . setState ( { jsonToFilter : this . jsonEditor . get ( ) } , ( ) => this . initSchema ( this . props ) ) ;
151- }
152-
153129 onSelectSuggestion ( suggestion ) {
154130 const initialJsonPathLength = this . props . jsonpath . length ;
155131
@@ -174,15 +150,20 @@ class Editor extends Component {
174150 left : this . props . position . x ,
175151 } ;
176152
153+ const { jsonpath} = this . props ;
154+ const { jsonToFilter, suggestions} = this . state ;
155+
177156 return < div className = 'react-json-path-editor-container' style = { style } onMouseEnter = { this . props . onMouseEnter } onMouseLeave = { this . props . onMouseLeave } >
178157 < div className = 'react-json-path-editor-intellisense' >
179158 < SuggestionList
180- suggestions = { this . state . suggestions }
159+ suggestions = { suggestions }
181160 onSelectSuggestion = { this . onSelectSuggestion }
182161 // onSelectSuggestionToSimulate={}
183162 />
184163 </ div >
185- < div className = 'react-json-path-editor-jsoneditor-container' ref = { this . jsonEditorRef } > </ div >
164+ < div className = 'react-json-path-editor-jsoneditor-container' >
165+ < JsonPathPreviewer json = { jsonToFilter } jsonPath = { jsonpath } />
166+ </ div >
186167 </ div > ;
187168 }
188169}
0 commit comments