Inner blocks

Inner blocks are the blocks attached to your value and statement inputs. Individual block-code generators need to handle the concatenation of their inner blocks so the code gets added in the correct place.

import {javascriptGenerator, Order} from 'blockly/javascript';

javascriptGenerator.forBlock['my_custom_block'] = function(block, generator) {
  // Generate innner block code.
  const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
  const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);

  // Concatenate the string.
  const code = `some code ${statement} ${value} some more code`;

  // Return the code.
  return code;
}

Statement inputs

The code of inner blocks attached to statement inputs can be generated using statementToCode. This calls the statement block's block-code generator and handles indenting code.

const statement = generator.statementToCode(block, 'MY_STATEMENT_INPUT');

You only need to call statementToCode for the inner block directly connected to a statement input.

Value inputs

The code of inner blocks attached to value inputs can be generated using valueTocode. This calls the value block's block-code code generator and handles adding parentheses around inner blocks' code when necessary.

See the parentheses documentation for more information about how to control parentheses.

const value = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);

Concatenate code

After you've gotten your inner block's code string, you can concatenate it in the correct place with your code string.

const code = `some code ${statement} ${value} some more code`;

Return code

Different types of blocks require the code string to be returned in different ways, so check out their individual pages for more information: