55 */
66
77import {
8- ASTNode ,
98 Events ,
109 ShortcutRegistry ,
1110 utils as BlocklyUtils ,
1211 getFocusManager ,
13- } from 'blockly/core' ;
14-
15- import type {
16- Block ,
1712 BlockSvg ,
18- Field ,
1913 FlyoutButton ,
14+ RenderedConnection ,
2015 WorkspaceSvg ,
16+ Field ,
17+ FocusableTreeTraverser ,
2118} from 'blockly/core' ;
2219
20+ import type { Block , INavigable } from 'blockly/core' ;
21+
2322import * as Constants from '../constants' ;
2423import type { Navigation } from '../navigation' ;
2524import { Mover } from './mover' ;
@@ -72,15 +71,10 @@ export class EnterAction {
7271 return false ;
7372 }
7473 curNode = flyoutCursor . getCurNode ( ) ;
75- nodeType = curNode ?. getType ( ) ;
76-
77- switch ( nodeType ) {
78- case ASTNode . types . STACK :
79- this . insertFromFlyout ( workspace ) ;
80- break ;
81- case ASTNode . types . BUTTON :
82- this . triggerButtonCallback ( workspace ) ;
83- break ;
74+ if ( curNode instanceof BlockSvg ) {
75+ this . insertFromFlyout ( workspace ) ;
76+ } else if ( curNode instanceof FlyoutButton ) {
77+ this . triggerButtonCallback ( workspace ) ;
8478 }
8579
8680 return true ;
@@ -102,18 +96,17 @@ export class EnterAction {
10296 if ( ! cursor ) return ;
10397 const curNode = cursor . getCurNode ( ) ;
10498 if ( ! curNode ) return ;
105- const nodeType = curNode . getType ( ) ;
106- if ( nodeType === ASTNode . types . FIELD ) {
107- ( curNode . getLocation ( ) as Field ) . showEditor ( ) ;
108- } else if ( nodeType === ASTNode . types . BLOCK ) {
109- const block = curNode . getLocation ( ) as Block ;
110- if ( ! this . tryShowFullBlockFieldEditor ( block ) ) {
99+ if ( curNode instanceof Field ) {
100+ curNode . showEditor ( ) ;
101+ } else if ( curNode instanceof BlockSvg ) {
102+ if ( ! this . tryShowFullBlockFieldEditor ( curNode ) ) {
111103 showHelpHint ( workspace ) ;
112104 }
113- } else if ( curNode . isConnection ( ) || nodeType === ASTNode . types . WORKSPACE ) {
105+ } else if (
106+ curNode instanceof RenderedConnection ||
107+ curNode instanceof WorkspaceSvg
108+ ) {
114109 this . navigation . openToolboxOrFlyout ( workspace ) ;
115- } else if ( nodeType === ASTNode . types . STACK ) {
116- console . warn ( 'Cannot mark a stack.' ) ;
117110 }
118111 }
119112
@@ -135,7 +128,9 @@ export class EnterAction {
135128 Events . setGroup ( true ) ;
136129 }
137130
138- const stationaryNode = this . navigation . getFocusedASTNode ( workspace ) ;
131+ const stationaryNode = FocusableTreeTraverser . findFocusedNode (
132+ workspace ,
133+ ) as unknown as INavigable < any > ;
139134 const newBlock = this . createNewBlock ( workspace ) ;
140135 if ( ! newBlock ) return ;
141136 const insertStartPoint = stationaryNode
@@ -148,8 +143,7 @@ export class EnterAction {
148143 workspace . setResizesEnabled ( true ) ;
149144
150145 getFocusManager ( ) . focusTree ( workspace ) ;
151- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
152- workspace . getCursor ( ) ?. setCurNode ( ASTNode . createBlockNode ( newBlock ) ! ) ;
146+ workspace . getCursor ( ) ?. setCurNode ( newBlock ) ;
153147 this . mover . startMove ( workspace , newBlock , insertStartPoint ) ;
154148
155149 const isStartBlock =
@@ -253,11 +247,8 @@ export class EnterAction {
253247 * containing a flyout with a button.
254248 */
255249 private triggerButtonCallback ( workspace : WorkspaceSvg ) {
256- const button = this . navigation
257- . getFlyoutCursor ( workspace )
258- ?. getCurNode ( )
259- ?. getLocation ( ) as FlyoutButton | undefined ;
260- if ( ! button ) return ;
250+ const button = this . navigation . getFlyoutCursor ( workspace ) ?. getCurNode ( ) ;
251+ if ( ! ( button instanceof FlyoutButton ) ) return ;
261252
262253 const flyoutButtonCallbacks : Map < string , ( p1 : FlyoutButton ) => void > =
263254 // @ts -expect-error private field access
@@ -310,11 +301,8 @@ export class EnterAction {
310301 return null ;
311302 }
312303
313- const curBlock = this . navigation
314- . getFlyoutCursor ( workspace )
315- ?. getCurNode ( )
316- ?. getLocation ( ) as BlockSvg | undefined ;
317- if ( ! curBlock ?. isEnabled ( ) ) {
304+ const curBlock = this . navigation . getFlyoutCursor ( workspace ) ?. getCurNode ( ) ;
305+ if ( ! ( curBlock instanceof BlockSvg ) || ! curBlock . isEnabled ( ) ) {
318306 console . warn ( "Can't insert a disabled block." ) ;
319307 return null ;
320308 }
0 commit comments