Has API for adding custom blocks changed?

517 views
Skip to first unread message

James Smith

unread,
Jul 29, 2022, 12:34:31 PM7/29/22
to Blockly
Hi
I've been away from Blockly for a few months and now back to developing the Blockly features of our site.

I thought I'd upgrade from Blockly 6 to Blockly 8 but some things have broken.

**nb I am not having trouble with the JSON blocks but only with the ones that use JS Functions)**

Previously when I added our custom Blocks I used to import Blockly at the top of the file and then import my custom Blocks and then do this

// merge our block with Blockly's blocks
Blockly.Blocks = {
...Blockly.Blocks,
...blockDefs,
};

and if I console.log the Blockly.Blocks I can see they are all there as they are supposed to be.

But I get a '
Invalid block definition for type' from the Block class

// Copy the type-specific functions and data from the prototype.
if (prototypeName) {
   /** @type {string} */
   this.type = prototypeName;
   const prototype = Blocks[prototypeName];
   if (!prototype || typeof prototype !== 'object') {
     throw TypeError('Invalid block definition for type: ' + prototypeName);
   }
   object.mixin(this, prototype);
}

and when I console the Blocks here, I do not find my custom blocks. It is definitely happening in the right order because the console.log happens before the error.

I looks like the Blocks list you check in the Block class is  imported like this:

const {Blocks} = goog.require('Blockly.blocks');

so is this Blocks not the same Blocks as I have added to  earlier when merged my blocks with Blockly.Blocks?

is there a new way to added my custom Blocks? (I have checked the docs and it doesn't say anything??)

my block definitions have not changed, and so I don't suspect that there is a problem with them, they check out with the docs ok...

many thanks for any pointers! very grateful :-)

James


Maribeth Bottorff

unread,
Jul 29, 2022, 5:15:04 PM7/29/22
to Blockly
Yes, prior to Blockly v8 everything was just one big Blockly object. In v8 things are modularized so when you reassign Blockly.Blocks, that means the Block class is no longer looking at the same object as what you reassigned to.

To fix this you can either assign each block individually to Blockly.Blocks['your_block_name_here'] = ... (which is what the docs recommend, if you know of a place where our docs say to use reassignment can you let me know so we can update them?) or you can possibly use Blockly.common.defineBlocks(blockDefs) which is new and as-yet undocumented.

Hope that helps,

Maribeth

Maribeth Bottorff

unread,
Jul 29, 2022, 6:54:42 PM7/29/22
to Blockly
Realized my previous message was slightly confusing. The API for adding custom blocks has not changed.

But the underling structure of Blockly has changed which made your previous approach no longer feasible. I think it should be an easy fix for you though.

Maribeth

Mark Friedman

unread,
Jul 29, 2022, 7:17:50 PM7/29/22
to blo...@googlegroups.com
Since you already have your custom block definitions in an object, I think that this should work too:

Object.assign(Blockly.Blocks, blockDefs)

-Mark


--
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/6d633768-67b5-48c6-bb17-5760736fcb93n%40googlegroups.com.
Message has been deleted
Message has been deleted

James Smith

unread,
Jul 30, 2022, 4:59:16 AM7/30/22
to Blockly
| "Yes, prior to Blockly v8 everything was just one big Blockly object. In v8 things are modularized so when you reassign Blockly.Blocks, that means the Block class is no longer looking at the same object as what you reassigned to"

ah right got it, so mutate the blocks object rather than replace it.... sorry of course it doesn't say in the docs you can just replace the Blocks object with a modified one, I just made that up... because I have > 50 blocks, and also the linter I use gets annoyed that I use array syntax instead of object :-)

the object.assign approach works out fine, all back on track.

I like the new (as yet undocumented API) though, it's nicer to have, I will switch to it when it's ready.

thanks to all
Reply all
Reply to author
Forward
0 new messages