Skip to content

Commit afdb337

Browse files
feat(preview) add preview orientation prop
1 parent 69fb625 commit afdb337

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
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={{

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
}

src/components/JsonPathPreviewer.js

Lines changed: 16 additions & 14 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 {
@@ -54,11 +54,11 @@ class JsonPathPreviewer extends Component {
5454
return taggedJSON.split(carriageReturnTag).map(line => {
5555
if (line.includes(indentationIncrementationTag)) increments++;
5656
if (line.includes(highlightingTags.start + '[') || line.includes(highlightingTags.start + '{')) isBlockHighlighted = true;
57-
const isLineSelectable = line.includes(':');
57+
// const isLineSelectable = line.includes(':');
5858
const toReturn = <React.Fragment>
59-
<p className={(isBlockHighlighted ? 'highlighted ' : '') + (isLineSelectable ? 'selectable' : '')}>
59+
<p className={(isBlockHighlighted ? 'highlighted ' : '') /*+ (isLineSelectable ? 'selectable' : '')*/}>
6060
{Array(increments).fill(<React.Fragment>&nbsp;</React.Fragment>)}
61-
{line.replace(new RegExp(indentationIncrementationTag,'g'), '').replace(new RegExp(indentationDecrementationTag,'g'), '')
61+
{line.replace(new RegExp(indentationIncrementationTag, 'g'), '').replace(new RegExp(indentationDecrementationTag, 'g'), '')
6262
.split(highlightingTags.start).map(jsonPart => {
6363
const parts = jsonPart.split(highlightingTags.end);
6464
if (parts.length === 2) {
@@ -75,14 +75,16 @@ class JsonPathPreviewer extends Component {
7575
}
7676

7777
render() {
78-
const {json, jsonPath} = this.props;
78+
const { json, jsonPath } = this.props;
7979

8080
const pathsEvaluated = this.evalJsonPath(json, jsonPath);
8181

8282
return (
83-
<code>
84-
{this.convertTaggedJsonAsReactComponent(this.tagPartOfJsonToHighlight(json, pathsEvaluated))}
85-
</code>
83+
<div className='react-json-path-editor-jsoneditor-container'>
84+
<code>
85+
{this.convertTaggedJsonAsReactComponent(this.tagPartOfJsonToHighlight(json, pathsEvaluated))}
86+
</code>
87+
</div>
8688
);
8789
}
8890
}

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;

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import JsonPathPreviewer from './components/JsonPathPreviewer';
1010
* - onChange? -- function called when input value change
1111
* - json? -- json to edit
1212
* - editorPosition? -- {x,y} overrides the position of the editor
13+
* - previewOrientation? -- left or rigth, default to rigth
1314
*/
1415
class ReactJsonPath extends Component {
1516

@@ -54,7 +55,8 @@ class ReactJsonPath extends Component {
5455
this.setState({editorPosition: this.props.editorPosition});
5556
} else if (this.inputRef && this.inputRef.current) {
5657
const inputBoundRect = this.inputRef.current.getBoundingClientRect();
57-
this.setState({editorPosition: {x:inputBoundRect.left, y: inputBoundRect.top + inputBoundRect.height}});
58+
const xOffset = this.props.previewOrientation && this.props.previewOrientation === 'left' ? -500 : 0;
59+
this.setState({editorPosition: {x:inputBoundRect.left + xOffset, y: inputBoundRect.top + inputBoundRect.height}});
5860
}
5961
}
6062

@@ -101,7 +103,7 @@ class ReactJsonPath extends Component {
101103
}
102104

103105
render() {
104-
const {inputProps, json} = this.props;
106+
const {inputProps, json, previewOrientation} = this.props;
105107
const {value, editorOpened, editorPosition} = this.state;
106108
return <React.Fragment>
107109
<input ref={this.inputRef} type='text' value={value} onChange={this.onChange} onFocus={this.onFocus} onBlur={this.onBlur} {...inputProps} />
@@ -112,7 +114,8 @@ class ReactJsonPath extends Component {
112114
json={json}
113115
onJsonPathChanged={this.changePath}
114116
onMouseEnter={this.disableBlur}
115-
onMouseLeave={this.onMouseLeaveFromEditor} />}
117+
onMouseLeave={this.onMouseLeaveFromEditor}
118+
previewOrientation={previewOrientation} />}
116119
</React.Fragment>;
117120
}
118121
}
@@ -131,6 +134,8 @@ ReactJsonPath.propTypes = {
131134
x: PropTypes.number,
132135
y: PropTypes.number
133136
}),
137+
/** Defines orientation of preview. default to right */
138+
previewOrientation: PropTypes.oneOf(['left', 'right'])
134139
};
135140

136141

0 commit comments

Comments
 (0)