Is it possible to add a badge of sequence number to all Blocks ?

105 views
Skip to first unread message

John K

unread,
Jun 27, 2022, 10:41:09 PM6/27/22
to Blockly
Hello, recently I created an electron application with Blockly
QQ截图20220628103634.png
The blocks generate the code in the right panel.
QQ图片20220628094531.png
 After running the generated code, the code throws an error. And I redirect the error to the output panel. 
QQ图片20220628094726.pngThe problem is two places might throw the error according to the layout of the blocks. Users might not know which blocks the error comes from. So I plan to change the error to "block#3 the Error: Division by zero". "3" is the block's sequence number that throws the error. 
QQ图片20220628102713.png
And add a badge to every block to indicate the sequence number of blocks.
QQ图片20220628102615.png

Is it possible to add a badge of sequence number to all Blocks ?
Or is there an alternative way? 

Aaron Dodson

unread,
Jun 28, 2022, 11:25:36 AM6/28/22
to Blockly
Have you considered attaching a Warning to the block that triggered the error? That might be a little easier for users since it would directly call out the affected block. Numbering the blocks is definitely an interesting idea from the perspective of illustrating the flow of execution though!

- Aaron

John K

unread,
Jun 28, 2022, 9:29:33 PM6/28/22
to Blockly
Thanks for your response.
Attaching a warning to the block is a good idea but may not suit my situation.
 The problem is attaching a warning to the block requiring the block file to be opened. If the ".tm" file is not opened in the editor, users can't see the warning on the block.
There are three ". tm" files.the blocks user created store in them. 
The current file is "test.tm". I hope I can import others ".tm" files into the current file and run them. In this case, the other files may not be opened in the editor.

Aaron Dodson

unread,
Jun 29, 2022, 6:36:58 PM6/29/22
to Blockly
That makes sense; perhaps it might work to create a custom Icon (much like Warning itself is) that displays the number, and attach those to all of the blocks in order?

John K

unread,
Jun 29, 2022, 10:25:37 PM6/29/22
to Blockly
Yes, I think so. If I want to add the custom icon to all blocks, do I have to add the icon to each block one by one? Is there a way to add the icon to all blocks globally?

Beka Westberg

unread,
Jul 1, 2022, 6:50:44 PM7/1/22
to Blockly
Hello!

I think the best way for you to achieve this given our current APIs is to define a custom field that you insert or remove depending on whether you want the badge to be shown or hidden.

You can insert the badge field at the top left of every block by doing the following:
```
function show() {
  const blocks = myWorkspace.getAllBlocks()
  for (let i = 0; i < blocks.length; i++) {
    blocks[i].inputsList[0].insertFieldAt(0, new myBadgeField(), 'BADGE');
  }
}
```

To remove them you can do:
```
function hide() {
  const blocks = myWorkspace.getAllBlocks()
  for (let i = 0; i < blocks.length; i++) {
    blocks[i].inputsList[0].removeField('BADGE');
  }
}
```

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

John K

unread,
Jul 3, 2022, 8:58:50 PM7/3/22
to Blockly
Cool! Thanks for your amazing solution!
Reply all
Reply to author
Forward
0 new messages