Sava a composition of blocks

70 views
Skip to first unread message

my comment

unread,
May 5, 2021, 3:25:21 AM5/5/21
to Blockly
I want to save a composition of blocks. My goal is to get the composition in the toolbar just like the other blocks so that the user doesent have to create the composition everytime. 


koz...@google.com

unread,
May 5, 2021, 1:45:13 PM5/5/21
to Blockly
Hello,
When do you say "composition of blocks" do you mean that you want preset blocks with blocks that are already connected together?
If so, we have documentation HERE that explains how to do that. Otherwise, could you provide an example of what you mean by composition?
~ Monica

my comment

unread,
May 10, 2021, 3:04:01 AM5/10/21
to Blockly
hello,
that it exactly my goal but with statement input. Is that possible?
thanks
Roi

Jason Schanker

unread,
May 10, 2021, 10:42:17 AM5/10/21
to Blockly
Hi Roi,

Yes, this is possible as the toolbox flyout uses an ordinary Blockly.Workspace (Blockly.getMainWorkspace().getFlyout().getWorkspace()).  So for example, if you were using a toolbox without categories, you could do the following to set the statement input of the whileUntil block to a text_print block:

const flyoutWorkspace = Blockly.getMainWorkspace().getFlyout().getWorkspace();
const whileUntilBlock = flyoutWorkspace.getAllBlocks().find(block => block.type === "controls_whileUntil") || flyoutWorkspace.newBlock("controls_whileUntil");
whileUntilBlock.getInput("DO").connection.connect(flyoutWorkspace.newBlock("text_print").previousConnection);
Blockly.getMainWorkspace().updateToolbox(Blockly.Xml.workspaceToDom(flyoutWorkspace));

I should mention that I haven't investigated the Blockly toolbox code enough to know if this is the best way to do this, but it works.

If the stack of blocks you use will remain unchanged, it's probably easiest to directly load the toolbox from XML.  If you're using built-in blocks, you can generate the desired XML by using the Code application (https://blockly-demo.appspot.com/static/demos/code/index.html) that appears on the page Monica linked to.  For example, if you put a text_print block in the statement input of a controls_whileUntil block, you'll see the following generated XML, which you could then include in the appropriate place in your toolbox XML:

  <block type="controls_whileUntil" id="6Ir.qE4b+]8]nAu9zbxr" x="138" y="338">
    <field name="MODE">WHILE</field>
    <statement name="DO">
      <block type="text_print" id="G`c$Hit:809ZXoxuJh5J">
        <value name="TEXT">
          <shadow type="text" id="zJ:}6k5dD5hr3x,gY9HW">
            <field name="TEXT">abc</field>
          </shadow>
        </value>
      </block>
    </statement>
  </block>

Please feel free to follow up with any additional questions.

Best,
Jason

koz...@google.com

unread,
May 10, 2021, 12:44:36 PM5/10/21
to Blockly
As Jason mentions, it is possible with a statement input with either setting it dynamically or by defining it in your toolbox directly at the start.
This is supported for both XML and Json toolbox definitions (the documentation I linked is about how to set it in the toolbox definition).
~ Monica


my comment

unread,
May 11, 2021, 3:37:33 AM5/11/21
to Blockly
Thanks that helped a lot :)
Reply all
Reply to author
Forward
0 new messages