Skip to content

Commit

Permalink
fix: fix bug that could cause duplicated procedure argument blocks to…
Browse files Browse the repository at this point in the history
… create more duplicates on drag (#217)
  • Loading branch information
gonfunko authored Oct 15, 2024
1 parent 88c700e commit 6a1c8a9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ class DuplicateOnDragDraggable {
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);
this.copy.startDrag(e);
}

drag(e) {
this.block.workspace
.getGesture(e)
.getCurrentDragger()
.setDraggable(this.copy);
this.baseStrat.drag(e);
this.copy.drag(e);
}

endDrag(e) {
this.baseStrat?.endDrag(e);
this.copy?.endDrag(e);
}

revertDrag(e) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { registerFieldVariableGetter } from "./fields/field_variable_getter.js";
import { registerFieldVariable } from "./fields/field_variable.js";
import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator.js";
import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater.js";
import { registerScratchBlockPaster } from "./scratch_block_paster.js";
import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater.js";
import { registerScratchContinuousCategory } from "./scratch_continuous_category.js";

Expand Down Expand Up @@ -94,6 +95,7 @@ export function inject(container, options) {
registerFieldVariable();
registerFieldVerticalSeparator();
registerRecyclableBlockFlyoutInflater();
registerScratchBlockPaster();
registerStatusIndicatorLabelFlyoutInflater();
registerScratchContinuousCategory();

Expand Down
46 changes: 46 additions & 0 deletions src/scratch_block_paster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import * as Blockly from "blockly/core";

/**
* Class responsible for handling the pasting of copied blocks.
*/
class ScratchBlockPaster extends Blockly.clipboard.BlockPaster {
/**
* Deserializes the given block data onto the workspace.
*
* @param {!Blockly.clipboard.BlockCopyData} copyData The serialized block
* state to create a copy of on the workspace.
* @param {!Blockly.WorkspaceSvg} workspace The workspace to paste the block
* onto.
* @param {?Blockly.utils.Coordinate} coordinate The location to paste the
* block.
*/
paste(copyData, workspace, coordinate) {
const block = super.paste(copyData, workspace, coordinate);
if (
block?.type === "argument_reporter_boolean" ||
block?.type === "argument_reporter_string_number"
) {
block.setDragStrategy(new Blockly.dragging.BlockDragStrategy(block));
}

return block;
}
}

/**
* Unregisters the default block paster and registers ScratchBlockPaster in its
* place.
*/
export function registerScratchBlockPaster() {
Blockly.clipboard.registry.unregister(ScratchBlockPaster.TYPE);
Blockly.clipboard.registry.register(
ScratchBlockPaster.TYPE,
new ScratchBlockPaster()
);
}

0 comments on commit 6a1c8a9

Please # to comment.