Skip to content

Commit

Permalink
keep track of state for whether the block or bubble would be deleted …
Browse files Browse the repository at this point in the history
…for use with drag enter exit
  • Loading branch information
kozbial committed Jun 10, 2021
1 parent 1139034 commit a8429fa
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
11 changes: 5 additions & 6 deletions core/block_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,20 @@ Blockly.BlockDragger.prototype.fireDragStartEvent_ = function() {
Blockly.BlockDragger.prototype.dragBlock = function(e, currentDragDeltaXY) {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);

this.draggingBlock_.moveDuringDrag(newLoc);
this.dragIcons_(delta);

var oldDragTarget = this.dragTarget_;
this.dragTarget_ = this.workspace_.getDragTarget(e);
if (this.dragTarget_ !== oldDragTarget) {
oldDragTarget && oldDragTarget.onDragExit();
this.dragTarget_ && this.dragTarget_.onDragEnter();
}

this.draggedConnectionManager_.update(delta, this.dragTarget_);
this.wouldDeleteBlock_ = this.draggedConnectionManager_.wouldDeleteBlock();

this.updateCursorDuringBlockDrag_();

if (this.dragTarget_ !== oldDragTarget) {
oldDragTarget && oldDragTarget.onDragExit();
this.dragTarget_ && this.dragTarget_.onDragEnter();
}
};

/**
Expand Down
8 changes: 4 additions & 4 deletions core/bubble_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ Blockly.BubbleDragger.prototype.startBubbleDrag = function() {
Blockly.BubbleDragger.prototype.dragBubble = function(e, currentDragDeltaXY) {
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);

this.draggingBubble_.moveDuringDrag(this.dragSurface_, newLoc);

var oldDragTarget = this.dragTarget_;
this.dragTarget_ = this.workspace_.getDragTarget(e);

this.wouldDeleteBubble_ = this.shouldDelete_(this.dragTarget_);
this.updateCursorDuringBubbleDrag_();

if (this.dragTarget_ !== oldDragTarget) {
oldDragTarget && oldDragTarget.onDragExit();
this.dragTarget_ && this.dragTarget_.onDragEnter();
}
this.wouldDeleteBubble_ = this.shouldDelete_(this.dragTarget_);

this.updateCursorDuringBubbleDrag_();
};

/**
Expand Down
64 changes: 63 additions & 1 deletion core/delete_area.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ goog.require('Blockly.IDeleteArea');
*/
Blockly.DeleteArea = function() {
Blockly.DeleteArea.superClass_.constructor.call(this);

/**
* Whether the current block or bubble dragged over this delete area would be
* deleted if dropped on this component.
* @type {boolean}
* @protected
*/
this.wouldDelete_ = false;
};
Blockly.utils.object.inherits(Blockly.DeleteArea, Blockly.DragTarget);

Expand All @@ -39,7 +47,8 @@ Blockly.utils.object.inherits(Blockly.DeleteArea, Blockly.DragTarget);
* this area.
*/
Blockly.DeleteArea.prototype.wouldDeleteBlock = function(_block, couldConnect) {
return !couldConnect;
this.wouldDelete_ = !couldConnect;
return this.wouldDelete_;
};

/**
Expand All @@ -49,5 +58,58 @@ Blockly.DeleteArea.prototype.wouldDeleteBlock = function(_block, couldConnect) {
* this area.
*/
Blockly.DeleteArea.prototype.wouldDeleteBubble = function(_bubble) {
this.wouldDelete_ = true;
return true;
};

/**
* 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.DeleteArea.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.DeleteArea.prototype.shouldPreventBubbleMove = function(_bubble) {
return false;
};

/**
* Handles when a cursor with a block or bubble exits this drag target.
* @override
*/
Blockly.DeleteArea.prototype.onDragExit = function() {
this.wouldDelete_ = false;
};

/**
* Handles when a block is dropped on this component. Should not handle delete
* here.
* @param {!Blockly.BlockSvg} _block The block.
* @override
*/
Blockly.DeleteArea.prototype.onBlockDrop = function(_block) {
this.wouldDelete_ = false;
};

/**
* Handles when a bubble is dropped on this component. Should not handle delete
* here.
* @param {!Blockly.IBubble} _bubble The bubble.
* @override
*/
Blockly.DeleteArea.prototype.onBubbleDrop = function(_bubble) {
this.wouldDelete_ = false;
};
4 changes: 3 additions & 1 deletion core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,12 @@ Blockly.Toolbox.prototype.getClientRect = function() {
* another.
* @return {boolean} Whether the block provided would be deleted if dropped on
* this area.
* @override
*/
Blockly.Toolbox.prototype.wouldDeleteBlock = function(_block, _couldConnect) {
// Prefer dragging to the toolbox over connecting to other blocks.
return true;
this.wouldDelete_ = true;
return this.wouldDelete_;
};

/**
Expand Down
19 changes: 12 additions & 7 deletions core/trashcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,35 +529,40 @@ Blockly.Trashcan.prototype.getClientRect = function() {
* @override
*/
Blockly.Trashcan.prototype.onDragEnter = function() {
this.setLidOpen(true);
Blockly.Trashcan.superClass_.onDragEnter.call(this);
if (this.wouldDelete_) {
this.setLidOpen(true);
}
};

/**
* Handles when a cursor with a block or bubble exits this drag target.
* @override
*/
Blockly.Trashcan.prototype.onDragExit = function() {
Blockly.Trashcan.superClass_.onDragExit.call(this);
this.setLidOpen(false);
};


/**
* Handles when a block is dropped on this component. Should not handle delete
* here.
* @param {!Blockly.BlockSvg} _block The block.
* @param {!Blockly.BlockSvg} block The block.
* @override
*/
Blockly.Trashcan.prototype.onBlockDrop = function(_block) {
Blockly.Trashcan.prototype.onBlockDrop = function(block) {
Blockly.Trashcan.superClass_.onBlockDrop.call(this, block);
this.onDrop_();
};

/**
* Handles when a bubble is dropped on this component. Should not handle delete
* here.
* @param {!Blockly.IBubble} _bubble The bubble.
* @param {!Blockly.IBubble} bubble The bubble.
* @override
*/
Blockly.Trashcan.prototype.onBubbleDrop = function(_bubble) {
Blockly.Trashcan.prototype.onBubbleDrop = function(bubble) {
Blockly.Trashcan.superClass_.onBubbleDrop.call(this, bubble);
this.onDrop_();
};

Expand All @@ -566,7 +571,7 @@ Blockly.Trashcan.prototype.onBubbleDrop = function(_bubble) {
* @private
*/
Blockly.Trashcan.prototype.onDrop_ = function() {
setTimeout(this.closeLid.bind(this), 100);
setTimeout(this.setLidOpen.bind(this, false), 100);
};

/**
Expand Down

0 comments on commit a8429fa

Please sign in to comment.