-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathjson-node.tsx
239 lines (213 loc) · 6.83 KB
/
json-node.tsx
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { useContext, useRef, useState, isValidElement, useMemo, useCallback } from 'react'
import { JsonViewContext } from './json-view'
import {
customCopy,
customDelete,
customEdit,
editableDelete,
editableEdit,
isObject,
isReactComponent,
safeCall,
stringifyForCopying,
resolveEvalFailedNewValue,
customMatchesURL
} from '../utils'
import ObjectNode from './object-node'
import LongString from './long-string'
import CopyButton from './copy-button'
import { ReactComponent as EditSVG } from '../svgs/edit.svg'
import { ReactComponent as DeleteSVG } from '../svgs/trash.svg'
import { ReactComponent as DoneSVG } from '../svgs/done.svg'
import { ReactComponent as CancelSVG } from '../svgs/cancel.svg'
import { ReactComponent as LinkSVG } from '../svgs/link.svg'
import type { CustomizeNode, CustomizeOptions } from '../types'
interface Props {
node: any
depth: number
deleteHandle?: (indexOrName: string | number) => void
editHandle?: (indexOrName: string | number, newValue: any, oldValue: any) => void
indexOrName?: number | string
parent?: Record<string, any> | Array<any>
}
export default function JsonNode({ node, depth, deleteHandle: _deleteHandle, indexOrName, parent, editHandle }: Props) {
// prettier-ignore
const { collapseStringsAfterLength, enableClipboard, editable, src, onDelete, onChange, customizeNode, matchesURL, urlRegExp, EditComponent, DoneComponent, CancelComponent } = useContext(JsonViewContext)
let customReturn: ReturnType<CustomizeNode> | undefined
if (typeof customizeNode === 'function') customReturn = safeCall(customizeNode, [{ node, depth, indexOrName }])
if (customReturn) {
if (isValidElement(customReturn)) return customReturn
else if (isReactComponent(customReturn)) {
const CustomComponent = customReturn
return <CustomComponent node={node} depth={depth} indexOrName={indexOrName} />
}
}
if (Array.isArray(node) || isObject(node)) {
return (
<ObjectNode
node={node}
depth={depth}
indexOrName={indexOrName}
deleteHandle={_deleteHandle}
customOptions={typeof customReturn === 'object' ? (customReturn as CustomizeOptions) : undefined}
/>
)
} else {
const type = typeof node
const [editing, setEditing] = useState(false)
const [deleting, setDeleting] = useState(false)
const valueRef = useRef<HTMLSpanElement>(null)
const edit = () => {
setEditing(true)
setTimeout(() => {
window.getSelection()?.selectAllChildren(valueRef.current!)
valueRef.current?.focus()
})
}
const done = useCallback(() => {
let newValue = valueRef.current!.innerText
try {
const parsedValue = JSON.parse(newValue)
if (editHandle) editHandle(indexOrName!, parsedValue, node)
} catch (e) {
const trimmedStringValue = resolveEvalFailedNewValue(type, newValue)
if (editHandle) editHandle(indexOrName!, trimmedStringValue, node)
}
setEditing(false)
}, [editHandle])
const cancel = () => {
setEditing(false)
setDeleting(false)
}
const deleteHandle = () => {
setDeleting(false)
if (_deleteHandle) _deleteHandle(indexOrName!)
if (onDelete)
onDelete({
value: node,
depth,
src,
indexOrName: indexOrName!,
parentType: Array.isArray(parent) ? 'array' : 'object'
})
if (onChange)
onChange({
depth,
src,
indexOrName: indexOrName!,
parentType: Array.isArray(parent) ? 'array' : 'object',
type: 'delete'
})
}
const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Enter') {
event.preventDefault()
done()
} else if (event.key === 'Escape') {
cancel()
}
},
[done]
)
const isEditing = editing || deleting
const ctrlClick =
!isEditing && editableEdit(editable) && customEdit(customReturn as CustomizeOptions | undefined) && editHandle
? (event: React.MouseEvent) => {
if (event.ctrlKey || event.metaKey) edit()
}
: undefined
const Icons = (
<>
{isEditing &&
(typeof DoneComponent === 'function' ? (
<DoneComponent className='json-view--edit' style={{ display: 'inline-block' }} onClick={deleting ? deleteHandle : done} />
) : (
<DoneSVG className='json-view--edit' style={{ display: 'inline-block' }} onClick={deleting ? deleteHandle : done} />
))}
{isEditing &&
(typeof CancelComponent === 'function' ? (
<CancelComponent className='json-view--edit' style={{ display: 'inline-block' }} onClick={cancel} />
) : (
<CancelSVG className='json-view--edit' style={{ display: 'inline-block' }} onClick={cancel} />
))}
{!isEditing && enableClipboard && customCopy(customReturn as CustomizeOptions | undefined) && <CopyButton node={node} />}
{!isEditing && matchesURL && type === 'string' && urlRegExp.test(node) && customMatchesURL(customReturn as CustomizeOptions | undefined) && (
<a href={node} target='_blank' className='json-view--link'>
<LinkSVG />
</a>
)}
{!isEditing &&
editableEdit(editable) &&
customEdit(customReturn as CustomizeOptions | undefined) &&
editHandle &&
(typeof EditComponent === 'function' ? (
<EditComponent className='json-view--edit' onClick={edit} />
) : (
<EditSVG className='json-view--edit' onClick={edit} />
))}
{!isEditing && editableDelete(editable) && customDelete(customReturn as CustomizeOptions | undefined) && _deleteHandle && (
<DeleteSVG className='json-view--edit' onClick={() => setDeleting(true)} />
)}
</>
)
let className = 'json-view--string'
switch (type) {
case 'number':
case 'bigint':
className = 'json-view--number'
break
case 'boolean':
className = 'json-view--boolean'
break
case 'object':
className = 'json-view--null'
break
}
if (typeof (customReturn as CustomizeOptions)?.className === 'string') className += ' ' + (customReturn as CustomizeOptions).className
if (deleting) className += ' json-view--deleting'
let displayValue = String(node)
if (type === 'bigint') displayValue += 'n'
const EditingElement = useMemo(
() => (
<span
contentEditable
className={className}
dangerouslySetInnerHTML={{ __html: type === 'string' ? `"${displayValue}"` : displayValue }}
ref={valueRef}
onKeyDown={handleKeyDown}
/>
),
[displayValue, type, handleKeyDown]
)
if (type === 'string')
return (
<>
{editing ? (
EditingElement
) : node.length > collapseStringsAfterLength ? (
<LongString str={node} ref={valueRef} className={className} ctrlClick={ctrlClick} />
) : (
<span className={className} onClick={ctrlClick}>
"{displayValue}"
</span>
)}
{Icons}
</>
)
else {
return (
<>
{editing ? (
EditingElement
) : (
<span className={className} onClick={ctrlClick}>
{displayValue}
</span>
)}
{Icons}
</>
)
}
}
}