Skip to content

Commit d0f51d1

Browse files
committed
Merge branch 'main' of github.com:wpilibsuite/systemcore-blocks-interface into pr_issue_229
2 parents 8cd9b69 + 003199a commit d0f51d1

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ const CALL_PYTHON_FUNCTION = {
317317
this.mrcMechanismId = extraState.mechanismId ? extraState.mechanismId : '';
318318
this.mrcComponentClassName = extraState.componentClassName ? extraState.componentClassName : '';
319319
this.mrcMechanismClassName = extraState.mechanismClassName ? extraState.mechanismClassName : '';
320-
// Initialize mrcMapComponentNameToId here. It will be filled during mrcOnLoad.
320+
// Initialize mrcMapComponentNameToId here. It will be filled during mrcValidate.
321321
this.mrcMapComponentNameToId = {};
322322
this.updateBlock_();
323323
},
@@ -403,7 +403,7 @@ const CALL_PYTHON_FUNCTION = {
403403
.appendField('.');
404404
}
405405
// Here we create a text field for the component name.
406-
// Later, in mrcOnLoad, we will replace it with a dropdown.
406+
// Later, in mrcValidate, we will replace it with a dropdown.
407407
titleInput
408408
.appendField(createFieldNonEditableText(''), FIELD_COMPONENT_NAME)
409409
.appendField('.')
@@ -563,8 +563,18 @@ const CALL_PYTHON_FUNCTION = {
563563
}
564564
return components;
565565
},
566+
/**
567+
* mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly
568+
* workspace.
569+
*/
566570
mrcOnLoad: function(this: CallPythonFunctionBlock): void {
567-
// mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly workspace.
571+
this.mrcValidate();
572+
},
573+
/**
574+
* mrcValidate checks the block, updates it, and/or adds a warning balloon if necessary.
575+
* It is called from mrcOnLoad above and from Editor.makeCurrent.
576+
*/
577+
mrcValidate: function(this: CallPythonFunctionBlock): void {
568578
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
569579
if (!editor) {
570580
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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxDefinition = {
3838
contents: [],
3939
};
4040

41+
const MRC_ON_LOAD = 'mrcOnLoad';
42+
const MRC_VALIDATE = 'mrcValidate';
43+
4144
export class Editor {
4245
private static workspaceIdToEditor: { [workspaceId: string]: Editor } = {};
4346
private static currentEditor: Editor | null = null;
@@ -97,8 +100,8 @@ export class Editor {
97100
blockCreateEvent.ids.forEach(id => {
98101
const block = this.blocklyWorkspace.getBlockById(id);
99102
if (block) {
100-
if ('mrcOnLoad' in block && typeof block.mrcOnLoad === "function") {
101-
block.mrcOnLoad();
103+
if (MRC_ON_LOAD in block && typeof block[MRC_ON_LOAD] === 'function') {
104+
block[MRC_ON_LOAD]();
102105
}
103106
}
104107
});
@@ -129,6 +132,13 @@ export class Editor {
129132
// Parse modules since they might have changed.
130133
this.parseModules(project, modulePathToContentText);
131134
this.updateToolboxImpl();
135+
136+
// Go through all the blocks in the workspace and call their mrcValidate method.
137+
this.blocklyWorkspace.getAllBlocks().forEach(block => {
138+
if (MRC_VALIDATE in block && typeof block[MRC_VALIDATE] === 'function') {
139+
block[MRC_VALIDATE]();
140+
}
141+
});
132142
}
133143

134144
public abandon(): void {
@@ -158,8 +168,8 @@ export class Editor {
158168
for (const mechanism of this.mechanisms) {
159169
const moduleContent = this.modulePathToModuleContent[mechanism.modulePath];
160170
if (!moduleContent) {
161-
console.error(this.modulePath + " editor.parseModules - modulePathToModuleContent['" +
162-
mechanism.modulePath + "'] is undefined");
171+
console.error(this.modulePath + ' editor.parseModules - modulePathToModuleContent["' +
172+
mechanism.modulePath + '"] is undefined');
163173
continue;
164174
}
165175
this.mechanismClassNameToModuleContent[mechanism.className] = moduleContent;

0 commit comments

Comments
 (0)