@@ -20,6 +20,7 @@ import {
2020 createBlockDefinitionsFromJsonArray ,
2121 defineBlocks ,
2222} from '../core/common.js' ;
23+ import * as eventUtils from '../core/events/utils.js' ;
2324import '../core/field_dropdown.js' ;
2425import '../core/field_label.js' ;
2526import '../core/field_number.js' ;
@@ -334,6 +335,11 @@ export type ControlFlowInLoopBlock = Block & ControlFlowInLoopMixin;
334335interface ControlFlowInLoopMixin extends ControlFlowInLoopMixinType { }
335336type ControlFlowInLoopMixinType = typeof CONTROL_FLOW_IN_LOOP_CHECK_MIXIN ;
336337
338+ /**
339+ * The language-neutral ID for when the reason why a block is disabled is
340+ * because the block is only valid inside of a loop.
341+ */
342+ const CONTROL_FLOW_NOT_IN_LOOP_DISABLED_REASON = 'CONTROL_FLOW_NOT_IN_LOOP' ;
337343/**
338344 * This mixin adds a check to make sure the 'controls_flow_statements' block
339345 * is contained in a loop. Otherwise a warning is added to the block.
@@ -365,19 +371,30 @@ const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
365371 // Don't change state if:
366372 // * It's at the start of a drag.
367373 // * It's not a move event.
368- if ( ! ws . isDragging || ws . isDragging ( ) || e . type !== Events . BLOCK_MOVE ) {
374+ if (
375+ ! ws . isDragging ||
376+ ws . isDragging ( ) ||
377+ ( e . type !== Events . BLOCK_MOVE && e . type !== Events . BLOCK_CREATE )
378+ ) {
369379 return ;
370380 }
371381 const enabled = ! ! this . getSurroundLoop ( ) ;
372382 this . setWarningText (
373383 enabled ? null : Msg [ 'CONTROLS_FLOW_STATEMENTS_WARNING' ] ,
374384 ) ;
385+
375386 if ( ! this . isInFlyout ) {
376- const group = Events . getGroup ( ) ;
377- // Makes it so the move and the disable event get undone together.
378- Events . setGroup ( e . group ) ;
379- this . setEnabled ( enabled ) ;
380- Events . setGroup ( group ) ;
387+ try {
388+ // There is no need to record the enable/disable change on the undo/redo
389+ // list since the change will be automatically recreated when replayed.
390+ eventUtils . setRecordUndo ( false ) ;
391+ this . setDisabledReason (
392+ ! enabled ,
393+ CONTROL_FLOW_NOT_IN_LOOP_DISABLED_REASON ,
394+ ) ;
395+ } finally {
396+ eventUtils . setRecordUndo ( true ) ;
397+ }
381398 }
382399 } ,
383400} ;
0 commit comments