Skip to content

Commit 64667d7

Browse files
authored
Fixed code in ComponentBlock.updateBlock_ where it needs the module type. (#305)
1 parent b6055f2 commit 64667d7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/blocks/mrc_component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ type ComponentExtraState = {
5959
// If staticFunctionName is not present, generate the constructor.
6060
staticFunctionName?: string,
6161
params?: ConstructorArg[],
62+
/**
63+
* The module type. Note that this is only present when blocks are created for the toolbox. It is not
64+
* saved to the blocks file.
65+
*/
66+
moduleType? : storageModule.ModuleType,
6267
}
6368

6469
export type ComponentBlock = Blockly.Block & ComponentMixin;
@@ -137,13 +142,13 @@ const COMPONENT = {
137142
});
138143
});
139144
}
140-
this.updateBlock_();
145+
this.updateBlock_(extraState);
141146
},
142147
/**
143148
* Update the block to reflect the newly loaded extra state.
144149
*/
145-
updateBlock_: function (this: ComponentBlock): void {
146-
const moduleType = getModuleTypeForWorkspace(this.workspace);
150+
updateBlock_: function (this: ComponentBlock, extraState: ComponentExtraState): void {
151+
const moduleType = extraState.moduleType ? extraState.moduleType : getModuleTypeForWorkspace(this.workspace);
147152
if (moduleType === storageModule.ModuleType.ROBOT) {
148153
// Add input sockets for the arguments.
149154
for (let i = 0; i < this.mrcArgs.length; i++) {
@@ -297,6 +302,7 @@ function createComponentBlock(
297302
//TODO(ags): Remove this because we know what the constructor name is
298303
staticFunctionName: constructorData.functionName,
299304
params: [],
305+
moduleType: moduleType,
300306
};
301307
const fields: {[key: string]: any} = {};
302308
fields[FIELD_NAME] = componentName;

src/blocks/utils/workspaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export function getModuleTypeForWorkspace(workspace: Blockly.Workspace): storage
3838
if (workspace.id in workspaceIdToModuleType) {
3939
return workspaceIdToModuleType[workspace.id];
4040
}
41+
// If the workspace id was not found, it might be because the workspace is associated with a
42+
// block mutator's flyout. Try this workspaces's root workspace.
43+
const rootWorkspace = workspace.getRootWorkspace();
44+
if (rootWorkspace &&
45+
rootWorkspace.id in workspaceIdToModuleType) {
46+
return workspaceIdToModuleType[rootWorkspace.id];
47+
}
48+
4149
throw new Error('getModuleTypeForWorkspace: workspaceId not found: ' + workspace.id);
4250
}
4351

0 commit comments

Comments
 (0)