Skip to content

Commit

Permalink
Updated load model block to keep variable names from snippet (still n…
Browse files Browse the repository at this point in the history
…eed to do for other creation blocks)
  • Loading branch information
tracygardner committed Jul 17, 2024
1 parent 5c0c20e commit 9c30d2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dev-dist/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ define(['./workbox-07658ed7'], (function (workbox) { 'use strict';
"revision": "3ca0b8505b4bec776b69afdba2768812"
}, {
"url": "index.html",
"revision": "0.k3rd2p6jpag"
"revision": "0.kkfrqbk9oao"
}], {});
workbox.cleanupOutdatedCaches();
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
Expand Down
41 changes: 25 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,25 +1556,34 @@ Blockly.Blocks["load_model"] = {
});

// Ensure the variable is created and linked properly when the block is used

this.setOnChange(function (changeEvent) {
if (
!this.isInFlyout &&
changeEvent.type === Blockly.Events.BLOCK_CREATE &&
changeEvent.ids.includes(this.id)
) {
let variable = this.workspace.getVariable(nextVariableName);
if (!variable) {
variable = this.workspace.createVariable(
nextVariableName,
null,
);
this.getField("ID_VAR").setValue(variable.getId());
}

// Increment for next use
nextVariableIndexes["model"] += 1;
if (
!this.isInFlyout &&
changeEvent.type === Blockly.Events.BLOCK_CREATE &&
changeEvent.ids.includes(this.id)
) {
const idVarField = this.getField("ID_VAR");

if (idVarField && idVarField.getValue()) {
// The block is from a snippet with a predefined variable name
const predefinedVarName = this.workspace.getVariableById(idVarField.getValue()).name;
let variable = this.workspace.getVariable(predefinedVarName);
if (!variable) {
variable = this.workspace.createVariable(predefinedVarName, null);
}
idVarField.setValue(variable.getId());
} else {
// The block is from the toolbox and needs a new variable name
let variable = this.workspace.getVariable(nextVariableName);
if (!variable) {
variable = this.workspace.createVariable(nextVariableName, null);
}
this.getField("ID_VAR").setValue(variable.getId());
}
}
});

},
};

Expand Down

0 comments on commit 9c30d2d

Please sign in to comment.