Merge pull request #1763 from rachel-fenichel/feature/goto_block

Add a function to scroll the workspace to center on a given block
This commit is contained in:
Rachel Fenichel
2018-04-06 11:42:10 -07:00
committed by GitHub
2 changed files with 60 additions and 1 deletions

View File

@@ -860,7 +860,10 @@ Blockly.Blocks['procedures_callnoreturn'] = {
var workspace = this.workspace;
option.callback = function() {
var def = Blockly.Procedures.getDefinition(name, workspace);
def && def.select();
if (def) {
workspace.centerOnBlock(def.id);
def.select();
}
};
options.push(option);
},

View File

@@ -1486,6 +1486,7 @@ Blockly.WorkspaceSvg.prototype.zoomToFit = function() {
Blockly.WorkspaceSvg.prototype.scrollCenter = function() {
if (!this.scrollbar) {
// Can't center a non-scrolling workspace.
console.warn('Tried to scroll a non-scrollable workspace.');
return;
}
var metrics = this.getMetrics();
@@ -1497,6 +1498,61 @@ Blockly.WorkspaceSvg.prototype.scrollCenter = function() {
this.scrollbar.set(x, y);
};
/**
* Scroll the workspace to center on the given block.
* @param {?string} id ID of block center on.
* @public
*/
Blockly.WorkspaceSvg.prototype.centerOnBlock = function(id) {
if (!this.scrollbar) {
console.warn('Tried to scroll a non-scrollable workspace.');
return;
}
var block = this.getBlockById(id);
if (!block) {
return;
}
// XY is in workspace coordinates.
var xy = block.getRelativeToSurfaceXY();
// Height/width is in workspace units.
var heightWidth = block.getHeightWidth();
// Find the enter of the block in workspace units.
var blockCenterY = xy.y + heightWidth.height / 2;
// In RTL the block's position is the top right of the block, not top left.
var multiplier = this.RTL ? -1 : 1;
var blockCenterX = xy.x + (multiplier * heightWidth.width / 2);
// Workspace scale, used to convert from workspace coordinates to pixels.
var scale = this.scale;
// Center in pixels. 0, 0 is at the workspace origin. These numbers may
// be negative.
var pixelX = blockCenterX * scale;
var pixelY = blockCenterY * scale;
var metrics = this.getMetrics();
// Scrolling to here would put the block in the top-left corner of the
// visible workspace.
var scrollToBlockX = pixelX - metrics.contentLeft;
var scrollToBlockY = pixelY - metrics.contentTop;
// viewHeight and viewWidth are in pixels.
var halfViewWidth = metrics.viewWidth / 2;
var halfViewHeight = metrics.viewHeight / 2;
// Put the block in the center of the visible workspace instead.
var scrollToCenterX = scrollToBlockX - halfViewWidth;
var scrollToCenterY = scrollToBlockY - halfViewHeight;
Blockly.hideChaff();
this.scrollbar.set(scrollToCenterX, scrollToCenterY);
};
/**
* Set the workspace's zoom factor.
* @param {number} newScale Zoom factor.