@@ -146,7 +146,6 @@ Blockly.Blocks['operator_expandableMath'] = {
146146 } ) ;
147147
148148 this . inputs_ = 2 ;
149- this . oldInputs_ = null ;
150149 if ( this . isInFlyout ) {
151150 const input1 = this . appendValueInput ( "NUM1" ) ;
152151 this . fillInBlock ( input1 . connection , "math_number" ) ;
@@ -196,49 +195,34 @@ Blockly.Blocks['operator_expandableMath'] = {
196195 } ,
197196 domToMutation : function ( xmlElement ) {
198197 // on load
199- if ( this . oldInputs_ === this . inputs_ ) return ;
200198 const inputCount = Number ( xmlElement . getAttribute ( "inputcount" ) ) ;
201199 const menuValues = String ( xmlElement . getAttribute ( "menuvalues" ) ) ;
202200 this . inputs_ = isNaN ( inputCount ) ? 0 : inputCount ;
203- let needsActionConnect = false , oldConnections ;
204201
205- if ( this . inputList . length > 0 ) {
202+ let repeatPreventer = false ;
203+ if ( this . inputList . length > 1 ) {
206204 // this was a control z action
207- needsActionConnect = true ;
208- oldConnections = this . getConnections_ ( ) . map ( c => c . targetBlock ( ) ) ;
209-
210- // clear block
211- for ( var i = this . inputList . length ; i -- ; ) {
212- if ( i === 0 ) break ;
213- const input = this . inputList [ i ] ;
214- if ( input . connection . targetBlock ( ) ) input . connection . disconnect ( ) ;
215- this . removeInput ( input . name ) ;
205+
206+ if ( this . inputList . length - 1 === menuValues . length ) repeatPreventer = true ;
207+ else {
208+ const lastInput = this . inputList [ this . inputList . length - 1 ] ;
209+ const innerBlock = lastInput . connection . targetBlock ( ) ;
210+ if ( innerBlock . isShadow ( ) ) innerBlock . dispose ( ) ;
211+ this . removeInput ( lastInput . name ) ;
212+ return ;
216213 }
217214 }
218215
219216 for ( let i = 0 ; i < this . inputs_ ; i ++ ) {
217+ if ( repeatPreventer && this . getInput ( `NUM${ i + 1 } ` ) ) continue ;
218+
220219 const input = this . appendValueInput ( `NUM${ i + 1 } ` ) ;
221220 if ( i > 0 ) {
222221 const menu = input . appendField ( this . menuGenerator ( ) ) ;
223222 menu . fieldRow [ 0 ] . setValue ( menuValues [ i - 1 ] ? menuValues [ i - 1 ] : "+" , true ) ;
224223 }
225224 // vm will automatically replace2 empty inputs with saved shadows
226225 }
227- this . oldInputs_ = this . inputs_ ;
228-
229- if ( needsActionConnect ) {
230- let index = 0 ;
231- for ( const input of this . inputList ) {
232- const oldBlock = oldConnections [ index ] ;
233- if ( oldBlock ) {
234- try {
235- const connector = oldBlock . outputConnection ;
236- input . connection . connect ( connector ) ;
237- } catch ( e ) { }
238- }
239- index ++ ;
240- }
241- }
242226 } ,
243227
244228 onExpandableButtonClicked_ : function ( isAdding ) {
@@ -685,8 +669,7 @@ Blockly.Blocks['operator_expandablejoininputs'] = {
685669 } ) ;
686670
687671 this . messageList = [ "apple" , "banana" , "pear" , "orange" , "mango" , "strawberry" , "pineapple" , "grape" , "kiwi" ] ;
688- this . inputs_ = 2 ;
689- this . oldInputs_ = null ;
672+ this . inputs_ = 0 ;
690673 } ,
691674
692675 fillInBlock : Blockly . scratchBlocksUtils . generateMutatorShadow ,
@@ -699,20 +682,15 @@ Blockly.Blocks['operator_expandablejoininputs'] = {
699682 } ,
700683 domToMutation : function ( xmlElement ) {
701684 // on load
702- if ( this . oldInputs_ === this . inputs_ ) return ;
703685 const inputCount = Number ( xmlElement . getAttribute ( "inputcount" ) ) ;
704- let needsActionConnect = false , oldConnections ;
705686 if ( this . inputList . length > 1 ) {
706687 // this was a control z action
707- needsActionConnect = true ;
708- oldConnections = this . getConnections_ ( ) . map ( c => c . targetBlock ( ) ) ;
709-
710- // clear block
711- for ( var i = this . inputList . length ; i -- ; ) {
712- if ( i === 0 ) break ;
713- const input = this . inputList [ i ] ;
714- if ( input . connection . targetBlock ( ) ) input . connection . disconnect ( ) ;
715- this . removeInput ( input . name ) ;
688+
689+ if ( this . inputs_ > inputCount ) {
690+ const lastInput = this . inputList [ this . inputList . length - 1 ] ;
691+ const innerBlock = lastInput . connection . targetBlock ( ) ;
692+ if ( innerBlock . isShadow ( ) ) innerBlock . dispose ( ) ;
693+ this . removeInput ( lastInput . name ) ;
716694 }
717695 }
718696
@@ -721,21 +699,6 @@ Blockly.Blocks['operator_expandablejoininputs'] = {
721699 // vm will automatically replace empty inputs with saved shadows
722700 if ( ! this . getInput ( `INPUT${ i + 1 } ` ) ) this . appendValueInput ( `INPUT${ i + 1 } ` ) ;
723701 }
724- this . oldInputs_ = this . inputs_ ;
725-
726- if ( needsActionConnect ) {
727- let index = 0 ;
728- for ( const input of this . inputList ) {
729- const oldBlock = oldConnections [ index ] ;
730- if ( oldBlock ) {
731- try {
732- const connector = oldBlock . outputConnection ;
733- input . connection . connect ( connector ) ;
734- } catch ( e ) { }
735- }
736- index ++ ;
737- }
738- }
739702 } ,
740703
741704 onExpandableButtonClicked_ : function ( isAdding ) {
0 commit comments