1

我已经在“blockly”中实现了这个演示,我也想在 Angular 中实现它。我一直在阅读官方文档,但没有找到太多关于此的内容,我也没有太多 Angular 经验。因此,如果您能帮助我,我将不胜感激。

下面是演示代码。

https://developers.google.com/blockly/guides/overview

在此处输入图像描述

在此处输入图像描述

//Custom block

Blockly.Blocks['login'] = {
  init: function () {
    this.appendDummyInput()
      .appendField('Login')
      .setAlign(Blockly.ALIGN_CENTRE);
    this.appendDummyInput()
      .appendField('user')
      .appendField(new Blockly.FieldTextInput(''), 'user');
    this.appendDummyInput()
      .appendField('password')
      .appendField(new Blockly.FieldTextInput(''), 'pass');
    this.appendDummyInput()
      .appendField('url')
      .appendField(new Blockly.FieldTextInput(''), 'url');
    this.setInputsInline(false);
    this.setNextStatement(true, null);
    this.setPreviousStatement(true, null);
    this.setColour(230);
    this.setTooltip('Login into the obtained page');
    this.setHelpUrl('');
  },
};

Blockly.JavaScript['login'] = function (block) {
  let user = block.getFieldValue('user');
  let pass = block.getFieldValue('pass');
  let url = block.getFieldValue('url');

  let code;

  if (user.length == 0 || /^\s+$/.test(user) || 
      pass.length == 0 || /^\s+$/.test(pass) ||
      url.length == 0 || /^\s+$/.test(url)) {

    code = 'window.alert("All fields are required!!!");\n';

  } else {
    code = `window.alert("` + user + ` ` + pass + ` ` + url + `");
window.open("`+url+`","_blank");\n`


  }

  return code;
};




//--------------------------------------------------

let workspacePlayground = Blockly.inject('blocklyDiv', {
  toolbox: document.getElementById('toolbox'),
  zoom:
  {
    controls: true,
    wheel: true,
    startScale: 1.0,
    maxScale: 2,
    minScale: 0.5,
    scaleSpeed: 1.2
  }
});

function UpdateFunction(event) {

  let code = Blockly.JavaScript.workspaceToCode(workspacePlayground);
  document.getElementById('code').innerHTML = code

}

workspacePlayground.addChangeListener(UpdateFunction);

// save blocks
function saveWorkspace() {

  let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  let domToPretty = Blockly.Xml.domToPrettyText(xml);
  window.localStorage.setItem("myProgram", domToPretty);

}

// restore blocks
function restoreWorkspace() {

  let program = window.localStorage.getItem("myProgram");
  Blockly.mainWorkspace.clear();

  let textToDom = Blockly.Xml.textToDom(program);
  Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, textToDom);

}

// delete blocks
function deleteWorkspace() {

  window.localStorage.clear();

}

