2016-12-07 67 views
0

我已經實現了一個上下文菜單處理程序,它將一個塊添加到工作區中。我正在嘗試將塊添加到調用上下文菜單的塊和可能已經連接到它的任何塊(previousConnection)之間。我正在拍攝的和代碼我有和它做什麼...在代碼中創建塊到塊的連接

context menu option handler: 
    var option = 
    { 
     text: "Comment", 
     enabled: true, 
     callback: function() 
     { 
     var comment = workspace.newBlock('ta_comment'); 
     var block = Blockly.ContextMenu.currentBlock; 

     block.previousConnection.connect(comment.nextConnection); 

     comment.initSvg(); 
     comment.render(); 
     } 
    } 

    menuOptions.push(option); 

before after

+0

記住,連接之前註釋,保存'塊的目標連接.previousConnection'。如果它不爲null,則需要將'comment.previousConnection'連接到它。 – Anm

+0

是有道理的,但看起來像什麼?我已經嘗試了一些混合結果的東西。例如下面的工作,直到你第三次在同一個塊上運行它,然後斷開塊和coment塊... var comment = workspace.newBlock('ta_comment'); var block = Blockly.ContextMenu.currentBlock; var targetConnection = block.previousConnection.targetConnection; block.previousConnection.connect(comment.nextConnection); comment.previousConnection.connect(targetConnection); – objectthink

回答

0
 //create comment block and get the block that summoned the context menu 
    var comment = workspace.newBlock('ta_comment'); 
    var block = Blockly.ContextMenu.currentBlock; 

    //connect up comment block 
    comment.previousConnection.connect(block.previousConnection.targetConnection); 
    comment.nextConnection.connect(block.previousConnection); 

    //init 
    comment.initSvg(); 

    //render updated workspace 
    workspace.render(); 
+0

上面的代碼做的伎倆... – objectthink