From 492d0719d63936773b82a8cf8fd3d5d69df71828 Mon Sep 17 00:00:00 2001 From: amber-cd <30151077+amber-cd@users.noreply.github.com> Date: Fri, 21 Jun 2019 18:14:11 -0400 Subject: [PATCH] Pull Request: Split showContextMenu_ (#2581) * Split showContextMenu_ Split showContextMenu_ into generateContextMenu_ and showContextMenu_. This allows for custom Blockly forks to easily extend the context menu options available on all blocks by just doing something like ``` var oldGenerateContextMenu_ = Blockly.BlockSvg.prototype.generateContextMenu_; Blockly.BlockSvg.prototype.generateContextMenu_ = function() { var menuOptions = Blockly.BlockSvg.prototype.generateContextMenu_() || []; // Push other options into menuOptions return menuOptions; } ``` Rather than having to modify the core code in such a way as to cause unnecessary maintenance overhead when upgrading Blockly versions. * Forgot the docblock, sorry * Oops, missed a few ESLint things * Update block_svg.js Updated to be protected rather than private. --- core/block_svg.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/core/block_svg.js b/core/block_svg.js index a1d41800f..6060039f9 100644 --- a/core/block_svg.js +++ b/core/block_svg.js @@ -634,11 +634,12 @@ Blockly.BlockSvg.prototype.showHelp_ = function() { }; /** - * Show the context menu for this block. - * @param {!Event} e Mouse event. - * @private + * Generate the context menu for this block. + * @protected + * @returns {Array} Context menu options */ -Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { +Blockly.BlockSvg.prototype.generateContextMenu = function() { + if (this.workspace.options.readOnly || !this.contextMenu) { return; } @@ -729,8 +730,21 @@ Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { this.customContextMenu(menuOptions); } - Blockly.ContextMenu.show(e, menuOptions, this.RTL); - Blockly.ContextMenu.currentBlock = this; + return menuOptions; +}; + +/** + * Show the context menu for this block. + * @param {!Event} e Mouse event. + * @private + */ +Blockly.BlockSvg.prototype.showContextMenu_ = function(e) { + var menuOptions = this.generateContextMenu(); + + if (menuOptions && menuOptions.length) { + Blockly.ContextMenu.show(e, menuOptions, this.RTL); + Blockly.ContextMenu.currentBlock = this; + } }; /**