diff --git a/src/context_menu_items.ts b/src/context_menu_items.ts index d79a114ae2..0ebae9f991 100644 --- a/src/context_menu_items.ts +++ b/src/context_menu_items.ts @@ -154,3 +154,27 @@ function deleteNext(deleteList: Blockly.BlockSvg[], eventGroup?: string) { } Blockly.Events.setGroup(false); } + +/** + * Registers a block duplicate option that duplicates the selected block and + * all subsequent blocks in the stack. + */ +export function registerDuplicateBlock() { + const original = + Blockly.ContextMenuRegistry.registry.getItem("blockDuplicate"); + const duplicateOption = { + displayText: original.displayText, + preconditionFn: original.preconditionFn, + callback(scope: Blockly.ContextMenuRegistry.Scope) { + if (!scope.block) return; + const data = scope.block.toCopyData(true); + if (!data) return; + Blockly.clipboard.paste(data, scope.block.workspace); + }, + scopeType: original.scopeType, + id: original.id, + weight: original.weight, + }; + Blockly.ContextMenuRegistry.registry.unregister(duplicateOption.id); + Blockly.ContextMenuRegistry.registry.register(duplicateOption); +} diff --git a/src/index.ts b/src/index.ts index f86ffef276..62a88de076 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,6 +129,7 @@ Blockly.ContextMenuRegistry.registry.unregister("blockInline"); Blockly.ContextMenuItems.registerCommentOptions(); Blockly.ContextMenuRegistry.registry.unregister("blockDelete"); contextMenuItems.registerDeleteBlock(); +contextMenuItems.registerDuplicateBlock(); Blockly.ContextMenuRegistry.registry.unregister("workspaceDelete"); contextMenuItems.registerDeleteAll(); Blockly.comments.CommentView.defaultCommentSize = new Blockly.utils.Size(