55 */
66
77import {
8+ common ,
89 ASTNode ,
910 BlockSvg ,
1011 ConnectionType ,
1112 RenderedConnection ,
13+ LineCursor ,
1214 dragging ,
1315 utils ,
1416} from 'blockly' ;
@@ -35,14 +37,21 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
3537 /** Where a constrained movement should start when traversing the tree. */
3638 private searchNode : ASTNode | null = null ;
3739
40+ private cursor : LineCursor | null ;
41+
3842 constructor (
3943 private block : BlockSvg ,
4044 private navigation : Navigation ,
4145 ) {
4246 super ( block ) ;
47+ this . cursor = block . workspace . getCursor ( ) ;
4348 }
4449
4550 override startDrag ( e ?: PointerEvent ) {
51+ if ( ! this . cursor ) throw new Error ( 'precondition failure' ) ;
52+ // Select and focus block.
53+ common . setSelected ( this . block ) ;
54+ this . cursor . setCurNode ( ASTNode . createBlockNode ( this . block ) ) ;
4655 super . startDrag ( e ) ;
4756 // Set position of the dragging block, so that it doesn't pop
4857 // to the top left of the workspace.
@@ -148,8 +157,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
148157 draggingBlock : BlockSvg ,
149158 localConns : RenderedConnection [ ] ,
150159 ) : ConnectionCandidate | null {
151- const cursor = draggingBlock . workspace . getCursor ( ) ;
152- if ( ! cursor ) return null ;
160+ // TODO: Handle the case where the cursor is null, or never return null
161+ // from workspace.getCursor()
162+ if ( ! this . cursor ) throw new Error ( 'precondition failure' ) ;
153163
154164 // Helper function for traversal.
155165 function isConnection ( node : ASTNode | null ) : boolean {
@@ -162,9 +172,9 @@ export class KeyboardDragStrategy extends dragging.BlockDragStrategy {
162172 const dir = this . currentDragDirection ;
163173 while ( potential && ! candidateConnection ) {
164174 if ( dir === Direction . Up || dir === Direction . Left ) {
165- potential = cursor . getPreviousNode ( potential , isConnection , true ) ;
175+ potential = this . cursor . getPreviousNode ( potential , isConnection , true ) ;
166176 } else if ( dir === Direction . Down || dir === Direction . Right ) {
167- potential = cursor . getNextNode ( potential , isConnection , true ) ;
177+ potential = this . cursor . getNextNode ( potential , isConnection , true ) ;
168178 }
169179
170180 localConns . forEach ( ( conn : RenderedConnection ) => {
0 commit comments