从用户提供的字符串生成自定义Blockly块,而无需eval [英] Generate a custom Blockly block from a user-provided string without eval

查看:65
本文介绍了从用户提供的字符串生成自定义Blockly块,而无需eval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript将字符串转换为Google Blockly块.

I am converting a string into a Google Blockly block using JavaScript.

输入字符串类似于"Hello %s World"-其中%s定义字符串输入.我需要将其转换为:

The input string is something like "Hello %s World" - where %s defines a string input. I need to turn that into:

Blockly.Blocks['blockname'] = {
  init: function() {
    this.appendDummyInput()
        .appendField("Hello ")
        .appendField(new Blockly.FieldTextInput("input1"), "")
        .appendField(" World");
  }
};

但是我不确定在不使用eval()的情况下如何实现这一目标,并且由于输入字符串来自用户,因此我知道使用eval()并不是一个好主意.

But I'm not sure how to achieve this without using eval(), and as the input string is from the user, I understand that using eval() would not be a good idea.

我当前的代码是:

currentLine = blockText[x].split(/(%s)/);
for( var y = 0; y < currentLine.length; y++ )
{
  if( currentLine[y] == "" )
  {
    //do nothing
  }
  else if( currentLine[y] == "%s" )
  {
    //create a input
  }
  else
  {
    //create a label
  }
}

但是我不确定如何创建所需的Blockly代码,而无需在字符串中构建JavaScript,然后最后使用eval().

But I'm not quite sure how to create the Blockly code that I need, without building up the JavaScript in a string and then using eval() at the end.

有人可以帮我吗?

推荐答案

您可以创建自定义通用块,而无需输入任何类似内容-

You can create a custom generic block without any inputs like below -

  Blockly.Blocks['generic_block'] = {
    init: function() {
      this.jsonInit({
        message0: '',
        colour: '230'
      });
    }
  };

现在,您可以通过代码创建此块的新实例.根据您的JS字符串解析,您可以在此块内创建输入和字段,如下所示-

Now you can create a new instance of this block though the code. Based on your JS string parsing, you can create the inputs and the fields inside this block as below -

var lineBlock=yourBlocklyWorkspace.newBlock('generic_block');         // create new instance of generic block
var input=lineBlock.appendDummyInput();                               // create a dummy input
var blockText="Hello %s World";                                       // one line of the JS code
var currentLine = blockText.split(/(%s)/);                            // split every word
for( var y = 0; y < currentLine.length; y++ ) {                       // loop through each word
  if(currentLine[y]==='%s') {                                         // if the word is %s, then append input field
    input.appendField(new Blockly.FieldTextInput('input'+y));         // input+y is the name of the field
  } else {                                                                         // else just append label field
    var labelField=new Blockly.FieldLabel('label'+y);                         // label+y is the name of the field
    labelField.setValue(currentLine[y]);                                          // set the label value to the word
    input.appendField(labelField)
  }
}

这篇关于从用户提供的字符串生成自定义Blockly块,而无需eval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