diff --git a/blocks/blocks.js b/blocks/blocks.js index a5e0c7fc..f92b2120 100644 --- a/blocks/blocks.js +++ b/blocks/blocks.js @@ -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({ diff --git a/generators/generators.js b/generators/generators.js index 526cd501..c0e0311b 100644 --- a/generators/generators.js +++ b/generators/generators.js @@ -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 ""; }; diff --git a/main/blocklyinit.js b/main/blocklyinit.js index 0fea5240..612c065c 100644 --- a/main/blocklyinit.js +++ b/main/blocklyinit.js @@ -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", diff --git a/toolbox.js b/toolbox.js index 4664f474..a633218d 100644 --- a/toolbox.js +++ b/toolbox.js @@ -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",