Propagate the event group when enforcing bounds on blocks (#2163)

Fixes #1653

Makes the bounds checker created in inject correctly set the event group
before moving blocks in bounds.

When a workspace can't scroll the inject method sets up an event listener
to bump blocks back into the visible bounds when dropped off the edge. The
event group wasn't being used, though, causing the move to be a separate
undo step. When undone, this would get you stuck in a loop where the
workspace would keep pulling it back into the visible range.
This commit is contained in:
RoboErikG
2018-12-12 13:03:25 -08:00
committed by GitHub
parent 8422f931f1
commit e49d07dd16

View File

@@ -224,7 +224,7 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface,
Blockly.mainWorkspace = mainWorkspace;
if (!options.readOnly && !options.hasScrollbars) {
var workspaceChanged = function() {
var workspaceChanged = function(e) {
if (!mainWorkspace.isDragging()) {
var metrics = mainWorkspace.getMetrics();
var edgeLeft = metrics.viewLeft + metrics.absoluteLeft;
@@ -239,6 +239,12 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface,
// One or more blocks may be out of bounds. Bump them back in.
var MARGIN = 25;
var blocks = mainWorkspace.getTopBlocks(false);
var oldGroup = null;
if (e) {
oldGroup = Blockly.Events.getGroup();
Blockly.Events.setGroup(e.group);
}
var movedBlocks = false;
for (var b = 0, block; block = blocks[b]; b++) {
var blockXY = block.getRelativeToSurfaceXY();
var blockHW = block.getHeightWidth();
@@ -246,26 +252,37 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface,
var overflowTop = edgeTop + MARGIN - blockHW.height - blockXY.y;
if (overflowTop > 0) {
block.moveBy(0, overflowTop);
movedBlocks = true;
}
// Bump any block that's below the bottom back inside.
var overflowBottom =
edgeTop + metrics.viewHeight - MARGIN - blockXY.y;
if (overflowBottom < 0) {
block.moveBy(0, overflowBottom);
movedBlocks = true;
}
// Bump any block that's off the left back inside.
var overflowLeft = MARGIN + edgeLeft -
blockXY.x - (options.RTL ? 0 : blockHW.width);
if (overflowLeft > 0) {
block.moveBy(overflowLeft, 0);
movedBlocks = true;
}
// Bump any block that's off the right back inside.
var overflowRight = edgeLeft + metrics.viewWidth - MARGIN -
blockXY.x + (options.RTL ? blockHW.width : 0);
if (overflowRight < 0) {
block.moveBy(overflowRight, 0);
movedBlocks = true;
}
}
if (e) {
if (!e.group && movedBlocks) {
console.log('WARNING: Moved blocks in bounds but there was no event group.'
+ ' This may break undo.');
}
Blockly.Events.setGroup(oldGroup);
}
}
}
};