@@ -68,9 +68,7 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
68
68
const [ frame ] = React . useState ( {
69
69
translate : [ 0 , 0 ] ,
70
70
} ) ;
71
- const setSelected = useStore ( store , ( state ) => state . setSelected ) ;
72
71
const selected = useStore ( store , ( state ) => state . selected ) ;
73
- const { setNodes } = useReactFlow ( ) ;
74
72
75
73
const onResize = useCallback ( ( { width, height, offx, offy } ) => {
76
74
const node = nodesMap . get ( id ) ;
@@ -186,6 +184,8 @@ const ScopeNode = memo<Props>(({ data, id, isConnectable }) => {
186
184
) ;
187
185
} ) ;
188
186
187
+ // FIXME: the resultblock is rendered every time the parent codeNode changes (e.g., dragging), we may set the result number as a state of a pod to memoize the resultblock.
188
+
189
189
function ResultBlock ( { pod, id } ) {
190
190
const store = useContext ( RepoContext ) ;
191
191
if ( ! store ) throw new Error ( "Missing BearContext.Provider in the tree" ) ;
@@ -252,12 +252,8 @@ function ResultBlock({ pod, id }) {
252
252
const CodeNode = memo < Props > ( ( { data, id, isConnectable } ) => {
253
253
const store = useContext ( RepoContext ) ;
254
254
if ( ! store ) throw new Error ( "Missing BearContext.Provider in the tree" ) ;
255
- const pod = useStore ( store , ( state ) => state . pods [ id ] ) ;
256
- const setPodContent = useStore ( store , ( state ) => state . setPodContent ) ;
257
- const updatePod = useStore ( store , ( state ) => state . updatePod ) ;
258
- const clearResults = useStore ( store , ( s ) => s . clearResults ) ;
255
+ // const pod = useStore(store, (state) => state.pods[id]);
259
256
const wsRun = useStore ( store , ( state ) => state . wsRun ) ;
260
- const nodesMap = useStore ( store , ( state ) => state . ydoc . getMap < Node > ( "pods" ) ) ;
261
257
const ref = useRef ( null ) ;
262
258
const [ target , setTarget ] = React . useState < any > ( null ) ;
263
259
const [ frame ] = React . useState ( {
@@ -266,30 +262,25 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
266
262
// right, bottom
267
263
const [ layout , setLayout ] = useState ( "right" ) ;
268
264
const { setNodes } = useReactFlow ( ) ;
269
- const selected = useStore ( store , ( state ) => state . selected ) ;
265
+ // const selected = useStore(store, (state) => state.selected);
270
266
const setSelected = useStore ( store , ( state ) => state . setSelected ) ;
267
+ const getPod = useStore ( store , ( state ) => state . getPod ) ;
268
+ const pod = getPod ( id ) ;
269
+
270
+ const showResult = useStore (
271
+ store ,
272
+ ( state ) =>
273
+ state . pods [ id ] . running ||
274
+ state . pods [ id ] . result ||
275
+ state . pods [ id ] . error ||
276
+ state . pods [ id ] . stdout ||
277
+ state . pods [ id ] . stderr
278
+ ) ;
271
279
272
- const onResize = useCallback ( ( { width, height, offx, offy } ) => {
273
- const node = nodesMap . get ( id ) ;
274
- if ( node ) {
275
- node . style = { ...node . style , width, height } ;
276
- node . position . x += offx ;
277
- node . position . y += offy ;
278
- nodesMap . set ( id , node ) ;
279
- }
280
- } , [ ] ) ;
281
- const onLayout = useCallback ( ( { height } ) => {
282
- const node = nodesMap . get ( id ) ;
283
- if ( node ) {
284
- node . style = { ...node . style , height } ;
285
- nodesMap . set ( id , node ) ;
286
- }
287
- } , [ ] ) ;
288
-
289
- React . useEffect ( ( ) => {
280
+ useEffect ( ( ) => {
290
281
setTarget ( ref . current ) ;
291
282
} , [ ] ) ;
292
- if ( ! pod ) return < Box > ERROR</ Box > ;
283
+ // if (!pod) return <Box>ERROR</Box>;
293
284
return (
294
285
< Box
295
286
sx = { {
@@ -387,24 +378,8 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
387
378
) ;
388
379
} }
389
380
>
390
- < MyMonaco
391
- value = { pod . content || "" }
392
- id = { pod . id }
393
- onChange = { ( value ) => {
394
- setPodContent ( { id : pod . id , content : value } ) ;
395
- } }
396
- lang = { pod . lang || "javascript" }
397
- onRun = { ( ) => {
398
- clearResults ( pod . id ) ;
399
- wsRun ( pod . id ) ;
400
- } }
401
- onLayout = { onLayout }
402
- />
403
- { ( pod . running ||
404
- pod . stdout ||
405
- pod . stderr ||
406
- pod . result ||
407
- pod . error ) && (
381
+ < MyMonaco id = { id } gitvalue = "" />
382
+ { showResult && (
408
383
< Box
409
384
className = "nowheel"
410
385
sx = { {
@@ -424,43 +399,6 @@ const CodeNode = memo<Props>(({ data, id, isConnectable }) => {
424
399
</ Box >
425
400
) }
426
401
</ Box >
427
- { false && (
428
- < Moveable
429
- target = { target }
430
- resizable = { true }
431
- keepRatio = { false }
432
- throttleResize = { 1 }
433
- renderDirections = { [ "e" , "s" , "se" ] }
434
- edge = { false }
435
- zoom = { 1 }
436
- origin = { true }
437
- padding = { { left : 0 , top : 0 , right : 0 , bottom : 0 } }
438
- onResizeStart = { ( e ) => {
439
- e . setOrigin ( [ "%" , "%" ] ) ;
440
- e . dragStart && e . dragStart . set ( frame . translate ) ;
441
- } }
442
- onResize = { ( e ) => {
443
- const beforeTranslate = e . drag . beforeTranslate ;
444
- frame . translate = beforeTranslate ;
445
- e . target . style . width = `${ e . width } px` ;
446
- e . target . style . height = `${ e . height } px` ;
447
- e . target . style . transform = `translate(${ beforeTranslate [ 0 ] } px, ${ beforeTranslate [ 1 ] } px)` ;
448
- onResize ( {
449
- width : e . width ,
450
- height : e . height ,
451
- offx : beforeTranslate [ 0 ] ,
452
- offy : beforeTranslate [ 1 ] ,
453
- } ) ;
454
- updatePod ( {
455
- id,
456
- data : {
457
- width : e . width ,
458
- height : e . height ,
459
- } ,
460
- } ) ;
461
- } }
462
- />
463
- ) }
464
402
</ Box >
465
403
) ;
466
404
} ) ;
@@ -484,35 +422,39 @@ export function Canvas() {
484
422
const store = useContext ( RepoContext ) ;
485
423
if ( ! store ) throw new Error ( "Missing BearContext.Provider in the tree" ) ;
486
424
// the real pods
487
- const id2children = useStore ( store , ( state ) => state . id2children ) ;
488
- const pods = useStore ( store , ( state ) => state . pods ) ;
425
+ const getId2children = useStore ( store , ( state ) => state . getId2children ) ;
426
+ // const pods = useStore(store, (state) => state.pods);
427
+ const getPod = useStore ( store , ( state ) => state . getPod ) ;
489
428
const nodesMap = useStore ( store , ( state ) => state . ydoc . getMap < Node > ( "pods" ) ) ;
490
429
491
430
const getRealNodes = useCallback (
492
431
( id , level ) => {
493
432
let res : any [ ] = [ ] ;
494
- let children = id2children [ id ] ;
433
+ let children = getId2children ( id ) || [ ] ;
434
+ console . log ( "getChildren" , id , children ) ;
435
+ const pod = getPod ( id ) ;
495
436
if ( id !== "ROOT" ) {
496
437
res . push ( {
497
438
id : id ,
498
- type : pods [ id ] . type === "CODE" ? "code" : "scope" ,
439
+ type : pod . type === "CODE" ? "code" : "scope" ,
499
440
data : {
500
441
// label: `ID: ${id}, parent: ${pods[id].parent}, pos: ${pods[id].x}, ${pods[id].y}`,
501
442
label : id ,
502
443
} ,
503
444
// position: { x: 100, y: 100 },
504
- position : { x : pods [ id ] . x , y : pods [ id ] . y } ,
505
- parentNode : pods [ id ] . parent !== "ROOT" ? pods [ id ] . parent : undefined ,
445
+ position : { x : pod . x , y : pod . y } ,
446
+ parentNode : pod . parent !== "ROOT" ? pod . parent : undefined ,
506
447
extent : "parent" ,
507
448
dragHandle : ".custom-drag-handle" ,
508
449
level,
509
450
style : {
510
451
backgroundColor :
511
- pods [ id ] . type === "CODE"
452
+ pod . type === "CODE"
512
453
? undefined
513
454
: level2color [ level ] || level2color [ "default" ] ,
514
455
width : 700 ,
515
- height : pods [ id ] . height ,
456
+ // for code node, don't set height, let it be auto
457
+ height : pod . height || undefined ,
516
458
} ,
517
459
} ) ;
518
460
}
@@ -521,7 +463,7 @@ export function Canvas() {
521
463
}
522
464
return res ;
523
465
} ,
524
- [ id2children , pods ]
466
+ [ getId2children , getPod ]
525
467
) ;
526
468
useEffect ( ( ) => {
527
469
let nodes = getRealNodes ( "ROOT" , - 1 ) ;
@@ -593,7 +535,8 @@ export function Canvas() {
593
535
} else {
594
536
style = {
595
537
width : 700 ,
596
- height : 300 ,
538
+ // we must not set the height here, otherwise the auto layout will not work
539
+ height : undefined ,
597
540
} ;
598
541
}
599
542
@@ -640,19 +583,19 @@ export function Canvas() {
640
583
( node ) => {
641
584
let x = node . position . x ;
642
585
let y = node . position . y ;
643
- let parent = pods [ node . parent ] ;
586
+ let parent = getPod ( node . parent ) ;
644
587
while ( parent ) {
645
588
x += parent . x ;
646
589
y += parent . y ;
647
590
if ( parent . parent ) {
648
- parent = pods [ parent . parent ] ;
591
+ parent = getPod ( parent . parent ) ;
649
592
} else {
650
593
break ;
651
594
}
652
595
}
653
596
return [ x , y ] ;
654
597
} ,
655
- [ pods ]
598
+ [ getPod ]
656
599
) ;
657
600
658
601
const getScopeAt = useCallback (
0 commit comments