From a78fddb3e9e71c5b17684252b7270996e38efc26 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:07:19 -0400 Subject: [PATCH 1/7] split procedure block type checking into 2 functions --- src/procedures.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/procedures.ts b/src/procedures.ts index 3098fa3a45..1abbffff69 100644 --- a/src/procedures.ts +++ b/src/procedures.ts @@ -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. * @@ -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() ); } From 3f03442f7f220f1774c8259f427e40d9057cf2b2 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:07:41 -0400 Subject: [PATCH 2/7] don't show the delete option on the procedure hat --- src/context_menu_items.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/context_menu_items.ts b/src/context_menu_items.ts index d79a114ae2..a40e83d6fd 100644 --- a/src/context_menu_items.ts +++ b/src/context_menu_items.ts @@ -5,6 +5,7 @@ */ import * as Blockly from "blockly/core"; +import { isProcedureDeclarationPart } from "./procedures"; /** * Registers a block delete option that ignores shadows in the block count. @@ -18,7 +19,11 @@ 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() && + !isProcedureDeclarationPart(scope.block) + ) { return "enabled"; } return "hidden"; From 1dfb2d106f92778471db3a58ea2b9b4a1adac374 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:22:26 -0400 Subject: [PATCH 3/7] pass the block --- src/procedures.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/procedures.ts b/src/procedures.ts index 1abbffff69..9ad3070cb0 100644 --- a/src/procedures.ts +++ b/src/procedures.ts @@ -440,7 +440,7 @@ export function isProcedureBlock( ): block is ProcedureBlock { return ( block.type === Constants.PROCEDURES_CALL_BLOCK_TYPE || - isProcedureDeclarationPart() + isProcedureDeclarationPart(block) ); } From 573f5415953a335007bc94a5f4ddff11b6e1e460 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:24:32 -0400 Subject: [PATCH 4/7] don't show on definition blocks --- src/context_menu_items.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/context_menu_items.ts b/src/context_menu_items.ts index a40e83d6fd..2ec3788289 100644 --- a/src/context_menu_items.ts +++ b/src/context_menu_items.ts @@ -6,6 +6,7 @@ import * as Blockly from "blockly/core"; import { isProcedureDeclarationPart } from "./procedures"; +import * as Constants from "./constants"; /** * Registers a block delete option that ignores shadows in the block count. @@ -22,7 +23,8 @@ export function registerDeleteBlock() { if ( !scope.block.isInFlyout && scope.block.isDeletable() && - !isProcedureDeclarationPart(scope.block) + !isProcedureDeclarationPart(scope.block) && + !scope.block.type === Constants.PROCEDURES_DEFINITION_BLOCK_TYPE ) { return "enabled"; } From 9d92a22e4d4512ce78f596c38a584eeaaa86c0b6 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:26:07 -0400 Subject: [PATCH 5/7] use !== --- src/context_menu_items.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context_menu_items.ts b/src/context_menu_items.ts index 2ec3788289..d601cb6be7 100644 --- a/src/context_menu_items.ts +++ b/src/context_menu_items.ts @@ -24,7 +24,7 @@ export function registerDeleteBlock() { !scope.block.isInFlyout && scope.block.isDeletable() && !isProcedureDeclarationPart(scope.block) && - !scope.block.type === Constants.PROCEDURES_DEFINITION_BLOCK_TYPE + scope.block.type !== Constants.PROCEDURES_DEFINITION_BLOCK_TYPE ) { return "enabled"; } From db8d969232772c00783e73612cf578ce463c7018 Mon Sep 17 00:00:00 2001 From: yuri-kiss <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:37:43 -0400 Subject: [PATCH 6/7] testing commit --- src/blocks/vertical_extensions.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/blocks/vertical_extensions.ts b/src/blocks/vertical_extensions.ts index cfda376d16..ac88495b5f 100644 --- a/src/blocks/vertical_extensions.ts +++ b/src/blocks/vertical_extensions.ts @@ -159,6 +159,8 @@ const PROCEDURE_DEF_CONTEXTMENU = function (this: Blockly.Block) { ) { // Add the edit option at the end. menuOptions.push(ScratchProcedures.makeEditOption(this)); + // duplicate for testing + menuOptions.push(ScratchProcedures.makeEditOption(this)); // Find and remove the duplicate option for (let i = 0, option; (option = menuOptions[i]); i++) { From b5a2e8a9e4781edf73c5249185660bf1ef04dabb Mon Sep 17 00:00:00 2001 From: Miyo Sho <135030944+yuri-kiss@users.noreply.github.com> Date: Fri, 25 Apr 2025 14:54:24 -0400 Subject: [PATCH 7/7] fix #222 final --- src/blocks/vertical_extensions.ts | 27 ++++++++++++++++++++------- src/context_menu_items.ts | 6 +----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/blocks/vertical_extensions.ts b/src/blocks/vertical_extensions.ts index ac88495b5f..2535fdb074 100644 --- a/src/blocks/vertical_extensions.ts +++ b/src/blocks/vertical_extensions.ts @@ -159,18 +159,31 @@ const PROCEDURE_DEF_CONTEXTMENU = function (this: Blockly.Block) { ) { // Add the edit option at the end. menuOptions.push(ScratchProcedures.makeEditOption(this)); - // duplicate for testing - 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()) { @@ -183,7 +196,7 @@ const PROCEDURE_DEF_CONTEXTMENU = function (this: Blockly.Block) { alert(Blockly.Msg.PROCEDURE_USED); } } - }, + }).bind(this), }, true ); diff --git a/src/context_menu_items.ts b/src/context_menu_items.ts index d601cb6be7..210f4d5ae4 100644 --- a/src/context_menu_items.ts +++ b/src/context_menu_items.ts @@ -5,8 +5,6 @@ */ import * as Blockly from "blockly/core"; -import { isProcedureDeclarationPart } from "./procedures"; -import * as Constants from "./constants"; /** * Registers a block delete option that ignores shadows in the block count. @@ -22,9 +20,7 @@ export function registerDeleteBlock() { preconditionFn(scope: Blockly.ContextMenuRegistry.Scope) { if ( !scope.block.isInFlyout && - scope.block.isDeletable() && - !isProcedureDeclarationPart(scope.block) && - scope.block.type !== Constants.PROCEDURES_DEFINITION_BLOCK_TYPE + scope.block.isDeletable() ) { return "enabled"; }