Commit bfd3834e authored by carlosperate's avatar carlosperate

Offset pasted block if in range of a snap connection.

parent d8a72df1
......@@ -396,7 +396,8 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
if (this.RTL) {
blockX = -blockX;
}
// Offset block until not clobbering another block.
// Offset block until not clobbering another block and not in connection
// distance with neighbouring blocks.
do {
var collide = false;
var allBlocks = this.getAllBlocks();
......@@ -404,15 +405,29 @@ Blockly.WorkspaceSvg.prototype.paste = function(xmlBlock) {
var otherXY = otherBlock.getRelativeToSurfaceXY();
if (Math.abs(blockX - otherXY.x) <= 1 &&
Math.abs(blockY - otherXY.y) <= 1) {
if (this.RTL) {
blockX -= Blockly.SNAP_RADIUS;
} else {
blockX += Blockly.SNAP_RADIUS;
}
blockY += Blockly.SNAP_RADIUS * 2;
collide = true;
}
}
if (!collide) {
// Check for blocks in snap range to any of its connections
var blockConnections = block.getConnections_(false);
for (var i = 0; i < blockConnections.length; i++) {
var blockConnection = blockConnections[i];
var neighbour =
blockConnection.closest(Blockly.SNAP_RADIUS, blockX, blockY);
if (neighbour.connection) {
collide = true;
}
}
}
if (collide) {
if (this.RTL) {
blockX -= Blockly.SNAP_RADIUS;
} else {
blockX += Blockly.SNAP_RADIUS;
}
blockY += Blockly.SNAP_RADIUS * 2;
}
} while (collide);
block.moveBy(blockX, blockY);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment