@@ -404,8 +404,8 @@ export class Navigation {
404404 const passiveFocusNode = this . passiveFocusIndicator . getCurNode ( ) ;
405405 this . passiveFocusIndicator . hide ( ) ;
406406 const disposed = passiveFocusNode ?. getSourceBlock ( ) ?. disposed ;
407- // If there's a gesture then it will either set the node if it has not
408- // been disposed (which can happen when blocks are reloaded) or be a click
407+ // If there's a gesture then it will either set the node if it has not
408+ // been disposed (which can happen when blocks are reloaded) or be a click
409409 // that should not set one.
410410 if ( ! Blockly . Gesture . inProgress ( ) && passiveFocusNode && ! disposed ) {
411411 cursor . setCurNode ( passiveFocusNode ) ;
@@ -662,25 +662,25 @@ export class Navigation {
662662 }
663663
664664 /**
665- * Tries to intelligently connect the blocks or connections
666- * represented by the given nodes, based on node types and locations.
665+ * Finds the starting point for an insert.
667666 *
668- * @param stationaryNode The first node to connect .
667+ * @param stationaryNode The first node to find a connection for .
669668 * @param movingBlock The block we're moving.
670- * @returns True if the key was handled; false if something went
671- * wrong.
669+ * @returns The initial connection to use or begin move mode at.
672670 */
673- tryToConnectBlock (
671+ findInsertStartPoint (
674672 stationaryNode : Blockly . ASTNode ,
675673 movingBlock : Blockly . BlockSvg ,
676- ) : boolean {
674+ ) : Blockly . RenderedConnection | null {
677675 const stationaryType = stationaryNode . getType ( ) ;
678676 const stationaryLoc = stationaryNode . getLocation ( ) ;
677+ const movingHasOutput = ! ! movingBlock . outputConnection ;
679678
680679 if ( stationaryNode . getType ( ) === Blockly . ASTNode . types . FIELD ) {
680+ // Can't connect a block to a field, so try going up to the source block.
681681 const sourceBlock = stationaryNode . getSourceBlock ( ) ;
682- if ( ! sourceBlock ) return false ;
683- return this . tryToConnectBlock (
682+ if ( ! sourceBlock ) return null ;
683+ return this . findInsertStartPoint (
684684 Blockly . ASTNode . createBlockNode ( sourceBlock ) ,
685685 movingBlock ,
686686 ) ;
@@ -691,27 +691,27 @@ export class Navigation {
691691 // Move to the block if we're trying to insert a statement block into
692692 // a value connection.
693693 if (
694- ! movingBlock . outputConnection &&
694+ ! movingHasOutput &&
695695 stationaryAsConnection . type === Blockly . ConnectionType . INPUT_VALUE
696696 ) {
697697 const sourceBlock = stationaryNode . getSourceBlock ( ) ;
698- if ( ! sourceBlock ) return false ;
699- return this . tryToConnectBlock (
698+ if ( ! sourceBlock ) return null ;
699+ return this . findInsertStartPoint (
700700 Blockly . ASTNode . createBlockNode ( sourceBlock ) ,
701701 movingBlock ,
702702 ) ;
703703 }
704704
705705 // Connect the moving block to the stationary connection using
706706 // the most plausible connection on the moving block.
707- return this . insertBlock ( movingBlock , stationaryAsConnection ) ;
707+ return stationaryAsConnection ;
708708 } else if ( stationaryType === Blockly . ASTNode . types . WORKSPACE ) {
709- return this . moveBlockToWorkspace ( movingBlock , stationaryNode ) ;
709+ return null ;
710710 } else if ( stationaryType === Blockly . ASTNode . types . BLOCK ) {
711711 const stationaryBlock = stationaryLoc as Blockly . BlockSvg ;
712712
713713 // 1. Connect blocks to first compatible input
714- const inputType = movingBlock . outputConnection
714+ const inputType = movingHasOutput
715715 ? Blockly . inputs . inputTypes . VALUE
716716 : Blockly . inputs . inputTypes . STATEMENT ;
717717 const compatibleInputs = stationaryBlock . inputList . filter (
@@ -726,36 +726,61 @@ export class Navigation {
726726 connection = connection . targetBlock ( ) ! . nextConnection ! ;
727727 }
728728 }
729- return this . insertBlock (
730- movingBlock ,
731- connection as Blockly . RenderedConnection ,
732- ) ;
729+ return connection as Blockly . RenderedConnection ;
733730 }
734731
735732 // 2. Connect statement blocks to next connection.
736- if ( stationaryBlock . nextConnection && ! movingBlock . outputConnection ) {
737- return this . insertBlock ( movingBlock , stationaryBlock . nextConnection ) ;
733+ if ( stationaryBlock . nextConnection && ! movingHasOutput ) {
734+ return stationaryBlock . nextConnection ;
738735 }
739736
740737 // 3. Output connection. This will wrap around or displace.
741738 if ( stationaryBlock . outputConnection ) {
742- // Move to parent if we're trying to insert a statement block.
743- if (
744- ! movingBlock . outputConnection &&
739+ // Try to wrap.
740+ const target = stationaryBlock . outputConnection . targetConnection ;
741+ if ( movingHasOutput && target ) {
742+ const sourceNode = Blockly . ASTNode . createConnectionNode ( target ) ;
743+ if ( sourceNode ) {
744+ return this . findInsertStartPoint ( sourceNode , movingBlock ) ;
745+ }
746+ } else if (
747+ ! movingHasOutput &&
745748 stationaryNode . getType ( ) === Blockly . ASTNode . types . BLOCK
746749 ) {
750+ // Move to parent if we're trying to insert a statement block.
747751 const parent = stationaryNode . getSourceBlock ( ) ?. getParent ( ) ;
748- if ( ! parent ) return false ;
749- return this . tryToConnectBlock (
752+ if ( ! parent ) return null ;
753+ return this . findInsertStartPoint (
750754 Blockly . ASTNode . createBlockNode ( parent ) ,
751755 movingBlock ,
752756 ) ;
753757 }
754- return this . insertBlock ( movingBlock , stationaryBlock . outputConnection ) ;
758+ return stationaryBlock . outputConnection ;
755759 }
756760 }
757- this . warn ( `Unexpected case in tryToConnectBlock ${ stationaryType } .` ) ;
758- return false ;
761+ this . warn ( `Unexpected case in findInsertStartPoint ${ stationaryType } .` ) ;
762+ return null ;
763+ }
764+
765+ /**
766+ * Tries to intelligently connect the blocks or connections
767+ * represented by the given nodes, based on node types and locations.
768+ *
769+ * @param stationaryNode The first node to connect.
770+ * @param movingBlock The block we're moving.
771+ * @returns True if the key was handled; false if something went
772+ * wrong.
773+ */
774+ tryToConnectBlock (
775+ stationaryNode : Blockly . ASTNode ,
776+ movingBlock : Blockly . BlockSvg ,
777+ ) : boolean {
778+ const destConnection = this . findInsertStartPoint (
779+ stationaryNode ,
780+ movingBlock ,
781+ ) ;
782+ if ( ! destConnection ) return false ;
783+ return this . insertBlock ( movingBlock , destConnection ) ;
759784 }
760785
761786 /**
0 commit comments