How do I get all blocks, whether they're used in the workspace or not?

96 views
Skip to first unread message

yanyus...@gmail.com

unread,
Aug 10, 2020, 10:26:02 PM8/10/20
to Blockly
Hi, 
I need to get all blocks defined in js file, whether they're used in workspace or not. I seached the API document, but just find the method to get all blocks in workspace. How can I realize my needs? I guess I missed something, there must be a method to get all blocks. Can anyone help me?
Thanks!

Coda Highland

unread,
Aug 10, 2020, 11:02:58 PM8/10/20
to blo...@googlegroups.com
Since Blockly can only use blocks stored in Blockly.Blocks, you can iterate over that.

/s/ Adam

--
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/66e3c86f-cbe7-4648-a418-c550d8c27da2n%40googlegroups.com.

Neil Fraser

unread,
Aug 10, 2020, 11:12:34 PM8/10/20
to blo...@googlegroups.com
On Mon, 10 Aug 2020 at 20:02, Coda Highland <chig...@gmail.com> wrote:
Since Blockly can only use blocks stored in Blockly.Blocks, you can iterate over that.

Correct.  But one should be aware of a couple of intruders:
* Non-block objects (like .lists, .colour, etc).  Just check that the property value is and object and has a .init property.
* Quark blocks for mutators. (like controls_if_if).  I've no idea how to detect these now.  I vaguely remember it used to be possible, but as it stands all data is in an inaccessible closure thanks to jsonInit.

--

yanyus...@gmail.com

unread,
Aug 11, 2020, 3:10:36 AM8/11/20
to Blockly
Thanks for your help! A new question is that how can I iterate them. 
I output them on the console and try to iterate them. But “Blockly.Blocks" is not a array. 
44.PNG
11.PNG
33.PNG
22.PNG

Beka Westberg

unread,
Aug 11, 2020, 9:40:14 AM8/11/20
to blo...@googlegroups.com
Hello!

Since it's an object you want to use a for...in loop. Here's an example from the MDN docs:

```
for (const prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    console.log(`obj.${prop} = ${obj[prop]}`);
  }
}
```

I hope that helps!
--Beka

Coda Highland

unread,
Aug 11, 2020, 10:18:05 AM8/11/20
to blo...@googlegroups.com
I would add one more caveat: This iteration will identify all of the blocks, but it won't tell you what variations are possible through mutators. You might need to cross-reference the toolbox XML.

/s/ Adam

--
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.
Reply all
Reply to author
Forward
0 new messages