Code simplifications

This commit is contained in:
Neil Fraser
2021-06-02 11:10:28 -07:00
committed by Neil Fraser
parent 91fe77e26c
commit c5a7b481bc
6 changed files with 38 additions and 62 deletions

View File

@@ -1551,16 +1551,15 @@ Blockly.Block.prototype.jsonInit = function(json) {
json['extensions'] = [json['extensions']]; // Correct and continue.
}
// Add the mutator to the block
// Add the mutator to the block.
if (json['mutator'] !== undefined) {
Blockly.Extensions.apply(json['mutator'], this, true);
}
if (Array.isArray(json['extensions'])) {
var extensionNames = json['extensions'];
var extensionNames = json['extensions'];
if (Array.isArray(extensionNames)) {
for (var j = 0; j < extensionNames.length; ++j) {
var extensionName = extensionNames[j];
Blockly.Extensions.apply(extensionName, this, false);
Blockly.Extensions.apply(extensionNames[j], this, false);
}
}
};

View File

@@ -340,33 +340,33 @@ Blockly.BlockSvg.prototype.unselect = function() {
/**
* Block's mutator icon (if any).
* @type {Blockly.Mutator}
* @type {?Blockly.Mutator}
*/
Blockly.BlockSvg.prototype.mutator = null;
/**
* Block's comment icon (if any).
* @type {Blockly.Comment}
* @type {?Blockly.Comment}
* @deprecated August 2019. Use getCommentIcon instead.
*/
Blockly.BlockSvg.prototype.comment = null;
/**
* Block's comment icon (if any).
* @type {Blockly.Comment}
* @type {?Blockly.Comment}
* @private
*/
Blockly.BlockSvg.prototype.commentIcon_ = null;
/**
* Block's warning icon (if any).
* @type {Blockly.Warning}
* @type {?Blockly.Warning}
*/
Blockly.BlockSvg.prototype.warning = null;
/**
* Returns a list of mutator, comment, and warning icons.
* @return {!Array} List of icons.
* @return {!Array<!Blockly.Icon>} List of icons.
*/
Blockly.BlockSvg.prototype.getIcons = function() {
var icons = [];
@@ -384,7 +384,7 @@ Blockly.BlockSvg.prototype.getIcons = function() {
/**
* Set parent of this block to be a new block or null.
* @param {Blockly.Block} newParent New parent block.
* @param {?Blockly.Block} newParent New parent block.
* @override
*/
Blockly.BlockSvg.prototype.setParent = function(newParent) {

View File

@@ -84,21 +84,21 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) {
yPos = conn.y;
// Walk forward and back on the y axis looking for the connection.
var pointerMin = bestGuess;
var pointerMax = bestGuess;
while (pointerMin >= 0 && this.connections_[pointerMin].y == yPos) {
if (this.connections_[pointerMin] == conn) {
return pointerMin;
var pointer = bestGuess;
while (pointer >= 0 && this.connections_[pointer].y == yPos) {
if (this.connections_[pointer] == conn) {
return pointer;
}
pointerMin--;
pointer--;
}
while (pointerMax < this.connections_.length &&
this.connections_[pointerMax].y == yPos) {
if (this.connections_[pointerMax] == conn) {
return pointerMax;
pointer = bestGuess;
while (pointer < this.connections_.length &&
this.connections_[pointer].y == yPos) {
if (this.connections_[pointer] == conn) {
return pointer;
}
pointerMax++;
pointer++;
}
return -1;
};

View File

@@ -222,17 +222,8 @@ Blockly.FieldNumber.prototype.setPrecision = function(precision) {
* @private
*/
Blockly.FieldNumber.prototype.setPrecisionInternal_ = function(precision) {
if (precision == null) {
// Number(precision) would also be 0, but set explicitly to be clear.
this.precision_ = 0;
} else {
precision = Number(precision);
if (!isNaN(precision)) {
this.precision_ = precision;
}
}
var precisionString = this.precision_.toLocaleString("en-US", {maximumFractionDigits: 20});
this.precision_ = Number(precision) || 0;
var precisionString = this.precision_.toLocaleString('en-US', {maximumFractionDigits: 20});
var decimalIndex = precisionString.indexOf('.');
if (decimalIndex == -1) {
// If the precision is 0 (float) allow any number of decimals,

View File

@@ -234,8 +234,7 @@ Blockly.bumpTopObjectsIntoBounds_ = function(workspace) {
var scrollMetricsInWsCoords = metricsManager.getScrollMetrics(true);
var topBlocks = workspace.getTopBoundedElements();
for (var i = 0, block; (block = topBlocks[i]); i++) {
Blockly.bumpObjectIntoBounds_(
workspace, scrollMetricsInWsCoords, block);
Blockly.bumpObjectIntoBounds_(workspace, scrollMetricsInWsCoords, block);
}
};

View File

@@ -210,14 +210,12 @@ Blockly.blockRendering.RenderInfo.prototype.createRows_ = function() {
// Icons always go on the first row, before anything else.
var icons = this.block_.getIcons();
if (icons.length) {
for (var i = 0, icon; (icon = icons[i]); i++) {
var iconInfo = new Blockly.blockRendering.Icon(this.constants_, icon);
if (this.isCollapsed && icon.collapseHidden) {
this.hiddenIcons.push(iconInfo);
} else {
activeRow.elements.push(iconInfo);
}
for (var i = 0, icon; (icon = icons[i]); i++) {
var iconInfo = new Blockly.blockRendering.Icon(this.constants_, icon);
if (this.isCollapsed && icon.collapseHidden) {
this.hiddenIcons.push(iconInfo);
} else {
activeRow.elements.push(iconInfo);
}
}
@@ -264,17 +262,12 @@ Blockly.blockRendering.RenderInfo.prototype.createRows_ = function() {
Blockly.blockRendering.RenderInfo.prototype.populateTopRow_ = function() {
var hasPrevious = !!this.block_.previousConnection;
var hasHat = (this.block_.hat ?
this.block_.hat === 'cap' : this.constants_.ADD_START_HATS) &&
!this.outputConnection && !hasPrevious;
var leftSquareCorner = this.topRow.hasLeftSquareCorner(this.block_);
this.block_.hat === 'cap' : this.constants_.ADD_START_HATS) &&
!this.outputConnection && !hasPrevious;
if (leftSquareCorner) {
this.topRow.elements.push(
new Blockly.blockRendering.SquareCorner(this.constants_));
} else {
this.topRow.elements.push(
new Blockly.blockRendering.RoundCorner(this.constants_));
}
var cornerClass = this.topRow.hasLeftSquareCorner(this.block_) ?
Blockly.blockRendering.SquareCorner : Blockly.blockRendering.RoundCorner;
this.topRow.elements.push(new cornerClass(this.constants_));
if (hasHat) {
var hat = new Blockly.blockRendering.Hat(this.constants_);
@@ -301,15 +294,9 @@ Blockly.blockRendering.RenderInfo.prototype.populateTopRow_ = function() {
this.topRow.minHeight = this.constants_.TOP_ROW_MIN_HEIGHT;
}
var rightSquareCorner = this.topRow.hasRightSquareCorner(this.block_);
if (rightSquareCorner) {
this.topRow.elements.push(
new Blockly.blockRendering.SquareCorner(this.constants_, 'right'));
} else {
this.topRow.elements.push(
new Blockly.blockRendering.RoundCorner(this.constants_, 'right'));
}
cornerClass = this.topRow.hasRightSquareCorner(this.block_) ?
Blockly.blockRendering.SquareCorner : Blockly.blockRendering.RoundCorner;
this.topRow.elements.push(new cornerClass(this.constants_, 'right'));
};
/**