Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/blockly/core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,36 @@ export class BlockSvg
return this.dragStrategy.isMovable();
}

/** Returns whether the block fully fits within the boundaries of the workspace */
isBlockFullyInBounds(): boolean {
let blockLeft;
let blockRight;
const {left, top, width, height} = this.workspace
.getMetricsManager()
.getViewMetrics(true);

const xy = this.getRelativeToSurfaceXY();
const {width: blockWidth, height: blockHeight} = this.getHeightWidth();

if (this.RTL) {
blockLeft = xy.x - blockWidth;
blockRight = xy.x;
} else {
blockLeft = xy.x;
blockRight = xy.x + blockWidth;
}

const blockTop = xy.y;
const blockBottom = xy.y + blockHeight;

return (
blockLeft >= left &&
blockRight <= left + width &&
blockTop >= top &&
blockBottom <= top + height
);
}

/** Starts a drag on the block. */
startDrag(e?: PointerEvent): void {
this.dragStrategy.startDrag(e);
Expand Down
4 changes: 3 additions & 1 deletion packages/blockly/core/workspace_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,9 @@ export class WorkspaceSvg
: block.getHeightWidth();

// Find the enter of the block in workspace units.
const blockCenterY = xy.y + heightWidth.height / 2;
const blockCenterY = block.isBlockFullyInBounds()
? xy.y + heightWidth.height / 2
: xy.y;

// In RTL the block's position is the top right of the block, not top left.
const multiplier = this.RTL ? -1 : 1;
Expand Down
15 changes: 15 additions & 0 deletions packages/blockly/tests/mocha/workspace_svg_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ suite('WorkspaceSvg', function () {
this.clock,
);
});
test('centerOnBlock when block partially out of bounds', function () {
const block = this.workspace.newBlock('stack_block');
block.initSvg();
block.render();

sinon.stub(block, 'getRelativeToSurfaceXY').returns({x: 10, y: 80});
sinon.stub(block, 'getHeightWidth').returns({width: 40, height: 40});

runViewportEventTest(
() => this.workspace.centerOnBlock(block.id),
this.changeListenerSpy,
this.workspace,
this.clock,
);
});
});
suite('Blocks triggering viewport changes', function () {
test('block move that triggers scroll', function () {
Expand Down