Popup via context menu

535 views
Skip to first unread message

Rick J

unread,
Feb 15, 2018, 4:37:56 PM2/15/18
to Blockly
Hi,

Is it possible to add an item to the context menu (shows up when you right click on a block) and clicking that item would launch a popup window? I want to be able to accomplish this without touching the core blockly library. My goal is to then alter the block after the user exits the popup.


Andrew n marshall

unread,
Feb 16, 2018, 12:35:41 PM2/16/18
to blo...@googlegroups.com
Block based context menus are constructed and shown via Blockly.BlockSvg.prototype.showContextMenu_(..).  If a block has a method customContextMenu(menuOptions), it will pass in the constructed list of menu options, allowing your code to append and otherwise modify the menu options.

Each menu option object has the following structure (as noted in Blockly.ContextMenu.populate_(..)):

  {
    text: 'Make It So',
    enabled: true,
    callback: Blockly.MakeItSo
  }

The menu item will be the target / this of the callback.  (I might look into making this the Block itself, in the context of a block context menu.) From this callback, you should be able to launch your popup, or whatever else you need to do.


It is also worth noting that one can disable the block altogether via the block.contextMenu boolean, or the JSON block definition property enableContextMenu.

On Thu, Feb 15, 2018 at 1:37 PM, Rick J <shari...@gmail.com> wrote:
Hi,

Is it possible to add an item to the context menu (shows up when you right click on a block) and clicking that item would launch a popup window? I want to be able to accomplish this without touching the core blockly library. My goal is to then alter the block after the user exits the popup.


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted
Message has been deleted

Mark Friedman

unread,
Feb 16, 2018, 7:05:24 PM2/16/18
to blo...@googlegroups.com
Rick,

You should also change 

var option;
to
var option = {};

You should have been seeing messages like 

Uncaught TypeError: Cannot set property 'enabled' of undefined

in your console, which should have alerted you to the problem.

On Fri, Feb 16, 2018 at 10:26 AM, Rick J <shari...@gmail.com> wrote:
Also not seeing that option show up in the context menu. I did make a small fix to option.enabled = true;


On Friday, February 16, 2018 at 11:24:26 AM UTC-7, Rick J wrote:
Andrew,

I am relatively new to JS but here was my jab at it. Do you see something that may be wrong in my implementation? I am not seeing any statements logged.

Blockly.js code fragment

Blockly.Blocks['create_function'] = {
/**
* Context Menu item to call create function popup
* @param {!Array} options List of menu options to add to.
* @this Blockly.Block
*/
customContextMenu: function(options) {
console.log(options);
var option;
option.enabled = {enabled: true};
option.text = 'Create Function';
option.callback = popupFunction();
options.push(option);
console.log(options);
},
};

function popupFunction() {
console.log('create function clicked!');
};

toolbox.js fragment
<category name="Functions" colour="200" >
<block type="create_function"></block>
</category>


To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Emil Hemdal

unread,
Apr 11, 2018, 4:37:58 AM4/11/18
to Blockly
Can you do this globally or do you have to do this for every block? /Emil
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.

Andrew n marshall

unread,
Apr 11, 2018, 1:47:43 PM4/11/18
to blo...@googlegroups.com
We don't expose an API that will affect the context menu of every block.

If you want this behavior, you could try overriding Blockly.ContextMenu.populate_().  JavaScript being JavaScript, you can probably get away with redefining the function without modifying the Blockly library:


var originalBlocklyContextMenuPopulate =

Blockly.ContextMenu.populate_;

Blockly.ContextMenu.populate_ = function(options, rtl) {

 var menu = originalBlocklyContextMenuPopulate(options, rtl);

 // Modify to your needs.

 return menu;

}



To unsubscribe from this group and stop receiving emails from it, send an email to blockly+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages