extends Block class

43 views
Skip to first unread message

kai pan

unread,
Jun 6, 2024, 11:31:08 AMJun 6
to Blockly
Can I extend the Block class to add some functionality? I tried subclassing Block, but how can I get the system to use my subclass instead of the original Block class? And since BlockSvg extends Block, how can I let BlockSvg know about my subclass?

Beka Westberg

unread,
Jun 6, 2024, 1:08:17 PMJun 6
to Blockly
Hello,

Can you explain a bit more about what functionality you want to add? In general you can't subclass Block or BlockSvg but there are a lot of other ways to add custom functionality!

Hopefully once we have a bit more information we can get this figured out =)
--Beka

kai pan

unread,
Jun 7, 2024, 11:37:49 AMJun 7
to Blockly
I need to check blocks when the workspace changes and highlight those with errors. For performance reasons, I want to cache the state in blocks and only check the recently changed blocks, their parent blocks, and surrounding blocks. Thus, I want to modify the Block class to add such functionality. While I could certainly save all states in a stand-alone system, modifying the Block class allows me to utilize the existing structure.

Thank you for your attention and response.

Beka Westberg

unread,
Jun 7, 2024, 11:51:05 AMJun 7
to blo...@googlegroups.com
Hello!

For data storage, I would recommend using a weakmap to associate data with an individual block. You can also use a normal map keyed by block ID.

You could also kludge the data onto the block objects, but I wouldn't recommend it. If you're using typescript, you would need to cast the class to `any`.

Sadly subclassing the block class isn't possible. The built-in classes are hard-coded into deserialization.

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

--
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/6ac12a71-d2b6-426b-8b36-81d375740281n%40googlegroups.com.

ewpa...@gmail.com

unread,
Jun 7, 2024, 1:37:13 PMJun 7
to Blockly
App Inventor does something similar with our error/warning system, especially because App Inventor projects can be pretty large so optimizing error checking is an important performance improvement for our users. Currently, this functionality is tightly coupled to our implementation of Blockly but with our unforking efforts the hope is to be able to publish it as a plugin for newer Blockly versions.

Here's the error handling in our current production system and here's a version adapted to Blockly 10. That said, we are still using Javascript and so the type enforcement isn't a blocker for us.

Cheers,
Evan
Reply all
Reply to author
Forward
0 new messages