Block and comment events should report language agnostic coordinates so that switching locales (between LTR and RTL languages) does not affect the x position of the blocks and comments on the workspace.

This commit is contained in:
Karishma Chadha 2018-10-02 19:13:08 -04:00
parent 0fa3ef6e78
commit 1fef923ad4
3 changed files with 49 additions and 9 deletions

View file

@ -56,6 +56,7 @@ Blockly.Events.BlockBase = function(block) {
*/
this.blockId = block.id;
this.workspaceId = block.workspace.id;
this.workspace = block.workspace;
};
goog.inherits(Blockly.Events.BlockBase, Blockly.Events.Abstract);
@ -463,7 +464,11 @@ Blockly.Events.Move.prototype.currentLocation_ = function() {
location.inputName = input.name;
}
} else {
location.coordinate = block.getRelativeToSurfaceXY();
var blockXY = block.getRelativeToSurfaceXY();
// The X position in the block move event should be the language agnostic
// position of the block. I.e. it should not be different in LTR vs. RTL.
var rtlAwareX = workspace.RTL ? workspace.getWidth() - blockXY.x : blockXY.x;
location.coordinate = new goog.math.Coordinate(rtlAwareX, blockXY.y);
}
return location;
};
@ -505,7 +510,8 @@ Blockly.Events.Move.prototype.run = function(forward) {
}
if (coordinate) {
var xy = block.getRelativeToSurfaceXY();
block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
var rtlAwareX = workspace.RTL ? workspace.getWidth() - coordinate.x : coordinate.x;
block.moveBy(rtlAwareX - xy.x, coordinate.y - xy.y);
} else {
var blockConnection = block.outputConnection || block.previousConnection;
var parentConnection;

View file

@ -402,11 +402,12 @@ Blockly.Events.CommentMove = function(comment) {
*/
this.comment_ = comment;
this.workspaceWidth_ = comment.workspace.getWidth();
/**
* The location before the move, in workspace coordinates.
* @type {!goog.math.Coordinate}
*/
this.oldCoordinate_ = comment.getXY();
this.oldCoordinate_ = this.currentLocation_();
/**
* The location after the move, in workspace coordinates.
@ -416,6 +417,28 @@ Blockly.Events.CommentMove = function(comment) {
};
goog.inherits(Blockly.Events.CommentMove, Blockly.Events.CommentBase);
/**
* Calculate the current, language agnostic location of the comment.
* This value should not report different numbers in LTR vs. RTL.
* @return {goog.math.Coordinate} The location of the comment.
* @private
*/
Blockly.Events.CommentMove.prototype.currentLocation_ = function() {
var xy = this.comment_.getXY();
if (!this.comment_.workspace.RTL) {
return xy;
}
var rtlAwareX;
if (this.comment_ instanceof Blockly.ScratchBlockComment) {
var commentWidth = this.comment_.getBubbleSize().width;
rtlAwareX = this.workspaceWidth_ - xy.x - commentWidth;
} else {
rtlAwareX = this.workspaceWidth_ - xy.x;
}
return new goog.math.Coordinate(rtlAwareX, xy.y);
};
/**
* Record the comment's new location. Called after the move. Can only be
* called once.
@ -425,7 +448,7 @@ Blockly.Events.CommentMove.prototype.recordNew = function() {
throw new Error('Tried to record the new position of a comment on the ' +
'same event twice.');
}
this.newCoordinate_ = this.comment_.getXY();
this.newCoordinate_ = this.currentLocation_();
this.comment_ = null;
};
@ -442,7 +465,8 @@ Blockly.Events.CommentMove.prototype.type = Blockly.Events.COMMENT_MOVE;
* coordinates.
*/
Blockly.Events.CommentMove.prototype.setOldCoordinate = function(xy) {
this.oldCoordinate_ = xy;
this.oldCoordinate_ = new goog.math.Coordinate(this.comment_.workspace.RTL ?
this.workspaceWidth_ - xy.x : xy.x, xy.y);
};
/**
@ -496,10 +520,20 @@ Blockly.Events.CommentMove.prototype.run = function(forward) {
var target = forward ? this.newCoordinate_ : this.oldCoordinate_;
if (comment instanceof Blockly.ScratchBlockComment) {
comment.moveTo(target.x, target.y);
if (comment.workspace.RTL) {
comment.moveTo(this.workspaceWidth_ - target.x, target.y);
} else {
comment.moveTo(target.x, target.y);
}
} else {
// TODO: Check if the comment is being dragged, and give up if so.
var current = comment.getXY();
comment.moveBy(target.x - current.x, target.y - current.y);
if (comment.workspace.RTL) {
var deltaX = target.x - (this.workspaceWidth_ - current.x);
comment.moveBy(-deltaX, target.y - current.y);
} else {
comment.moveBy(target.x - current.x, target.y - current.y);
}
}
};

View file

@ -419,8 +419,8 @@ Blockly.WorkspaceCommentSvg.prototype.clearTransformAttributes_ = function() {
Blockly.WorkspaceCommentSvg.prototype.getBubbleSize = function() {
if (this.rendered_) {
return {
width: this.svgRect_.getAttribute('width'),
height: this.svgRect_.getAttribute('height')
width: parseInt(this.svgRect_.getAttribute('width')),
height: parseInt(this.svgRect_.getAttribute('height'))
};
} else {
this.getHeightWidth();