Attaching custom block to pre build block

147 views
Skip to first unread message

Anshika Mishra

unread,
Jul 21, 2022, 6:16:33 AM7/21/22
to Blockly
Hii, 

I am trying to add my custom block with "if block" already available in the toolbox from blockly library. 
but it is not getting attached. 

Please help me with this. 

Custom block: 

--------------------------code --------------------------------------------
(Blockly as any).Blocks['textIn'] = {
  init: function() {
    this.jsonInit(
    {
        "type": "textIn",
        "message0": "%1 includes %2",
        "args0": [
            {
                "type": "input_value",
                "name": "var1",
                "check": ['VAR',"MATH",'nonstring', 'String', 'NUMBER', 'MATH', 'TEXT', 'var_name' , "CONDITIONCHOOSER", "freeinput"]
            },
            {
                "type": "input_value",
                "name": "var2",
                "check":  ['VAR', "MATH",'nonstring', 'String', 'NUMBER', 'MATH', 'TEXT', 'var_name' , "CONDITIONCHOOSER", "freeinput"]
            }
        ],
        "inputsInline": true,
        // "previousStatement": ['controls_if'],
        "output": ['textIn', 'controls_if'],
        "colour": '#0ddb69',
        "tooltip": "",
        "helpUrl": ""
    })
}};

___________________________________________________________
Thanks in advance !

Regards, 
Anshika

Beka Westberg

unread,
Jul 22, 2022, 6:23:35 PM7/22/22
to Blockly
Hello,

Could you explain a bit more about where you're running into problems? Is the problem that your block doesn't show up at all? or that it's not attached to the other block properly?

If you could post your toolbox definition code, that would be really helpful for debugging =) Hopefully with a bit more info we can get this sorted!

Best wishes,
--Beka

Anshika Mishra

unread,
Jul 25, 2022, 3:08:55 AM7/25/22
to Blockly
It's not getting attached to pre build blocks from blockly .

Here goes the toolbox definition: 
____________________________________________________________________
export const toolbarBlocks = `<xml xmlns="https://developers.google.com/blockly/xml" id="toolbox-categories" style="display: block">
<toolboxlabel name="Custom Toolbox" colour="darkslategrey">
</toolboxlabel>
<category  name="Logic" categorystyle="logic_category">
  <block type="controls_if"></block>
  <block type="logic_compare"></block>
  <block type="logic_operation"></block>
  <block type="logic_negate"></block>
  <block type="logic_boolean"></block>
  <block type="logic_null" ></block>
  <block type="logic_ternary"></block>

  <block type="switch">
    <value name="switch_condi">
    <shadow type="switch_condition">
      <field name="switch_condition">condition</field>
    </shadow>
  </value>
</block>
  <block type="cases">
      <value name="case_condition">
      <shadow type="freeinput">
        <field name="freeinput">case</field>
      </shadow>
    </value>
  </block>
</category>
<category name="Loops" categorystyle="loop_category">
  <block type="controls_repeat_ext">
    <value name="TIMES">
      <shadow type="math_number">
        <field name="NUM">10</field>
      </shadow>
    </value>
  </block>
  <block type="controls_repeat" disabled="true"></block>
  <block type="controls_whileUntil"></block>
  <block type="controls_for">
    <value name="FROM">
      <shadow type="math_number">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <value name="TO">
      <shadow type="math_number">
        <field name="NUM">10</field>
      </shadow>
    </value>
    <value name="BY">
      <shadow type="math_number">
        <field name="NUM">1</field>
      </shadow>
    </value>
  </block>
  <block type="controls_forEach"></block>
  <block type="controls_flow_statements"></block>
</category>

Maribeth Bottorff

unread,
Jul 25, 2022, 6:02:11 PM7/25/22
to Blockly
Hi, I think your toolbox definition got cut off or the entire definition was not pasted, because it ends after the "loops" category, and I don't see your custom block "textIn" anywhere in the toolbox definition. Can you double check and post either the full definition, or if it's easier just the snippet where you're including the custom block?

Anshika Mishra

unread,
Jul 26, 2022, 12:42:16 AM7/26/22
to Blockly
Here goes the toolbox code
<category name="Text" categorystyle="text_category">
  <block type="text"></block>
  <block type="text_multiline">
  </block>
  <block type="text_join"></block>
  <block type="text_append">
    <value name="TEXT">
      <shadow type="text"></shadow>
    </value>
  </block>
  <block type="text_length">
    <value name="VALUE">
      <shadow type="text">
        <field name="TEXT">abc</field>
      </shadow>
    </value>
  </block>
  <block type="text_isEmpty">
    <value name="VALUE">
      <shadow type="text">
        <field name="TEXT"></field>
      </shadow>
    </value>
  </block>
  <block type="concatText"></block>
  <block type="text_indexOf">
    <value name="VALUE">
      <block type="variables_get">
        <field name="VAR">text</field>
      </block>
    </value>
    <value name="FIND">
      <shadow type="text">
        <field name="TEXT">abc</field>
      </shadow>
    </value>
  </block>
  <block type="text_charAt">
    <value name="VALUE">
      <block type="variables_get">
        <field name="VAR">text</field>
      </block>
    </value>
  </block>

  <block type="text_getSubstring">
    <value name="STRING">
      <block type="variables_get">
        <field name="VAR">text</field>
      </block>
    </value>
  </block>
  <block type="text_changeCase">
    <value name="TEXT">
      <shadow type="text">
        <field name="TEXT">abc</field>
      </shadow>
    </value>
  </block>
  <block type="text_trim">
    <value name="TEXT">
      <shadow type="text">
        <field name="TEXT">abc</field>
      </shadow>
    </value>
  </block>
  <block type="text_count">
    <value name="SUB">
      <shadow type="text"></shadow>
    </value>
    <value name="TEXT">
      <shadow type="text"></shadow>
    </value>
  </block>
  <block type="text_replace">
    <value name="FROM">
      <shadow type="text"></shadow>
    </value>
    <value name="TO">
      <shadow type="text"></shadow>
    </value>
    <value name="TEXT">
      <shadow type="text"></shadow>
    </value>
  </block>
  <block type="text_reverse">
    <value name="TEXT">
      <shadow type="text"></shadow>
    </value>
  </block>
  <block type="textIn">
    <value name="var1">
      <block type="variables_get">
        <field name="VAR"></field>
      </block>
    </value>
    <value name="var2">
      <block type="variables_get">
        <field name="VAR"></field>
      </block>
    </value>
  </block>
</category>
</xml>
  `;
_______________________________________________________________________
textIn.PNG
I have two images here , textIn block & if block, I want them to attach together which isn't happening. textIn block is custom block whereas if block is pre build block from blockly
if.PNG

Maribeth Bottorff

unread,
Jul 26, 2022, 2:14:01 PM7/26/22
to Blockly
The problem is in your block definition in the "output" key. That property is used for type checks that determine which blocks can connect to each other. You can read more here: https://developers.google.com/blockly/guides/create-custom-blocks/type-checks?hl=en#value_inputs_and_outputs

The key piece of information we need here is that this refers to a "type" and not a block name. Let's look at the block definition for that controls_if block: https://github.com/google/blockly/blob/61322294da70c770b32abb28b9d695af75f61a16/blocks/logic.js#L69 

In the input for the controls_if block, we have a type check that says whatever is connected must include "Boolean" in the output type. So in your custom block definition, the output array needs to include the type "Boolean". 

The toolbox definition was a red herring, we misunderstood your initial question, sorry about that. Hope this helps!

Maribeth

Reply all
Reply to author
Forward
0 new messages