Blockly

by elenat82

HTML

<!DOCTYPE html>
<html lang="it">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Blockly</title>
    <script src="https://unpkg.com/blockly/blockly.min.js"></script>
    <script src="https://unpkg.com/blockly/javascript_compressed"></script>
    <script src="script.js"></script>
    <style>
        html,
        body,
        main {
            height: 100%;
        }

        main {
            display: flex;
            width: 100%;
            height: 90%;
        }

        #blocklyDiv {
            height: 100%;
            width: 50%;
        }

        #code {
            height: 100%;
            width: 50%;
            display: flex;
            flex-direction: column;
            padding: 10px;
            background-color: rgb(240, 240, 240);
        }

        #code-container {
            height: 100%;
            margin: 0;
        }

        #button-run {
            align-self: flex-end;
        }
        #resultBox {
            display: flex;
            margin-top: 20px;
        }
        #resultBox div {
            height: 20px;
            width: 20px;
            border: solid 1px darkgray;
            background-color: white;
            border-radius: 50%;
            margin-right: 20px;
        }
    </style>

</head>

<body>
    <main>
        <div id="blocklyDiv"></div>
        <div id="code">
            <pre id="code-container"></pre>
            <div id="button-run"><button id="run-code">Run code</button></div>
        </div>
    </main>
    <aside>
        <div id="resultBox">
            <div id="red"></div>
            <div id="yellow"></div>
            <div id="green"></div>
        </div>
    </aside>
</body>

</html>

JavaScript

document.addEventListener("DOMContentLoaded", (event) => {

  const toolbox = {
    "kind": "categoryToolbox",
    "contents": [
      {
        "kind": "category",
        "name": "Logica",
        "categorystyle": "logic_category",
        "contents": [
          {
            "kind": "category",
            "name": "If",
            "categorystyle": "logic_category",
            "contents": [
              {
                "kind": "block",
                "type": "controls_if"
              },
              {
                "kind": "block",
                "type": "controls_if",
                "extraState": {
                  "hasElse": "true"
                }
              }
            ]
          },
          {
            "kind": "category",
            "name": "Boolean",
            "categorystyle": "logic_category",
            "contents": [
              {
                "kind": "block",
                "type": "logic_compare"
              },
              {
                "kind": "block",
                "type": "logic_operation"
              },
              {
                "kind": "block",
                "type": "logic_boolean"
              }
            ]
          }
        ]
      },
      {
        "kind": "category",
        "name": "Cicli",
        "categorystyle": "loop_category",
        "contents": [
          {
            "kind": "block",
            "type": "controls_for",
            "fields": {
              "VAR": "i"
            },
            "inputs": {
              "FROM": {
                "block": {
                  "type": "math_number",
                  "fields": {
                    "NUM": 1
                  }
                }
              },
              "TO": {
                "block": {
                  "type": "math_number",
                  "fields": {
                    "NUM": 10
                  }
                }
              },
              "BY": {
                "block": {
               ...