3.实例
3.1.基础块
无输入 , 无输出
3.1.1.json
var textOneJson = {
"type": "sql_test_text_one",
"message0": " one ",
"colour": 30,
"tooltip":' 无输入 , 无输出'
};
javascriptGenerator.forBlock['sql_test_text_one'] = function(block) {
return ' one ';
};
Blockly.Blocks['sql_test_text_one'] = {
init: function() {
this.jsonInit(textOneJson);
}
};
3.1.2.js
.appendField(new Blockly.FieldLabelSerializable("显示内容"), "TEXT_NAME");
Blockly.Blocks['js_base_one'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable("无入, 无出"), "TEXT_NAME");
this.setColour(145);
this.setTooltip("无入, 无出");
}
}
javascriptGenerator.forBlock['js_base_one'] = function(block) {
return ' one ' ;
};
3.2.输出类型 output
3.2.1.json
“output”: 指定类型 如: String | Number | …
注意生成代码函数的设置
var textBaseOneJson = {
"type": "sql_test_base_one",
"message0": " one ",
"output": "String",
"colour": 30,
"tooltip":' 返回 字符串 '
};
javascriptGenerator.forBlock['sql_test_base_one'] = function(block) {
return [ ' base one ' , javascriptGenerator.ORDER_ATOMIC];
};
Blockly.Blocks['sql_test_base_one'] = {
init: function() {
this.jsonInit(textBaseOneJson);
}
};
3.2.2.js
this.setOutput(true, "String");
Blockly.Blocks['js_base_two'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable("无入, 有出"), "TEXT_NAME");
this.setColour(145);
this.setTooltip("无入, 有出");
this.setOutput(true, "String");
}
}
javascriptGenerator.forBlock['js_base_two'] = function(block) {
return [ ' base two ' , javascriptGenerator.ORDER_ATOMIC];
};
3.3.上下连接
3.3.1.json
previousStatement / nextStatement
var textFiveJson = {
"type": "sql_test_text_five",
"message0": " five ",
"colour": 30,
"previousStatement": null,
"nextStatement": null,
};
javascriptGenerator.forBlock['sql_test_text_five'] = function(block) {
return ' five ' ;
};
Blockly.Blocks['sql_test_text_five'] = {
init: function() {
this.jsonInit(textFiveJson);
let thisBlock = this;
}
};
3.3.2.js
Blockly.Blocks['js_base_three'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable("上下连接"));
this.setColour(145);
this.setTooltip("上下连接");
// this.setOutput(true, "String");
// 下连
this.setNextStatement(true);
// this.setNextStatement(true, 'Action');
// 上连
this.setPreviousStatement(true);
// this.setPreviousStatement(true, 'Action');
}
}
javascriptGenerator.forBlock['js_base_three'] = function(block) {
return ' base three ' ;
};
3.4.输入 input
在 args#
数组内定义
-
name (
string
): 输入的名称,用于识别输入。 -
type (
string
): 输入类型,如'input_value'
、'input_statement'
。 -
check (
string
): 限制此输入可以接受的值或语句类型。 -
align (
string
): 输入的对齐方式,如'LEFT'
或'RIGHT'
。
3.4.1.input_value
接受一个值输入的槽
3.4.1.1.json
var textTwoJson = {
"type": "sql_test_text_two",
"message0": " two %1 ",
"args0": [
{
"type": "input_value",
"name": "NAME",
"check": "String",
}
],
"colour": 30,
"tooltip":' 有输入 , 无输出'
};
javascriptGenerator.forBlock['sql_test_text_two'] = function(block) {
var value_name = javascriptGenerator.valueToCode(block, 'NAME', javascriptGenerator.ORDER_ATOMIC);
return ' two ' + value_name ;
};
Blockly.Blocks['sql_test_text_two'] = {
init: function() {
this.jsonInit(textTwoJson);
}
};
3.4.1.2.inputsInline
"inputsInline": true,
输入块 为 内置块
var textInputFiveJson = {
"type": "sql_test_input_five",
"message0": " input five %1",
"args0": [
{
"type": "input_value",
"name": "NUMBER",
"align": "RIGHT",
"check": "Number"
}
],
"inputsInline": true, // 输入应该与块主体保持在同一行,而不是下一行
"colour": 230,
"tooltip":' 内置 有输入'
};
javascriptGenerator.forBlock['sql_test_input_five'] = function(block) {
// 获取输入值
var numberInput = javascriptGenerator.valueToCode(block, 'NUMBER', javascriptGenerator.ORDER_ATOMIC);
return ' two ' + numberInput ;
};
Blockly.Blocks['sql_test_input_five'] = {
init: function() {
this.jsonInit(textInputFiveJson);
let thisBlock = this;
}
};
3.4.1.3.js
Blockly.Blocks['js_base_four'] = {
init: function () {
this.appendValueInput('VALUE')
.setCheck('Number') // 检测数据类型
.appendField(new Blockly.FieldLabelSerializable("计算值为:"));
this.setColour(145);
this.setTooltip("有入, 无出");
// this.setInputsInline(true);
}
}
javascriptGenerator.forBlock['js_base_four'] = function(block) {
var value_input = javascriptGenerator.valueToCode(block, 'VALUE', javascriptGenerator.ORDER_ATOMIC);
return value_input;
};
3.4.2.input_statement
接受另一个块作为输入的槽。
3.4.2.1. 案例 1
var doCode = javascriptGenerator.statementToCode(block, 'DO');
var textBaseFourJson = {
"type": "sql_test_base_four",
"message0": "( ) %1 ",
"args0": [
{
"type": "input_statement",
"name": "DO"
}
],
"colour": 120,
"tooltip": "包裹 其它块 "
};
javascriptGenerator.forBlock['sql_test_base_four'] = function(block) {
// 获取"DO"部分的代码
var doCode = javascriptGenerator.statementToCode(block, 'DO');
// 生成循环结构的代码
var loopCode = ' ( ' +
doCode.replace(/\n/g, '\n ') + // 缩进内联代码
'\n ) ';
return loopCode;
};
Blockly.Blocks['sql_test_base_four'] = {
init: function() {
this.jsonInit(textBaseFourJson);
}
};
3.4.2.2. 案例 2
var textInputFourJson = {
"type": "sql_test_input_four",
"message0": "input four 重复 %1 次:\n%2",
"args0": [
{
"type": "input_value",
"name": "TIMES",
"check": "Number"
},
{
"type": "input_statement",
"name": "DO"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": 120,
"tooltip": "重复执行指定次数的代码块。"
};
javascriptGenerator.forBlock['sql_test_input_four'] = function(block) {
// 获取重复次数
var times = javascriptGenerator.valueToCode(block, 'TIMES', javascriptGenerator.ORDER_ATOMIC);
// 获取"DO"部分的代码
var doCode = javascriptGenerator.statementToCode(block, 'DO');
// 生成循环结构的代码
var loopCode = 'for (var i = 0; i < ' + times + '; i++) {\n' +
doCode.replace(/\n/g, '\n ') + // 缩进内联代码
'\n}';
return loopCode;
};
Blockly.Blocks['sql_test_input_four'] = {
init: function() {
this.jsonInit(textInputFourJson);
}
};
3.4.2.3.js
Blockly.Blocks['js_base_six'] = {
init: function () {
this.appendStatementInput('DO')
.appendField(new Blockly.FieldLabelSerializable("包括:"));
this.setColour(145);
}
}
javascriptGenerator.forBlock['js_base_six'] = function(block) {
var value_input = javascriptGenerator.statementToCode(block, 'DO' );
return value_input;
};
3.4.2.4.三句
Blockly.Blocks['js_base_seven'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable(" ( "));
this.appendStatementInput('DO')
.appendField(new Blockly.FieldLabelSerializable(""));
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable(" ) "));
this.setColour(145);
}
}
javascriptGenerator.forBlock['js_base_seven'] = function(block) {
var value_input = javascriptGenerator.statementToCode(block, 'DO' );
return "(" + value_input + ")";
};
3.5.属性 Field
在 args#
数组内定义的特定类型对象
- type (
string
): 字段类型,如'field_input'
,'field_dropdown'
,'field_label_serializable'
。 - name (
string
): 字段的变量名,在生成代码时使用。 - text (
string
): 显示的默认文本(对于某些字段类型)。 - options / params (
Array
): 选项列表(对于下拉菜单等字段类型)。
3.5.1.field_input
可编辑的文本输入字段。
3.5.1.1.json
var textInputOneJson = {
"type": "sql_test_input_one",
"message0": " input one %1",
"args0": [
{
"type": "field_input",
"name": "TEXT",
"text": "默认文本"
}
],
"colour": 60,
};
javascriptGenerator.forBlock['sql_test_input_one'] = function(block) {
var text = block.getFieldValue('TEXT');
// 返回生成的代码字符串,这里假设输出就是用户输入的文本
return 'input one : "' + text.replace(/"/g, '\\"') + '"';
};
Blockly.Blocks['sql_test_input_one'] = {
init: function() {
this.jsonInit(textInputOneJson);
let thisBlock = this;
}
};
3.5.1.2.js
new Blockly.FieldTextInput("name")
Blockly.Blocks['js_field_one'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldLabelSerializable("名称"), "TEXT_NAME")
.appendField(new Blockly.FieldTextInput("name"), "TEXT_INPUT");
this.setColour(85);
}
}
javascriptGenerator.forBlock['js_field_one'] = function(block) {
var text = block.getFieldValue('TEXT_INPUT');
return "名称 : " + text ;
};
3.5.2.field_Number
3.5.2.1.json
“value”: 1,
“min”: 0,
“max”: 100,
“precision”: 1
初始值、最小值、最大值和精度
var textInputThreeJson = {
"type": "sql_test_input_three",
"message0": " input three %1",
"args0": [
{
"type": "field_Number",
"name": "NUMBER",
"value": 1,
"min": 0,
"max": 100,
"precision": 1
}
],
"colour": 60,
};
javascriptGenerator.forBlock['sql_test_input_three'] = function(block) {
var number = block.getFieldValue('NUMBER');
// 生成计算平方的JavaScript代码
var code = 'Math.pow(' + number + ', 2)';
// 返回生成的代码
return code;
};
Blockly.Blocks['sql_test_input_three'] = {
init: function() {
this.jsonInit(textInputThreeJson);
let thisBlock = this;
}
};
3.5.2.2.js
Blockly.Blocks['js_field_two'] = {
init: function () {
this.appendDummyInput()
.appendField(new Blockly.FieldNumber(0), "NUM_INPUT");
this.setOutput(true, "Number");
this.setColour(85);
}
}
javascriptGenerator.forBlock['js_field_two'] = function(block) {
var text = block.getFieldValue('NUM_INPUT');
return [ text , javascriptGenerator.ORDER_ATOMIC ];
};
3.5.3.field_dropdown
3.5.3.1.json
var textInputTwoJson = {
"type": "sql_test_input_two",
"message0": " input Two %1",
"args0": [
{
"type": "field_dropdown",
"name": "TEXT",
"options":[
["狗", "Dog"],
["猫", "Cat"],
["大象", "Elephant"]
]
}
],
"colour": 60,
};
javascriptGenerator.forBlock['sql_test_input_two'] = function(block) {
var value_name = block.getFieldValue('TEXT');
// 返回生成的代码字符串
return ' input Two ' + value_name ;
};
Blockly.Blocks['sql_test_input_two'] = {
init: function() {
this.jsonInit(textInputTwoJson);
}
};
3.5.3.2.js
Blockly.Blocks['js_field_three'] = {
init: function () {
var options = [
['选项1', 'option1'],
['选项2', 'option2'],
['选项3', 'option3']
];
this.appendDummyInput()
.appendField(new Blockly.FieldDropdown(options), 'SELECTED_OPTION');
this.setOutput(true, 'String'); // 输出类型为字符串
this.setColour(85);
}
}
javascriptGenerator.forBlock['js_field_three'] = function(block) {
var selectedOption = block.getFieldValue('SELECTED_OPTION');
// 生成的代码基于用户选择的选项
var code = `'${selectedOption}'`;
return [code, javascriptGenerator.ORDER_ATOMIC];
};