Re: use workspaceToCode() with nested custom blocks

75 views
Skip to first unread message
Message has been deleted

Beka Westberg

unread,
Aug 25, 2021, 6:26:07 PM8/25/21
to blo...@googlegroups.com
Hello,

Thank you for your question! If your blocks are nested inside of each other, the outer block needs to explicitly generate the code of the inner block. The system is set up this way because different outer blocks need to wrap their inner blocks in different code (think of the difference between an if statement and a while loop).

So your code would probably look something like:
```
(Blockly as any).JavaScript['chapter'] = function (block) {
      let value = '\'' + block.getFieldValue('VAR_FILTER') + '\'';
      let text = Blockly.JavaScript.valueToCode(block, 'MY_INPUT', Blockly.JavaScript.ORDER_ATOMIC);
      return 'chapter ' + value + text;  // Or whatever you want to return
};

(Blockly as any).JavaScript['text_input'] = function (block) {
      let question_text = '\'' + block.getFieldValue('VAR_QUESTION_TEXT') + '\'';
      return [question_text, Blockly.JavaScript.ORDER_NONE];  // Make sure to return an array for value blocks!
}
```

See the generating code documentation for more information.

I hope that helps! If you have any further questions please reply!
--Beka

On Wed, Aug 25, 2021 at 7:19 AM Jana <janawi...@gmail.com> wrote:
Hi,

I have a problem with the translation of custom blocks in javascript. 
When I build the blocks one below the other I get the code of all blocks displayed correctly. But as soon as I nest one of my blocks inside another one I only get the code for the outer block.
Does anyone have an idea how I could solve this problem?

Function of the outer custom block:
(Blockly as any).JavaScript['chapter'] = function (block) {
      let value = '\'' + block.getFieldValue('VAR_FILTER') + '\'';
      return 'chapter ';
};

Function of inner custom block:
(Blockly as any).JavaScript['text_input'] = function (block) {
      let question_text = '\'' + block.getFieldValue('VAR_QUESTION_TEXT') + '\'';
      return question_text;
}

Function that outputs the code:
blocklyToCode() {
    (Blockly as any).JavaScript.INFINITE_LOOP_TRAP = null;
    let code = (Blockly as any).JavaScript.workspaceToCode(Blockly.mainWorkspace, (Blockly as any).JavaScript.ORDER_ATOMIC);
    console.log(code);
}

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/c5ea8391-9025-4fca-abf6-cf84768905e1n%40googlegroups.com.
Message has been deleted

Beka Westberg

unread,
Aug 26, 2021, 9:43:47 AM8/26/21
to blo...@googlegroups.com
Hello again :D

MY_INPUT stands for the name you gave your input when you appended it to your block. For example:

```
Blockly.Blocks['chapter'] = {
  init: function() {
    this.appendValueInput('MY_INPUt');
  }
}
```

If you post your block definition I can tell you exactly what string it needs to be as well =)

I hope that helps!
--Beka

On Wed, Aug 25, 2021 at 11:21 PM Jana <janawi...@gmail.com> wrote:
Hi, thank you very much for your answer! 
I still do not quite understand what I have to put in instead of MY_INPUT. I tried 'text_input' but it didnt work.
Reply all
Reply to author
Forward
0 new messages