Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/blocks/vertical_extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,30 @@ const PROCEDURE_DEF_CONTEXTMENU = function (this: Blockly.Block) {
// Add the edit option at the end.
menuOptions.push(ScratchProcedures.makeEditOption(this));

// Find and remove the duplicate option
// Find and remove the duplicate option,
// and update the delete option
for (let i = 0, option; (option = menuOptions[i]); i++) {
if (option.text == Blockly.Msg.DUPLICATE) {
if (
option.text == Blockly.Msg.DUPLICATE_BLOCK
) {
menuOptions.splice(i, 1);
break;
} else if (
option.text == Blockly.Msg.DELETE_BLOCK
) {
const newOption = {
callback: this.checkAndDelete,
enabled: option.enabled,
text: option.text,
};
// @ts-expect-error
newOption.scope = option.scope;
// @ts-expect-error
newOption.weight = option.weight;
menuOptions.splice(i, 1, newOption);
}
}
},
checkAndDelete: function () {
checkAndDelete: (function () {
const input = this.getInput("custom_block");
// this is the root block, not the shadow block.
if (input && input.connection && input.connection.targetBlock()) {
Expand All @@ -181,7 +196,7 @@ const PROCEDURE_DEF_CONTEXTMENU = function (this: Blockly.Block) {
alert(Blockly.Msg.PROCEDURE_USED);
}
}
},
}).bind(this),
},
true
);
Expand Down
5 changes: 4 additions & 1 deletion src/context_menu_items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export function registerDeleteBlock() {
: Blockly.Msg["DELETE_X_BLOCKS"].replace("%1", `${descendantCount}`);
},
preconditionFn(scope: Blockly.ContextMenuRegistry.Scope) {
if (!scope.block.isInFlyout && scope.block.isDeletable()) {
if (
!scope.block.isInFlyout &&
scope.block.isDeletable()
) {
return "enabled";
}
return "hidden";
Expand Down
18 changes: 16 additions & 2 deletions src/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ function deleteProcedureDefCallback(
return true;
}

/**
* Returns whether the given block is a part of a procedure hat.
*
* @param block The block to check.
* @returns True if the block is a procedure hat block part, otherwise false.
*/
export function isProcedureDeclarationPart(
block: Blockly.BlockSvg
): block is ProcedureBlock {
return (
block.type === Constants.PROCEDURES_DECLARATION_BLOCK_TYPE ||
block.type === Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE
);
}

/**
* Returns whether the given block is a procedure block and narrows its type.
*
Expand All @@ -425,8 +440,7 @@ export function isProcedureBlock(
): block is ProcedureBlock {
return (
block.type === Constants.PROCEDURES_CALL_BLOCK_TYPE ||
block.type === Constants.PROCEDURES_DECLARATION_BLOCK_TYPE ||
block.type === Constants.PROCEDURES_PROTOTYPE_BLOCK_TYPE
isProcedureDeclarationPart(block)
);
}

Expand Down