@@ -170,114 +170,111 @@ const EVENT_HANDLER = {
170170 /**
171171 * mrcOnModuleCurrent is called for each EventHandlerBlock when the module becomes the current module.
172172 */
173- mrcOnModuleCurrent : function ( this : EventHandlerBlock ) : void {
174- this . checkEvent ( ) ;
173+ mrcOnModuleCurrent : function ( this : EventHandlerBlock , editor : Editor ) : void {
174+ this . checkEvent ( editor ) ;
175175 } ,
176176 /**
177177 * mrcOnLoad is called for each EventHandlerBlock when the blocks are loaded in the blockly
178178 * workspace.
179179 */
180- mrcOnLoad : function ( this : EventHandlerBlock ) : void {
181- this . checkEvent ( ) ;
180+ mrcOnLoad : function ( this : EventHandlerBlock , editor : Editor ) : void {
181+ this . checkEvent ( editor ) ;
182182 } ,
183183 /**
184184 * checkEvent checks the block, updates it, and/or adds a warning balloon if necessary.
185185 * It is called from mrcOnModuleCurrent and mrcOnLoad above.
186186 */
187- checkEvent : function ( this : EventHandlerBlock ) : void {
187+ checkEvent : function ( this : EventHandlerBlock , editor : Editor ) : void {
188188 const warnings : string [ ] = [ ] ;
189189
190- const editor = Editor . getEditorForBlocklyWorkspace ( this . workspace , true /* returnCurrentIfNotFound */ ) ;
191- if ( editor ) {
192- if ( this . mrcSenderType === SenderType . ROBOT ) {
193- // This block is an event handler for a robot event.
194- // Check whether the robot event still exists and whether it has been changed.
195- // If the robot event doesn't exist, put a visible warning on this block.
196- // If the robot event has changed, update the block if possible or put a
197- // visible warning on it.
198- let foundRobotEvent = false ;
199- const robotEvents = editor . getEventsFromRobot ( ) ;
200- for ( const robotEvent of robotEvents ) {
201- if ( robotEvent . eventId === this . mrcEventId ) {
202- foundRobotEvent = true ;
203- if ( this . getFieldValue ( FIELD_EVENT_NAME ) !== robotEvent . name ) {
204- this . setFieldValue ( robotEvent . name , FIELD_EVENT_NAME ) ;
205- }
206- this . mrcParameters = [ ] ;
207- robotEvent . args . forEach ( arg => {
208- this . mrcParameters . push ( {
209- name : arg . name ,
210- type : arg . type ,
211- } ) ;
190+ if ( this . mrcSenderType === SenderType . ROBOT ) {
191+ // This block is an event handler for a robot event.
192+ // Check whether the robot event still exists and whether it has been changed.
193+ // If the robot event doesn't exist, put a visible warning on this block.
194+ // If the robot event has changed, update the block if possible or put a
195+ // visible warning on it.
196+ let foundRobotEvent = false ;
197+ const robotEvents = editor . getEventsFromRobot ( ) ;
198+ for ( const robotEvent of robotEvents ) {
199+ if ( robotEvent . eventId === this . mrcEventId ) {
200+ foundRobotEvent = true ;
201+ if ( this . getFieldValue ( FIELD_EVENT_NAME ) !== robotEvent . name ) {
202+ this . setFieldValue ( robotEvent . name , FIELD_EVENT_NAME ) ;
203+ }
204+ this . mrcParameters = [ ] ;
205+ robotEvent . args . forEach ( arg => {
206+ this . mrcParameters . push ( {
207+ name : arg . name ,
208+ type : arg . type ,
212209 } ) ;
213- this . mrcUpdateParams ( ) ;
210+ } ) ;
211+ this . mrcUpdateParams ( ) ;
214212
215- // Since we found the robot event, we can break out of the loop.
216- break ;
217- }
218- }
219- if ( ! foundRobotEvent ) {
220- warnings . push ( Blockly . Msg . EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND ) ;
213+ // Since we found the robot event, we can break out of the loop.
214+ break ;
221215 }
222216 }
217+ if ( ! foundRobotEvent ) {
218+ warnings . push ( Blockly . Msg . EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND ) ;
219+ }
220+ }
223221
224- if ( this . mrcSenderType === SenderType . MECHANISM ) {
225- // This block is an event handler for a mechanism event.
226- // Check whether the mechanism still exists, whether it has been
227- // changed, whether the event still exists, and whether the event has
228- // been changed.
229- // If the mechanism doesn't exist, put a visible warning on this block.
230- // If the mechanism has changed, update the block if possible or put a
231- // visible warning on it.
232- // If the event doesn't exist, put a visible warning on this block.
233- // If the event has changed, update the block if possible or put a
234- // visible warning on it.
235- let foundMechanism = false ;
236- const mechanismsInRobot = editor . getMechanismsFromRobot ( ) ;
237- for ( const mechanismInRobot of mechanismsInRobot ) {
238- if ( mechanismInRobot . mechanismId === this . mrcMechanismId ) {
239- foundMechanism = true ;
240-
241- // If the mechanism name has changed, we can handle that.
242- if ( this . getFieldValue ( FIELD_SENDER ) !== mechanismInRobot . name ) {
243- this . setFieldValue ( mechanismInRobot . name , FIELD_SENDER ) ;
244- }
222+ if ( this . mrcSenderType === SenderType . MECHANISM ) {
223+ // This block is an event handler for a mechanism event.
224+ // Check whether the mechanism still exists, whether it has been
225+ // changed, whether the event still exists, and whether the event has
226+ // been changed.
227+ // If the mechanism doesn't exist, put a visible warning on this block.
228+ // If the mechanism has changed, update the block if possible or put a
229+ // visible warning on it.
230+ // If the event doesn't exist, put a visible warning on this block.
231+ // If the event has changed, update the block if possible or put a
232+ // visible warning on it.
233+ let foundMechanism = false ;
234+ const mechanismsInRobot = editor . getMechanismsFromRobot ( ) ;
235+ for ( const mechanismInRobot of mechanismsInRobot ) {
236+ if ( mechanismInRobot . mechanismId === this . mrcMechanismId ) {
237+ foundMechanism = true ;
238+
239+ // If the mechanism name has changed, we can handle that.
240+ if ( this . getFieldValue ( FIELD_SENDER ) !== mechanismInRobot . name ) {
241+ this . setFieldValue ( mechanismInRobot . name , FIELD_SENDER ) ;
242+ }
245243
246- let foundMechanismEvent = false ;
247- const mechanism = editor . getMechanism ( mechanismInRobot ) ;
248- const mechanismEvents : storageModuleContent . Event [ ] = mechanism
249- ? editor . getEventsFromMechanism ( mechanism ) : [ ] ;
250- for ( const mechanismEvent of mechanismEvents ) {
251- if ( mechanismEvent . eventId === this . mrcEventId ) {
252- foundMechanismEvent = true ;
253- if ( this . getFieldValue ( FIELD_EVENT_NAME ) !== mechanismEvent . name ) {
254- this . setFieldValue ( mechanismEvent . name , FIELD_EVENT_NAME ) ;
255- }
256-
257- this . mrcParameters = [ ] ;
258- mechanismEvent . args . forEach ( arg => {
259- this . mrcParameters . push ( {
260- name : arg . name ,
261- type : arg . type ,
262- } ) ;
244+ let foundMechanismEvent = false ;
245+ const mechanism = editor . getMechanism ( mechanismInRobot ) ;
246+ const mechanismEvents : storageModuleContent . Event [ ] = mechanism
247+ ? editor . getEventsFromMechanism ( mechanism ) : [ ] ;
248+ for ( const mechanismEvent of mechanismEvents ) {
249+ if ( mechanismEvent . eventId === this . mrcEventId ) {
250+ foundMechanismEvent = true ;
251+ if ( this . getFieldValue ( FIELD_EVENT_NAME ) !== mechanismEvent . name ) {
252+ this . setFieldValue ( mechanismEvent . name , FIELD_EVENT_NAME ) ;
253+ }
254+
255+ this . mrcParameters = [ ] ;
256+ mechanismEvent . args . forEach ( arg => {
257+ this . mrcParameters . push ( {
258+ name : arg . name ,
259+ type : arg . type ,
263260 } ) ;
264- this . mrcUpdateParams ( ) ;
261+ } ) ;
262+ this . mrcUpdateParams ( ) ;
265263
266- // Since we found the mechanism event, we can break out of the loop.
267- break ;
268- }
264+ // Since we found the mechanism event, we can break out of the loop.
265+ break ;
269266 }
270- if ( ! foundMechanismEvent ) {
271- warnings . push ( Blockly . Msg . EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND ) ;
272- }
273-
274- // Since we found the mechanism, we can break out of the loop.
275- break ;
276267 }
268+ if ( ! foundMechanismEvent ) {
269+ warnings . push ( Blockly . Msg . EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND ) ;
270+ }
271+
272+ // Since we found the mechanism, we can break out of the loop.
273+ break ;
277274 }
278- if ( ! foundMechanism ) {
279- warnings . push ( Blockly . Msg . EVENT_HANDLER_MECHANISM_NOT_FOUND ) ;
280- }
275+ }
276+ if ( ! foundMechanism ) {
277+ warnings . push ( Blockly . Msg . EVENT_HANDLER_MECHANISM_NOT_FOUND ) ;
281278 }
282279 }
283280
0 commit comments