@@ -573,18 +573,13 @@ export class BlockDragStrategy implements IDragStrategy {
573573 const direction = this . getDirectionToNewLocation (
574574 Coordinate . sum ( this . startLoc ! , delta ) ,
575575 ) ;
576- const candidate = this . findTraversalCandidate ( direction ) ;
577- if ( candidate ) {
578- return candidate ;
579- }
580-
581- delta = new Coordinate ( 0 , 0 ) ;
576+ return this . findTraversalCandidate ( direction ) ;
582577 }
583578
584579 // If we do not have a candidate yet, we fallback to the closest one nearby.
585580 let radius = this . getSearchRadius ( ) ;
586581 const localConns = this . getLocalConnections ( this . block ) ;
587- let candidate = null ;
582+ let candidate : ConnectionCandidate | null = null ;
588583
589584 for ( const conn of localConns ) {
590585 const { connection : neighbour , radius : rad } = conn . closest ( radius , delta ) ;
@@ -775,30 +770,40 @@ export class BlockDragStrategy implements IDragStrategy {
775770 * @returns A candidate connection and radius, or null if none was found.
776771 */
777772 findTraversalCandidate ( direction : Direction ) : ConnectionCandidate | null {
778- const currentPairIndex = this . allConnectionPairs . findIndex (
773+ const pairs = this . allConnectionPairs ;
774+ if ( direction === Direction . NONE || ! pairs . length ) {
775+ return this . connectionCandidate ;
776+ }
777+ const forwardTraversal =
778+ direction === Direction . RIGHT || direction === Direction . DOWN ;
779+ const currentPairIndex = pairs . findIndex (
779780 ( pair ) =>
780781 this . connectionCandidate ?. local === pair . local &&
781782 this . connectionCandidate ?. neighbour === pair . neighbour ,
782783 ) ;
783- if ( currentPairIndex !== - 1 ) {
784- if ( direction === Direction . UP || direction === Direction . LEFT ) {
785- const nextPair =
786- this . allConnectionPairs [ currentPairIndex - 1 ] ??
787- this . allConnectionPairs [ this . allConnectionPairs . length - 1 ] ;
788- return { ...nextPair , distance : 0 } ;
789- } else if (
790- direction === Direction . DOWN ||
791- direction === Direction . RIGHT
792- ) {
793- const nextPair =
794- this . allConnectionPairs [ currentPairIndex + 1 ] ??
795- this . allConnectionPairs [ 0 ] ;
796- return { ...nextPair , distance : 0 } ;
784+
785+ if ( forwardTraversal ) {
786+ if ( currentPairIndex === - 1 ) {
787+ return this . pairToCandidate ( pairs [ 0 ] ) ;
788+ } else if ( currentPairIndex === pairs . length - 1 ) {
789+ return null ;
790+ } else {
791+ return this . pairToCandidate ( pairs [ currentPairIndex + 1 ] ) ;
792+ }
793+ } else {
794+ if ( currentPairIndex === - 1 ) {
795+ return this . pairToCandidate ( pairs [ pairs . length - 1 ] ) ;
796+ } else if ( currentPairIndex === 0 ) {
797+ return null ;
798+ } else {
799+ return this . pairToCandidate ( pairs [ currentPairIndex - 1 ] ) ;
797800 }
798801 }
799- return null ;
800802 }
801803
804+ private pairToCandidate ( pair : ConnectionPair ) : ConnectionCandidate {
805+ return { ...pair , distance : 0 } ;
806+ }
802807 /**
803808 * Returns the cardinal direction that the block being dragged would have to
804809 * move in to reach the given location.
0 commit comments