Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding IDragTarget support. #4852

Merged
merged 26 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions blockly_uncompressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 27 additions & 42 deletions core/block_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ goog.require('Blockly.utils.Coordinate');
goog.require('Blockly.utils.dom');

goog.requireType('Blockly.BlockSvg');
goog.requireType('Blockly.IDragTarget');
goog.requireType('Blockly.WorkspaceSvg');


Expand Down Expand Up @@ -59,13 +60,11 @@ Blockly.BlockDragger = function(block, workspace) {
this.draggingBlock_);

/**
* Which delete area the mouse pointer is over, if any.
* One of {@link Blockly.DELETE_AREA_TRASH},
* {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
* @type {?number}
* Which drag area the mouse pointer is over, if any.
* @type {?Blockly.IDragTarget}
* @private
*/
this.deleteArea_ = null;
this.dragTarget_ = null;

/**
* Whether the block would be deleted if dropped immediately.
Expand Down Expand Up @@ -210,8 +209,15 @@ Blockly.BlockDragger.prototype.dragBlock = function(e, currentDragDeltaXY) {
this.draggingBlock_.moveDuringDrag(newLoc);
this.dragIcons_(delta);

this.deleteArea_ = this.workspace_.isDeleteArea(e);
this.draggedConnectionManager_.update(delta, this.deleteArea_);
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_();
};
Expand All @@ -234,11 +240,22 @@ Blockly.BlockDragger.prototype.endBlockDrag = function(e, currentDragDeltaXY) {
Blockly.blockAnimations.disconnectUiStop();

var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
var newLoc = this.dragTarget_ ?
this.dragTarget_.getEndDragLoc(this.startXY_, delta) :
Blockly.utils.Coordinate.sum(this.startXY_, delta);
delta = Blockly.utils.Coordinate.difference(newLoc, this.startXY_);
this.draggingBlock_.moveOffDragSurface(newLoc);

var deleted = this.maybeDeleteBlock_();
if (!deleted) {
if (this.dragTarget_) {
this.dragTarget_.onBlockDrop(this.draggingBlock_);
}

if (this.wouldDeleteBlock_) {
// Fire a move event, so we know where to go back to for an undo.
this.fireMoveEvent_();
this.draggingBlock_.dispose(false, true);
Blockly.draggingConnections = [];
} else {
// These are expensive and don't need to be done if we're deleting.
this.draggingBlock_.moveConnections(delta.x, delta.y);
this.draggingBlock_.setDragging(false);
Expand Down Expand Up @@ -284,48 +301,16 @@ Blockly.BlockDragger.prototype.fireMoveEvent_ = function() {
Blockly.Events.fire(event);
};

/**
* Shut the trash can and, if necessary, delete the dragging block.
* Should be called at the end of a block drag.
* @return {boolean} Whether the block was deleted.
* @private
*/
Blockly.BlockDragger.prototype.maybeDeleteBlock_ = function() {
var trashcan = this.workspace_.trashcan;

if (this.wouldDeleteBlock_) {
if (trashcan) {
setTimeout(trashcan.closeLid.bind(trashcan), 100);
}
// Fire a move event, so we know where to go back to for an undo.
this.fireMoveEvent_();
this.draggingBlock_.dispose(false, true);
Blockly.draggingConnections = [];
} else if (trashcan) {
// Make sure the trash can lid is closed.
trashcan.closeLid();
}
return this.wouldDeleteBlock_;
};

/**
* Update the cursor (and possibly the trash can lid) to reflect whether the
* dragging block would be deleted if released immediately.
* @private
*/
Blockly.BlockDragger.prototype.updateCursorDuringBlockDrag_ = function() {
this.wouldDeleteBlock_ = this.draggedConnectionManager_.wouldDeleteBlock();
moniika marked this conversation as resolved.
Show resolved Hide resolved
var trashcan = this.workspace_.trashcan;
if (this.wouldDeleteBlock_) {
this.draggingBlock_.setDeleteStyle(true);
if (this.deleteArea_ == Blockly.DELETE_AREA_TRASH && trashcan) {
trashcan.setLidOpen(true);
}
} else {
this.draggingBlock_.setDeleteStyle(false);
if (trashcan) {
trashcan.setLidOpen(false);
}
}
};

