Skip to content

Commit d14ccfe

Browse files
committed
Add mechanismId to extra state of mrc_mechanism block.
1 parent addaad8 commit d14ccfe

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/blocks/mrc_mechanism.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Parameter = {
4848

4949
type MechanismExtraState = {
5050
mechanismModuleId?: string,
51+
mechanismId?: string,
5152
importModule?: string,
5253
parameters?: Parameter[],
5354
}
@@ -57,6 +58,7 @@ const WARNING_ID_MECHANISM_CHANGED = 'mechanism changed';
5758
export type MechanismBlock = Blockly.Block & MechanismMixin & Blockly.BlockSvg;
5859
interface MechanismMixin extends MechanismMixinType {
5960
mrcMechanismModuleId: string
61+
mrcMechanismId: string,
6062
mrcImportModule: string,
6163
mrcParameters: Parameter[],
6264
}
@@ -84,6 +86,7 @@ const MECHANISM = {
8486
saveExtraState: function (this: MechanismBlock): MechanismExtraState {
8587
const extraState: MechanismExtraState = {
8688
mechanismModuleId: this.mrcMechanismModuleId,
89+
mechanismId: this.mrcMechanismId,
8790
};
8891
extraState.parameters = [];
8992
this.mrcParameters.forEach((arg) => {
@@ -102,6 +105,7 @@ const MECHANISM = {
102105
*/
103106
loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void {
104107
this.mrcMechanismModuleId = extraState.mechanismModuleId ? extraState.mechanismModuleId : '';
108+
this.mrcMechanismId = extraState.mechanismId ? extraState.mechanismId : this.id;
105109
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
106110
this.mrcParameters = [];
107111
if (extraState.parameters) {
@@ -154,9 +158,9 @@ const MECHANISM = {
154158
const oldName = nameField.getValue();
155159
if (oldName && oldName !== name && oldName !== legalName) {
156160
// Rename any callers.
157-
renameMethodCallers(this.workspace, this.id, legalName);
161+
renameMethodCallers(this.workspace, this.mrcMechanismId, legalName);
158162
// Rename any event handlers
159-
renameMechanismNameInEventHandlers(this.workspace, this.id, legalName);
163+
renameMechanismNameInEventHandlers(this.workspace, this.mrcMechanismId, legalName);
160164
}
161165
return legalName;
162166
},
@@ -165,7 +169,7 @@ const MECHANISM = {
165169
const mechanismType = this.mrcImportModule + '.' + this.getFieldValue(FIELD_TYPE);
166170
return {
167171
moduleId: this.mrcMechanismModuleId,
168-
blockId: this.id,
172+
mechanismId: this.mrcMechanismId,
169173
name: mechanismName,
170174
className: mechanismType,
171175
};
@@ -239,7 +243,15 @@ const MECHANISM = {
239243
this.setWarningText(null, WARNING_ID_MECHANISM_CHANGED);
240244
}
241245
},
242-
}
246+
/**
247+
* mrcChangeIds is called when a module is copied so that the copy has different ids than the original.
248+
*/
249+
mrcChangeIds: function (this: MechanismBlock, oldIdToNewId: { [oldId: string]: string }): void {
250+
if (this.mrcMechanismId in oldIdToNewId) {
251+
this.mrcMechanismId = oldIdToNewId[this.mrcMechanismId];
252+
}
253+
},
254+
};
243255

244256
export const setup = function () {
245257
Blockly.Blocks[BLOCK_NAME] = MECHANISM;

0 commit comments

Comments
 (0)