Disable/hide context menu

430 views
Skip to first unread message

Akram Alam

unread,
Jan 2, 2024, 9:16:34 AM1/2/24
to Blockly
When a user right-clicks (or long presses) on a block, context menu should be disabled or hidden.
contextmenu.jpg

Maribeth Moffatt

unread,
Jan 2, 2024, 4:20:46 PM1/2/24
to Blockly
Hello,

You can use the Context Menu API to unregister any of the default options you don't want to show. If you unregister them all, no context menu will be shown. I would caution you to make sure your app is still usable for users who may rely on this to delete or duplicate blocks, but at the end of the day your app's UX is up to you.

Best,
Maribeth

Akram Alam

unread,
Jan 3, 2024, 11:14:00 AM1/3/24
to Blockly
$(document).ready(function() {
    $('#btnSave').on('click',
        function() {
            Save();
        });
    $('#btnRestore').on('click',
        function() {
            Clear();
        });
    $.each(Blockly.ContextMenuRegistry.registry.registry_, function (_index, item) {
        Blockly.ContextMenuRegistry.registry.unregister(item.id);
    });
...
.....
});

this line of code already being used in my project but still it is not working with right click.

Maribeth Moffatt

unread,
Jan 3, 2024, 1:18:17 PM1/3/24
to Blockly
1. The `registry_` property is private, so this is not a supported way of accessing the registered items. I would create a list of all the built-in IDs and loop through that instead. Feel free to file a feature request for us to make this process smoother!
2. I'm not that familiar with jQuery but is it possible this code is running before Blockly is finished loading? The `ready` function seems to run when the DOM is loaded, not necessarily with other scripts are done running. If you run this code before Blockly loads the context menu items, they'll be overwritten.

Best,
Maribeth

Max Stephen Russell

unread,
Mar 18, 2025, 11:10:58 AMMar 18
to Blockly
Blockly guide says "You can disable the context menu of an individual block by doing:
block.contextMenu = false;"


Where is the best place to do this? We've tried once and failed.

Thank you.

Steve

Ronald Bourret

unread,
Mar 18, 2025, 12:08:27 PMMar 18
to blo...@googlegroups.com
If you define a block with an init function, do it there:

Blockly.Blocks["my_block"] = {
  init: function () {
    this.jsonInit(myJson)
    this.contextMenu = false;
    ...
  },

}

Alternatively, use enableContextMenu in JSON:

Blockly.defineBlocksWithJsonArray([{
  "type": "my_block",
  "enableContextMenu": false,
  ...
}]);

Ronald Bourret
Technical Writer (Provided by Synergis)


--
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 visit https://groups.google.com/d/msgid/blockly/36dce940-a4fb-43c7-9121-f14b7de2e989n%40googlegroups.com.

Max Stephen Russell

unread,
Mar 18, 2025, 12:43:55 PMMar 18
to Blockly
Where in the world would I have found this beautiful gold nugget "enableContextMenu: false" for JSON in the Blockly literature or in a Google search?!?!

Thank you, Ronald!

-Steve

Ronald Bourret

unread,
Mar 18, 2025, 2:38:42 PMMar 18
to blo...@googlegroups.com
It's not currently in the docs, but it's being added.

As to where to find information, I usually start with the docs, then search the web. If those don't turn up anything, I look at the Blockly code base on GitHub. The core code is at https://github.com/google/blockly/tree/master and the examples, plugins, etc. are at https://github.com/google/blockly-samples/tree/master/examples. It takes some time to learn your way around the code base, but eventually it's very useful.

One thing you have to be very careful of is to make sure you only use the public parts of Blockly -- just because a useful-looking property or function is available in the code base doesn't mean you should use it. For more information, see API visibility.

Ronald Bourret
Technical Writer (Provided by Synergis)

Max Stephen Russell

unread,
Mar 18, 2025, 10:14:59 PMMar 18
to Blockly
Normally when I root through all the places you mention, I have a fairly good idea of what I’m looking for. Now that you have enlightened me, I see it in blocks/text.ts. 

I wonder, assuming blockly is a public repository, could it be that it actually hasn’t been indexed by Google? Supposedly Google indexes the contents of public repositories. But Google doesn’t seem to know enableContextMenu is in the blockly repository. Another interesting (to me at least) piece of this subject, is that wherever there is a weakness in search, AI can be stopped cold, and this was one of those cases.
Reply all
Reply to author
Forward
0 new messages