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
51 changes: 51 additions & 0 deletions blocks/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,57 @@ export function defineBlocks() {
},
};

Blockly.Blocks["lists_add_item"] = {
init: function () {
this.jsonInit({
type: "lists_add_item",
message0: "add %1 to %2",
args0: [
{
type: "input_value",
name: "TO",
},
{
type: "field_variable",
name: "LIST",
variable: "list1",
},
],
previousStatement: null,
nextStatement: null,
tooltip: "Add an item to the end of a list.",
});
this.setStyle("list_blocks");
this.setHelpUrl(getHelpUrlFor(this.type));
},
};

Blockly.Blocks["lists_delete_nth"] = {
init: function () {
this.jsonInit({
type: "lists_delete_nth",
message0: "delete %1 from %2",
args0: [
{
type: "input_value",
name: "INDEX",
check: "Number",
},
{
type: "field_variable",
name: "LIST",
variable: "list1",
},
],
previousStatement: null,
nextStatement: null,
tooltip: "Delete item at index n from a list (0-based).",
});
this.setStyle("list_blocks");
this.setHelpUrl(getHelpUrlFor(this.type));
},
};

Blockly.Blocks["to_number"] = {
init: function () {
this.jsonInit({
Expand Down
33 changes: 33 additions & 0 deletions generators/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4087,6 +4087,39 @@ javascriptGenerator.forBlock["lists_setIndex"] = function (block) {
throw Error("Unhandled combination (lists_setIndex).");
};

javascriptGenerator.forBlock["lists_add_item"] = function (block) {
const listName = javascriptGenerator.nameDB_.getName(
block.getFieldValue("LIST"),
Blockly.Names.NameType.VARIABLE,
);
const value =
javascriptGenerator.valueToCode(
block,
"TO",
javascriptGenerator.ORDER_ASSIGNMENT,
) || '""';

return `if (!Array.isArray(${listName})) {\n ${listName} = [];\n}\n${listName}.push(${value});\n`;
};

javascriptGenerator.forBlock["lists_delete_nth"] = function (block) {
const listName = javascriptGenerator.nameDB_.getName(
block.getFieldValue("LIST"),
Blockly.Names.NameType.VARIABLE,
);
const index =
javascriptGenerator.valueToCode(
block,
"INDEX",
javascriptGenerator.ORDER_NONE,
) || "0";

return `if (Array.isArray(${listName})) {
${listName}.splice(${index}, 1);
}
`;
};

javascriptGenerator.forBlock["keyword"] = function (block) {
return "";
};
Expand Down
32 changes: 32 additions & 0 deletions main/blocklyinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,38 @@ export function initializeWorkspace() {
),
);

const addItemBlock = document.createElement("block");
addItemBlock.setAttribute("type", "lists_add_item");

const toValue = document.createElement("value");
toValue.setAttribute("name", "TO");
const toShadow = document.createElement("shadow");
toShadow.setAttribute("type", "text");
const toField = document.createElement("field");
toField.setAttribute("name", "TEXT");
toField.textContent = "";
toShadow.appendChild(toField);
toValue.appendChild(toShadow);
addItemBlock.appendChild(toValue);

xmlList.push(addItemBlock);

const deleteItemBlock = document.createElement("block");
deleteItemBlock.setAttribute("type", "lists_delete_nth");

const indexValue = document.createElement("value");
indexValue.setAttribute("name", "INDEX");
const indexShadow = document.createElement("shadow");
indexShadow.setAttribute("type", "math_number");
const indexField = document.createElement("field");
indexField.setAttribute("name", "NUM");
indexField.textContent = "1";
indexShadow.appendChild(indexField);
indexValue.appendChild(indexShadow);
deleteItemBlock.appendChild(indexValue);

xmlList.push(deleteItemBlock);

[
"lists_create_empty",
"lists_create_with",
Expand Down
10 changes: 10 additions & 0 deletions toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3275,6 +3275,16 @@ const toolboxLists = {
categorystyle: "variables_category",
custom: "LIST",
contents: [
{
kind: "block",
type: "lists_add_item",
keyword: "add",
},
{
kind: "block",
type: "lists_delete_nth",
keyword: "delete",
},
{
kind: "block",
type: "lists_create_empty",
Expand Down