Simplify workspace.getBlocksBoundingBox

Previously it returned x,y,width,height.  Returning top,bottom,left,right results in simpler code, both in this function and in downstream callers.

This commit makes the minumum change to the metrics_test.  I’m happy to change the test’s data if that makes more sense.
This commit is contained in:
Neil Fraser
2019-06-06 21:59:37 -07:00
committed by Neil Fraser
parent a9fdf7844a
commit 775ce34eac
5 changed files with 25 additions and 32 deletions

View File

@@ -257,12 +257,10 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface,
if (mainWorkspace.isContentBounded()) {
// Already in workspace units, no need to divide by scale.
var blocksBoundingBox = mainWorkspace.getBlocksBoundingBox();
workspaceMetrics.contentLeft = blocksBoundingBox.x;
workspaceMetrics.contentTop = blocksBoundingBox.y;
workspaceMetrics.contentRight =
blocksBoundingBox.x + blocksBoundingBox.width;
workspaceMetrics.contentBottom =
blocksBoundingBox.y + blocksBoundingBox.height;
workspaceMetrics.contentLeft = blocksBoundingBox.left;
workspaceMetrics.contentTop = blocksBoundingBox.top;
workspaceMetrics.contentRight = blocksBoundingBox.right;
workspaceMetrics.contentBottom = blocksBoundingBox.bottom;
} else {
workspaceMetrics.contentLeft = defaultMetrics.contentLeft / scale;
workspaceMetrics.contentTop = defaultMetrics.contentTop / scale;

View File

@@ -430,7 +430,7 @@ Blockly.WorkspaceCommentSvg.prototype.getBoundingRectangle = function() {
var bottom = blockXY.y + commentBounds.height;
var left, right;
if (this.RTL) {
left = blockXY.x - (commentBounds.width);
left = blockXY.x - commentBounds.width;
// Add the width of the tab/puzzle piece knob to the x coordinate
// since X is the corner of the rectangle, not the whole puzzle piece.
right = blockXY.x;

View File

@@ -1361,7 +1361,7 @@ Blockly.WorkspaceSvg.prototype.getBlocksBoundingBox = function() {
var topElements = topBlocks.concat(topComments);
// There are no blocks, return empty rectangle.
if (!topElements.length) {
return {x: 0, y: 0, width: 0, height: 0};
return {top: 0, bottom: 0, left: 0, right: 0};
}
// Initialize boundary using the first block.
@@ -1383,12 +1383,7 @@ Blockly.WorkspaceSvg.prototype.getBlocksBoundingBox = function() {
boundary.bottom = blockBoundary.bottom;
}
}
return {
x: boundary.left,
y: boundary.top,
width: boundary.right - boundary.left,
height: boundary.bottom - boundary.top
};
return boundary;
};
/**
@@ -1726,8 +1721,8 @@ Blockly.WorkspaceSvg.prototype.zoomToFit = function() {
var workspaceWidth = metrics.viewWidth;
var workspaceHeight = metrics.viewHeight;
var blocksBox = this.getBlocksBoundingBox();
var blocksWidth = blocksBox.width;
var blocksHeight = blocksBox.height;
var blocksWidth = blocksBox.right - blocksBox.left;
var blocksHeight = blocksBox.bottom - blocksBox.top;
if (!blocksWidth) {
return; // Prevents zooming to infinity.
}
@@ -2018,18 +2013,18 @@ Blockly.WorkspaceSvg.getContentDimensionsExact_ = function(ws) {
var scale = ws.scale;
// Convert to pixels.
var width = blockBox.width * scale;
var height = blockBox.height * scale;
var left = blockBox.x * scale;
var top = blockBox.y * scale;
var top = blockBox.top * scale;
var bottom = blockBox.bottom * scale;
var left = blockBox.left * scale;
var right = blockBox.right * scale;
return {
left: left,
top: top,
right: left + width,
bottom: top + height,
width: width,
height: height
bottom: bottom,
left: left,
right: right,
width: right - left,
height: bottom - top
};
};

View File

@@ -461,11 +461,11 @@ Blockly.Xml.appendDomToWorkspace = function(xml, workspace) {
}
// Load the new blocks into the workspace and get the IDs of the new blocks.
var newBlockIds = Blockly.Xml.domToWorkspace(xml,workspace);
if (bbox && bbox.height) { // check if any previous block
if (bbox && bbox.top != bbox.bottom) { // check if any previous block
var offsetY = 0; // offset to add to y of the new block
var offsetX = 0;
var farY = bbox.y + bbox.height; // bottom position
var topX = bbox.x; // x of bounding box
var farY = bbox.bottom; // bottom position
var topX = bbox.left; // x of bounding box
// Check position of the new blocks.
var newX = Infinity; // x of top corner
var newY = Infinity; // y of top corner

View File

@@ -34,10 +34,10 @@ function makeMockWs(scale, x, y, width, height) {
return {
getBlocksBoundingBox: function() {
return {
width: width,
height: height,
x: x,
y: y
top: y,
bottom: y + height,
left: x,
right: x + width
}
},
scale: scale