@@ -48,13 +48,16 @@ export const FIELD_TYPE = 'TYPE';
4848type Parameter = {
4949 name : string ,
5050 type : string ,
51+ componentId ?: string ,
52+ componentPortsIndex ?: number , // The zero-based number when iterating through component.ports.
5153} ;
5254
5355type MechanismExtraState = {
5456 mechanismModuleId ?: string ,
5557 mechanismId ?: string ,
5658 importModule ?: string ,
5759 parameters ?: Parameter [ ] ,
60+ parametersHaveComponentInformation ?: boolean ,
5861}
5962
6063const WARNING_ID_NOT_IN_HOLDER = 'not in holder' ;
@@ -66,6 +69,7 @@ interface MechanismMixin extends MechanismMixinType {
6669 mrcMechanismId : string ,
6770 mrcImportModule : string ,
6871 mrcParameters : Parameter [ ] ,
72+ mrcParametersHaveComponentInformation : boolean ,
6973
7074 /**
7175 * mrcHasNotInHolderWarning is set to true if we set the NOT_IN_HOLDER warning text on the block.
@@ -83,6 +87,7 @@ const MECHANISM = {
8387 * Block initialization.
8488 */
8589 init : function ( this : MechanismBlock ) : void {
90+ this . mrcParametersHaveComponentInformation = false ;
8691 this . mrcHasNotInHolderWarning = false ;
8792 this . setStyle ( MRC_STYLE_MECHANISMS ) ;
8893 const nameField = new Blockly . FieldTextInput ( '' )
@@ -102,13 +107,11 @@ const MECHANISM = {
102107 const extraState : MechanismExtraState = {
103108 mechanismModuleId : this . mrcMechanismModuleId ,
104109 mechanismId : this . mrcMechanismId ,
110+ parametersHaveComponentInformation : this . mrcParametersHaveComponentInformation ,
105111 } ;
106112 extraState . parameters = [ ] ;
107113 this . mrcParameters . forEach ( ( arg ) => {
108- extraState . parameters ! . push ( {
109- name : arg . name ,
110- type : arg . type ,
111- } ) ;
114+ extraState . parameters ! . push ( { ...arg } ) ;
112115 } ) ;
113116 if ( this . mrcImportModule ) {
114117 extraState . importModule = this . mrcImportModule ;
@@ -125,12 +128,11 @@ const MECHANISM = {
125128 this . mrcParameters = [ ] ;
126129 if ( extraState . parameters ) {
127130 extraState . parameters . forEach ( ( arg ) => {
128- this . mrcParameters . push ( {
129- name : arg . name ,
130- type : arg . type ,
131- } ) ;
131+ this . mrcParameters . push ( { ...arg } ) ;
132132 } ) ;
133133 }
134+ this . mrcParametersHaveComponentInformation = ( extraState . parametersHaveComponentInformation == undefined )
135+ ? false : extraState . parametersHaveComponentInformation ;
134136 this . updateBlock_ ( ) ;
135137 } ,
136138 /**
@@ -289,13 +291,18 @@ const MECHANISM = {
289291 }
290292 this . mrcParameters = [ ] ;
291293 components . forEach ( component => {
294+ let componentPortsIndex = 0 ;
292295 for ( const port in component . ports ) {
293296 this . mrcParameters . push ( {
294297 name : port ,
295298 type : component . ports [ port ] ,
299+ componentId : component . componentId ,
300+ componentPortsIndex,
296301 } ) ;
302+ componentPortsIndex ++ ;
297303 }
298304 } ) ;
305+ this . mrcParametersHaveComponentInformation = true ;
299306 this . updateBlock_ ( ) ;
300307 } else {
301308 // Did not find the mechanism.
@@ -324,6 +331,12 @@ const MECHANISM = {
324331 this . mrcMechanismId = oldIdToNewId [ this . mrcMechanismId ] ;
325332 }
326333 } ,
334+ upgrade_005_to_006 : function ( this : MechanismBlock ) {
335+ // At the time when this upgrade method is called, we can't look up the component information
336+ // for each parameter. Instead, we just mark this block as not having the information. Later,
337+ // in the checkMechanism method, we will retrieve the component information for each parameter.
338+ this . mrcParametersHaveComponentInformation = false ;
339+ } ,
327340} ;
328341
329342export const setup = function ( ) {
@@ -359,17 +372,22 @@ export function createMechanismBlock(
359372 mechanismModuleId : mechanism . moduleId ,
360373 importModule : snakeCaseName ,
361374 parameters : [ ] ,
375+ parametersHaveComponentInformation : true ,
362376 } ;
363377 const inputs : { [ key : string ] : any } = { } ;
364378 let i = 0 ;
365379 components . forEach ( component => {
380+ let componentPortsIndex = 0 ;
366381 for ( const port in component . ports ) {
367382 const parameterType = component . ports [ port ] ;
368383 extraState . parameters ?. push ( {
369384 name : port ,
370385 type : parameterType ,
386+ componentId : component . componentId ,
387+ componentPortsIndex,
371388 } ) ;
372389 inputs [ 'ARG' + i ] = createPort ( parameterType ) ;
390+ componentPortsIndex ++ ;
373391 i ++ ;
374392 }
375393 } ) ;
@@ -378,3 +396,13 @@ export function createMechanismBlock(
378396 fields [ FIELD_TYPE ] = mechanism . className ;
379397 return new toolboxItems . Block ( BLOCK_NAME , extraState , fields , inputs ) ;
380398}
399+
400+ /**
401+ * Upgrades the MechanismBlocks in the given workspace from version 005 to 006.
402+ * This function should only be called when upgrading old projects.
403+ */
404+ export function upgrade_005_to_006 ( workspace : Blockly . Workspace ) : void {
405+ workspace . getBlocksByType ( BLOCK_NAME ) . forEach ( block => {
406+ ( block as MechanismBlock ) . upgrade_005_to_006 ( ) ;
407+ } ) ;
408+ }
0 commit comments