@blockly/block-dynamic-connection plugin

175 views
Skip to first unread message

Martynas Brazauskas

unread,
Aug 7, 2024, 6:32:32 AM8/7/24
to Blockly
First of all, not sure how google conversations work - I posted a conversation but can't find it anywhere so I'm posting again...

Anyway, I'm trying to use the @blockly/block-dynamic-connection plugin but either the docs on using it is incomplete or I'm missing something. Apparently the new dynamic blocks don't generate code...why? Isn't their idea to be the same as the original if/create_list_with/create_text_from blocks just with enhanced functionality of dynamically adding blocks? How am I supposed to provide a code generator to these new blocks? I want them to generate the same code as original blocks do, however I get this type of errors:
JavaScript generator does not know how to generate code for block type "dynamic_text_join".

Any help would be appreciated.

Martynas Brazauskas

unread,
Aug 7, 2024, 6:32:42 AM8/7/24
to Blockly
Hello, I'm trying to use the @blockly/block-dynamic-connection plugin for dynamic connectors, however the new dynamic blocks don't seem to generate javascript code. Did I miss a step of setup or am I supposed to provide the javascript generator to it somehow? Would be really nice if this was described in the readme. 

This is the error I'm getting when trying to convert my workspace to code
JavaScript generator does not know how to generate code for block type "dynamic_text_join".

Any help would be much appreciated.

Christopher Allen

unread,
Aug 7, 2024, 7:23:43 AM8/7/24
to blo...@googlegroups.com
Hi Martynas,

First of all, not sure how google conversations work - I posted a conversation but can't find it anywhere so I'm posting again...

We enabled premoderation on the Blockly group last year after it kept getting spam messages that were not being caught by the spam filter.  Unfortunately Groups doesn't really make it that clear to posters that their messages are being held for approval.

Anyway, I'm trying to use the @blockly/block-dynamic-connection plugin but either the docs on using it is incomplete or I'm missing something. Apparently the new dynamic blocks don't generate code...why? Isn't their idea to be the same as the original if/create_list_with/create_text_from blocks just with enhanced functionality of dynamically adding blocks? How am I supposed to provide a code generator to these new blocks? I want them to generate the same code as original blocks do, however I get this type of errors:
JavaScript generator does not know how to generate code for block type "dynamic_text_join".

Any help would be appreciated.

You are correct that the plugin provides only the blocks, not the corresponding generator functions.  We normally expect that most users will use the overrideOldBlockDefinitions API function to replace the standard blocks with the dynamic ones, in which case (by effectively renaming the dynamic versions) the generators for the ordinary versions will be used automatically—but if for some reason you want to use the dynamic blocks under their own names you could alternatively rename the generator functions:

javascriptGenerator.forBlock['dynamic_if'] = javascriptGenerator.forBlock['controls_if'];
// etc.

…or you could of course write your own block generator functions for them from scratch.


Best wishes,

Christopher

Martynas Brazauskas

unread,
Aug 8, 2024, 9:10:49 AM8/8/24
to Blockly
I have tried modifying my code based on your recommendations, however now I have another issue - the generated code does not represent the blocks. As you can see I have items combined into text, however the code thinks nothing is being combined.
blockly.png

Martynas Brazauskas

unread,
Aug 8, 2024, 9:11:01 AM8/8/24
to Blockly
Oh, okay, that makes sense. I did use the  overrideOldBlockDefinitions function but I also replaced the original blocks with the new ones. I now understand that I should've left the original block names. Thank you so much!

On Wednesday, August 7, 2024 at 2:23:43 PM UTC+3 cpca...@google.com wrote:

Christopher Allen

unread,
Aug 8, 2024, 12:36:52 PM8/8/24
to blo...@googlegroups.com
Hi Martynas,

I have tried modifying my code based on your recommendations, however now I have another issue - the generated code does not represent the blocks. As you can see I have items combined into text, however the code thinks nothing is being combined.


You are quite correct.  I am sorry for having given you incorrect information; I had assumed they must be compatiblebut after investigating the history of the relevant parts of the two repositories I can assert that the dynamic_text_join block is not and never was compatible with the generator for the text_join block.  I expect that similar incompatibilities may exist for the other dynamic blocks.  I have filed a bug report about this with additional information.

As a workaround, it should be pretty easy to provide your own block generator definition for the `dynamic_text_join` block by making some minor modifications to the `text_join` generator function.


With apologies,

Christopher

Martynas Brazauskas

unread,
Aug 9, 2024, 5:25:55 AM8/9/24
to Blockly
Thank you Christopher for the clarification. Based on what you told me and then also some digging around, here's the solution that worked for me:
import * as Blockly from "blockly/core";
import { javascriptGenerator, Order } from "blockly/javascript";

javascriptGenerator.forBlock["dynamic_if"] =
  javascriptGenerator.forBlock["controls_if"];
javascriptGenerator.forBlock["dynamic_text_join"] = function (
  block,
  generator
) {
  const joinBlock = block;
  switch (joinBlock.itemCount) {
    case 0:
      return ["''", Order.ATOMIC];
    case 1: {
      const element =
        generator.valueToCode(joinBlock, "ADD0", Order.NONE) || "''";
      const codeAndOrder = forceString(element);
      return codeAndOrder;
    }
    case 2: {
      const element0 =
        generator.valueToCode(joinBlock, "ADD0", Order.NONE) || "''";
      const element1 =
        generator.valueToCode(joinBlock, "ADD1", Order.NONE) || "''";
      const code = forceString(element0)[0] + " + " + forceString(element1)[0];
      return [code, Order.ADDITION];
    }
    default: {
      const elements = new Array(joinBlock.itemCount);
      for (let i = 0; i < joinBlock.itemCount; i++) {
        elements[i] =
          generator.valueToCode(joinBlock, "ADD" + i, Order.NONE) || "''";
      }
      const code = "[" + elements.join(",") + "].join('')";
      return [code, Order.FUNCTION_CALL];
    }
  }
};
javascriptGenerator.forBlock["dynamic_list_create"] = function (
  block,
  generator
) {
  const createWithBlock = block;
  const elements = new Array(createWithBlock.itemCount);
  for (let i = 0; i < createWithBlock.itemCount; i++) {
    elements[i] = generator.valueToCode(block, "ADD" + i, Order.NONE) || "null";
  }
  const code = "[" + elements.join(", ") + "]";
  return [code, Order.ATOMIC];
};


Reply all
Reply to author
Forward
0 new messages