From 0ca0620aad343cdbb1d632ddf3ae73a70085aa03 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 12 Aug 2024 09:22:43 -0700 Subject: [PATCH] fix: enable dragging arguments out of procedure blocks (#119) --- blocks_vertical/procedures.js | 36 +++++++++++++++++++++++++++++++++++ src/index.js | 1 + src/scratch_dragger.js | 20 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 src/scratch_dragger.js diff --git a/blocks_vertical/procedures.js b/blocks_vertical/procedures.js index 7fb54c9477..cdde28ff1f 100644 --- a/blocks_vertical/procedures.js +++ b/blocks_vertical/procedures.js @@ -26,6 +26,40 @@ import * as Blockly from "blockly/core"; import { Colours } from "../core/colours.js"; import { FieldTextInputRemovable } from "../core/field_textinput_removable.js"; +class DuplicateOnDragDraggable { + constructor(block) { + this.block = block; + } + + isMovable() { + return true; + } + + startDrag(e) { + const data = this.block.toCopyData(); + this.copy = Blockly.clipboard.paste(data, this.block.workspace); + this.baseStrat = new Blockly.dragging.BlockDragStrategy(this.copy); + this.copy.setDragStrategy(this.baseStrat); + this.baseStrat.startDrag(e); + } + + drag(e) { + this.block.workspace + .getGesture(e) + .getCurrentDragger() + .setDraggable(this.copy); + this.baseStrat.drag(e); + } + + endDrag(e) { + this.baseStrat?.endDrag(e); + } + + revertDrag(e) { + this.copy?.dispose(); + } +} + // Serialization and deserialization. /** @@ -918,6 +952,7 @@ Blockly.Blocks["argument_reporter_boolean"] = { ], extensions: ["colours_more", "output_boolean"], }); + this.setDragStrategy(new DuplicateOnDragDraggable(this)); }, }; @@ -934,6 +969,7 @@ Blockly.Blocks["argument_reporter_string_number"] = { ], extensions: ["colours_more", "output_number", "output_string"], }); + this.setDragStrategy(new DuplicateOnDragDraggable(this)); }, }; diff --git a/src/index.js b/src/index.js index 4bc54f1df9..acaea58aec 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,7 @@ import { buildGlowFilter, glowStack } from "./glows.js"; import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox.js"; import "./scratch_continuous_category.js"; import "./scratch_comment_icon.js"; +import "./scratch_dragger.js"; import "./scratch_variable_model.js"; import "./scratch_connection_checker.js"; import "./events_block_comment_change.js"; diff --git a/src/scratch_dragger.js b/src/scratch_dragger.js new file mode 100644 index 0000000000..66f4be8bc7 --- /dev/null +++ b/src/scratch_dragger.js @@ -0,0 +1,20 @@ +/** + * @license + * Copyright 2024 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as Blockly from "blockly/core"; + +class ScratchDragger extends Blockly.dragging.Dragger { + setDraggable(draggable) { + this.draggable = draggable; + } +} + +Blockly.registry.register( + Blockly.registry.Type.BLOCK_DRAGGER, + Blockly.registry.DEFAULT, + ScratchDragger, + true +);