Skip to content

Commit 61405a2

Browse files
committed
Add componentId to extra state of mrc_component block.
1 parent e64c865 commit 61405a2

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/blocks/mrc_component.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type ConstructorArg = {
4949
};
5050

5151
type ComponentExtraState = {
52+
componentId?: string,
5253
importModule?: string,
5354
// If staticFunctionName is not present, generate the constructor.
5455
staticFunctionName?: string,
@@ -57,6 +58,7 @@ type ComponentExtraState = {
5758

5859
export type ComponentBlock = Blockly.Block & ComponentMixin;
5960
interface ComponentMixin extends ComponentMixinType {
61+
mrcComponentId: string,
6062
mrcArgs: ConstructorArg[],
6163
mrcImportModule: string,
6264
mrcStaticFunctionName: string,
@@ -84,6 +86,7 @@ const COMPONENT = {
8486
*/
8587
saveExtraState: function (this: ComponentBlock): ComponentExtraState {
8688
const extraState: ComponentExtraState = {
89+
componentId: this.mrcComponentId,
8790
};
8891
extraState.params = [];
8992
if (this.mrcArgs){
@@ -106,6 +109,7 @@ const COMPONENT = {
106109
* Applies the given state to this block.
107110
*/
108111
loadExtraState: function (this: ComponentBlock, extraState: ComponentExtraState): void {
112+
this.mrcComponentId = extraState.componentId ? extraState.componentId : this.id;
109113
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
110114
this.mrcStaticFunctionName = extraState.staticFunctionName ? extraState.staticFunctionName : '';
111115
this.mrcArgs = [];
@@ -146,7 +150,7 @@ const COMPONENT = {
146150
const oldName = nameField.getValue();
147151
if (oldName && oldName !== name && oldName !== legalName) {
148152
// Rename any callers.
149-
renameMethodCallers(this.workspace, this.id, legalName);
153+
renameMethodCallers(this.workspace, this.mrcComponentId, legalName);
150154
}
151155
return legalName;
152156
},
@@ -156,7 +160,7 @@ const COMPONENT = {
156160
const ports: {[port: string]: string} = {};
157161
this.getComponentPorts(ports);
158162
return {
159-
blockId: this.id,
163+
componentId: this.mrcComponentId,
160164
name: componentName,
161165
className: componentType,
162166
ports: ports,
@@ -172,7 +176,15 @@ const COMPONENT = {
172176
ports[argName] = this.mrcArgs[i].type;
173177
}
174178
},
175-
}
179+
/**
180+
* mrcChangeIds is called when a module is copied so that the copy has different ids than the original.
181+
*/
182+
mrcChangeIds: function (this: ComponentBlock, oldIdToNewId: { [oldId: string]: string }): void {
183+
if (this.mrcComponentId in oldIdToNewId) {
184+
this.mrcComponentId = oldIdToNewId[this.mrcComponentId];
185+
}
186+
},
187+
};
176188

177189
export const setup = function () {
178190
Blockly.Blocks[BLOCK_NAME] = COMPONENT;

0 commit comments

Comments
 (0)