@@ -15,7 +15,7 @@ import {
15
15
UnShow ,
16
16
TacoButton ,
17
17
} from "lowcoder-design" ;
18
- import React , { ReactNode , useCallback , useContext , useEffect , useMemo , useRef , useState } from "react" ;
18
+ import React , { ReactNode , useCallback , useContext , useMemo , useState } from "react" ;
19
19
import { hookCompCategory } from "comps/hooks/hookCompTypes" ;
20
20
import _ from "lodash" ;
21
21
import styled from "styled-components" ;
@@ -33,7 +33,6 @@ import { CollapseWrapper, DirectoryTreeStyle, Node } from "./styledComponents";
33
33
import { DataNode , EventDataNode } from "antd/es/tree" ;
34
34
import { isAggregationApp } from "util/appUtils" ;
35
35
import Modal from "antd/es/modal/Modal" ;
36
- import { useUnmount } from "react-use" ;
37
36
38
37
const CollapseTitleWrapper = styled . div `
39
38
display: flex;
@@ -69,20 +68,9 @@ function getLen(config: string | boolean | number) {
69
68
return 0 ;
70
69
}
71
70
72
- const stringifyCache = new Map ( ) ;
73
-
74
71
function safeStringify ( obj : any , space = 2 ) {
75
- if ( obj === null || obj === undefined || typeof obj !== 'object' ) {
76
- return JSON . stringify ( obj ) ;
77
- }
78
-
79
- const cacheKey = obj ;
80
- if ( stringifyCache . has ( cacheKey ) ) {
81
- return stringifyCache . get ( cacheKey ) ;
82
- }
83
-
84
72
const cache = new Set ( ) ;
85
- const result = JSON . stringify ( obj , ( key , value ) => {
73
+ return JSON . stringify ( obj , ( key , value ) => {
86
74
if ( typeof value === 'object' && value !== null ) {
87
75
if ( cache . has ( value ) ) {
88
76
return '[Circular]' ;
@@ -91,14 +79,9 @@ function safeStringify(obj: any, space = 2) {
91
79
}
92
80
return value ;
93
81
} , space ) ;
94
-
95
- if ( result . length < 10000 ) {
96
- stringifyCache . set ( cacheKey , result ) ;
97
- }
98
-
99
- return result ;
100
82
}
101
83
84
+
102
85
function toDataView ( value : any , name : string , desc ?: ReactNode , modal ?: boolean ) {
103
86
const str = typeof value === "function" ? "Function" : safeStringify ( value ) ;
104
87
const descRecord : Record < string , ReactNode > = { } ;
@@ -138,6 +121,7 @@ function toDataView(value: any, name: string, desc?: ReactNode, modal?: boolean)
138
121
}
139
122
}
140
123
124
+
141
125
export default toDataView ;
142
126
143
127
function sliceArr ( arr : string [ ] ) {
@@ -187,13 +171,11 @@ const CollapseView = React.memo(
187
171
onClick ?: ( compName : string ) => void ;
188
172
isSelected ?: boolean ;
189
173
isOpen ?: boolean ;
190
- children ?: React . ReactNode ;
174
+ children ?: React . ReactNode ; // Accept children
191
175
modal ?: boolean ;
192
176
} ) => {
193
177
const { data = { } } = props ;
194
178
const onlyOne = Object . keys ( data ) . length === 1 ;
195
- const dataString = useMemo ( ( ) => safeStringify ( data ) , [ data ] ) ;
196
-
197
179
return (
198
180
< Collapse
199
181
isSelected = { props . isSelected }
@@ -236,7 +218,7 @@ const CollapseView = React.memo(
236
218
</ CollapseTitleWrapper >
237
219
</ Tooltip >
238
220
{ Object . keys ( data ) . length > 0 &&
239
- < CopyTextButton text = { dataString } style = { { color : "#aaa" , marginRight : "8px" } } />
221
+ < CopyTextButton text = { safeStringify ( data ) } style = { { color : "#aaa" , marginRight : "8px" } } />
240
222
}
241
223
</ div >
242
224
) ,
@@ -248,6 +230,7 @@ const CollapseView = React.memo(
248
230
}
249
231
) ;
250
232
233
+
251
234
interface LeftContentProps {
252
235
uiComp : InstanceType < typeof UIComp > ;
253
236
}
@@ -296,21 +279,13 @@ const LeftContentWrapper = styled.div`
296
279
height: calc(100vh - ${ TopHeaderHeight } );
297
280
` ;
298
281
299
- export const LeftContent = React . memo ( ( props : LeftContentProps ) => {
282
+ export const LeftContent = ( props : LeftContentProps ) => {
300
283
const { uiComp } = props ;
301
284
const editorState = useContext ( EditorContext ) ;
302
- const mountedRef = useRef ( true ) ;
303
285
const [ expandedKeys , setExpandedKeys ] = useState < Array < React . Key > > ( [ ] ) ;
304
286
const [ showData , setShowData ] = useState < NodeInfo [ ] > ( [ ] ) ;
305
287
306
- useEffect ( ( ) => {
307
- return ( ) => {
308
- mountedRef . current = false ;
309
- } ;
310
- } , [ ] ) ;
311
-
312
- const getTree = useCallback ( ( tree : CompTree , result : NodeItem [ ] , key ?: string ) => {
313
- if ( ! mountedRef . current ) return result ;
288
+ const getTree = ( tree : CompTree , result : NodeItem [ ] , key ?: string ) => {
314
289
const { items, children } = tree ;
315
290
if ( Object . keys ( items ) . length ) {
316
291
for ( const i in items ) {
@@ -335,64 +310,60 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
335
310
}
336
311
}
337
312
return result ;
338
- } , [ ] ) ;
313
+ } ;
339
314
340
- const handleNodeClick = useCallback (
341
- ( e : React . MouseEvent < HTMLSpanElement , MouseEvent > ,
315
+ const handleNodeClick = (
316
+ e : React . MouseEvent < HTMLSpanElement , MouseEvent > ,
342
317
node : EventDataNode < DataNode > ,
343
- uiCompInfos : CompInfo [ ] ) => {
344
- if ( ! mountedRef . current ) return ;
345
- uiCollapseClick ( node . title + "" ) ;
346
- const data = uiCompInfos . find ( ( item ) => item . name === node . title ) ;
347
- if ( ! node . children ?. length && data && Object . keys ( data . data ) ?. length && node . selected ) {
348
- const index = showData . findIndex ( ( item ) => item . key === node . key ) ;
349
- let newData : NodeInfo [ ] = [ ] ;
350
- let clientX = e . currentTarget ?. offsetLeft + 20 ;
351
- if ( index > - 1 ) {
352
- newData = showData . map ( ( item ) => {
353
- if ( item . key === node . key ) {
354
- return {
355
- key : item . key ,
356
- show : ! item . show ,
357
- clientX,
358
- } ;
359
- }
360
- return item ;
361
- } ) ;
362
- } else {
363
- newData = [
364
- ...showData ,
365
- {
366
- key : node . key + "" ,
367
- show : true ,
318
+ uiCompInfos : CompInfo [ ]
319
+ ) => {
320
+ uiCollapseClick ( node . title + "" ) ;
321
+ const data = uiCompInfos . find ( ( item ) => item . name === node . title ) ;
322
+ if ( ! node . children ?. length && data && Object . keys ( data . data ) ?. length && node . selected ) {
323
+ // leaf and selected node, toggle showData
324
+ const index = showData . findIndex ( ( item ) => item . key === node . key ) ;
325
+ let newData : NodeInfo [ ] = [ ] ;
326
+ let clientX = e . currentTarget ?. offsetLeft + 20 ;
327
+ if ( index > - 1 ) {
328
+ newData = showData . map ( ( item ) => {
329
+ if ( item . key === node . key ) {
330
+ return {
331
+ key : item . key ,
332
+ show : ! item . show ,
368
333
clientX,
369
- } ,
370
- ] ;
371
- }
372
- setShowData ( newData ) ;
334
+ } ;
335
+ }
336
+ return item ;
337
+ } ) ;
338
+ } else {
339
+ newData = [
340
+ ...showData ,
341
+ {
342
+ key : node . key + "" ,
343
+ show : true ,
344
+ clientX,
345
+ } ,
346
+ ] ;
373
347
}
374
- } ,
375
- [ showData ]
376
- ) ;
348
+ setShowData ( newData ) ;
349
+ }
350
+ } ;
377
351
378
352
const uiCollapseClick = useCallback (
379
353
( compName : string ) => {
380
- if ( ! mountedRef . current ) return ;
381
354
editorState . setSelectedCompNames ( new Set ( [ compName ] ) , "leftPanel" ) ;
382
355
} ,
383
356
[ editorState ]
384
357
) ;
385
358
386
359
const handleBottomResItemClick = useCallback (
387
360
( type : BottomResTypeEnum , name : string ) => {
388
- if ( ! mountedRef . current ) return ;
389
361
editorState . setSelectedBottomRes ( name , type ) ;
390
362
} ,
391
363
[ editorState ]
392
364
) ;
393
365
394
- const getTreeNode = useCallback ( ( node : NodeItem , uiCompInfos : CompInfo [ ] ) => {
395
- if ( ! mountedRef . current ) return null ;
366
+ const getTreeNode = ( node : NodeItem , uiCompInfos : CompInfo [ ] ) => {
396
367
const info = showData . find ( ( item ) => item . key === node . key ) ;
397
368
const data = uiCompInfos . find ( ( item ) => item . name === node . title ) ;
398
369
@@ -420,7 +391,6 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
420
391
title = ""
421
392
className = "show-data"
422
393
onClick = { ( e ) => {
423
- if ( ! mountedRef . current ) return ;
424
394
e . stopPropagation ( ) ;
425
395
const newData = showData . map ( ( item ) => {
426
396
if ( item . key === node . key ) {
@@ -435,7 +405,7 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
435
405
setShowData ( newData ) ;
436
406
} }
437
407
>
438
- < LeftOpen />
408
+
439
409
</ div >
440
410
</ Tooltip >
441
411
) : (
@@ -445,20 +415,26 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
445
415
>
446
416
< div
447
417
title = ""
448
- className = "show -data"
418
+ className = "no -data"
449
419
onClick = { ( e ) => {
450
- if ( ! mountedRef . current ) return ;
451
420
e . stopPropagation ( ) ;
452
- const newData = showData . map ( ( item ) => {
453
- if ( item . key === node . key ) {
454
- return {
455
- key : item . key ,
456
- show : true ,
457
- clientX : undefined ,
458
- } ;
459
- }
460
- return item ;
461
- } ) ;
421
+ const index = showData . findIndex ( ( item ) => item . key === node . key ) ;
422
+ let newData : NodeInfo [ ] = [ ] ;
423
+ const info = {
424
+ key : node . key ,
425
+ show : true ,
426
+ clientX : e . currentTarget . parentElement ?. offsetLeft ,
427
+ } ;
428
+ if ( index > - 1 ) {
429
+ newData = showData . map ( ( item ) => {
430
+ if ( item . key === node . key ) {
431
+ return info ;
432
+ }
433
+ return item ;
434
+ } ) ;
435
+ } else {
436
+ newData = [ ...showData , info ] ;
437
+ }
462
438
setShowData ( newData ) ;
463
439
} }
464
440
>
@@ -489,10 +465,10 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
489
465
) }
490
466
</ Node >
491
467
) ;
492
- } , [ showData ] ) ;
468
+ } ;
469
+
493
470
494
- const getTreeUI = useCallback ( ( type : TreeUIKey ) => {
495
- if ( ! mountedRef . current ) return null ;
471
+ const getTreeUI = ( type : TreeUIKey ) => {
496
472
const uiCompInfos = _ . sortBy ( editorState . uiCompInfoList ( ) , [ ( x ) => x . name ] ) ;
497
473
const tree =
498
474
type === TreeUIKey . Components
@@ -529,22 +505,13 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
529
505
props . expanded ? < FoldedIcon /> : < UnfoldIcon />
530
506
}
531
507
expandedKeys = { expandedKeys }
532
- onExpand = { ( keys ) => {
533
- if ( ! mountedRef . current ) return ;
534
- setExpandedKeys ( keys ) ;
535
- } }
508
+ onExpand = { ( keys ) => setExpandedKeys ( keys ) }
536
509
onClick = { ( e , node ) => handleNodeClick ( e , node , uiCompInfos ) }
537
510
selectedKeys = { selectedKeys }
538
511
titleRender = { ( nodeData ) => getTreeNode ( nodeData as NodeItem , uiCompInfos ) }
539
512
/>
540
513
) ;
541
- } , [ editorState , expandedKeys , getTree , handleNodeClick , getTreeNode ] ) ;
542
-
543
- useUnmount ( ( ) => {
544
- mountedRef . current = false ;
545
- setExpandedKeys ( [ ] ) ;
546
- setShowData ( [ ] ) ;
547
- } ) ;
514
+ } ;
548
515
549
516
const uiCollapse = useMemo ( ( ) => {
550
517
if ( isAggregationApp ( editorState . getAppType ( ) ) ) {
@@ -644,4 +611,4 @@ export const LeftContent = React.memo((props: LeftContentProps) => {
644
611
</ LeftContentTabs >
645
612
</ LeftContentWrapper >
646
613
) ;
647
- } ) ;
614
+ } ;
0 commit comments