From f77d5f12647fcfa3c9d262cd32187a4539bf1886 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 19 Jul 2021 10:49:20 -0700 Subject: [PATCH 1/5] Migrate core/connection_db.js to ES6 const/let --- core/connection_db.js | 52 +++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/core/connection_db.js b/core/connection_db.js index 4b2615725..b6d990af1 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -56,7 +56,7 @@ Blockly.ConnectionDB = function(checker) { * @package */ Blockly.ConnectionDB.prototype.addConnection = function(connection, yPos) { - var index = this.calculateIndexForYPos_(yPos); + const index = this.calculateIndexForYPos_(yPos); this.connections_.splice(index, 0, connection); }; @@ -76,7 +76,7 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { return -1; } - var bestGuess = this.calculateIndexForYPos_(yPos); + const bestGuess = this.calculateIndexForYPos_(yPos); if (bestGuess >= this.connections_.length) { // Not in list return -1; @@ -84,7 +84,7 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { yPos = conn.y; // Walk forward and back on the y axis looking for the connection. - var pointer = bestGuess; + let pointer = bestGuess; while (pointer >= 0 && this.connections_[pointer].y == yPos) { if (this.connections_[pointer] == conn) { return pointer; @@ -114,10 +114,10 @@ Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { if (!this.connections_.length) { return 0; } - var pointerMin = 0; - var pointerMax = this.connections_.length; + let pointerMin = 0; + let pointerMax = this.connections_.length; while (pointerMin < pointerMax) { - var pointerMid = Math.floor((pointerMin + pointerMax) / 2); + const pointerMid = Math.floor((pointerMin + pointerMax) / 2); if (this.connections_[pointerMid].y < yPos) { pointerMin = pointerMid + 1; } else if (this.connections_[pointerMid].y > yPos) { @@ -137,7 +137,7 @@ Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { * @throws {Error} If the connection cannot be found in the database. */ Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { - var index = this.findIndexOfConnection_(connection, yPos); + const index = this.findIndexOfConnection_(connection, yPos); if (index == -1) { throw Error('Unable to find connection in connectionDB.'); } @@ -153,14 +153,14 @@ Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { * @return {!Array} List of connections. */ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { - var db = this.connections_; - var currentX = connection.x; - var currentY = connection.y; + const db = this.connections_; + const currentX = connection.x; + const currentY = connection.y; // Binary search to find the closest y location. - var pointerMin = 0; - var pointerMax = db.length - 2; - var pointerMid = pointerMax; + let pointerMin = 0; + let pointerMax = db.length - 2; + let pointerMid = pointerMax; while (pointerMin < pointerMid) { if (db[pointerMid].y < currentY) { pointerMin = pointerMid; @@ -170,7 +170,7 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { pointerMid = Math.floor((pointerMin + pointerMax) / 2); } - var neighbours = []; + const neighbours = []; /** * Computes if the current connection is within the allowed radius of another * connection. @@ -180,9 +180,9 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { * the other connection is less than the allowed radius. */ function checkConnection_(yIndex) { - var dx = currentX - db[yIndex].x; - var dy = currentY - db[yIndex].y; - var r = Math.sqrt(dx * dx + dy * dy); + const dx = currentX - db[yIndex].x; + const dy = currentY - db[yIndex].y; + const r = Math.sqrt(dx * dx + dy * dy); if (r <= maxRadius) { neighbours.push(db[yIndex]); } @@ -237,8 +237,8 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, } // Stash the values of x and y from before the drag. - var baseY = conn.y; - var baseX = conn.x; + const baseY = conn.y; + const baseX = conn.x; conn.x = baseX + dxy.x; conn.y = baseY + dxy.y; @@ -246,14 +246,14 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, // calculateIndexForYPos_ finds an index for insertion, which is always // after any block with the same y index. We want to search both forward // and back, so search on both sides of the index. - var closestIndex = this.calculateIndexForYPos_(conn.y); + const closestIndex = this.calculateIndexForYPos_(conn.y); - var bestConnection = null; - var bestRadius = maxRadius; - var temp; + let bestConnection = null; + let bestRadius = maxRadius; + let temp; // Walk forward and back on the y axis looking for the closest x,y point. - var pointerMin = closestIndex - 1; + let pointerMin = closestIndex - 1; while (pointerMin >= 0 && this.isInYRange_(pointerMin, conn.y, maxRadius)) { temp = this.connections_[pointerMin]; if (this.connectionChecker_.canConnect(conn, temp, true, bestRadius)) { @@ -263,7 +263,7 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, pointerMin--; } - var pointerMax = closestIndex; + let pointerMax = closestIndex; while (pointerMax < this.connections_.length && this.isInYRange_(pointerMax, conn.y, maxRadius)) { temp = this.connections_[pointerMax]; @@ -290,7 +290,7 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, */ Blockly.ConnectionDB.init = function(checker) { // Create four databases, one for each connection type. - var dbList = []; + const dbList = []; dbList[Blockly.connectionTypes.INPUT_VALUE] = new Blockly.ConnectionDB(checker); dbList[Blockly.connectionTypes.OUTPUT_VALUE] = From a87b2b0bfa8cb88cc8dec88362b9c0ee4b05a888 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 19 Jul 2021 10:53:42 -0700 Subject: [PATCH 2/5] Migrate core/connection_db.js to goog.module --- core/connection_db.js | 33 ++++++++++++++++++--------------- tests/deps.js | 2 +- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/core/connection_db.js b/core/connection_db.js index b6d990af1..81782397b 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -12,7 +12,8 @@ */ 'use strict'; -goog.provide('Blockly.ConnectionDB'); +goog.module('Blockly.ConnectionDB'); +goog.module.declareLegacyNamespace(); goog.require('Blockly.connectionTypes'); /** @suppress {extraRequire} */ @@ -32,7 +33,7 @@ goog.requireType('Blockly.utils.Coordinate'); * drag. * @constructor */ -Blockly.ConnectionDB = function(checker) { +const ConnectionDB = function(checker) { /** * Array of connections sorted by y position in workspace units. * @type {!Array} @@ -55,7 +56,7 @@ Blockly.ConnectionDB = function(checker) { * connection. * @package */ -Blockly.ConnectionDB.prototype.addConnection = function(connection, yPos) { +ConnectionDB.prototype.addConnection = function(connection, yPos) { const index = this.calculateIndexForYPos_(yPos); this.connections_.splice(index, 0, connection); }; @@ -71,7 +72,7 @@ Blockly.ConnectionDB.prototype.addConnection = function(connection, yPos) { * not found. * @private */ -Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { +ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { if (!this.connections_.length) { return -1; } @@ -110,7 +111,7 @@ Blockly.ConnectionDB.prototype.findIndexOfConnection_ = function(conn, yPos) { * @return {number} The candidate index. * @private */ -Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { +ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { if (!this.connections_.length) { return 0; } @@ -136,7 +137,7 @@ Blockly.ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { * @param {number} yPos The y position used to find the index of the connection. * @throws {Error} If the connection cannot be found in the database. */ -Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { +ConnectionDB.prototype.removeConnection = function(connection, yPos) { const index = this.findIndexOfConnection_(connection, yPos); if (index == -1) { throw Error('Unable to find connection in connectionDB.'); @@ -152,7 +153,7 @@ Blockly.ConnectionDB.prototype.removeConnection = function(connection, yPos) { * @param {number} maxRadius The maximum radius to another connection. * @return {!Array} List of connections. */ -Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { +ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { const db = this.connections_; const currentX = connection.x; const currentY = connection.y; @@ -213,7 +214,7 @@ Blockly.ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { * @return {boolean} True if connection is in range. * @private */ -Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { +ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { return (Math.abs(this.connections_[index].y - baseY) <= maxRadius); }; @@ -229,7 +230,7 @@ Blockly.ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { * Contains two properties: 'connection' which is either another * connection or null, and 'radius' which is the distance. */ -Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, +ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dxy) { if (!this.connections_.length) { // Don't bother. @@ -286,18 +287,20 @@ Blockly.ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, * Initialize a set of connection DBs for a workspace. * @param {!Blockly.IConnectionChecker} checker The workspace's * connection checker, used to decide if connections are valid during a drag. - * @return {!Array} Array of databases. + * @return {!Array} Array of databases. */ -Blockly.ConnectionDB.init = function(checker) { +ConnectionDB.init = function(checker) { // Create four databases, one for each connection type. const dbList = []; dbList[Blockly.connectionTypes.INPUT_VALUE] = - new Blockly.ConnectionDB(checker); + new ConnectionDB(checker); dbList[Blockly.connectionTypes.OUTPUT_VALUE] = - new Blockly.ConnectionDB(checker); + new ConnectionDB(checker); dbList[Blockly.connectionTypes.NEXT_STATEMENT] = - new Blockly.ConnectionDB(checker); + new ConnectionDB(checker); dbList[Blockly.connectionTypes.PREVIOUS_STATEMENT] = - new Blockly.ConnectionDB(checker); + new ConnectionDB(checker); return dbList; }; + +exports = ConnectionDB; diff --git a/tests/deps.js b/tests/deps.js index cdeae73f4..7531e34ae 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -21,7 +21,7 @@ goog.addDependency('../../core/comment.js', ['Blockly.Comment'], ['Blockly.Bubbl goog.addDependency('../../core/component_manager.js', ['Blockly.ComponentManager'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection.js', ['Blockly.Connection'], ['Blockly.Events', 'Blockly.Events.BlockMove', 'Blockly.IASTNodeLocationWithBlock', 'Blockly.Xml', 'Blockly.connectionTypes', 'Blockly.constants', 'Blockly.utils.deprecation']); goog.addDependency('../../core/connection_checker.js', ['Blockly.ConnectionChecker'], ['Blockly.Connection', 'Blockly.IConnectionChecker', 'Blockly.connectionTypes', 'Blockly.constants', 'Blockly.registry'], {'lang': 'es6', 'module': 'goog'}); -goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.RenderedConnection', 'Blockly.connectionTypes', 'Blockly.constants']); +goog.addDependency('../../core/connection_db.js', ['Blockly.ConnectionDB'], ['Blockly.RenderedConnection', 'Blockly.connectionTypes', 'Blockly.constants'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/connection_types.js', ['Blockly.connectionTypes'], [], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../core/constants.js', ['Blockly.constants'], ['Blockly.connectionTypes']); goog.addDependency('../../core/contextmenu.js', ['Blockly.ContextMenu'], ['Blockly.Events', 'Blockly.Events.BlockCreate', 'Blockly.Menu', 'Blockly.MenuItem', 'Blockly.Msg', 'Blockly.WidgetDiv', 'Blockly.Xml', 'Blockly.browserEvents', 'Blockly.constants', 'Blockly.utils', 'Blockly.utils.Coordinate', 'Blockly.utils.Rect', 'Blockly.utils.aria', 'Blockly.utils.dom', 'Blockly.utils.userAgent']); From acd7e5596898e39097a7a91faf2760226137ba25 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 19 Jul 2021 10:59:08 -0700 Subject: [PATCH 3/5] Migrate core/connection_db.js to named requires --- core/connection_db.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/core/connection_db.js b/core/connection_db.js index 81782397b..0493cf077 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -15,20 +15,19 @@ goog.module('Blockly.ConnectionDB'); goog.module.declareLegacyNamespace(); -goog.require('Blockly.connectionTypes'); +const Coordinate = goog.requireType('Blockly.utils.Coordinate'); +const IConnectionChecker = goog.requireType('Blockly.IConnectionChecker'); +const RenderedConnection = goog.require('Blockly.RenderedConnection'); +const connectionTypes = goog.require('Blockly.connectionTypes'); /** @suppress {extraRequire} */ goog.require('Blockly.constants'); -goog.require('Blockly.RenderedConnection'); - -goog.requireType('Blockly.IConnectionChecker'); -goog.requireType('Blockly.utils.Coordinate'); /** * Database of connections. * Connections are stored in order of their vertical component. This way * connections in an area may be looked up quickly using a binary search. - * @param {!Blockly.IConnectionChecker} checker The workspace's + * @param {!IConnectionChecker} checker The workspace's * connection type checker, used to decide if connections are valid during a * drag. * @constructor @@ -36,14 +35,14 @@ goog.requireType('Blockly.utils.Coordinate'); const ConnectionDB = function(checker) { /** * Array of connections sorted by y position in workspace units. - * @type {!Array} + * @type {!Array} * @private */ this.connections_ = []; /** * The workspace's connection type checker, used to decide if connections are * valid during a drag. - * @type {!Blockly.IConnectionChecker} + * @type {!IConnectionChecker} * @private */ this.connectionChecker_ = checker; @@ -51,7 +50,7 @@ const ConnectionDB = function(checker) { /** * Add a connection to the database. Should not already exist in the database. - * @param {!Blockly.RenderedConnection} connection The connection to be added. + * @param {!RenderedConnection} connection The connection to be added. * @param {number} yPos The y position used to decide where to insert the * connection. * @package @@ -66,7 +65,7 @@ ConnectionDB.prototype.addConnection = function(connection, yPos) { * * Starts by doing a binary search to find the approximate location, then * linearly searches nearby for the exact connection. - * @param {!Blockly.RenderedConnection} conn The connection to find. + * @param {!RenderedConnection} conn The connection to find. * @param {number} yPos The y position used to find the index of the connection. * @return {number} The index of the connection, or -1 if the connection was * not found. @@ -133,7 +132,7 @@ ConnectionDB.prototype.calculateIndexForYPos_ = function(yPos) { /** * Remove a connection from the database. Must already exist in DB. - * @param {!Blockly.RenderedConnection} connection The connection to be removed. + * @param {!RenderedConnection} connection The connection to be removed. * @param {number} yPos The y position used to find the index of the connection. * @throws {Error} If the connection cannot be found in the database. */ @@ -148,10 +147,10 @@ ConnectionDB.prototype.removeConnection = function(connection, yPos) { /** * Find all nearby connections to the given connection. * Type checking does not apply, since this function is used for bumping. - * @param {!Blockly.RenderedConnection} connection The connection whose + * @param {!RenderedConnection} connection The connection whose * neighbours should be returned. * @param {number} maxRadius The maximum radius to another connection. - * @return {!Array} List of connections. + * @return {!Array} List of connections. */ ConnectionDB.prototype.getNeighbours = function(connection, maxRadius) { const db = this.connections_; @@ -220,13 +219,13 @@ ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { /** * Find the closest compatible connection to this connection. - * @param {!Blockly.RenderedConnection} conn The connection searching for a compatible + * @param {!RenderedConnection} conn The connection searching for a compatible * mate. * @param {number} maxRadius The maximum radius to another connection. - * @param {!Blockly.utils.Coordinate} dxy Offset between this connection's + * @param {!Coordinate} dxy Offset between this connection's * location in the database and the current location (as a result of * dragging). - * @return {!{connection: Blockly.RenderedConnection, radius: number}} + * @return {!{connection: RenderedConnection, radius: number}} * Contains two properties: 'connection' which is either another * connection or null, and 'radius' which is the distance. */ @@ -285,20 +284,20 @@ ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, /** * Initialize a set of connection DBs for a workspace. - * @param {!Blockly.IConnectionChecker} checker The workspace's + * @param {!IConnectionChecker} checker The workspace's * connection checker, used to decide if connections are valid during a drag. * @return {!Array} Array of databases. */ ConnectionDB.init = function(checker) { // Create four databases, one for each connection type. const dbList = []; - dbList[Blockly.connectionTypes.INPUT_VALUE] = + dbList[connectionTypes.INPUT_VALUE] = new ConnectionDB(checker); - dbList[Blockly.connectionTypes.OUTPUT_VALUE] = + dbList[connectionTypes.OUTPUT_VALUE] = new ConnectionDB(checker); - dbList[Blockly.connectionTypes.NEXT_STATEMENT] = + dbList[connectionTypes.NEXT_STATEMENT] = new ConnectionDB(checker); - dbList[Blockly.connectionTypes.PREVIOUS_STATEMENT] = + dbList[connectionTypes.PREVIOUS_STATEMENT] = new ConnectionDB(checker); return dbList; }; From f2ca65944763ac76339eadb708d0caca8db447cc Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 19 Jul 2021 10:59:44 -0700 Subject: [PATCH 4/5] clang-format core/connection_db.js --- core/connection_db.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/core/connection_db.js b/core/connection_db.js index 0493cf077..abc47beb3 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -229,8 +229,7 @@ ConnectionDB.prototype.isInYRange_ = function(index, baseY, maxRadius) { * Contains two properties: 'connection' which is either another * connection or null, and 'radius' which is the distance. */ -ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, - dxy) { +ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dxy) { if (!this.connections_.length) { // Don't bother. return {connection: null, radius: maxRadius}; @@ -265,7 +264,7 @@ ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, let pointerMax = closestIndex; while (pointerMax < this.connections_.length && - this.isInYRange_(pointerMax, conn.y, maxRadius)) { + this.isInYRange_(pointerMax, conn.y, maxRadius)) { temp = this.connections_[pointerMax]; if (this.connectionChecker_.canConnect(conn, temp, true, bestRadius)) { bestConnection = temp; @@ -285,20 +284,17 @@ ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, /** * Initialize a set of connection DBs for a workspace. * @param {!IConnectionChecker} checker The workspace's - * connection checker, used to decide if connections are valid during a drag. + * connection checker, used to decide if connections are valid during a + * drag. * @return {!Array} Array of databases. */ ConnectionDB.init = function(checker) { // Create four databases, one for each connection type. const dbList = []; - dbList[connectionTypes.INPUT_VALUE] = - new ConnectionDB(checker); - dbList[connectionTypes.OUTPUT_VALUE] = - new ConnectionDB(checker); - dbList[connectionTypes.NEXT_STATEMENT] = - new ConnectionDB(checker); - dbList[connectionTypes.PREVIOUS_STATEMENT] = - new ConnectionDB(checker); + dbList[connectionTypes.INPUT_VALUE] = new ConnectionDB(checker); + dbList[connectionTypes.OUTPUT_VALUE] = new ConnectionDB(checker); + dbList[connectionTypes.NEXT_STATEMENT] = new ConnectionDB(checker); + dbList[connectionTypes.PREVIOUS_STATEMENT] = new ConnectionDB(checker); return dbList; }; From a8cba441e7d2a7766b4b5941a2519ce0e45e61af Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 19 Jul 2021 14:33:15 -0700 Subject: [PATCH 5/5] Fix comment indentation in core/connection_db.js --- core/connection_db.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/connection_db.js b/core/connection_db.js index abc47beb3..49bccb504 100644 --- a/core/connection_db.js +++ b/core/connection_db.js @@ -285,7 +285,7 @@ ConnectionDB.prototype.searchForClosest = function(conn, maxRadius, dxy) { * Initialize a set of connection DBs for a workspace. * @param {!IConnectionChecker} checker The workspace's * connection checker, used to decide if connections are valid during a - * drag. + * drag. * @return {!Array} Array of databases. */ ConnectionDB.init = function(checker) {