Skip to content

Commit 0ca0620

Browse files
authored
fix: enable dragging arguments out of procedure blocks (#119)
1 parent 453ffa9 commit 0ca0620

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

blocks_vertical/procedures.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,40 @@ import * as Blockly from "blockly/core";
2626
import { Colours } from "../core/colours.js";
2727
import { FieldTextInputRemovable } from "../core/field_textinput_removable.js";
2828

29+
class DuplicateOnDragDraggable {
30+
constructor(block) {
31+
this.block = block;
32+
}
33+
34+
isMovable() {
35+
return true;
36+
}
37+
38+
startDrag(e) {
39+
const data = this.block.toCopyData();
40+
this.copy = Blockly.clipboard.paste(data, this.block.workspace);
41+
this.baseStrat = new Blockly.dragging.BlockDragStrategy(this.copy);
42+
this.copy.setDragStrategy(this.baseStrat);
43+
this.baseStrat.startDrag(e);
44+
}
45+
46+
drag(e) {
47+
this.block.workspace
48+
.getGesture(e)
49+
.getCurrentDragger()
50+
.setDraggable(this.copy);
51+
this.baseStrat.drag(e);
52+
}
53+
54+
endDrag(e) {
55+
this.baseStrat?.endDrag(e);
56+
}
57+
58+
revertDrag(e) {
59+
this.copy?.dispose();
60+
}
61+
}
62+
2963
// Serialization and deserialization.
3064

3165
/**
@@ -918,6 +952,7 @@ Blockly.Blocks["argument_reporter_boolean"] = {
918952
],
919953
extensions: ["colours_more", "output_boolean"],
920954
});
955+
this.setDragStrategy(new DuplicateOnDragDraggable(this));
921956
},
922957
};
923958

@@ -934,6 +969,7 @@ Blockly.Blocks["argument_reporter_string_number"] = {
934969
],
935970
extensions: ["colours_more", "output_number", "output_string"],
936971
});
972+
this.setDragStrategy(new DuplicateOnDragDraggable(this));
937973
},
938974
};
939975

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { buildGlowFilter, glowStack } from "./glows.js";
3737
import { ScratchContinuousToolbox } from "./scratch_continuous_toolbox.js";
3838
import "./scratch_continuous_category.js";
3939
import "./scratch_comment_icon.js";
40+
import "./scratch_dragger.js";
4041
import "./scratch_variable_model.js";
4142
import "./scratch_connection_checker.js";
4243
import "./events_block_comment_change.js";

src/scratch_dragger.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import * as Blockly from "blockly/core";
8+
9+
class ScratchDragger extends Blockly.dragging.Dragger {
10+
setDraggable(draggable) {
11+
this.draggable = draggable;
12+
}
13+
}
14+
15+
Blockly.registry.register(
16+
Blockly.registry.Type.BLOCK_DRAGGER,
17+
Blockly.registry.DEFAULT,
18+
ScratchDragger,
19+
true
20+
);

0 commit comments

Comments
 (0)