How to add a block to workspace programmatically and then refresh workspace

5,114 views
Skip to first unread message

stephen eshelman

unread,
Dec 2, 2016, 12:03:22 PM12/2/16
to Blockly
How do I add a block to the workspace programmatically and then refresh the workspace?

stephen eshelman

unread,
Dec 2, 2016, 12:16:43 PM12/2/16
to Blockly


On Friday, December 2, 2016 at 12:03:22 PM UTC-5, stephen eshelman wrote:
How do I add a block to the workspace programmatically and then refresh the workspace?

check that...
I can see that workspace.newBlock('') will add a block to the workspace.
What I really need is to connect the new block to an existing block ( that I have a reference to ) that may or may not already be connected to another block.

Rachel Fenichel

unread,
Dec 2, 2016, 2:01:23 PM12/2/16
to Blockly
Hi Stephen,

If you have a reference to your existing block you can find the connections that you want to connect on each block and simply call connect().  You will have to figure out if there is already a block there, decide whether to disconnect it, etc.  I suggest checking out the Block and Connection documentation to find the functions you need.

Cheers,
Rachel

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

phil cleaver

unread,
Dec 2, 2016, 9:48:47 PM12/2/16
to Blockly
see this thread https://groups.google.com/forum/#!searchin/blockly/join$20blocks%7Csort:relevance/blockly/vBMMNIyTr6Q/zTbMWV80-fYJ

specically (from Neil)

Open the console and type:

var parentBlock = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'text_print');
parentBlock.initSvg();
parentBlock.render();

var childBlock = Blockly.Block.obtain(Blockly.getMainWorkspace(), 'text');
childBlock.setFieldValue('Hello', 'TEXT');
childBlock.initSvg();
childBlock.render();

var parentConnection = parentBlock.getInput('TEXT').connection;
var childConnection = childBlock.outputConnection;
parentConnection.connect(childConnection);


That last line could equally well be childConnection.connect(parentConnection), order doesn't matter.

Neil Fraser

unread,
Dec 2, 2016, 11:04:59 PM12/2/16
to blo...@googlegroups.com
And for the record, the API has shifted a bit, so although this code snippet still works, it generates a warning on the console.  Here's the new version:

var parentBlock = workspace.newBlock('text_print');
parentBlock.initSvg();
parentBlock.render();

var childBlock = workspace.newBlock('text');

childBlock.setFieldValue('Hello', 'TEXT');
childBlock.initSvg();
childBlock.render();

var parentConnection = parentBlock.getInput('TEXT').connection;
var childConnection = childBlock.outputConnection;
parentConnection.connect(childConnection);

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Nicolas Mothiron

unread,
Jul 26, 2018, 10:12:01 AM7/26/18
to Blockly
Hi,

I've made 2 blocks I want to connect to each other this way :


Using your code with those blocks, I have :

var parentBlock = demoWorkspace.newBlock('feature');
parentBlock.initSvg();
parentBlock.render();

var childBlock = demoWorkspace.newBlock('scenario');

childBlock.initSvg();
childBlock.render();

var parentConnection = parentBlock.getInput('scenario').connection;

var childConnection = childBlock.outputConnection;
parentConnection.connect(childConnection);

And it just show me the two component, unconnected

knowing the feature component is like this :



And the scenario is like this :



Is there a way to connect each other by code ? Am I looking for the wrong input ?

Also I tried to know why it can't connect with the function "connection.canConnect()" and "canConnectForReason" from the documentation but it appears that they aren't functions... Is the documentation wrong in some points ?
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Rachel Fenichel

unread,
Jul 26, 2018, 4:04:08 PM7/26/18
to Blockly
You're looking for the right input, but the wrong connection on the scenario block.  Use previousConnection, not outputConnection.  That block doesn't have an output.

Where are you seeing canConnect and canConnectForReason in the documentation?  The documentation for the javascript library is generated directly from the code and should be up to date.  I don't see those functions mentioned.

Cheers,
Rachel

Nicolas Mothiron

unread,
Jul 27, 2018, 9:32:08 AM7/27/18
to Blockly
Thank you that worked for me

I can't find where I've seen functions like "canConnect" so I suppose I was in the wrong page
Reply all
Reply to author
Forward
0 new messages