Skip to content

Commit 0bc4013

Browse files
committed
Fixed a problem that occurs with mechanisms that don't have module ids yet.
1 parent 86ce70e commit 0bc4013

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/blocks/mrc_mechanism.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,32 @@ const MECHANISM = {
175175
if (editor) {
176176
// Find the mechanism.
177177
let foundMechanism: storageModule.Mechanism | null = null;
178-
const components: storageModuleContent.Component[] = []
179-
for (const mechanism of editor.getMechanisms()) {
180-
if (mechanism.moduleId === this.mrcMechanismModuleId) {
181-
foundMechanism = mechanism;
182-
components.push(...editor.getComponentsFromMechanism(mechanism));
183-
break;
178+
179+
if (this.mrcMechanismModuleId) {
180+
// Find the mechanism by module id.
181+
for (const mechanism of editor.getMechanisms()) {
182+
if (mechanism.moduleId === this.mrcMechanismModuleId) {
183+
foundMechanism = mechanism;
184+
break;
185+
}
186+
}
187+
} else {
188+
// Find the mechanism by class name.
189+
const className = this.getFieldValue(FIELD_TYPE);
190+
for (const mechanism of editor.getMechanisms()) {
191+
if (mechanism.className === className) {
192+
// Grap the mechanism module id, so we have it for next time.
193+
this.mrcMechanismModuleId = mechanism.moduleId;
194+
foundMechanism = mechanism;
195+
break;
196+
}
184197
}
185198
}
186199

187200
if (foundMechanism) {
201+
const components: storageModuleContent.Component[] = [];
202+
components.push(...editor.getComponentsFromMechanism(foundMechanism));
203+
188204
// If the mechanism class name has changed, update this blcok.
189205
if (this.getFieldValue(FIELD_TYPE) !== foundMechanism.className) {
190206
this.setFieldValue(foundMechanism.className, FIELD_TYPE);

src/storage/module_content.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export function newOpModeContent(projectName: string, opModeClassName: string):
124124

125125
/**
126126
* Make the module content from the given python code and blocks content.
127+
* If the given module has an empty moduleId field, it will be set to a valid id.
127128
*/
128129
export function makeModuleContentText(
129130
module: storageModule.Module,
@@ -132,6 +133,9 @@ export function makeModuleContentText(
132133
components: Component[],
133134
events: Event[],
134135
methods: Method[]): string {
136+
if (!module.moduleId) {
137+
module.moduleId = Blockly.utils.idGenerator.genUid();
138+
}
135139
const moduleContent = new ModuleContent(
136140
module.moduleType,
137141
module.moduleId,
@@ -145,8 +149,8 @@ export function makeModuleContentText(
145149

146150
export function parseModuleContentText(moduleContentText: string): ModuleContent {
147151
const parsedContent = JSON.parse(moduleContentText);
148-
if (!parsedContent.moduleId) {
149-
parsedContent.moduleId = Blockly.utils.idGenerator.genUid();
152+
if (!('moduleId' in parsedContent)) {
153+
parsedContent.moduleId = '';
150154
}
151155
return new ModuleContent(
152156
parsedContent.moduleType,

0 commit comments

Comments
 (0)