@@ -991,10 +991,14 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
991991 id2width . set ( node . id , ( node . width || 0 ) + boxwidth ) ;
992992 // id2height.set(node.id, node.height!);
993993 } ) ;
994+ // Save initial minimum offset of the nodes.
995+ let initOffX = Math . min ( ...nodes . map ( ( node ) => node . position . x ) ) ;
996+ let initOffY = Math . min ( ...nodes . map ( ( node ) => node . position . y ) ) ;
997+
994998 const tmpNodes : NodeType [ ] = nodes . map ( ( node ) => ( {
995999 id : node . id ,
9961000 x : node . position . x + id2width . get ( node . id ) ! / 2 ,
997- y : node . position . y + id2height . get ( node . id ) ! / 2 + paddingTopPod / 2 ,
1001+ y : node . position . y + id2height . get ( node . id ) ! / 2 ,
9981002 width : id2width . get ( node . id ) ! ,
9991003 height : id2height . get ( node . id ) ! + paddingTopPod ,
10001004 } ) ) ;
@@ -1032,41 +1036,52 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
10321036 simulation . tick ( 10 ) ;
10331037 tmpNodes . forEach ( ( node ) => {
10341038 node . x -= id2width . get ( node . id ) ! / 2 ;
1035- node . y -= id2height . get ( node . id ) ! / 2 - paddingTopPod ;
1036- } ) ;
1037- // The nodes will all have new positions now. I'll need to make the graph to be top-left, i.e., the leftmost is 20, the topmost is 20.
1038- // get the min x and y
1039- let x1s = tmpNodes . map ( ( node ) => node . x ) ;
1040- let minx = Math . min ( ...x1s ) ;
1041- let y1s = tmpNodes . map ( ( node ) => node . y ) ;
1042- let miny = Math . min ( ...y1s ) ;
1043- // calculate the offset, leave 50 padding for the scope.
1044- // Leave some room at the top of the scope for inner pod toolbars.
1045- const paddingTop = 70 ;
1046- const paddingBottom = 50 ;
1047- const paddingLeft = 50 ;
1048- const paddingRight = 50 ;
1049- const offsetx = paddingLeft - minx ;
1050- const offsety = paddingTop - miny ;
1051- // move the nodes
1052- tmpNodes . forEach ( ( node ) => {
1053- node . x += offsetx ;
1054- node . y += offsety ;
1055- } ) ;
1056- // Apply the new positions
1057- // TODO need to transform the nodes to the center of the scope.
1058- tmpNodes . forEach ( ( { id, x, y } ) => {
1059- // FIXME I should assert here.
1060- if ( nodesMap . has ( id ) ) {
1061- nodesMap . set ( id , {
1062- ...nodesMap . get ( id ) ! ,
1063- // position: { x: x + scope!.position!.x, y: y + scope!.position!.y },
1064- position : { x, y } ,
1065- } ) ;
1066- }
1039+ node . y -= id2height . get ( node . id ) ! / 2 ;
10671040 } ) ;
10681041
1069- if ( scopeId !== "ROOT" ) {
1042+ if ( scopeId === "ROOT" ) {
1043+ // reset the node positions
1044+ tmpNodes . forEach ( ( { id, x, y } ) => {
1045+ // FIXME I should assert here.
1046+ if ( nodesMap . has ( id ) ) {
1047+ nodesMap . set ( id , {
1048+ ...nodesMap . get ( id ) ! ,
1049+ position : { x, y } ,
1050+ } ) ;
1051+ }
1052+ } ) ;
1053+ } else {
1054+ // The nodes will all have new positions now. I'll need to make the graph to be top-left, i.e., the leftmost is 20, the topmost is 20.
1055+ // get the min x and y
1056+ let x1s = tmpNodes . map ( ( node ) => node . x ) ;
1057+ let minx = Math . min ( ...x1s ) ;
1058+ let y1s = tmpNodes . map ( ( node ) => node . y ) ;
1059+ let miny = Math . min ( ...y1s ) ;
1060+ // calculate the offset, leave 50 padding for the scope.
1061+ // Leave some room at the top of the scope for inner pod toolbars.
1062+ const paddingTop = 70 ;
1063+ const paddingBottom = 50 ;
1064+ const paddingLeft = 50 ;
1065+ const paddingRight = 50 ;
1066+ const offsetx = paddingLeft - minx ;
1067+ const offsety = paddingTop - miny ;
1068+ // move the nodes
1069+ tmpNodes . forEach ( ( node ) => {
1070+ node . x += offsetx ;
1071+ node . y += offsety ;
1072+ } ) ;
1073+ // Apply the new positions
1074+ // TODO need to transform the nodes to the center of the scope.
1075+ tmpNodes . forEach ( ( { id, x, y } ) => {
1076+ // FIXME I should assert here.
1077+ if ( nodesMap . has ( id ) ) {
1078+ nodesMap . set ( id , {
1079+ ...nodesMap . get ( id ) ! ,
1080+ // position: { x: x + scope!.position!.x, y: y + scope!.position!.y },
1081+ position : { x, y } ,
1082+ } ) ;
1083+ }
1084+ } ) ;
10701085 // update the scope's size to enclose all the nodes
10711086 x1s = tmpNodes . map ( ( node ) => node . x ) ;
10721087 minx = Math . min ( ...x1s ) ;
@@ -1076,9 +1091,13 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
10761091 const maxx = Math . max ( ...x2s ) ;
10771092 const y2s = tmpNodes . map ( ( node ) => node . y + id2height . get ( node . id ) ! ) ;
10781093 const maxy = Math . max ( ...y2s ) ;
1079- const scope = nodesMap . get ( scopeId ) ;
1094+ const scope = nodesMap . get ( scopeId ) ! ;
10801095 nodesMap . set ( scopeId , {
1081- ...scope ! ,
1096+ ...scope ,
1097+ position : {
1098+ x : scope . position . x + initOffX - paddingLeft ,
1099+ y : scope . position . y + initOffY - paddingTop ,
1100+ } ,
10821101 width : maxx - minx + paddingLeft + paddingRight ,
10831102 height : maxy - miny + paddingTop + paddingBottom ,
10841103 style : {
0 commit comments