66
77import {
88 Events ,
9- Msg ,
109 ShortcutRegistry ,
1110 utils as BlocklyUtils ,
12- getFocusManager ,
1311 BlockSvg ,
1412 FlyoutButton ,
1513 RenderedConnection ,
@@ -55,33 +53,48 @@ export class EnterAction {
5553 */
5654 ShortcutRegistry . registry . register ( {
5755 name : Constants . SHORTCUT_NAMES . EDIT_OR_CONFIRM ,
58- preconditionFn : ( workspace ) =>
59- this . navigation . canCurrentlyEdit ( workspace ) ,
60- callback : ( workspace , event ) => {
56+ preconditionFn : ( workspace ) : boolean => {
57+ switch ( this . navigation . getState ( ) ) {
58+ case Constants . STATE . WORKSPACE :
59+ return this . shouldHandleEnterForWS ( workspace ) ;
60+ case Constants . STATE . FLYOUT : {
61+ // If we're in the flyout the only supported actions are inserting
62+ // blocks or clicking buttons, so don't handle this if the
63+ // main workspace is read only.
64+ const targetWorkspace = workspace . isFlyout
65+ ? workspace . targetWorkspace
66+ : workspace ;
67+ return ! ! targetWorkspace && ! targetWorkspace . isReadOnly ( ) ;
68+ }
69+ default :
70+ return false ;
71+ }
72+ } ,
73+ callback : ( workspace , event ) : boolean => {
6174 event . preventDefault ( ) ;
6275
76+ const targetWorkspace = workspace . isFlyout
77+ ? workspace . targetWorkspace
78+ : workspace ;
79+ if ( ! targetWorkspace ) return false ;
80+
6381 let flyoutCursor ;
6482 let curNode ;
6583
66- switch ( this . navigation . getState ( workspace ) ) {
84+ switch ( this . navigation . getState ( ) ) {
6785 case Constants . STATE . WORKSPACE :
68- this . handleEnterForWS ( workspace ) ;
69- return true ;
86+ return this . handleEnterForWS ( workspace ) ;
7087 case Constants . STATE . FLYOUT :
71- if ( ! workspace . targetWorkspace ) return false ;
72- flyoutCursor = this . navigation . getFlyoutCursor (
73- workspace . targetWorkspace ,
74- ) ;
88+ flyoutCursor = this . navigation . getFlyoutCursor ( targetWorkspace ) ;
7589 if ( ! flyoutCursor ) {
7690 return false ;
7791 }
7892 curNode = flyoutCursor . getCurNode ( ) ;
7993 if ( curNode instanceof BlockSvg ) {
80- this . insertFromFlyout ( workspace . targetWorkspace ) ;
94+ this . insertFromFlyout ( targetWorkspace ) ;
8195 } else if ( curNode instanceof FlyoutButton ) {
82- this . triggerButtonCallback ( workspace ) ;
96+ this . triggerButtonCallback ( targetWorkspace ) ;
8397 }
84-
8598 return true ;
8699 default :
87100 return false ;
@@ -91,33 +104,61 @@ export class EnterAction {
91104 } ) ;
92105 }
93106
107+ /**
108+ * Checks if the enter key should do anything for this ws.
109+ *
110+ * @param workspace The workspace to check.
111+ * @returns True if the enter action should be handled.
112+ */
113+ private shouldHandleEnterForWS ( workspace : WorkspaceSvg ) : boolean {
114+ const cursor = workspace . getCursor ( ) ;
115+ const curNode = cursor ?. getCurNode ( ) ;
116+ if ( ! curNode ) return false ;
117+ if ( curNode instanceof Field ) return curNode . isClickable ( ) ;
118+ if (
119+ curNode instanceof RenderedConnection ||
120+ curNode instanceof WorkspaceSvg
121+ ) {
122+ return ! workspace . isReadOnly ( ) ;
123+ }
124+ if ( curNode instanceof BlockSvg ) return true ;
125+ // Returning true is sometimes incorrect for icons, but there's no API to check.
126+ if ( curNode instanceof icons . Icon ) return true ;
127+ return false ;
128+ }
129+
94130 /**
95131 * Handles hitting the enter key on the workspace.
96132 *
97133 * @param workspace The workspace.
134+ * @returns True if the enter was handled, false otherwise.
98135 */
99- private handleEnterForWS ( workspace : WorkspaceSvg ) {
136+ private handleEnterForWS ( workspace : WorkspaceSvg ) : boolean {
100137 const cursor = workspace . getCursor ( ) ;
101- if ( ! cursor ) return ;
102- const curNode = cursor . getCurNode ( ) ;
103- if ( ! curNode ) return ;
138+ const curNode = cursor ?. getCurNode ( ) ;
139+ if ( ! curNode ) return false ;
104140 if ( curNode instanceof Field ) {
105141 curNode . showEditor ( ) ;
142+ return true ;
106143 } else if ( curNode instanceof BlockSvg ) {
107144 if ( ! this . tryShowFullBlockFieldEditor ( curNode ) ) {
108145 showHelpHint ( workspace ) ;
109146 }
147+ return true ;
110148 } else if (
111149 curNode instanceof RenderedConnection ||
112150 curNode instanceof WorkspaceSvg
113151 ) {
114152 this . navigation . openToolboxOrFlyout ( workspace ) ;
153+ return true ;
115154 } else if ( curNode instanceof icons . Icon ) {
116155 curNode . onClick ( ) ;
117156 renderManagement . finishQueuedRenders ( ) . then ( ( ) => {
118157 cursor . in ( ) ;
119158 } ) ;
159+ return true ;
120160 }
161+ return false ;
121162 }
122163
123164 /**
@@ -150,8 +191,6 @@ export class EnterAction {
150191
151192 workspace . setResizesEnabled ( true ) ;
152193
153- getFocusManager ( ) . focusTree ( workspace ) ;
154- workspace . getCursor ( ) ?. setCurNode ( newBlock ) ;
155194 this . mover . startMove ( workspace , newBlock , insertStartPoint ) ;
156195
157196 const isStartBlock =
0 commit comments