Skip to content

Commit d137e87

Browse files
committed
fix: make Duplicate context menu item duplicate subsequent blocks
1 parent dc17159 commit d137e87

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/context_menu_items.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,27 @@ function deleteNext(deleteList: Blockly.BlockSvg[], eventGroup?: string) {
154154
}
155155
Blockly.Events.setGroup(false);
156156
}
157+
158+
/**
159+
* Registers a block duplicate option that duplicates the selected block and
160+
* all subsequent blocks in the stack.
161+
*/
162+
export function registerDuplicateBlock() {
163+
const original =
164+
Blockly.ContextMenuRegistry.registry.getItem("blockDuplicate");
165+
const duplicateOption = {
166+
displayText: original.displayText,
167+
preconditionFn: original.preconditionFn,
168+
callback(scope: Blockly.ContextMenuRegistry.Scope) {
169+
if (!scope.block) return;
170+
const data = scope.block.toCopyData(true);
171+
if (!data) return;
172+
Blockly.clipboard.paste(data, scope.block.workspace);
173+
},
174+
scopeType: original.scopeType,
175+
id: original.id,
176+
weight: original.weight,
177+
};
178+
Blockly.ContextMenuRegistry.registry.unregister(duplicateOption.id);
179+
Blockly.ContextMenuRegistry.registry.register(duplicateOption);
180+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Blockly.ContextMenuRegistry.registry.unregister("blockInline");
129129
Blockly.ContextMenuItems.registerCommentOptions();
130130
Blockly.ContextMenuRegistry.registry.unregister("blockDelete");
131131
contextMenuItems.registerDeleteBlock();
132+
contextMenuItems.registerDuplicateBlock();
132133
Blockly.ContextMenuRegistry.registry.unregister("workspaceDelete");
133134
contextMenuItems.registerDeleteAll();
134135
Blockly.comments.CommentView.defaultCommentSize = new Blockly.utils.Size(

0 commit comments

Comments
 (0)