// execute code
function execute() {
  Blockly.JavaScript.addReservedWords('code');
  let code = Blockly.JavaScript.workspaceToCode(workspacePlayground);
  try {
    eval(code);
  } catch (e) {
    alert(e);
  }

}
.blockly-container {
  
  height: 60vh; 
  width: 100%;
  
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  <script src="https://unpkg.com/google-blockly@1.0.0/blockly_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/javascript_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/blocks_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/msg/js/en.js"></script>
</head>

<body>
    
    <div class="container py-5">

        <h1 class="text-center">Blockly Test</h1>
    
        <div class="row my-3">
          <div class="col">
            <div id="blocklyDiv" class="blockly-container"></div>
          </div>
        </div>
    
        <div class="row my-3">
          <div class="col-md-8">
            <h2>Output</h2>
            <pre id="code"></pre>
          </div>
          <div class="col-md">
            <button type="button" class="btn btn-success" onclick="saveWorkspace();">Save</button>
            <button type="button" class="btn btn-info" onclick="restoreWorkspace();">Restore</button>
            <button type="button" class="btn btn-danger" onclick="deleteWorkspace();">Delete</button>
            <button id="runButton" type="button" class="btn btn-success" onclick="execute();">Execute</button>

          </div>
        </div>
    
      </div>

    <xml id="toolbox" style="display: none">
        <category name="Variables" custom="VARIABLE" colour="330"></category>
        <category name="Functions" custom="PROCEDURE" colour="330"></category>
        <category name="Logic" colour="210">
            <block type="controls_if"></block>
            <block type="logic_compare"></block>
            <block type="logic_operation"></block>
            <block type="logic_boolean"></block>
        </category>
        <category name="Loops" colour="120">
            <block type="controls_whileUntil"></block>
            <block type="controls_repeat_ext"></block>
            <block type="controls_for"></block>
        </category>
        <category name="Math" colour="230">
            <block type="math_number"></block>
            <block type="math_arithmetic"></block>
        </category>
        <category name="Functions" colour="290" custom="PROCEDURE"></category>
        <category name="Text" colour="150">
            <block type="text"></block>
            <block type="text_print"></block>
        </category>
        <category name="Login" colour="300">
            <block type="login"></block>
        </category>
    </xml>



    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
        crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
        crossorigin="anonymous"></script>
        
</body>

</html>

4

1 回答 1

0
//Custom block

Blockly.Blocks['login'] = {
  init: function () {
    this.appendDummyInput()
      .appendField('Login')
      .setAlign(Blockly.ALIGN_CENTRE);
    this.appendDummyInput()
      .appendField('user')
      .appendField(new Blockly.FieldTextInput(''), 'user');
    this.appendDummyInput()
      .appendField('password')
      .appendField(new Blockly.FieldTextInput(''), 'pass');
    this.appendDummyInput()
      .appendField('url')
      .appendField(new Blockly.FieldTextInput(''), 'url');
    this.setInputsInline(false);
    this.setNextStatement(true, null);
    this.setPreviousStatement(true, null);
    this.setColour(230);
    this.setTooltip('Login into the obtained page');
    this.setHelpUrl('');
  },
};

Blockly.JavaScript['login'] = function (block) {
  let user = block.getFieldValue('user');
  let pass = block.getFieldValue('pass');
  let url = block.getFieldValue('url');

  let code;

  if (user.length == 0 || /^\s+$/.test(user) || 
      pass.length == 0 || /^\s+$/.test(pass) ||
      url.length == 0 || /^\s+$/.test(url)) {

    code = 'window.alert("All fields are required!!!");\n';

  } else {
    code = `window.alert("` + user + ` ` + pass + ` ` + url + `");
window.open("`+url+`","_blank");\n`


  }

  return code;
};




//--------------------------------------------------

let workspacePlayground = Blockly.inject('blocklyDiv', {
  toolbox: document.getElementById('toolbox'),
  zoom:
  {
    controls: true,
    wheel: true,
    startScale: 1.0,
    maxScale: 2,
    minScale: 0.5,
    scaleSpeed: 1.2
  }
});

function UpdateFunction(event) {

  let code = Blockly.JavaScript.workspaceToCode(workspacePlayground);
  document.getElementById('code').innerHTML = code

}

workspacePlayground.addChangeListener(UpdateFunction);

// save blocks
function saveWorkspace() {

  let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  let domToPretty = Blockly.Xml.domToPrettyText(xml);
  window.localStorage.setItem("myProgram", domToPretty);

}

// restore blocks
function restoreWorkspace() {

  let program = window.localStorage.getItem("myProgram");
  Blockly.mainWorkspace.clear();

  let textToDom = Blockly.Xml.textToDom(program);
  Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, textToDom);

}

// delete blocks
function deleteWorkspace() {

  window.localStorage.clear();

}

// execute code
function execute() {
  Blockly.JavaScript.addReservedWords('code');
  let code = Blockly.JavaScript.workspaceToCode(workspacePlayground);
  try {
    eval(code);
  } catch (e) {
    alert(e);
  }

}
.blockly-container {
  
  height: 60vh; 
  width: 100%;
  
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

  <script src="https://unpkg.com/google-blockly@1.0.0/blockly_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/javascript_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/blocks_compressed.js"></script>
  <script src="https://unpkg.com/google-blockly@1.0.0/msg/js/en.js"></script>
</head>

<body>
    
    <div class="container py-5">

        <h1 class="text-center">Blockly Test</h1>
    
        <div class="row my-3">
          <div class="col">
            <div id="blocklyDiv" class="blockly-container"></div>
          </div>
        </div>
    
        <div class="row my-3">
          <div class="col-md-8">
            <h2>Output</h2>
            <pre id="code"></pre>
          </div>
          <div class="col-md">
            <button type="button" class="btn btn-success" onclick="saveWorkspace();">Save</button>
            <button type="button" class="btn btn-info" onclick="restoreWorkspace();">Restore</button>
            <button type="button" class="btn btn-danger" onclick="deleteWorkspace();">Delete</button>
            <button id="runButton" type="button" class="btn btn-success" onclick="execute();">Execute</button>

          </div>
        </div>
    
      </div>

    <xml id="toolbox" style="display: none">
        <category name="Variables" custom="VARIABLE" colour="330"></category>
        <category name="Functions" custom="PROCEDURE" colour="330"></category>
        <category name="Logic" colour="210">
            <block type="controls_if"></block>
            <block type="logic_compare"></block>
            <block type="logic_operation"></block>
            <block type="logic_boolean"></block>
        </category>
        <category name="Loops" colour="120">
            <block type="controls_whileUntil"></block>
            <block type="controls_repeat_ext"></block>
            <block type="controls_for"></block>
        </category>
        <category name="Math" colour="230">
            <block type="math_number"></block>
            <block type="math_arithmetic"></block>
        </category>
        <category name="Functions" colour="290" custom="PROCEDURE"></category>
        <category name="Text" colour="150">
            <block type="text"></block>
            <block type="text_print"></block>
        </category>
        <category name="Login" colour="300">
            <block type="login"></block>
        </category>
    </xml>



    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
        crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
        crossorigin="anonymous"></script>
        
</body>

</html>
于 2022-01-29T09:58:14.053 回答