mirror of
https://github.com/google/blockly.git
synced 2026-01-08 09:30:06 +01:00
* Remove cargo-culted bloat from CSS The `goog-menuitem-icon` and `goog-menuitem-noicon` classes are not present in Blockly. Blockly doesn’t support the CSS compiler, so #noflip has no effect. Shorten uncompressible warning string. Also remove the “Copied from Closure” notes. These were intended so that the CSS could be easily updated as the Closure Library evolved. We are no longer linked to the Closure Library. * Fix bug (in prod) where menu highlighting is lost Previously, open playground. Right-click on workspace. Mouse-over “Add comment” (it highlights). Mouse over “Download screenshot” (disabled option). Mouse over “Add comment” (highlighting is lost). Also remove `canHighlightItem` helper function. In theory this helps abstract the concept of non-highlightable options. But in practice it was only called in one of the several places that it should have been. This was a false abstraction. * Add support for Space/PgUp/PgDn/Home/End to menus * Eliminate calls to clearHighlighted The JSDoc for `setHighlightedIndex` specifically states, “If another item was previously highlighted, it is un-highlighted.” This is not what was implemented, but it should be. This commit adds the un-highlighting, and removes all the calls previously required to correct this bug. * Stop wrapping at top or bottom of menu. Real OS menus don’t wrap when one cursors off the top or bottom. Also, replace the overly complicated helper function with a simple 1/-1 step value. * Remove unused menu code * Simplify menu roles Remove unneeded sets to RTL on Menu (only MenuItem cares). * Fix lack of disposal for context menus. Context menus only disposed properly when an option was clicked. If they were dismissed by clicking outside the menu there was no disposal. This might result in a memory leak. Also un-extract (inject?) several now trivial functions. * Remove Component dependency from Menu & MenuItem Component is now only used by the category tree. * Remove unused functions in Component These were used by Menu/MenuItem. * Fix dependencies. * Record highlighted menu item by object, not index Less code, simpler. * Rename CSS classes goog-menu* to blocklyMenu* Old classes remain in DOM and are deprecated so that any custom CSS will continue to function. * Remove unused focus tracker in tree. * Add support for space/enter to toggle tree cats * Delete unsettable .isUserCollapsible_ from tree * Change visibility tags throughout menus. The previous tags were inherited from Closure and don’t reflect current usage in the Blockly codebase. The core/components/tree files are non-compliant in this regard, but I’m not going to update them since they need to be replaced and there’s no need to create an interim API change. * Remove property on DOM element linking to JS obj Performance is slower (O(n) rather than (O(1)), but ’n’ is the number of entries on the menu, so shouldn’t be more than a dozen or so. * Fixes a compile error (node != element) Usually we avoid parentElement in Blockly. That’s because it has very spotty behaviour with SVG. But in this case we are in pure HTML.
378 lines
12 KiB
JavaScript
378 lines
12 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2017 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Methods for dragging a block visually.
|
|
* @author fenichel@google.com (Rachel Fenichel)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.BlockDragger');
|
|
|
|
goog.require('Blockly.blockAnimations');
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.Events.BlockMove');
|
|
goog.require('Blockly.Events.Ui');
|
|
goog.require('Blockly.InsertionMarkerManager');
|
|
goog.require('Blockly.utils.Coordinate');
|
|
goog.require('Blockly.utils.dom');
|
|
|
|
|
|
/**
|
|
* Class for a block dragger. It moves blocks around the workspace when they
|
|
* are being dragged by a mouse or touch.
|
|
* @param {!Blockly.BlockSvg} block The block to drag.
|
|
* @param {!Blockly.WorkspaceSvg} workspace The workspace to drag on.
|
|
* @constructor
|
|
*/
|
|
Blockly.BlockDragger = function(block, workspace) {
|
|
/**
|
|
* The top block in the stack that is being dragged.
|
|
* @type {!Blockly.BlockSvg}
|
|
* @private
|
|
*/
|
|
this.draggingBlock_ = block;
|
|
|
|
/**
|
|
* The workspace on which the block is being dragged.
|
|
* @type {!Blockly.WorkspaceSvg}
|
|
* @private
|
|
*/
|
|
this.workspace_ = workspace;
|
|
|
|
/**
|
|
* Object that keeps track of connections on dragged blocks.
|
|
* @type {!Blockly.InsertionMarkerManager}
|
|
* @private
|
|
*/
|
|
this.draggedConnectionManager_ = new Blockly.InsertionMarkerManager(
|
|
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}
|
|
* @private
|
|
*/
|
|
this.deleteArea_ = null;
|
|
|
|
/**
|
|
* Whether the block would be deleted if dropped immediately.
|
|
* @type {boolean}
|
|
* @private
|
|
*/
|
|
this.wouldDeleteBlock_ = false;
|
|
|
|
/**
|
|
* The location of the top left corner of the dragging block at the beginning
|
|
* of the drag in workspace coordinates.
|
|
* @type {!Blockly.utils.Coordinate}
|
|
* @private
|
|
*/
|
|
this.startXY_ = this.draggingBlock_.getRelativeToSurfaceXY();
|
|
|
|
/**
|
|
* A list of all of the icons (comment, warning, and mutator) that are
|
|
* on this block and its descendants. Moving an icon moves the bubble that
|
|
* extends from it if that bubble is open.
|
|
* @type {Array.<!Object>}
|
|
* @private
|
|
*/
|
|
this.dragIconData_ = Blockly.BlockDragger.initIconData_(block);
|
|
};
|
|
|
|
/**
|
|
* Sever all links from this object.
|
|
* @package
|
|
*/
|
|
Blockly.BlockDragger.prototype.dispose = function() {
|
|
this.dragIconData_.length = 0;
|
|
|
|
if (this.draggedConnectionManager_) {
|
|
this.draggedConnectionManager_.dispose();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Make a list of all of the icons (comment, warning, and mutator) that are
|
|
* on this block and its descendants. Moving an icon moves the bubble that
|
|
* extends from it if that bubble is open.
|
|
* @param {!Blockly.BlockSvg} block The root block that is being dragged.
|
|
* @return {!Array.<!Object>} The list of all icons and their locations.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.initIconData_ = function(block) {
|
|
// Build a list of icons that need to be moved and where they started.
|
|
var dragIconData = [];
|
|
var descendants = block.getDescendants(false);
|
|
for (var i = 0, descendant; (descendant = descendants[i]); i++) {
|
|
var icons = descendant.getIcons();
|
|
for (var j = 0; j < icons.length; j++) {
|
|
var data = {
|
|
// Blockly.utils.Coordinate with x and y properties (workspace coordinates).
|
|
location: icons[j].getIconLocation(),
|
|
// Blockly.Icon
|
|
icon: icons[j]
|
|
};
|
|
dragIconData.push(data);
|
|
}
|
|
}
|
|
return dragIconData;
|
|
};
|
|
|
|
/**
|
|
* Start dragging a block. This includes moving it to the drag surface.
|
|
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
|
|
* moved from the position at mouse down, in pixel units.
|
|
* @param {boolean} healStack Whether or not to heal the stack after
|
|
* disconnecting.
|
|
* @package
|
|
*/
|
|
Blockly.BlockDragger.prototype.startBlockDrag = function(currentDragDeltaXY,
|
|
healStack) {
|
|
if (!Blockly.Events.getGroup()) {
|
|
Blockly.Events.setGroup(true);
|
|
}
|
|
this.fireDragStartEvent_();
|
|
|
|
// Mutators don't have the same type of z-ordering as the normal workspace
|
|
// during a drag. They have to rely on the order of the blocks in the SVG.
|
|
// For performance reasons that usually happens at the end of a drag,
|
|
// but do it at the beginning for mutators.
|
|
if (this.workspace_.isMutator) {
|
|
this.draggingBlock_.bringToFront();
|
|
}
|
|
|
|
// During a drag there may be a lot of rerenders, but not field changes.
|
|
// Turn the cache on so we don't do spurious remeasures during the drag.
|
|
Blockly.utils.dom.startTextWidthCache();
|
|
this.workspace_.setResizesEnabled(false);
|
|
Blockly.blockAnimations.disconnectUiStop();
|
|
|
|
if (this.draggingBlock_.getParent() ||
|
|
(healStack && this.draggingBlock_.nextConnection &&
|
|
this.draggingBlock_.nextConnection.targetBlock())) {
|
|
this.draggingBlock_.unplug(healStack);
|
|
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
|
|
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
|
|
|
|
this.draggingBlock_.translate(newLoc.x, newLoc.y);
|
|
Blockly.blockAnimations.disconnectUiEffect(this.draggingBlock_);
|
|
}
|
|
this.draggingBlock_.setDragging(true);
|
|
// For future consideration: we may be able to put moveToDragSurface inside
|
|
// the block dragger, which would also let the block not track the block drag
|
|
// surface.
|
|
this.draggingBlock_.moveToDragSurface();
|
|
|
|
var toolbox = this.workspace_.getToolbox();
|
|
if (toolbox) {
|
|
var style = this.draggingBlock_.isDeletable() ? 'blocklyToolboxDelete' :
|
|
'blocklyToolboxGrab';
|
|
toolbox.addStyle(style);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Fire a UI event at the start of a block drag.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.prototype.fireDragStartEvent_ = function() {
|
|
var event = new Blockly.Events.Ui(this.draggingBlock_, 'dragStart',
|
|
null, this.draggingBlock_.getDescendants(false));
|
|
Blockly.Events.fire(event);
|
|
};
|
|
|
|
/**
|
|
* Execute a step of block dragging, based on the given event. Update the
|
|
* display accordingly.
|
|
* @param {!Event} e The most recent move event.
|
|
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
|
|
* moved from the position at the start of the drag, in pixel units.
|
|
* @package
|
|
*/
|
|
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);
|
|
|
|
this.deleteArea_ = this.workspace_.isDeleteArea(e);
|
|
this.draggedConnectionManager_.update(delta, this.deleteArea_);
|
|
|
|
this.updateCursorDuringBlockDrag_();
|
|
};
|
|
|
|
/**
|
|
* Finish a block drag and put the block back on the workspace.
|
|
* @param {!Event} e The mouseup/touchend event.
|
|
* @param {!Blockly.utils.Coordinate} currentDragDeltaXY How far the pointer has
|
|
* moved from the position at the start of the drag, in pixel units.
|
|
* @package
|
|
*/
|
|
Blockly.BlockDragger.prototype.endBlockDrag = function(e, currentDragDeltaXY) {
|
|
// Make sure internal state is fresh.
|
|
this.dragBlock(e, currentDragDeltaXY);
|
|
this.dragIconData_ = [];
|
|
this.fireDragEndEvent_();
|
|
|
|
Blockly.utils.dom.stopTextWidthCache();
|
|
|
|
Blockly.blockAnimations.disconnectUiStop();
|
|
|
|
var delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY);
|
|
var newLoc = Blockly.utils.Coordinate.sum(this.startXY_, delta);
|
|
this.draggingBlock_.moveOffDragSurface(newLoc);
|
|
|
|
var deleted = this.maybeDeleteBlock_();
|
|
if (!deleted) {
|
|
// 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);
|
|
this.fireMoveEvent_();
|
|
if (this.draggedConnectionManager_.wouldConnectBlock()) {
|
|
// Applying connections also rerenders the relevant blocks.
|
|
this.draggedConnectionManager_.applyConnections();
|
|
} else {
|
|
this.draggingBlock_.render();
|
|
}
|
|
this.draggingBlock_.scheduleSnapAndBump();
|
|
}
|
|
this.workspace_.setResizesEnabled(true);
|
|
|
|
var toolbox = this.workspace_.getToolbox();
|
|
if (toolbox) {
|
|
var style = this.draggingBlock_.isDeletable() ? 'blocklyToolboxDelete' :
|
|
'blocklyToolboxGrab';
|
|
toolbox.removeStyle(style);
|
|
}
|
|
Blockly.Events.setGroup(false);
|
|
};
|
|
|
|
/**
|
|
* Fire a UI event at the end of a block drag.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.prototype.fireDragEndEvent_ = function() {
|
|
var event = new Blockly.Events.Ui(this.draggingBlock_, 'dragStop',
|
|
this.draggingBlock_.getDescendants(false), null);
|
|
Blockly.Events.fire(event);
|
|
};
|
|
|
|
/**
|
|
* Fire a move event at the end of a block drag.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.prototype.fireMoveEvent_ = function() {
|
|
var event = new Blockly.Events.BlockMove(this.draggingBlock_);
|
|
event.oldCoordinate = this.startXY_;
|
|
event.recordNew();
|
|
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.close.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 is closed.
|
|
trashcan.close();
|
|
}
|
|
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();
|
|
var trashcan = this.workspace_.trashcan;
|
|
if (this.wouldDeleteBlock_) {
|
|
this.draggingBlock_.setDeleteStyle(true);
|
|
if (this.deleteArea_ == Blockly.DELETE_AREA_TRASH && trashcan) {
|
|
trashcan.setOpen(true);
|
|
}
|
|
} else {
|
|
this.draggingBlock_.setDeleteStyle(false);
|
|
if (trashcan) {
|
|
trashcan.setOpen(false);
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Convert a coordinate object from pixels to workspace units, including a
|
|
* correction for mutator workspaces.
|
|
* This function does not consider differing origins. It simply scales the
|
|
* input's x and y values.
|
|
* @param {!Blockly.utils.Coordinate} pixelCoord A coordinate with x and y values
|
|
* in CSS pixel units.
|
|
* @return {!Blockly.utils.Coordinate} The input coordinate divided by the workspace
|
|
* scale.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.prototype.pixelsToWorkspaceUnits_ = function(pixelCoord) {
|
|
var result = new Blockly.utils.Coordinate(
|
|
pixelCoord.x / this.workspace_.scale,
|
|
pixelCoord.y / this.workspace_.scale);
|
|
if (this.workspace_.isMutator) {
|
|
// If we're in a mutator, its scale is always 1, purely because of some
|
|
// oddities in our rendering optimizations. The actual scale is the same as
|
|
// the scale on the parent workspace.
|
|
// Fix that for dragging.
|
|
var mainScale = this.workspace_.options.parentWorkspace.scale;
|
|
result.scale(1 / mainScale);
|
|
}
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* Move all of the icons connected to this drag.
|
|
* @param {!Blockly.utils.Coordinate} dxy How far to move the icons from their
|
|
* original positions, in workspace units.
|
|
* @private
|
|
*/
|
|
Blockly.BlockDragger.prototype.dragIcons_ = function(dxy) {
|
|
// Moving icons moves their associated bubbles.
|
|
for (var i = 0; i < this.dragIconData_.length; i++) {
|
|
var data = this.dragIconData_[i];
|
|
data.icon.setIconLocation(Blockly.utils.Coordinate.sum(data.location, dxy));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get a list of the insertion markers that currently exist. Drags have 0, 1,
|
|
* or 2 insertion markers.
|
|
* @return {!Array.<!Blockly.BlockSvg>} A possibly empty list of insertion
|
|
* marker blocks.
|
|
* @package
|
|
*/
|
|
Blockly.BlockDragger.prototype.getInsertionMarkers = function() {
|
|
// No insertion markers with the old style of dragged connection managers.
|
|
if (this.draggedConnectionManager_ &&
|
|
this.draggedConnectionManager_.getInsertionMarkers) {
|
|
return this.draggedConnectionManager_.getInsertionMarkers();
|
|
}
|
|
return [];
|
|
};
|