From e49d07dd16f7bbdae560d125c62de4c6785639a4 Mon Sep 17 00:00:00 2001 From: RoboErikG Date: Wed, 12 Dec 2018 13:03:25 -0800 Subject: [PATCH] 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. --- core/inject.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/inject.js b/core/inject.js index 5ee706d15..df7b48179 100644 --- a/core/inject.js +++ b/core/inject.js @@ -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); + } } } };