66
77import {
88 Events ,
9- Msg ,
109 ShortcutRegistry ,
1110 utils as BlocklyUtils ,
1211 getFocusManager ,
@@ -54,33 +53,48 @@ export class EnterAction {
5453 */
5554 ShortcutRegistry . registry . register ( {
5655 name : Constants . SHORTCUT_NAMES . EDIT_OR_CONFIRM ,
57- preconditionFn : ( workspace ) =>
58- this . navigation . canCurrentlyEdit ( workspace ) ,
59- 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 => {
6074 event . preventDefault ( ) ;
6175
76+ const targetWorkspace = workspace . isFlyout
77+ ? workspace . targetWorkspace
78+ : workspace ;
79+ if ( ! targetWorkspace ) return false ;
80+
6281 let flyoutCursor ;
6382 let curNode ;
6483
65- switch ( this . navigation . getState ( workspace ) ) {
84+ switch ( this . navigation . getState ( ) ) {
6685 case Constants . STATE . WORKSPACE :
67- this . handleEnterForWS ( workspace ) ;
68- return true ;
86+ return this . handleEnterForWS ( workspace ) ;
6987 case Constants . STATE . FLYOUT :
70- if ( ! workspace . targetWorkspace ) return false ;
71- flyoutCursor = this . navigation . getFlyoutCursor (
72- workspace . targetWorkspace ,
73- ) ;
88+ flyoutCursor = this . navigation . getFlyoutCursor ( targetWorkspace ) ;
7489 if ( ! flyoutCursor ) {
7590 return false ;
7691 }
7792 curNode = flyoutCursor . getCurNode ( ) ;
7893 if ( curNode instanceof BlockSvg ) {
79- this . insertFromFlyout ( workspace . targetWorkspace ) ;
94+ this . insertFromFlyout ( targetWorkspace ) ;
8095 } else if ( curNode instanceof FlyoutButton ) {
8196 this . triggerButtonCallback ( workspace ) ;
8297 }
83-
8498 return true ;
8599 default :
86100 return false ;
@@ -90,30 +104,57 @@ export class EnterAction {
90104 } ) ;
91105 }
92106
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+ // Returning true is sometimes incorrect for icons, but there's no API to check.
125+ if ( curNode instanceof icons . Icon ) return true ;
126+ return false ;
127+ }
128+
93129 /**
94130 * Handles hitting the enter key on the workspace.
95131 *
96132 * @param workspace The workspace.
133+ * @returns True if the enter was handled, false otherwise.
97134 */
98- private handleEnterForWS ( workspace : WorkspaceSvg ) {
135+ private handleEnterForWS ( workspace : WorkspaceSvg ) : boolean {
99136 const cursor = workspace . getCursor ( ) ;
100- if ( ! cursor ) return ;
101- const curNode = cursor . getCurNode ( ) ;
102- if ( ! curNode ) return ;
137+ const curNode = cursor ?. getCurNode ( ) ;
138+ if ( ! curNode ) return false ;
103139 if ( curNode instanceof Field ) {
104140 curNode . showEditor ( ) ;
141+ return true ;
105142 } else if ( curNode instanceof BlockSvg ) {
106143 if ( ! this . tryShowFullBlockFieldEditor ( curNode ) ) {
107144 showHelpHint ( workspace ) ;
108145 }
146+ return true ;
109147 } else if (
110148 curNode instanceof RenderedConnection ||
111149 curNode instanceof WorkspaceSvg
112150 ) {
113151 this . navigation . openToolboxOrFlyout ( workspace ) ;
152+ return true ;
114153 } else if ( curNode instanceof icons . Icon ) {
115154 curNode . onClick ( ) ;
155+ return true ;
116156 }
157+ return false ;
117158 }
118159
119160 /**
0 commit comments