@@ -32,29 +32,29 @@ export type MethodArg = {
3232} ;
3333
3434export type Method = {
35- blockId : string , // ID of the mrc_class_method_def block that defines the method.
35+ methodId : string , // The mrcMethodId of the mrc_class_method_def block that defines the method.
3636 visibleName : string ,
3737 pythonName : string ,
3838 returnType : string , // 'None' for no return value, '' for an untyped return value.
3939 args : MethodArg [ ] ,
4040} ;
4141
4242export type MechanismInRobot = {
43- moduleId : string // ID of the mechanism module.
44- blockId : string , // ID of the mrc_mechanism block that adds the mechanism to the robot.
43+ moduleId : string // The id of the mechanism module.
44+ mechanismId : string , // The mrcMechanismId of the mrc_mechanism block that adds the mechanism to the robot.
4545 name : string ,
4646 className : string , // Includes the module name, for example 'game_piece_shooter.GamePieceShooter'.
4747}
4848
4949export type Component = {
50- blockId : string , // ID of the mrc_component block that adds the component to the robot or to a mechanism.
50+ componentId : string , // The mrcComponentId of the mrc_component block that adds the component to the robot or to a mechanism.
5151 name : string ,
5252 className : string , // Includes the module name, for example 'smart_motor.SmartMotor'.
5353 ports : { [ port : string ] : string } , // The value is the type.
5454}
5555
5656export type Event = {
57- blockId : string , // ID of the mrc_event block that defines the event.
57+ eventId : string , // The mrcEventId of the mrc_event block that defines the event.
5858 name : string ,
5959 args : MethodArg [ ] ,
6060} ;
@@ -152,6 +152,30 @@ export function parseModuleContentText(moduleContentText: string): ModuleContent
152152 if ( ! ( 'moduleId' in parsedContent ) ) {
153153 parsedContent . moduleId = '' ;
154154 }
155+ parsedContent . mechanisms . forEach ( ( mechanism : any ) => {
156+ if ( ! ( 'mechanismId' in mechanism ) && ( 'blockId' in mechanism ) ) {
157+ mechanism . mechanismId = mechanism [ 'blockId' ] ;
158+ delete mechanism [ 'blockId' ] ;
159+ }
160+ } ) ;
161+ parsedContent . components . forEach ( ( component : any ) => {
162+ if ( ! ( 'componentId' in component ) && ( 'blockId' in component ) ) {
163+ component . componentId = component [ 'blockId' ] ;
164+ delete component [ 'blockId' ] ;
165+ }
166+ } ) ;
167+ parsedContent . events . forEach ( ( event : any ) => {
168+ if ( ! ( 'eventId' in event ) && ( 'blockId' in event ) ) {
169+ event . eventId = event [ 'blockId' ] ;
170+ delete event [ 'blockId' ] ;
171+ }
172+ } ) ;
173+ parsedContent . methods . forEach ( ( method : any ) => {
174+ if ( ! ( 'methodId' in method ) && ( 'blockId' in method ) ) {
175+ method . methodId = method [ 'blockId' ] ;
176+ delete method [ 'blockId' ] ;
177+ }
178+ } ) ;
155179 return new ModuleContent (
156180 parsedContent . moduleType ,
157181 parsedContent . moduleId ,
@@ -204,4 +228,59 @@ export class ModuleContent {
204228 getMethods ( ) : Method [ ] {
205229 return this . methods ;
206230 }
231+
232+ changeIds ( ) : void {
233+ const oldIdToNewId : { [ oldId : string ] : string } = { } ; // value is new id
234+
235+ // Change the ids for the mechanisms defined in this module.
236+ this . mechanisms . forEach ( mechanism => {
237+ const oldMechanismId = mechanism . mechanismId ;
238+ mechanism . mechanismId = Blockly . utils . idGenerator . genUid ( ) ;
239+ oldIdToNewId [ oldMechanismId ] = mechanism . mechanismId ;
240+ } ) ;
241+ // Change the ids for the components defined in this module.
242+ this . components . forEach ( component => {
243+ const oldComponentId = component . componentId ;
244+ component . componentId = Blockly . utils . idGenerator . genUid ( ) ;
245+ oldIdToNewId [ oldComponentId ] = component . componentId ;
246+ } ) ;
247+ // Change the ids for the events defined in this module.
248+ this . events . forEach ( event => {
249+ const oldEventId = event . eventId ;
250+ event . eventId = Blockly . utils . idGenerator . genUid ( ) ;
251+ oldIdToNewId [ oldEventId ] = event . eventId ;
252+ } ) ;
253+ // Change the ids for the methods defined in this module.
254+ this . methods . forEach ( method => {
255+ const oldMethodId = method . methodId ;
256+ method . methodId = Blockly . utils . idGenerator . genUid ( ) ;
257+ oldIdToNewId [ oldMethodId ] = method . methodId ;
258+ } ) ;
259+
260+ // Change the ids in the blocks.
261+ const workspace = new Blockly . Workspace ( ) ;
262+ const onChange = ( event : Blockly . Events . Abstract ) => {
263+ if ( event . type === Blockly . Events . BLOCK_CREATE ) {
264+ const blockCreateEvent = event as Blockly . Events . BlockCreate ;
265+ if ( blockCreateEvent . ids ) {
266+ blockCreateEvent . ids . forEach ( id => {
267+ const block = workspace . getBlockById ( id ) ;
268+ if ( block ) {
269+ if ( 'mrcChangeIds' in block && typeof block . mrcChangeIds === "function" ) {
270+ block . mrcChangeIds ( oldIdToNewId ) ;
271+ }
272+ }
273+ } ) ;
274+ }
275+ }
276+ } ;
277+ workspace . addChangeListener ( onChange ) ;
278+ Blockly . serialization . workspaces . load ( this . blocks , workspace ) ;
279+ workspace . removeChangeListener ( onChange ) ;
280+ this . blocks = Blockly . serialization . workspaces . save ( workspace ) ;
281+
282+ // Clean up the workspace
283+ workspace . dispose ( ) ;
284+
285+ }
207286}
0 commit comments