Skip to content

Commit 02e6f3b

Browse files
committed
In blocks that have an mrcOnLoad method, create a method called mrcValidate. Make mrcOnLoad call mrcValidate.
In editor.makeCurrent, call the mrcValidate method on all blocks that implement it.
1 parent 6a1a2f5 commit 02e6f3b

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const CALL_PYTHON_FUNCTION = {
318318
this.mrcMechanismId = extraState.mechanismId ? extraState.mechanismId : '';
319319
this.mrcComponentClassName = extraState.componentClassName ? extraState.componentClassName : '';
320320
this.mrcMechanismClassName = extraState.mechanismClassName ? extraState.mechanismClassName : '';
321-
// Initialize mrcComponentNames and mrcMapComponentNameToId here. They will be filled during mrcOnLoad.
321+
// Initialize mrcComponentNames and mrcMapComponentNameToId here. They will be filled during mrcValidate.
322322
this.mrcComponentNames = [];
323323
this.mrcMapComponentNameToId = {};
324324
this.updateBlock_();
@@ -405,7 +405,7 @@ const CALL_PYTHON_FUNCTION = {
405405
.appendField('.');
406406
}
407407
// Here we create a text field for the component name.
408-
// Later, in mrcOnLoad, we will replace it with a dropdown.
408+
// Later, in mrcValidate, we will replace it with a dropdown.
409409
titleInput
410410
.appendField(createFieldNonEditableText(''), FIELD_COMPONENT_NAME)
411411
.appendField('.')
@@ -565,8 +565,18 @@ const CALL_PYTHON_FUNCTION = {
565565
}
566566
return components;
567567
},
568+
/**
569+
* mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly
570+
* workspace.
571+
*/
568572
mrcOnLoad: function(this: CallPythonFunctionBlock): void {
569-
// mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly workspace.
573+
this.mrcValidate();
574+
},
575+
/**
576+
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
577+
* It is called from mrcOnLoad above and from Editor.makeCurrent.
578+
*/
579+
mrcValidate: function(this: CallPythonFunctionBlock): void {
570580
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
571581
if (!editor) {
572582
return;

src/blocks/mrc_event_handler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ const EVENT_HANDLER = {
176176
* workspace.
177177
*/
178178
mrcOnLoad: function(this: EventHandlerBlock): void {
179+
this.mrcValidate();
180+
},
181+
/**
182+
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
183+
* It is called from mrcOnLoad above and from Editor.makeCurrent.
184+
*/
185+
mrcValidate: function(this: EventHandlerBlock): void {
179186
const warnings: string[] = [];
180187

181188
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);

src/blocks/mrc_mechanism.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,19 @@ const MECHANISM = {
174174
className: mechanismType,
175175
};
176176
},
177+
178+
/**
179+
* mrcOnLoad is called for each MechanismBlock when the blocks are loaded in the blockly
180+
* workspace.
181+
*/
177182
mrcOnLoad: function(this: MechanismBlock): void {
178-
// mrcOnLoad is called for each MechanismBlock when the blocks are loaded in the blockly workspace.
183+
this.mrcValidate();
184+
},
185+
/**
186+
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
187+
* It is called from mrcOnLoad above and from Editor.makeCurrent.
188+
*/
189+
mrcValidate: function(this: MechanismBlock): void {
179190
const warnings: string[] = [];
180191

181192
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);

src/editor/editor.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ export class Editor {
129129
// Parse modules since they might have changed.
130130
this.parseModules(project, modulePathToContentText);
131131
this.updateToolboxImpl();
132+
133+
// Go through all the blocks in the workspace and call their mrcValidate method.
134+
this.blocklyWorkspace.getAllBlocks().forEach(block => {
135+
if ('mrcValidate' in block && typeof block.mrcValidate === "function") {
136+
block.mrcValidate();
137+
}
138+
});
132139
}
133140

134141
public abandon(): void {

0 commit comments

Comments
 (0)