Custom procedure deletion

This commit is contained in:
Rachel Fenichel 2017-10-18 16:49:27 -07:00
parent b02333edf0
commit 0c82dcb0c8
3 changed files with 37 additions and 3 deletions

View file

@ -148,6 +148,15 @@ Blockly.Blocks['procedures_callnoreturn_internal'] = {
this.argumentDefaults_ = [];
this.warp_ = false;
},
/**
* Returns the name of the procedure this block calls, or the empty string if
* it has not yet been set.
* @return {string} Procedure name.
* @this Blockly.Block
*/
getProcCode: function() {
return this.procCode_;
},
/**
* Create XML to represent the (non-editable) name and arguments.
* @return {!Element} XML storage element.

View file

@ -160,11 +160,20 @@ Blockly.ScratchBlocks.VerticalExtensions.PROCEDURE_DEF_CONTEXTMENU = {
// Add the edit option at the end.
menuOptions.push(Blockly.Procedures.makeEditOption(this));
// Find the delete option and update its callback to be specific to functions.
// Find the delete option and update its callback to be specific to
// functions.
for (var i = 0, option; option = menuOptions[i]; i++) {
if (option.text == Blockly.Msg.DELETE_BLOCK) {
var input = this.getInput('custom_block');
// this is the root block, not the shadow block.
if (input && input.connection && input.connection.targetBlock()) {
var procCode = input.connection.targetBlock().getProcCode();
} else {
return;
}
var rootBlock = this;
option.callback = function() {
alert('TODO(#1130): implement function deletion');
Blockly.Procedures.deleteProcedureDefCallback(procCode, rootBlock);
};
}
}

View file

@ -276,7 +276,7 @@ Blockly.Procedures.getCallers = function(name, ws, definitionRoot,
var callers = [];
for (var i = 0; i < allBlocks.length; i++) {
var block = allBlocks[i];
if (block.type == 'procedure_callnoreturn') {
if (block.type == 'procedures_callnoreturn') {
var procCode = block.getProcCode();
if (procCode && procCode == name) {
callers.push(block);
@ -412,3 +412,19 @@ Blockly.Procedures.makeShowDefinitionOption = function(block) {
};
return option;
};
Blockly.Procedures.deleteProcedureDefCallback = function(procCode,
definitionRoot) {
var callers = Blockly.Procedures.getCallers(procCode,
definitionRoot.workspace, definitionRoot, false /* allowRecursive */);
if (callers.length > 0) {
// TODO(#1151)
alert('Can\'t delete function because there were ' + callers.length +
' non-recursive calls');
return;
}
// Delete the whole stack.
Blockly.Events.setGroup(true);
definitionRoot.dispose();
Blockly.Events.setGroup(false);
};