Values

Value blocks are blocks with output connections. These act like values (aka expressions) in a text-based language.

Just like all blocks in Blockly, value blocks can be turned into code strings by defining a block-code generator.

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

javascriptGenerator.forBlock['custom_block'] = function(block, generator) {
  // Collect argument strings.
  const fieldValue = block.getFieldValue('MY_FIELD');
  const innerCode = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);

  // Return code.
  return ['my code string', Order.NONE];
}

Collect argument strings

All block-code generators require collecting the values of fields and collecting the code of inner blocks.

// Collect field values.
const fieldValue = block.getFieldValue('MY_FIELD');

// Collect inner block code strings.
const innerCode = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);

If you a referencing the code of an inner block multiple times, you should add argument caching to your block.

Return code

The return type of a value block-code generator is an array where the first value is a code string and the second value is a precedence.

return ['my code string', Order.NONE];

The precedence controls when parentheses are added around block-code strings. See the parentheses documentation for more information about how to control when parentheses are added.