Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const CALL_PYTHON_FUNCTION = {
this.mrcMechanismId = extraState.mechanismId ? extraState.mechanismId : '';
this.mrcComponentClassName = extraState.componentClassName ? extraState.componentClassName : '';
this.mrcMechanismClassName = extraState.mechanismClassName ? extraState.mechanismClassName : '';
// Initialize mrcComponentNames and mrcMapComponentNameToId here. They will be filled during mrcOnLoad.
// Initialize mrcComponentNames and mrcMapComponentNameToId here. They will be filled during mrcValidate.
this.mrcComponentNames = [];
this.mrcMapComponentNameToId = {};
this.updateBlock_();
Expand Down Expand Up @@ -405,7 +405,7 @@ const CALL_PYTHON_FUNCTION = {
.appendField('.');
}
// Here we create a text field for the component name.
// Later, in mrcOnLoad, we will replace it with a dropdown.
// Later, in mrcValidate, we will replace it with a dropdown.
titleInput
.appendField(createFieldNonEditableText(''), FIELD_COMPONENT_NAME)
.appendField('.')
Expand Down Expand Up @@ -565,8 +565,18 @@ const CALL_PYTHON_FUNCTION = {
}
return components;
},
/**
* mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly
* workspace.
*/
mrcOnLoad: function(this: CallPythonFunctionBlock): void {
// mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly workspace.
this.mrcValidate();
},
/**
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
* It is called from mrcOnLoad above and from Editor.makeCurrent.
*/
mrcValidate: function(this: CallPythonFunctionBlock): void {
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
if (!editor) {
return;
Expand Down
7 changes: 7 additions & 0 deletions src/blocks/mrc_event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ const EVENT_HANDLER = {
* workspace.
*/
mrcOnLoad: function(this: EventHandlerBlock): void {
this.mrcValidate();
},
/**
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
* It is called from mrcOnLoad above and from Editor.makeCurrent.
*/
mrcValidate: function(this: EventHandlerBlock): void {
const warnings: string[] = [];

const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
Expand Down
13 changes: 12 additions & 1 deletion src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,19 @@ const MECHANISM = {
className: mechanismType,
};
},

/**
* mrcOnLoad is called for each MechanismBlock when the blocks are loaded in the blockly
* workspace.
*/
mrcOnLoad: function(this: MechanismBlock): void {
// mrcOnLoad is called for each MechanismBlock when the blocks are loaded in the blockly workspace.
this.mrcValidate();
},
/**
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
* It is called from mrcOnLoad above and from Editor.makeCurrent.
*/
mrcValidate: function(this: MechanismBlock): void {
const warnings: string[] = [];

const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
Expand Down
18 changes: 14 additions & 4 deletions src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxDefinition = {
contents: [],
};

const MRC_ON_LOAD = 'mrcOnLoad';
const MRC_VALIDATE = 'mrcValidate';

export class Editor {
private static workspaceIdToEditor: { [workspaceId: string]: Editor } = {};
private static currentEditor: Editor | null = null;
Expand Down Expand Up @@ -97,8 +100,8 @@ export class Editor {
blockCreateEvent.ids.forEach(id => {
const block = this.blocklyWorkspace.getBlockById(id);
if (block) {
if ('mrcOnLoad' in block && typeof block.mrcOnLoad === "function") {
block.mrcOnLoad();
if (MRC_ON_LOAD in block && typeof block[MRC_ON_LOAD] === 'function') {
block[MRC_ON_LOAD]();
}
}
});
Expand Down Expand Up @@ -129,6 +132,13 @@ export class Editor {
// Parse modules since they might have changed.
this.parseModules(project, modulePathToContentText);
this.updateToolboxImpl();

// Go through all the blocks in the workspace and call their mrcValidate method.
this.blocklyWorkspace.getAllBlocks().forEach(block => {
if (MRC_VALIDATE in block && typeof block[MRC_VALIDATE] === 'function') {
block[MRC_VALIDATE]();
}
});
}

public abandon(): void {
Expand Down Expand Up @@ -158,8 +168,8 @@ export class Editor {
for (const mechanism of this.mechanisms) {
const moduleContent = this.modulePathToModuleContent[mechanism.modulePath];
if (!moduleContent) {
console.error(this.modulePath + " editor.parseModules - modulePathToModuleContent['" +
mechanism.modulePath + "'] is undefined");
console.error(this.modulePath + ' editor.parseModules - modulePathToModuleContent["' +
mechanism.modulePath + '"] is undefined');
continue;
}
this.mechanismClassNameToModuleContent[mechanism.className] = moduleContent;
Expand Down