Skip to content

Commit 53ff9b9

Browse files
author
JeanBaptisteWATENBERG
authored
Merge pull request #3 from JeanBaptisteWATENBERG/preview-json-path-evaluation-in-context
Preview json path evaluation in context
2 parents 1f6905f + def64ae commit 53ff9b9

18 files changed

+630
-335
lines changed

.travis.yml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
sudo: false
2-
32
language: node_js
43
node_js:
5-
- 8
6-
4+
- 8
75
before_install:
8-
- npm install codecov.io coveralls
9-
6+
- npm install codecov.io coveralls
107
after_success:
11-
- cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js
12-
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
13-
8+
- cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js
9+
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
10+
- npm run docz:build
1411
branches:
1512
only:
16-
- master
13+
- master
14+
deploy:
15+
- provider: npm
16+
email: "jeanbaptiste.watenberg@gmail.com"
17+
on:
18+
branch: master
19+
api_key:
20+
secure: gpLMqNojmvhWMHWoskbjdNY053xujK8VbxiksEXj0b44eUvdGkiRnugeKEgaDPjBLl2vx9jjw+awQlUnNieWn+V3fFAq1BAAChJYR7kDD7Wl4hV2TiihXFWWwE89GdGggol5VbIsX+JZsCaPrrQnEjc6CFFSBBRdCLokGlURreT67cHb+n4kuwBalq5HX9Gvymdcpc4akWWhr73HQFT6hK4wpqsXU5TSvCe9jsHBjTTFmviLtUE3sLQB7iUG++2vLlSaRVqUx37ty5RSZgaPrvymMdb8xWMpCkdN+fr2fRSs/8YDy0eva4Vezcfjc5eBY7+LbEportVKp9JLaoyNvA0JJpUaHkGlOKTrfAIRVPfXJkk9IBxSqg+Lnse+RMKCU+eCN6W86EjLlvjREfpK8KkhWBqNBhg3ar0bcVm+7XQYp2FhXMrKK4KMK6PCQGyPr7ip7sbUJLJ5/zYraOq/s7vJuY1MqmJV2+QalYeWmD1+6iFOLlmQ4/hCwbp9Gdco7PHRX2flA2+Td1V++3rvRRLATfBQKosue00Bux1u1FSGIUTYGcSdHlTKZ0U0CsJXoUn0DnOOhwURdRsNK2t8j6R+CB4z0e0lIEqx4SG2+/+74FOJE7RuNi7+Us/By5g8a3fpbH/MSJ1cXlw5vrE4B88La4/tx4RMEUGAIU5R56U=
21+
- provider: surge
22+
project: .docz/dist
23+
domain: react-jsonpath-editor.surge.sh
24+
on:
25+
branch: master

demo/src/index.js

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,72 @@
1-
import React, {Component} from 'react'
2-
import {render} from 'react-dom'
1+
import React, {Component} from 'react';
2+
import {render} from 'react-dom';
33

4-
import JsonPathEditor from '../../src'
4+
import JsonPathEditor from '../../src';
5+
import Previewer from '../../src/components/JsonPathPreviewer';
56
import { generateRandomJson } from './generateRandomJson';
67

78

