-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathJSONEditor.tsx
More file actions
121 lines (107 loc) · 4.08 KB
/
JSONEditor.tsx
File metadata and controls
121 lines (107 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { useEffect, useState } from 'react'
import { JsonEditor } from 'json-edit-react'
import './JSONEditor.styles.scss'
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { AppDispatch, RootState } from '../../store';
import { setModifiedResult } from '../../store/slices/centerPanelSlice';
import { SearchBox } from "@fluentui/react-components";
interface JSONEditorProps {
processId?: string | null;
}
const JSONEditor: React.FC<JSONEditorProps> = () => {
const [jsonData, setJsonData] = React.useState({})
const [isFocused, setIsFocused] = useState(false);
const dispatch = useDispatch<AppDispatch>();
const [searchText, setSearchText] = useState('');
const searchBoxRef = React.useRef<HTMLDivElement | null>(null);
const store = useSelector((state: RootState) => ({
processId: state.leftPanel.processId,
contentData: state.centerPanel.contentData,
cLoader: state.centerPanel.cLoader,
cError: state.centerPanel.cError,
isJSONEditorSearchEnabled: state.centerPanel.isJSONEditorSearchEnabled
}), shallowEqual);
useEffect(() => {
if (!store.cLoader) {
if (Object.keys(store.contentData).length > 0) {
const formattedJson = store.contentData.result;
const data = {
...formattedJson
}
setJsonData(data);
} else {
setJsonData({})
}
}
}, [store.contentData])
const onUpdateHandle = (newData: any) => {
dispatch(setModifiedResult(newData));
}
const handleFocus = () => setIsFocused(true);
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
const newFocusTarget = e.relatedTarget as HTMLElement | null;
if (
searchBoxRef.current &&
newFocusTarget &&
searchBoxRef.current.contains(newFocusTarget)
) {
return;
}
setIsFocused(false);
};
return (
<>{
store.cLoader ? <div className={"JSONEditorLoader"}><p>Loading...</p></div> :
Object.keys(jsonData).length == 0 ? <p style={{ textAlign: 'center' }}>No data available</p> :
<div className="JSONEditor-container">
{store.isJSONEditorSearchEnabled &&
<div className="JSONEditor-searchDiv">
<div style={{ display: 'flex', justifyContent: 'flex-end' }} ref={searchBoxRef}>
<SearchBox
size="small"
placeholder="Search"
onFocus={handleFocus}
onBlur={handleBlur}
value={searchText}
onChange={(e, data) => { setIsFocused(true); setSearchText(data.value) }}
style={{
width: isFocused ? '200px' : '100px',
transition: 'width 0.3s ease',
}}
/>
</div></div>}
<div className="JSONEditor-contentDiv">
<JsonEditor
data={jsonData}
className='JSONEditorClass'
rootName="extracted_result"
searchText={searchText}
searchFilter={"all"}
searchDebounceTime={300}
theme={[{
styles: {
container: {
width: '89%',
minWidth: '100%',
fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Apple Color Emoji", "Segoe UI Emoji", sans-serif',
fontSize: '14px',
paddingTop: '0px'
},
}
}]}
onUpdate={({ newData, currentData, newValue, currentValue, name, path }) => {
onUpdateHandle(newData)
}}
//setData={ setJsonData } // optional
// restrictEdit={({ key, path, level, index, value, size, parentData, fullData, collapsed }) => {
// return !path.includes('extracted_result')
// }
// }
restrictDelete={true}
/>
</div>
</div>
}</>
)
}
export default JSONEditor;