Add API to IDragTarget to prevent block move (#4886)

This commit is contained in:
Monica Kozbial
2021-06-10 16:17:07 -07:00
committed by GitHub
parent 0014ad3257
commit e40093dc23
5 changed files with 76 additions and 11 deletions

View File

@@ -265,8 +265,14 @@ Blockly.BlockDragger.prototype.endDrag = function(e, currentDragDeltaXY) {
Blockly.blockAnimations.disconnectUiStop();
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
var preventMove = !!this.dragTarget_ &&
this.dragTarget_.shouldPreventBlockMove(this.draggingBlock_);
if (preventMove) {
var newLoc = this.startXY_;
} else {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
}
this.draggingBlock_.moveOffDragSurface(newLoc);
if (this.dragTarget_) {
@@ -279,8 +285,17 @@ Blockly.BlockDragger.prototype.endDrag = function(e, currentDragDeltaXY) {
this.draggingBlock_.dispose(false, true);
Blockly.draggingConnections = [];
} else {
// Moving the block is expensive, so only do it if the block is not deleted.
this.updateBlockAfterMove_(delta);
this.draggingBlock_.setDragging(false);
if (delta) { // !preventMove
this.updateBlockLocationAfterMove_(delta);
} else {
// Blocks dragged directly from a flyout may need to be bumped into
// bounds.
Blockly.bumpObjectIntoBounds_(
this.draggingBlock_.workspace,
this.workspace_.getMetricsManager()
.getScrollMetrics(true), this.draggingBlock_);
}
}
this.workspace_.setResizesEnabled(true);
@@ -293,9 +308,8 @@ Blockly.BlockDragger.prototype.endDrag = function(e, currentDragDeltaXY) {
* the block started the drag to where it ended the drag.
* @protected
*/
Blockly.BlockDragger.prototype.updateBlockAfterMove_ = function(delta) {
Blockly.BlockDragger.prototype.updateBlockLocationAfterMove_ = function(delta) {
this.draggingBlock_.moveConnections(delta.x, delta.y);
this.draggingBlock_.setDragging(false);
this.fireMoveEvent_();
if (this.draggedConnectionManager_.wouldConnectBlock()) {
// Applying connections also rerenders the relevant blocks.

View File

@@ -194,8 +194,14 @@ Blockly.BubbleDragger.prototype.endBubbleDrag = function(
// Make sure internal state is fresh.
this.dragBubble(e, currentDragDeltaXY);
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
var preventMove = this.dragTarget_ &&
this.dragTarget_.shouldPreventBubbleMove(this.draggingBubble_);
if (preventMove) {
var newLoc = this.startXY_;
} else {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
}
// Move the bubble to its final location.
this.draggingBubble_.moveTo(newLoc.x, newLoc.y);
@@ -212,8 +218,9 @@ Blockly.BubbleDragger.prototype.endBubbleDrag = function(
if (this.dragSurface_) {
this.dragSurface_.clearAndHide(this.workspace_.getBubbleCanvas());
}
this.draggingBubble_.setDragging && this.draggingBubble_.setDragging(false);
if (this.draggingBubble_) {
this.draggingBubble_.setDragging(false);
}
this.fireMoveEvent_();
}
this.workspace_.setResizesEnabled(true);

View File

@@ -68,3 +68,27 @@ Blockly.DragTarget.prototype.onBlockDrop = function(_block) {
Blockly.DragTarget.prototype.onBubbleDrop = function(_bubble) {
// no-op
};
/**
* Returns whether the provided block should not be moved after being dropped
* on this component. If true, block will return to where it was when the drag
* started.
* @param {!Blockly.BlockSvg} _block The block.
* @return {boolean} Whether the block provided should be returned to drag
* start.
*/
Blockly.DragTarget.prototype.shouldPreventBlockMove = function(_block) {
return false;
};
/**
* Returns whether the provided bubble should not be moved after being dropped
* on this component. If true, bubble will return to where it was when the drag
* started.
* @param {!Blockly.IBubble} _bubble The bubble.
* @return {boolean} Whether the bubble provided should be returned to drag
* start.
*/
Blockly.DragTarget.prototype.shouldPreventBubbleMove = function(_bubble) {
return false;
};

View File

@@ -291,7 +291,7 @@ Blockly.bumpIntoBoundsHandler_ = function(workspace) {
* in workspace coordinates.
* @param {!Blockly.IBoundedElement} object The object to bump.
* @return {boolean} True if block was bumped.
* @private
* @package
*/
Blockly.bumpObjectIntoBounds_ = function(workspace, scrollMetrics, object) {
// Compute new top/left position for object.

View File

@@ -58,3 +58,23 @@ Blockly.IDragTarget.prototype.onBlockDrop;
* @param {!Blockly.IBubble} bubble The bubble.
*/
Blockly.IDragTarget.prototype.onBubbleDrop;
/**
* Returns whether the provided block should not be moved after being dropped
* on this component. If true, block will return to where it was when the drag
* started.
* @param {!Blockly.BlockSvg} block The block.
* @return {boolean} Whether the block provided should be returned to drag
* start.
*/
Blockly.IDragTarget.prototype.shouldPreventBlockMove;
/**
* Returns whether the provided bubble should not be moved after being dropped
* on this component. If true, bubble will return to where it was when the drag
* started.
* @param {!Blockly.IBubble} bubble The bubble.
* @return {boolean} Whether the bubble provided should be returned to drag
* start.
*/
Blockly.IDragTarget.prototype.shouldPreventBubbleMove;