89
class Demo extends Component {
9-
constructor(props) {
10-
super(props)
10+
constructor(props) {
11+
super(props);
1112

12-
this.state = {
13-
json: generateRandomJson(5)
13+
this.state = {
14+
json: generateRandomJson(5)
15+
};
1416
}
15-
}
1617

17-
render() {
18-
const {json} = this.state;
19-
return <div>
20-
<h1>react-jsonpath-editor Demo</h1>
21-
<h2>With a default json</h2>
22-
<JsonPathEditor/>
23-
<h2>With a random json and a default value <button onClick={() => this.setState({json: generateRandomJson(5)})}>Click here to generate a new input json</button></h2>
24-
<JsonPathEditor value='$' json={json}/>
25-
<h2>With a default value</h2>
26-
<JsonPathEditor value='$.store.book'/>
27-
</div>
28-
}
18+
render() {
19+
const {json} = this.state;
20+
return <div>
21+
<h1>react-jsonpath-editor Demo</h1>
22+
<h2>With a default json</h2>
23+
<JsonPathEditor/>
24+
<h2>With a random json and a default value <button onClick={() => this.setState({json: generateRandomJson(5)})}>Click here to generate a new input json</button></h2>
25+
<JsonPathEditor value='$' json={json}/>
26+
<h2>With a default value</h2>
27+
<JsonPathEditor value='$.store.book'/>
28+
<br/>
29+
<br/>
30+
<br/>
31+
<Previewer json={{
32+
'store': {
33+
'book': [
34+
{
35+
'category': 'reference',
36+
'author': 'Nigel Rees',
37+
'title': 'Sayings of the Century',
38+
'price': 8.95
39+
},
40+
{
41+
'category': 'fiction',
42+
'author': 'Evelyn Waugh',
43+
'title': 'Sword of Honour',
44+
'price': 12.99
45+
},
46+
{
47+
'category': 'fiction',
48+
'author': 'Herman Melville',
49+
'title': 'Moby Dick',
50+
'isbn': '0-553-21311-3',
51+
'price': 8.99
52+
},
53+
{
54+
'category': 'fiction',
55+
'author': 'J. R. R. Tolkien',
56+
'title': 'The Lord of the Rings',
57+
'isbn': '0-395-19395-8',
58+
'price': 22.99
59+
}
60+
],
61+
'bicycle': {
62+
'color': 'red',
63+
'price': 19.95
64+
}
65+
}
66+
}} jsonPath='$.store' />
67+
68+
</div>;
69+
}
2970
}
3071

31-
render(<Demo/>, document.querySelector('#demo'))
72+
render(<Demo/>, document.querySelector('#demo'));

docs/react-jsonpath-editor.png

4.71 KB
Loading

doczrc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { css } from 'docz-plugin-css';
22
import { svgr } from 'docz-plugin-svgr';
33

44
export default {
5-
themeConfig: {
6-
mode: 'dark'
7-
},
85
plugins: [
96
svgr(),
107
css()

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-jsonpath-editor",
3-
"version": "1.1.0-beta.3",
3+
"version": "1.2.0",
44
"description": "react-jsonpath-editor React component",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -22,7 +22,6 @@
2222
},
2323
"dependencies": {
2424
"easy-json-schema": "0.0.2-beta",
25-
"jsoneditor": "^5.16.0",
2625
"jsonpath": "^1.0.0",
2726
"prop-types": "^15.6.2"
2827
},

src/JsonPathPreviewer.mdx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
name: react-json-path-previewer
3+
route: /json-path-previewer
4+
---
5+
6+
import { Playground, PropsTable } from 'docz'
7+
import { JsonPathPreviewer } from './';
8+
9+
# React JSON Path previewer
10+
11+
<PropsTable of={JsonPathPreviewer} />
12+
13+
## Basic usage
14+
15+
<Playground>
16+
<JsonPathPreviewer
17+
jsonPath='$..author'
18+
json={{
19+
'store': {
20+
'book': [
21+
{
22+
'category': 'reference',
23+
'author': 'Nigel Rees',
24+
'title': 'Sayings of the Century',
25+
'price': 8.95
26+
},
27+
{
28+
'category': 'fiction',
29+
'author': 'Evelyn Waugh',
30+
'title': 'Sword of Honour',
31+
'price': 12.99
32+
},
33+
{
34+
'category': 'fiction',
35+
'author': 'Herman Melville',
36+
'title': 'Moby Dick',
37+
'isbn': '0-553-21311-3',
38+
'price': 8.99
39+
},
40+
{
41+
'category': 'fiction',
42+
'author': 'J. R. R. Tolkien',
43+
'title': 'The Lord of the Rings',
44+
'isbn': '0-395-19395-8',
45+
'price': 22.99
46+
}
47+
],
48+
'bicycle': {
49+
'color': 'red',
50+
'price': 19.95
51+
}
52+
}
53+
}}/>
54+
</Playground>

src/components/Editor.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React, { Component } from 'react';
2-
import JSONEditor from 'jsoneditor';
32
import jp from 'jsonpath/jsonpath.min';
43
import ejs from 'easy-json-schema';
54
import { getSuggestions } from './suggestionBuilder';
65
import { getInputSelection, setCaretPosition, insertAtCursor } from './getInputSelection';
76
import SuggestionList from './SuggestionList';
87

9-
import 'jsoneditor/dist/jsoneditor.min.css';
108
import './editor.css';
9+
import JsonPathPreviewer from './JsonPathPreviewer';
1110

1211
const 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
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.highlighted {
2+
color: green;
3+
font-weight: bold;
4+
}
5+
6+
code {
7+
color: #777;
8+
font: 12px/normal 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
9+
}
10+
11+
code p {
12+
margin: 0;
13+
}

0 commit comments

Comments
 (0)