@@ -676,8 +676,10 @@ export class Navigation {
676676 ) : Blockly . RenderedConnection | null {
677677 const stationaryType = stationaryNode . getType ( ) ;
678678 const stationaryLoc = stationaryNode . getLocation ( ) ;
679+ const movingHasOutput = ! ! movingBlock . outputConnection ;
679680
680681 if ( stationaryNode . getType ( ) === Blockly . ASTNode . types . FIELD ) {
682+ // Can't connect a block to a field, so try going up to the source block.
681683 const sourceBlock = stationaryNode . getSourceBlock ( ) ;
682684 if ( ! sourceBlock ) return null ;
683685 return this . findInsertStartPoint (
@@ -691,7 +693,7 @@ export class Navigation {
691693 // Move to the block if we're trying to insert a statement block into
692694 // a value connection.
693695 if (
694- ! movingBlock . outputConnection &&
696+ ! movingHasOutput &&
695697 stationaryAsConnection . type === Blockly . ConnectionType . INPUT_VALUE
696698 ) {
697699 const sourceBlock = stationaryNode . getSourceBlock ( ) ;
@@ -711,7 +713,7 @@ export class Navigation {
711713 const stationaryBlock = stationaryLoc as Blockly . BlockSvg ;
712714
713715 // 1. Connect blocks to first compatible input
714- const inputType = movingBlock . outputConnection
716+ const inputType = movingHasOutput
715717 ? Blockly . inputs . inputTypes . VALUE
716718 : Blockly . inputs . inputTypes . STATEMENT ;
717719 const compatibleInputs = stationaryBlock . inputList . filter (
@@ -730,17 +732,24 @@ export class Navigation {
730732 }
731733
732734 // 2. Connect statement blocks to next connection.
733- if ( stationaryBlock . nextConnection && ! movingBlock . outputConnection ) {
735+ if ( stationaryBlock . nextConnection && ! movingHasOutput ) {
734736 return stationaryBlock . nextConnection ;
735737 }
736738
737739 // 3. Output connection. This will wrap around or displace.
738740 if ( stationaryBlock . outputConnection ) {
739- // Move to parent if we're trying to insert a statement block.
740- if (
741- ! movingBlock . outputConnection &&
741+ // Try to wrap.
742+ const target = stationaryBlock . outputConnection . targetConnection ;
743+ if ( movingHasOutput && target ) {
744+ const sourceNode = Blockly . ASTNode . createConnectionNode ( target ) ;
745+ if ( sourceNode ) {
746+ return this . findInsertStartPoint ( sourceNode , movingBlock ) ;
747+ }
748+ } else if (
749+ ! movingHasOutput &&
742750 stationaryNode . getType ( ) === Blockly . ASTNode . types . BLOCK
743751 ) {
752+ // Move to parent if we're trying to insert a statement block.
744753 const parent = stationaryNode . getSourceBlock ( ) ?. getParent ( ) ;
745754 if ( ! parent ) return null ;
746755 return this . findInsertStartPoint (
0 commit comments