如果目标用作StatementInput,则防止将新块附加到相同类型

时间:2019-08-15 16:11:26

标签: blockly google-blockly

我基本上有两种类型的块:规则块和事实块(就像在Prolog中一样)。它们可以彼此连接。规则块需要两个“事实”类型的输入。 但是,不可能将多个附加的事实块作为单个输入。因此,每当附加事实块作为规则块的输入时,我都必须将“ setNextStatement”设置为false。

这是我在事实块中尝试做的事情:

this.setOnChange(function(changeEvent) {
    if(changeEvent.type == Blockly.Events.MOVE) {
        let prevBlock = this.getPreviousBlock();
        if(prevBlock != null && prevBlock.type == "rule") {
            let nextBlock = prevBlock.getNextBlock();
            if((nextBlock != null && nextBlock != this) || (nextBlock == null)) {

                this.setNextStatement(false);
            }
        } else {
            this.setNextStatement(true);
        }
    }
});

当然还有规则块:

Blockly.Blocks['rule'] = {
    init: function() {
        this.appendDummyInput("RULE_DATA")
            .appendField('Rule: ');

        this.appendStatementInput('INPUT_HEAD')
            .setCheck("fact")
            .appendField("Head");

        this.appendStatementInput('INPUT_BODY')
            .setCheck("fact")
            .appendField("Body");
...

这实际上可行,但是当我将事实与规则的输入部分分开时,总是会出现以下错误:

Uncaught Error: Connection lists did not match in length.
    at Blockly.BlockSvg.Blockly.Block.getMatchingConnection (blockly_compressed.js:1447)
    at Blockly.InsertionMarkerManager.connectMarker_ (blockly_compressed.js:1125)
    at Blockly.InsertionMarkerManager.showPreview_ (blockly_compressed.js:1118)
    at Blockly.InsertionMarkerManager.maybeShowPreview_ (blockly_compressed.js:1117)
    at Blockly.InsertionMarkerManager.update (blockly_compressed.js:1110)
    at Blockly.BlockDragger.dragBlock (blockly_compressed.js:1130)
    at Blockly.TouchGesture.Blockly.Gesture.startDraggingBlock_ (blockly_compressed.js:1177)
    at Blockly.TouchGesture.Blockly.Gesture.updateIsDraggingBlock_ (blockly_compressed.js:1174)
    at Blockly.TouchGesture.Blockly.Gesture.updateIsDragging_ (blockly_compressed.js:1176)
    at Blockly.TouchGesture.Blockly.Gesture.updateFromEvent_ (blockly_compressed.js:1171)

有人有什么主意吗?

1 个答案:

答案 0 :(得分:0)

您好,您需要从代码块中覆盖getMatchingConnection函数并在函数中选中检查内容

window.Blockly.Block.prototype.getMatchingConnection = function(otherBlock, conn) {
  var connections = this.getConnections_(true);
  var otherConnections = otherBlock.getConnections_(true);
  // if (connections.length !== otherConnections.length) {
  //   throw Error("Connection lists did not match in length.");
  // }
  for (var i = 0; i < otherConnections.length; i++) {
    if (otherConnections[i] === conn) {
      return connections[i];
    }
  }
  return null;
};