Expand Down
38 changes: 22 additions & 16 deletions core/bubble_dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ Blockly.BubbleDragger = function(bubble, workspace) {
this.workspace_ = workspace;

/**
* Which delete area the mouse pointer is over, if any.
* One of {@link Blockly.DELETE_AREA_TRASH},
* {@link Blockly.DELETE_AREA_TOOLBOX}, or {@link Blockly.DELETE_AREA_NONE}.
* @type {?number}
* Which drag target the mouse pointer is over, if any.
* @type {?Blockly.IDragTarget}
* @private
*/
this.deleteArea_ = null;
this.dragTarget_ = null;

/**
* Whether the bubble would be deleted if dropped immediately.
Expand Down Expand Up @@ -136,10 +134,26 @@ Blockly.BubbleDragger.prototype.dragBubble = function(e, currentDragDeltaXY) {

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

if (this.draggingBubble_.isDeletable()) {
this.deleteArea_ = this.workspace_.isDeleteArea(e);
this.updateCursorDuringBubbleDrag_();
var oldDragTarget = this.dragTarget_;
moniika marked this conversation as resolved.
Show resolved Hide resolved
this.dragTarget_ = this.workspace_.getDragTarget(e);
if (this.dragTarget_ !== oldDragTarget) {
oldDragTarget && oldDragTarget.onDragExit();
this.dragTarget_ && this.dragTarget_.onDragEnter();
}
if (this.draggingBubble_.isDeletable() && this.dragTarget_) {
// TODO(#4881) use hasCapability instead of getComponents
var deleteAreas = this.workspace_.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.DELETE_AREA, false);
var isDeleteArea = deleteAreas.some(function(deleteArea) {
return deleteArea === this.dragTarget_;
});
this.wouldDeleteBubble_ =
isDeleteArea && this.dragTarget_.isBubbleDeleteArea;
} else {
this.wouldDeleteBubble_ = false;
}

this.updateCursorDuringBubbleDrag_();
};

/**
Expand Down Expand Up @@ -171,18 +185,10 @@ Blockly.BubbleDragger.prototype.maybeDeleteBubble_ = function() {
* @private
*/
Blockly.BubbleDragger.prototype.updateCursorDuringBubbleDrag_ = function() {
this.wouldDeleteBubble_ = this.deleteArea_ != Blockly.DELETE_AREA_NONE;
var trashcan = this.workspace_.trashcan;
if (this.wouldDeleteBubble_) {
this.draggingBubble_.setDeleteStyle(true);
if (this.deleteArea_ == Blockly.DELETE_AREA_TRASH && trashcan) {
trashcan.setLidOpen(true);
}
} else {
this.draggingBubble_.setDeleteStyle(false);
if (trashcan) {
trashcan.setLidOpen(false);
}
}
};

Expand Down
11 changes: 11 additions & 0 deletions core/component_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ goog.provide('Blockly.ComponentManager');

goog.requireType('Blockly.IAutoHideable');
goog.requireType('Blockly.IComponent');
goog.requireType('Blockly.IDeleteArea');
goog.requireType('Blockly.IDragTarget');
goog.requireType('Blockly.IPositionable');


Expand Down Expand Up @@ -150,6 +152,15 @@ Blockly.ComponentManager.Capability.prototype.toString = function() {
/** @type {!Blockly.ComponentManager.Capability<!Blockly.IPositionable>} */
Blockly.ComponentManager.Capability.POSITIONABLE =
new Blockly.ComponentManager.Capability('positionable');

moniika marked this conversation as resolved.
Show resolved Hide resolved
/** @type {!Blockly.ComponentManager.Capability<!Blockly.IDragTarget>} */
Blockly.ComponentManager.Capability.DRAG_TARGET =
new Blockly.ComponentManager.Capability('drag_target');

/** @type {!Blockly.ComponentManager.Capability<!Blockly.IDeleteArea>} */
Blockly.ComponentManager.Capability.DELETE_AREA =
new Blockly.ComponentManager.Capability('delete_area');

/** @type {!Blockly.ComponentManager.Capability<!Blockly.IAutoHideable>} */
Blockly.ComponentManager.Capability.AUTOHIDEABLE =
new Blockly.ComponentManager.Capability('autohideable');
20 changes: 0 additions & 20 deletions core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,6 @@ Blockly.OPPOSITE_TYPE[Blockly.connectionTypes.NEXT_STATEMENT] =
Blockly.OPPOSITE_TYPE[Blockly.connectionTypes.PREVIOUS_STATEMENT] =
Blockly.connectionTypes.NEXT_STATEMENT;

/**
* ENUM representing that an event is not in any delete areas.
* Null for backwards compatibility reasons.
* @const
*/
Blockly.DELETE_AREA_NONE = null;

/**
* ENUM representing that an event is in the delete area of the trash can.
* @const
*/
Blockly.DELETE_AREA_TRASH = 1;

/**
* ENUM representing that an event is in the delete area of the toolbox or
* flyout.
* @const
*/
Blockly.DELETE_AREA_TOOLBOX = 2;

/**
* String for use in the "custom" attribute of a category in toolbox XML.
* This string indicates that the category should be dynamically populated with
Expand Down
49 changes: 49 additions & 0 deletions core/delete_area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview The abstract class for a component that can delete a block that
* is dropped on top of it.
moniika marked this conversation as resolved.
Show resolved Hide resolved
* @author kozbial@google.com (Monica Kozbial)
*/

'use strict';

goog.provide('Blockly.DeleteArea');

goog.require('Blockly.DragTarget');
goog.require('Blockly.IDeleteArea');


/**
* Abstract class for a component that can delete a block that is dropped on top
* of it.
* @extends {Blockly.DragTarget}
* @implements {Blockly.IDeleteArea}
* @constructor
*/
Blockly.DeleteArea = function() {
Blockly.DeleteArea.superClass_.constructor.call(this);
};
Blockly.utils.object.inherits(Blockly.DeleteArea, Blockly.DragTarget);

/**
* Returns whether the provided block would be deleted if dropped on this area.
* @param {!Blockly.BlockSvg} _block The block.
* @param {boolean} couldConnect Whether the block could could connect to
* another.
* @return {boolean} Whether the block provided would be deleted if dropped on
* this area.
*/
Blockly.DeleteArea.prototype.wouldDeleteBlock = function(_block, couldConnect) {
return !couldConnect;
};

/**
* Whether this is a bubble delete area.
* @type {boolean}
*/
Blockly.DeleteArea.prototype.isBubbleDeleteArea = true;
moniika marked this conversation as resolved.
Show resolved Hide resolved
Loading