Skip to content

Commit 8961a8d

Browse files
Merge pull request #8 from JeanBaptisteWATENBERG/improve-property-matching
(feat) improve property matching suggestion for array of object
2 parents ecba2d7 + 975c74f commit 8961a8d

File tree

10 files changed

+183
-104
lines changed

10 files changed

+183
-104
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ class App extends Component {
3434

3535
## Properties
3636

37-
| Props | Type | Required | Description |
38-
|---------------|:-------------:|:---------:|-------------------------------------------------------|
39-
| inputProps | `object` | `false` | Properties passed down to the input |
40-
| value | `string` | `false` | input default value |
41-
| json | `object` | `false` | json to test the json path and provide autocompletion |
42-
| onChange | `function` | `false` | callback called when jsonPath changed |
43-
| editorPosition| `object` | `false` | {x,y} overrides the position of the editor |
37+
| Props | Type | Required | Description |
38+
|-------------------|:----------------------:|:---------:|-------------------------------------------------------|
39+
| inputProps | `object` | `false` | Properties passed down to the input |
40+
| value | `string` | `false` | input default value |
41+
| json | `object` | `false` | json to test the json path and provide autocompletion |
42+
| onChange | `function` | `false` | callback called when jsonPath changed |
43+
| editorPosition | `object` | `false` | {x,y} overrides the position of the editor |
44+
| previewOrientation| `right` or `left` | `false` | Defines orientation of preview. default to right |
4445

4546
[build-badge]: https://img.shields.io/travis/JeanBaptisteWATENBERG/react-jsonpath-editor/master.png?style=flat-square
4647
[build]: https://travis-ci.org/JeanBaptisteWATENBERG/react-jsonpath-editor

demo/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Demo extends Component {
2626
<h2>With a default value</h2>
2727
<JsonPathEditor value='$.store.book'/>
2828
<br/>
29+
{'------------------------------------------------------------------------------------------------------------------ '}<JsonPathEditor value='$.store.book' previewOrientation='left'/>
2930
<br/>
3031
<br/>
3132
<Previewer json={{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-jsonpath-editor",
3-
"version": "1.3.3",
3+
"version": "1.4.0",
44
"description": "react-jsonpath-editor React component",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/components/Editor.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,19 @@ class Editor extends Component {
150150
left: this.props.position.x,
151151
};
152152

153-
const {jsonpath} = this.props;
153+
const {jsonpath, previewOrientation} = this.props;
154154
const {jsonToFilter, suggestions} = this.state;
155155

156156
return <div className='react-json-path-editor-container' style={style} onMouseEnter={this.props.onMouseEnter} onMouseLeave={this.props.onMouseLeave}>
157+
{previewOrientation && previewOrientation === 'left' && <JsonPathPreviewer json={jsonToFilter} jsonPath={jsonpath}/>}
157158
<div className='react-json-path-editor-intellisense'>
158159
<SuggestionList
159160
suggestions={suggestions}
160161
onSelectSuggestion={this.onSelectSuggestion}
161162
// onSelectSuggestionToSimulate={}
162163
/>
163164
</div>
164-
<div className='react-json-path-editor-jsoneditor-container'>
165-
<JsonPathPreviewer json={jsonToFilter} jsonPath={jsonpath}/>
166-
</div>
165+
{(!previewOrientation || previewOrientation === 'right') && <JsonPathPreviewer json={jsonToFilter} jsonPath={jsonpath}/>}
167166
</div>;
168167
}
169168
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
.highlighted {
1+
.react-json-path-editor-jsoneditor-container code .highlighted {
22
color: green;
33
font-weight: bold;
44
}
55

6-
code {
6+
.react-json-path-editor-jsoneditor-container code p.selectable:hover {
7+
background-color: grey;
8+
color: white;
9+
cursor: pointer;
10+
font-weight: bold;
11+
}
12+
13+
.react-json-path-editor-jsoneditor-container code {
714
color: #777;
815
font: 12px/normal 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
916
}
1017

11-
code p {
18+
.react-json-path-editor-jsoneditor-container code p {
1219
margin: 0;
1320
}

src/components/JsonPathPreviewer.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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>&nbsp;</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
}

src/components/editor.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
}
88

99
.react-json-path-editor-jsoneditor-container {
10-
width: 500px;
10+
width: 487px;
11+
margin-right: 10px;
1112
max-height: 500px;
1213
overflow: auto;
1314
background: white;

0 commit comments

Comments
 (0)