8
8
ThemeProvider
9
9
} from '@mui/material'
10
10
import type { OverridesStyleRules } from '@mui/material/styles/overrides'
11
+ import { DevelopmentError } from '@textea/dev-kit/utils'
11
12
import type React from 'react'
12
13
import { useCallback , useDebugValue , useEffect , useMemo } from 'react'
13
14
@@ -52,6 +53,68 @@ function getType (value: unknown) {
52
53
return type
53
54
}
54
55
56
+ const needExpand = ( value : unknown ) : boolean => {
57
+ const type = getType ( value )
58
+ switch ( type ) {
59
+ case 'object' :
60
+ case 'function' :
61
+ case 'array' :
62
+ return true
63
+ default :
64
+ return false
65
+ }
66
+ }
67
+
68
+ const getEndingClosure = ( value : unknown ) : string => {
69
+ const type = getType ( value )
70
+ switch ( type ) {
71
+ case 'object' :
72
+ case 'function' :
73
+ return '}'
74
+ case 'array' :
75
+ return ']'
76
+ default :
77
+ throw new DevelopmentError ( )
78
+ }
79
+ }
80
+
81
+ function shortPreviewValue ( value : unknown ) : string {
82
+ const type = getType ( value )
83
+ if ( type === 'function' ) {
84
+ return ( value as Function ) . toString ( ) . slice ( 9 , - 1 ) . replace ( / \{ [ \s \S ] + / , '' )
85
+ } else if ( type === 'array' ) {
86
+ return '[...]'
87
+ } else if ( type === 'object' ) {
88
+ return '{...}'
89
+ } else {
90
+ return `${ value } `
91
+ }
92
+ }
93
+
94
+ function longPreviewValue ( value : unknown ) : string {
95
+ const type = getType ( value )
96
+ if ( type === 'function' ) {
97
+ const functionHead = ( value as Function ) . toString ( )
98
+ . slice ( 9 , - 1 )
99
+ . replace ( / \{ [ \s \S ] + / , '' )
100
+ return `${ functionHead } {`
101
+ } else if ( type === 'array' ) {
102
+ return ' ['
103
+ } else if ( type === 'object' ) {
104
+ return '{'
105
+ } else {
106
+ return ''
107
+ }
108
+ }
109
+
110
+ function shortPreviewKeyValuePair ( key : string , value : unknown ) : string {
111
+ return `${ key } : ${ shortPreviewValue ( value ) } `
112
+ }
113
+
114
+ function longPreviewKeyValuePair ( key : string , value : unknown ) : string {
115
+ return `${ key } : ${ longPreviewValue ( value ) } `
116
+ }
117
+
55
118
const ObjectJson : React . FC < DataProps > = ( {
56
119
value,
57
120
isRoot,
@@ -76,51 +139,69 @@ const ObjectJson: React.FC<DataProps> = ({
76
139
onNodeToggle = { handleToggle }
77
140
>
78
141
< TreeItem nodeId = 'data-viewer-root' label = 'root' >
79
- {
80
- Object . entries ( value as object ) . map ( ( [ key , value ] ) => {
81
- const path = `${ father } ${ father ? '.' : '' } ${ key } `
82
- const isExpend = expanded . includes ( path )
83
- return (
84
- < TreeItem
85
- nodeId = { path }
86
- key = { key }
87
- label = { isExpend ? path : `${ key } : ${ value } ` }
88
- >
89
- < ObjectJson
90
- father = { key }
91
- value = { value }
92
- isRoot = { false }
93
- />
94
- </ TreeItem >
95
- )
96
- } )
97
- }
142
+ < ObjectJson
143
+ father = 'root'
144
+ value = { value }
145
+ isRoot = { false }
146
+ />
98
147
</ TreeItem >
99
148
</ TreeView >
100
149
)
101
150
} else {
102
151
return (
103
152
Object . entries ( value as object ) . map ( ( [ key , value ] ) => {
104
153
const path = `${ father } ${ father ? '.' : '' } ${ key } `
105
- return (
106
- < TreeItem
107
- nodeId = { path }
108
- key = { key }
109
- label = { `${ value } ` }
110
- >
111
- < ObjectJson
112
- father = { key }
113
- value = { value }
114
- isRoot = { false }
154
+ const isExpend = expanded . includes ( path )
155
+ const shouldExpand = needExpand ( value )
156
+ if ( shouldExpand ) {
157
+ return (
158
+ < TreeItem
159
+ nodeId = { path }
160
+ key = { key }
161
+ label = { isExpend
162
+ ? longPreviewKeyValuePair ( key , value )
163
+ : shortPreviewKeyValuePair ( key , value ) }
164
+ >
165
+ {
166
+ shouldExpand
167
+ ? (
168
+ < ObjectJson
169
+ father = { key }
170
+ value = { value }
171
+ isRoot = { false }
172
+ />
173
+ )
174
+ : null
175
+ }
176
+ { shouldExpand && isExpend && (
177
+ < TreeItem nodeId = { `${ path } -ending` }
178
+ label = { getEndingClosure ( value ) } />
179
+ ) }
180
+ </ TreeItem >
181
+ )
182
+ } else {
183
+ return (
184
+ < TreeItem
185
+ nodeId = { path }
186
+ key = { key }
187
+ label = { shortPreviewKeyValuePair ( key , value ) }
115
188
/>
116
- </ TreeItem >
117
- )
189
+ )
190
+ }
118
191
} )
119
192
)
120
193
}
121
194
} else {
122
195
const path = `${ father } ${ father ? '.' : '' } ${ value } `
123
- return < TreeItem nodeId = { path } label = { `${ value } ` } />
196
+ const type = getType ( value )
197
+ if ( type === 'function' ) {
198
+ const entire = ( value as Function ) . toString ( )
199
+ const label = entire . slice ( entire . indexOf ( '{' ) + 1 ,
200
+ entire . lastIndexOf ( '}' ) )
201
+ return < TreeItem nodeId = { path } label = { label } />
202
+ } else {
203
+ return < TreeItem nodeId = { path } label = { `${ value } ` } />
204
+ }
124
205
}
125
206
} , [ expanded , father , handleToggle , isRoot , type , value ] )
126
207
return < > { elements } </ >
0 commit comments