-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathDataKeyPair.tsx
273 lines (266 loc) · 8.13 KB
/
DataKeyPair.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import {
Check as CheckIcon,
ChevronRight as ChevronRightIcon,
Close as CloseIcon,
ContentCopy as ContentCopyIcon,
Edit as EditIcon,
ExpandMore as ExpandMoreIcon
} from '@mui/icons-material'
import { Box, styled } from '@mui/material'
import type React from 'react'
import { useCallback, useMemo, useState } from 'react'
import { useTextColor } from '../hooks/useColor'
import { useClipboard } from '../hooks/useCopyToClipboard'
import { useInspect } from '../hooks/useInspect'
import { useJsonViewerStore } from '../stores/JsonViewerStore'
import { useTypeComponents } from '../stores/typeRegistry'
import type { DataItemProps } from '../type'
import { DataBox } from './mui/DataBox'
export type DataKeyPairProps = {
value: unknown
nestedIndex?: number
editable?: boolean
path: (string | number)[]
}
const IconBox = styled(props => <Box {...props} component='span'/>)`
cursor: pointer;
padding-left: 0.7rem;
` as typeof Box
export const DataKeyPair: React.FC<DataKeyPairProps> = (props) => {
const { value, path, nestedIndex } = props
const propsEditable = props.editable ?? undefined
const storeEditable = useJsonViewerStore(store => store.editable)
const editable = useMemo(() => {
if (storeEditable === false) {
return false
}
if (propsEditable === false) {
// props.editable is false which means we cannot provide the suitable way to edit it
return false
}
if (typeof storeEditable === 'function') {
return !!storeEditable(path, value)
}
return storeEditable
}, [path, propsEditable, storeEditable, value])
const [tempValue, setTempValue] = useState(typeof value === 'function' ? () => value : value)
const depth = path.length
const key = path[depth - 1]
const hoverPath = useJsonViewerStore(store => store.hoverPath)
const isHover = useMemo(() => {
return hoverPath && path.every(
(value, index) => value === hoverPath.path[index] && nestedIndex ===
hoverPath.nestedIndex)
}, [hoverPath, path, nestedIndex])
const setHover = useJsonViewerStore(store => store.setHover)
const root = useJsonViewerStore(store => store.value)
const [inspect, setInspect] = useInspect(path, value, nestedIndex)
const [editing, setEditing] = useState(false)
const onChange = useJsonViewerStore(store => store.onChange)
const keyColor = useTextColor()
const numberKeyColor = useJsonViewerStore(
store => store.colorspace.base0C)
const { Component, PreComponent, PostComponent, Editor } = useTypeComponents(
value, path)
const quotesOnKeys = useJsonViewerStore(store => store.quotesOnKeys)
const rootName = useJsonViewerStore(store => store.rootName)
const isRoot = root === value
const isNumberKey = Number.isInteger(Number(key))
const enableClipboard = useJsonViewerStore(store => store.enableClipboard)
const { copy, copied } = useClipboard()
const actionIcons = useMemo(() => {
if (editing) {
return (
<>
<IconBox>
<CloseIcon
sx={{
fontSize: '.8rem'
}}
onClick={() => {
// abort editing
setEditing(false)
setTempValue(value)
}}
/>
</IconBox>
<IconBox>
<CheckIcon
sx={{
fontSize: '.8rem'
}}
onClick={() => {
// finish editing, save data
setEditing(false)
onChange(path, value, tempValue)
}}
/>
</IconBox>
</>
)
}
return (
<>
{enableClipboard && (
<IconBox
onClick={event => {
event.preventDefault()
try {
copy(
JSON.stringify(
typeof value === 'function' ? value.toString() : value,
null,
' '
)
)
} catch (e) {
// in some case, this will throw error
// for example: circular structure
// fixme: `useAlert` hook
console.error(e)
}
}}
>
{
copied
? (
<CheckIcon
sx={{
fontSize: '.8rem'
}}
/>
)
: (
<ContentCopyIcon
sx={{
fontSize: '.8rem'
}}
/>
)
}
</IconBox>
)}
{/* todo: support edit object */}
{(Editor && editable) &&
(
<IconBox
onClick={event => {
event.preventDefault()
setEditing(true)
}}
>
<EditIcon
sx={{
fontSize: '.8rem'
}}
/>
</IconBox>
)
}
</>
)
},
[
Editor,
copied,
copy,
editable,
editing,
enableClipboard,
onChange,
path,
tempValue,
value
])
const expandable = !!(PreComponent && PostComponent)
const KeyRenderer = useJsonViewerStore(store => store.keyRenderer)
const downstreamProps: DataItemProps = useMemo(() => ({
path,
inspect,
setInspect,
value
}), [inspect, path, setInspect, value])
return (
<Box className='data-key-pair'
data-testid={'data-key-pair' + path.join('.')}
onMouseEnter={
useCallback(() => setHover(path, nestedIndex),
[setHover, path, nestedIndex])
}
>
<DataBox
component='span'
className='data-key'
sx={{
lineHeight: 1.5,
color: keyColor,
letterSpacing: 0.5,
opacity: 0.8
}}
onClick={
useCallback((event: React.MouseEvent<HTMLSpanElement>) => {
if (event.isDefaultPrevented()) {
return
}
setInspect(state => !state)
}, [setInspect])
}
>
{
expandable
? (inspect
? <ExpandMoreIcon sx={{
fontSize: '.8rem',
'&:hover': { cursor: 'pointer' }
}}/>
: <ChevronRightIcon sx={{
fontSize: '.8rem',
'&:hover': { cursor: 'pointer' }
}}/>
)
: null
}
{
(isRoot
? (
rootName !== false
? (quotesOnKeys ? <>"{rootName}"</> : <>{rootName}</>)
: null
)
: KeyRenderer.when(downstreamProps)
? <KeyRenderer {...downstreamProps} />
: nestedIndex === undefined && (
isNumberKey
? <Box component='span' style={{ color: numberKeyColor }}>{key}</Box>
: quotesOnKeys ? <>"{key}"</> : <>{key}</>
)
)
}
{
(
isRoot
? (rootName !== false
? <DataBox sx={{ mx: 0.5 }}>:</DataBox>
: null)
: nestedIndex === undefined && (
<DataBox sx={{ mx: 0.5 }}>:</DataBox>
)
)
}
{PreComponent && <PreComponent {...downstreamProps} />}
{(isHover && expandable && inspect) && actionIcons}
</DataBox>
{
(editing && editable)
? (Editor && <Editor value={tempValue} setValue={setTempValue}/>)
: (Component)
? <Component {...downstreamProps} />
: <Box component='span'
className='data-value-fallback'>{`fallback: ${value}`}</Box>
}
{PostComponent && <PostComponent {...downstreamProps} />}
{(isHover && expandable && !inspect) && actionIcons}
{(isHover && !expandable) && actionIcons}
</Box>
)
}