Create a block from Context menu

40 views
Skip to first unread message

Julia

unread,
Nov 23, 2022, 8:42:12 AM11/23/22
to Blockly
Hi!
I use Blockly v.7.20211209

How can I add a block to workspace by clicking a context menu option?
I tried this: 
Blockly.Blocks['some_block'] = {
    ... 
    customContextMenu(options) {
        const option = {
            enabled: true,
            callback() {
                Blockly.serialization.blocks.append({
                    'type': 'text',
                    'fields': { 'TEXT': 'Now there is a block' }
                })
            },
        }
        const name = this.getFieldValue('NAME')
        option.text = Blockly.Msg.PROCEDURES_CREATE_DO.replace('%1', name)
        options.push(option)
     }
}

But it didn't work.

Julia

unread,
Nov 23, 2022, 10:26:33 AM11/23/22
to Blockly
Actually,  by pressing the context menu button I need to add a block for the function with the ability to enter parameters.

In some previous version we used something like goog.dom.createDom('field', null, name)

среда, 23 ноября 2022 г. в 16:42:12 UTC+3, Julia:

Christopher Allen

unread,
Nov 24, 2022, 12:46:05 PM11/24/22
to blo...@googlegroups.com
Hi Julia,
 
How can I add a block to workspace by clicking a context menu option?
I tried this: 
Blockly.Blocks['some_block'] = {
    ... 
    customContextMenu(options) {
        const option = {
            enabled: true,
            callback() {
                Blockly.serialization.blocks.append({
                    'type': 'text',
                    'fields': { 'TEXT': 'Now there is a block' }
                })

It looks like you have supplied only one argument to append(); it has a second required argument which is the workspace you want to append the block to.

            },
        }
        const name = this.getFieldValue('NAME')
        option.text = Blockly.Msg.PROCEDURES_CREATE_DO.replace('%1', name)
        options.push(option)
     }
}

But it didn't work.


 Could you be a bit more specific about what you mean by "it didn't work"?  Were there some error messages in the console, for example?

Actually,  by pressing the context menu button I need to add a block for the function with the ability to enter parameters.

Then you will need to create a block with type: 'procedures_callnoreturn' (rather than type: 'text' as given in your example code).

In some previous version we used something like goog.dom.createDom('field', null, name)

I'm not sure what that would have done or how it could possibly have worked to add blocks to the workspace!


Best wishes,

Christopher

Julia

unread,
Nov 25, 2022, 6:36:37 AM11/25/22
to Blockly
Hi Christopher!
Thanks for the answer. I'll try to figure it out some more.

четверг, 24 ноября 2022 г. в 20:46:05 UTC+3, cpca...@google.com:
Reply all
Reply to author
Forward
0 new messages