Skip to content

Commit a87b185

Browse files
committed
feat: implement variables
1 parent 752dac8 commit a87b185

File tree

4 files changed

+84
-411
lines changed

4 files changed

+84
-411
lines changed

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
77
"@blockly/block-plus-minus": "^8.0.9",
8+
"@blockly/block-shareable-procedures": "^5.0.9",
89
"@blockly/field-colour": "^5.0.9",
910
"@blockly/field-grid-dropdown": "^5.0.9",
1011
"@blockly/field-multilineinput": "^5.0.10",

src/components/Blockly/BlocklyComponent.jsx

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ import { connect } from "react-redux";
3737

3838
import { PositionedMinimap } from "@blockly/workspace-minimap";
3939

40+
import { TypedVariableModal } from "@blockly/plugin-typed-variable-modal";
41+
42+
import {
43+
blocks,
44+
unregisterProcedureBlocks,
45+
} from "@blockly/block-shareable-procedures";
46+
4047
import { CategoryBuilder, ToolboxBuilder } from "./toolbox/builder";
4148
import { getColour } from "./helpers/colour";
4249
import sensors from "./toolbox/modules/sensors";
@@ -72,6 +79,9 @@ class BlocklyComponent extends React.Component {
7279
}
7380

7481
getToolbox(board) {
82+
if (!board) {
83+
return new ToolboxBuilder().buildToolbox();
84+
}
7585
const senseBoxColor = getColour().sensebox;
7686

7787
// Create the advanced categories
@@ -118,7 +128,11 @@ class BlocklyComponent extends React.Component {
118128
.addCategory(Blockly.Msg.toolbox_time, getColour().time, time[board])
119129
.addCategory(Blockly.Msg.toolbox_math, getColour().math, math[board])
120130
.addCategory("Audio", getColour().audio, audio[board])
121-
// .addCustomCategory(Blockly.Msg.toolbox_variables, getColour().variables, "CREATE_TYPED_VARIABLE") // TODO: This is not working
131+
.addCustomCategory(
132+
Blockly.Msg.toolbox_variables,
133+
getColour().variables,
134+
"CREATE_TYPED_VARIABLE",
135+
)
122136
// .addCustomCategory(Blockly.Msg.toolbox_functions, getColour().procedures, "PROCEDURE") // TODO: This is not working
123137
.addNestedCategory(Blockly.Msg.toolbox_advanced, getColour().io, [
124138
serialCategory,
@@ -145,6 +159,26 @@ class BlocklyComponent extends React.Component {
145159
},
146160
...rest,
147161
});
162+
163+
unregisterProcedureBlocks();
164+
Blockly.common.defineBlocks(blocks);
165+
166+
workspace.registerToolboxCategoryCallback(
167+
"CREATE_TYPED_VARIABLE",
168+
this.createFlyout,
169+
);
170+
171+
const typedVarModal = new TypedVariableModal(workspace, "callbackName", [
172+
[`${Blockly.Msg.variable_NUMBER}`, "int"],
173+
[`${Blockly.Msg.variable_LONG}`, "long"],
174+
[`${Blockly.Msg.variable_DECIMAL}`, "float"],
175+
[`${Blockly.Msg.variables_TEXT}`, "String"],
176+
[`${Blockly.Msg.variables_CHARACTER}`, "char"],
177+
[`${Blockly.Msg.variables_BOOLEAN}`, "boolean"],
178+
]);
179+
typedVarModal.init();
180+
// workspace.updateToolbox(this.props.toolbox.current);
181+
148182
// Initialize plugin.
149183

150184
// Initialize plugin.
@@ -164,6 +198,24 @@ class BlocklyComponent extends React.Component {
164198
}
165199
}
166200

201+
createFlyout(workspace) {
202+
let xmlList = [];
203+
204+
// Add your button and give it a callback name.
205+
const button = document.createElement("button");
206+
button.setAttribute("text", Blockly.Msg.button_createVariable);
207+
button.setAttribute("callbackKey", "callbackName");
208+
209+
xmlList.push(button);
210+
211+
// This gets all the variables that the user creates and adds them to the
212+
// flyout.
213+
const blockList = Blockly.VariablesDynamic.flyoutCategoryBlocks(workspace);
214+
console.log(blockList);
215+
xmlList = xmlList.concat(blockList);
216+
return xmlList;
217+
}
218+
167219
componentDidUpdate(prevProps) {
168220
if (prevProps.selectedBoard !== this.props.selectedBoard) {
169221
const senseBoxColor = getColour().sensebox;

0 commit comments

Comments
 (0)