Migrate core/events/block_events.js to ES6 const/let

This commit is contained in:
kozbial
2021-08-05 15:41:52 -07:00
committed by Monica Kozbial
parent abbc1b3de3
commit 20dad54d38
5 changed files with 46 additions and 41 deletions

View File

@@ -51,7 +51,7 @@ Blockly.utils.object.inherits(Blockly.Events.BlockBase,
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockBase.prototype.toJson = function() {
var json = Blockly.Events.BlockBase.superClass_.toJson.call(this);
const json = Blockly.Events.BlockBase.superClass_.toJson.call(this);
json['blockId'] = this.blockId;
return json;
};

View File

@@ -54,7 +54,7 @@ Blockly.Events.BlockChange.prototype.type = Blockly.Events.BLOCK_CHANGE;
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockChange.prototype.toJson = function() {
var json = Blockly.Events.BlockChange.superClass_.toJson.call(this);
const json = Blockly.Events.BlockChange.superClass_.toJson.call(this);
json['element'] = this.element;
if (this.name) {
json['name'] = this.name;
@@ -89,8 +89,8 @@ Blockly.Events.BlockChange.prototype.isNull = function() {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockChange.prototype.run = function(forward) {
var workspace = this.getEventWorkspace_();
var block = workspace.getBlockById(this.blockId);
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn("Can't change non-existent block: " + this.blockId);
return;
@@ -99,16 +99,17 @@ Blockly.Events.BlockChange.prototype.run = function(forward) {
// Close the mutator (if open) since we don't want to update it.
block.mutator.setVisible(false);
}
var value = forward ? this.newValue : this.oldValue;
const value = forward ? this.newValue : this.oldValue;
switch (this.element) {
case 'field':
var field = block.getField(this.name);
case 'field': {
const field = block.getField(this.name);
if (field) {
field.setValue(value);
} else {
console.warn("Can't set non-existent field: " + this.name);
}
break;
}
case 'comment':
block.setCommentText(/** @type {string} */ (value) || null);
break;
@@ -121,19 +122,21 @@ Blockly.Events.BlockChange.prototype.run = function(forward) {
case 'inline':
block.setInputsInline(!!value);
break;
case 'mutation':
var oldMutation = '';
case 'mutation': {
let oldMutation = '';
if (block.mutationToDom) {
var oldMutationDom = block.mutationToDom();
const oldMutationDom = block.mutationToDom();
oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
}
if (block.domToMutation) {
var dom = Blockly.Xml.textToDom(/** @type {string} */ (value) || '<mutation/>');
const dom = Blockly.Xml.textToDom(/** @type {string} */
(value) || '<mutation/>');
block.domToMutation(dom);
}
Blockly.Events.fire(new Blockly.Events.BlockChange(
block, 'mutation', null, oldMutation, value));
break;
}
default:
console.warn('Unknown change type: ' + this.element);
}

View File

@@ -58,7 +58,7 @@ Blockly.Events.BlockCreate.prototype.type = Blockly.Events.BLOCK_CREATE;
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockCreate.prototype.toJson = function() {
var json = Blockly.Events.BlockCreate.superClass_.toJson.call(this);
const json = Blockly.Events.BlockCreate.superClass_.toJson.call(this);
json['xml'] = Blockly.Xml.domToText(this.xml);
json['ids'] = this.ids;
if (!this.recordUndo) {
@@ -85,14 +85,15 @@ Blockly.Events.BlockCreate.prototype.fromJson = function(json) {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockCreate.prototype.run = function(forward) {
var workspace = this.getEventWorkspace_();
const workspace = this.getEventWorkspace_();
if (forward) {
var xml = Blockly.utils.xml.createElement('xml');
const xml = Blockly.utils.xml.createElement('xml');
xml.appendChild(this.xml);
Blockly.Xml.domToWorkspace(xml, workspace);
} else {
for (var i = 0, id; (id = this.ids[i]); i++) {
var block = workspace.getBlockById(id);
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id == this.blockId) {

View File

@@ -62,7 +62,7 @@ Blockly.Events.BlockDelete.prototype.type = Blockly.Events.BLOCK_DELETE;
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockDelete.prototype.toJson = function() {
var json = Blockly.Events.BlockDelete.superClass_.toJson.call(this);
const json = Blockly.Events.BlockDelete.superClass_.toJson.call(this);
json['oldXml'] = Blockly.Xml.domToText(this.oldXml);
json['ids'] = this.ids;
if (!this.recordUndo) {
@@ -89,10 +89,11 @@ Blockly.Events.BlockDelete.prototype.fromJson = function(json) {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockDelete.prototype.run = function(forward) {
var workspace = this.getEventWorkspace_();
const workspace = this.getEventWorkspace_();
if (forward) {
for (var i = 0, id; (id = this.ids[i]); i++) {
var block = workspace.getBlockById(id);
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id == this.blockId) {
@@ -101,7 +102,7 @@ Blockly.Events.BlockDelete.prototype.run = function(forward) {
}
}
} else {
var xml = Blockly.utils.xml.createElement('xml');
const xml = Blockly.utils.xml.createElement('xml');
xml.appendChild(this.oldXml);
Blockly.Xml.domToWorkspace(xml, workspace);
}

View File

@@ -39,7 +39,7 @@ Blockly.Events.BlockMove = function(opt_block) {
this.recordUndo = false;
}
var location = this.currentLocation_();
const location = this.currentLocation_();
this.oldParentId = location.parentId;
this.oldInputName = location.inputName;
this.oldCoordinate = location.coordinate;
@@ -57,7 +57,7 @@ Blockly.Events.BlockMove.prototype.type = Blockly.Events.BLOCK_MOVE;
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockMove.prototype.toJson = function() {
var json = Blockly.Events.BlockMove.superClass_.toJson.call(this);
const json = Blockly.Events.BlockMove.superClass_.toJson.call(this);
if (this.newParentId) {
json['newParentId'] = this.newParentId;
}
@@ -83,7 +83,7 @@ Blockly.Events.BlockMove.prototype.fromJson = function(json) {
this.newParentId = json['newParentId'];
this.newInputName = json['newInputName'];
if (json['newCoordinate']) {
var xy = json['newCoordinate'].split(',');
const xy = json['newCoordinate'].split(',');
this.newCoordinate =
new Blockly.utils.Coordinate(Number(xy[0]), Number(xy[1]));
}
@@ -96,7 +96,7 @@ Blockly.Events.BlockMove.prototype.fromJson = function(json) {
* Record the block's new location. Called after the move.
*/
Blockly.Events.BlockMove.prototype.recordNew = function() {
var location = this.currentLocation_();
const location = this.currentLocation_();
this.newParentId = location.parentId;
this.newInputName = location.inputName;
this.newCoordinate = location.coordinate;
@@ -109,13 +109,13 @@ Blockly.Events.BlockMove.prototype.recordNew = function() {
* @private
*/
Blockly.Events.BlockMove.prototype.currentLocation_ = function() {
var workspace = this.getEventWorkspace_();
var block = workspace.getBlockById(this.blockId);
var location = {};
var parent = block.getParent();
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
const location = {};
const parent = block.getParent();
if (parent) {
location.parentId = parent.id;
var input = parent.getInputWithBlock(block);
const input = parent.getInputWithBlock(block);
if (input) {
location.inputName = input.name;
}
@@ -140,16 +140,16 @@ Blockly.Events.BlockMove.prototype.isNull = function() {
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockMove.prototype.run = function(forward) {
var workspace = this.getEventWorkspace_();
var block = workspace.getBlockById(this.blockId);
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn("Can't move non-existent block: " + this.blockId);
return;
}
var parentId = forward ? this.newParentId : this.oldParentId;
var inputName = forward ? this.newInputName : this.oldInputName;
var coordinate = forward ? this.newCoordinate : this.oldCoordinate;
var parentBlock = null;
const parentId = forward ? this.newParentId : this.oldParentId;
const inputName = forward ? this.newInputName : this.oldInputName;
const coordinate = forward ? this.newCoordinate : this.oldCoordinate;
let parentBlock;
if (parentId) {
parentBlock = workspace.getBlockById(parentId);
if (!parentBlock) {
@@ -161,14 +161,14 @@ Blockly.Events.BlockMove.prototype.run = function(forward) {
block.unplug();
}
if (coordinate) {
var xy = block.getRelativeToSurfaceXY();
const xy = block.getRelativeToSurfaceXY();
block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
} else {
var blockConnection = block.outputConnection || block.previousConnection;
var parentConnection;
var connectionType = blockConnection.type;
const blockConnection = block.outputConnection || block.previousConnection;
let parentConnection;
const connectionType = blockConnection.type;
if (inputName) {
var input = parentBlock.getInput(inputName);
const input = parentBlock.getInput(inputName);
if (input) {
parentConnection = input.connection;
}