refactor: convert some files to es classes (#5917)

* refactor: update several files to es6 classes

* refactor: update several files to es6 classes

* chore: add some type casts for specificity about event types

* chore: run formatter

* chore: rebuild
This commit is contained in:
Rachel Fenichel
2022-02-09 08:29:39 -08:00
committed by GitHub
parent e3f40a6abc
commit 9e8c5ea8ed
22 changed files with 2593 additions and 2539 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -17,7 +17,6 @@
*/
goog.module('Blockly.BasicCursor');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {ASTNode} = goog.require('Blockly.ASTNode');
const {Cursor} = goog.require('Blockly.Cursor');
@@ -27,14 +26,191 @@ const {Cursor} = goog.require('Blockly.Cursor');
* Class for a basic cursor.
* This will allow the user to get to all nodes in the AST by hitting next or
* previous.
* @constructor
* @extends {Cursor}
* @alias Blockly.BasicCursor
*/
const BasicCursor = function() {
BasicCursor.superClass_.constructor.call(this);
};
object.inherits(BasicCursor, Cursor);
class BasicCursor extends Cursor {
/**
* @alias Blockly.BasicCursor
*/
constructor() {
super();
}
/**
* Find the next node in the pre order traversal.
* @return {?ASTNode} The next node, or null if the current node is
* not set or there is no next value.
* @override
*/
next() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
const newNode = this.getNextNode_(curNode, this.validNode_);
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
/**
* For a basic cursor we only have the ability to go next and previous, so
* in will also allow the user to get to the next node in the pre order
* traversal.
* @return {?ASTNode} The next node, or null if the current node is
* not set or there is no next value.
* @override
*/
in() {
return this.next();
}
/**
* Find the previous node in the pre order traversal.
* @return {?ASTNode} The previous node, or null if the current node
* is not set or there is no previous value.
* @override
*/
prev() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
const newNode = this.getPreviousNode_(curNode, this.validNode_);
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
/**
* For a basic cursor we only have the ability to go next and previous, so
* out will allow the user to get to the previous node in the pre order
* traversal.
* @return {?ASTNode} The previous node, or null if the current node is
* not set or there is no previous value.
* @override
*/
out() {
return this.prev();
}
/**
* Uses pre order traversal to navigate the Blockly AST. This will allow
* a user to easily navigate the entire Blockly AST without having to go in
* and out levels on the tree.
* @param {?ASTNode} node The current position in the AST.
* @param {!function(ASTNode) : boolean} isValid A function true/false
* depending on whether the given node should be traversed.
* @return {?ASTNode} The next node in the traversal.
* @protected
*/
getNextNode_(node, isValid) {
if (!node) {
return null;
}
const newNode = node.in() || node.next();
if (isValid(newNode)) {
return newNode;
} else if (newNode) {
return this.getNextNode_(newNode, isValid);
}
const siblingOrParent = this.findSiblingOrParent_(node.out());
if (isValid(siblingOrParent)) {
return siblingOrParent;
} else if (siblingOrParent) {
return this.getNextNode_(siblingOrParent, isValid);
}
return null;
}
/**
* Reverses the pre order traversal in order to find the previous node. This
* will allow a user to easily navigate the entire Blockly AST without having
* to go in and out levels on the tree.
* @param {?ASTNode} node The current position in the AST.
* @param {!function(ASTNode) : boolean} isValid A function true/false
* depending on whether the given node should be traversed.
* @return {?ASTNode} The previous node in the traversal or null if no
* previous node exists.
* @protected
*/
getPreviousNode_(node, isValid) {
if (!node) {
return null;
}
let newNode = node.prev();
if (newNode) {
newNode = this.getRightMostChild_(newNode);
} else {
newNode = node.out();
}
if (isValid(newNode)) {
return newNode;
} else if (newNode) {
return this.getPreviousNode_(newNode, isValid);
}
return null;
}
/**
* Decides what nodes to traverse and which ones to skip. Currently, it
* skips output, stack and workspace nodes.
* @param {?ASTNode} node The AST node to check whether it is valid.
* @return {boolean} True if the node should be visited, false otherwise.
* @protected
*/
validNode_(node) {
let isValid = false;
const type = node && node.getType();
if (type === ASTNode.types.OUTPUT || type === ASTNode.types.INPUT ||
type === ASTNode.types.FIELD || type === ASTNode.types.NEXT ||
type === ASTNode.types.PREVIOUS || type === ASTNode.types.WORKSPACE) {
isValid = true;
}
return isValid;
}
/**
* From the given node find either the next valid sibling or parent.
* @param {?ASTNode} node The current position in the AST.
* @return {?ASTNode} The parent AST node or null if there are no
* valid parents.
* @private
*/
findSiblingOrParent_(node) {
if (!node) {
return null;
}
const nextNode = node.next();
if (nextNode) {
return nextNode;
}
return this.findSiblingOrParent_(node.out());
}
/**
* Get the right most child of a node.
* @param {?ASTNode} node The node to find the right most child of.
* @return {?ASTNode} The right most child of the given node, or the node
* if no child exists.
* @private
*/
getRightMostChild_(node) {
if (!node.in()) {
return node;
}
let newNode = node.in();
while (newNode.next()) {
newNode = newNode.next();
}
return this.getRightMostChild_(newNode);
}
}
/**
* Name used for registering a basic cursor.
@@ -42,182 +218,6 @@ object.inherits(BasicCursor, Cursor);
*/
BasicCursor.registrationName = 'basicCursor';
/**
* Find the next node in the pre order traversal.
* @return {?ASTNode} The next node, or null if the current node is
* not set or there is no next value.
* @override
*/
BasicCursor.prototype.next = function() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
const newNode = this.getNextNode_(curNode, this.validNode_);
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
/**
* For a basic cursor we only have the ability to go next and previous, so
* in will also allow the user to get to the next node in the pre order
* traversal.
* @return {?ASTNode} The next node, or null if the current node is
* not set or there is no next value.
* @override
*/
BasicCursor.prototype.in = function() {
return this.next();
};
/**
* Find the previous node in the pre order traversal.
* @return {?ASTNode} The previous node, or null if the current node
* is not set or there is no previous value.
* @override
*/
BasicCursor.prototype.prev = function() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
const newNode = this.getPreviousNode_(curNode, this.validNode_);
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
/**
* For a basic cursor we only have the ability to go next and previous, so
* out will allow the user to get to the previous node in the pre order
* traversal.
* @return {?ASTNode} The previous node, or null if the current node is
* not set or there is no previous value.
* @override
*/
BasicCursor.prototype.out = function() {
return this.prev();
};
/**
* Uses pre order traversal to navigate the Blockly AST. This will allow
* a user to easily navigate the entire Blockly AST without having to go in
* and out levels on the tree.
* @param {?ASTNode} node The current position in the AST.
* @param {!function(ASTNode) : boolean} isValid A function true/false
* depending on whether the given node should be traversed.
* @return {?ASTNode} The next node in the traversal.
* @protected
*/
BasicCursor.prototype.getNextNode_ = function(node, isValid) {
if (!node) {
return null;
}
const newNode = node.in() || node.next();
if (isValid(newNode)) {
return newNode;
} else if (newNode) {
return this.getNextNode_(newNode, isValid);
}
const siblingOrParent = this.findSiblingOrParent_(node.out());
if (isValid(siblingOrParent)) {
return siblingOrParent;
} else if (siblingOrParent) {
return this.getNextNode_(siblingOrParent, isValid);
}
return null;
};
/**
* Reverses the pre order traversal in order to find the previous node. This
* will allow a user to easily navigate the entire Blockly AST without having to
* go in and out levels on the tree.
* @param {?ASTNode} node The current position in the AST.
* @param {!function(ASTNode) : boolean} isValid A function true/false
* depending on whether the given node should be traversed.
* @return {?ASTNode} The previous node in the traversal or null if no
* previous node exists.
* @protected
*/
BasicCursor.prototype.getPreviousNode_ = function(node, isValid) {
if (!node) {
return null;
}
let newNode = node.prev();
if (newNode) {
newNode = this.getRightMostChild_(newNode);
} else {
newNode = node.out();
}
if (isValid(newNode)) {
return newNode;
} else if (newNode) {
return this.getPreviousNode_(newNode, isValid);
}
return null;
};
/**
* Decides what nodes to traverse and which ones to skip. Currently, it
* skips output, stack and workspace nodes.
* @param {?ASTNode} node The AST node to check whether it is valid.
* @return {boolean} True if the node should be visited, false otherwise.
* @protected
*/
BasicCursor.prototype.validNode_ = function(node) {
let isValid = false;
const type = node && node.getType();
if (type === ASTNode.types.OUTPUT || type === ASTNode.types.INPUT ||
type === ASTNode.types.FIELD || type === ASTNode.types.NEXT ||
type === ASTNode.types.PREVIOUS || type === ASTNode.types.WORKSPACE) {
isValid = true;
}
return isValid;
};
/**
* From the given node find either the next valid sibling or parent.
* @param {?ASTNode} node The current position in the AST.
* @return {?ASTNode} The parent AST node or null if there are no
* valid parents.
* @private
*/
BasicCursor.prototype.findSiblingOrParent_ = function(node) {
if (!node) {
return null;
}
const nextNode = node.next();
if (nextNode) {
return nextNode;
}
return this.findSiblingOrParent_(node.out());
};
/**
* Get the right most child of a node.
* @param {?ASTNode} node The node to find the right most child of.
* @return {?ASTNode} The right most child of the given node, or the node
* if no child exists.
* @private
*/
BasicCursor.prototype.getRightMostChild_ = function(node) {
if (!node.in()) {
return node;
}
let newNode = node.in();
while (newNode.next()) {
newNode = newNode.next();
}
return this.getRightMostChild_(newNode);
};
registry.register(
registry.Type.CURSOR, BasicCursor.registrationName, BasicCursor);

View File

@@ -17,7 +17,6 @@
*/
goog.module('Blockly.Cursor');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {ASTNode} = goog.require('Blockly.ASTNode');
const {Marker} = goog.require('Blockly.Marker');
@@ -25,117 +24,119 @@ const {Marker} = goog.require('Blockly.Marker');
/**
* Class for a cursor.
* A cursor controls how a user navigates the Blockly AST.
* @constructor
* @extends {Marker}
* @alias Blockly.Cursor
*/
const Cursor = function() {
Cursor.superClass_.constructor.call(this);
class Cursor extends Marker {
/**
* @alias Blockly.Cursor
*/
constructor() {
super();
/**
* @override
*/
this.type = 'cursor';
}
/**
* @override
* Find the next connection, field, or block.
* @return {ASTNode} The next element, or null if the current node is
* not set or there is no next value.
* @public
*/
this.type = 'cursor';
};
object.inherits(Cursor, Marker);
next() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
/**
* Find the next connection, field, or block.
* @return {ASTNode} The next element, or null if the current node is
* not set or there is no next value.
* @public
*/
Cursor.prototype.next = function() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
let newNode = curNode.next();
while (newNode && newNode.next() &&
(newNode.getType() === ASTNode.types.NEXT ||
newNode.getType() === ASTNode.types.BLOCK)) {
newNode = newNode.next();
}
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
let newNode = curNode.next();
while (newNode && newNode.next() &&
(newNode.getType() === ASTNode.types.NEXT ||
newNode.getType() === ASTNode.types.BLOCK)) {
newNode = newNode.next();
/**
* Find the in connection or field.
* @return {ASTNode} The in element, or null if the current node is
* not set or there is no in value.
* @public
*/
in() {
let curNode = this.getCurNode();
if (!curNode) {
return null;
}
// If we are on a previous or output connection, go to the block level
// before performing next operation.
if (curNode.getType() === ASTNode.types.PREVIOUS ||
curNode.getType() === ASTNode.types.OUTPUT) {
curNode = curNode.next();
}
const newNode = curNode.in();
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
/**
* Find the previous connection, field, or block.
* @return {ASTNode} The previous element, or null if the current node
* is not set or there is no previous value.
* @public
*/
prev() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
let newNode = curNode.prev();
/**
* Find the in connection or field.
* @return {ASTNode} The in element, or null if the current node is
* not set or there is no in value.
* @public
*/
Cursor.prototype.in = function() {
let curNode = this.getCurNode();
if (!curNode) {
return null;
}
// If we are on a previous or output connection, go to the block level before
// performing next operation.
if (curNode.getType() === ASTNode.types.PREVIOUS ||
curNode.getType() === ASTNode.types.OUTPUT) {
curNode = curNode.next();
}
const newNode = curNode.in();
while (newNode && newNode.prev() &&
(newNode.getType() === ASTNode.types.NEXT ||
newNode.getType() === ASTNode.types.BLOCK)) {
newNode = newNode.prev();
}
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
/**
* Find the previous connection, field, or block.
* @return {ASTNode} The previous element, or null if the current node
* is not set or there is no previous value.
* @public
*/
Cursor.prototype.prev = function() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
let newNode = curNode.prev();
while (newNode && newNode.prev() &&
(newNode.getType() === ASTNode.types.NEXT ||
newNode.getType() === ASTNode.types.BLOCK)) {
newNode = newNode.prev();
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
/**
* Find the out connection, field, or block.
* @return {ASTNode} The out element, or null if the current node is
* not set or there is no out value.
* @public
*/
out() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
let newNode = curNode.out();
/**
* Find the out connection, field, or block.
* @return {ASTNode} The out element, or null if the current node is
* not set or there is no out value.
* @public
*/
Cursor.prototype.out = function() {
const curNode = this.getCurNode();
if (!curNode) {
return null;
}
let newNode = curNode.out();
if (newNode && newNode.getType() === ASTNode.types.BLOCK) {
newNode = newNode.prev() || newNode;
}
if (newNode && newNode.getType() === ASTNode.types.BLOCK) {
newNode = newNode.prev() || newNode;
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
}
if (newNode) {
this.setCurNode(newNode);
}
return newNode;
};
}
registry.register(registry.Type.CURSOR, registry.DEFAULT, Cursor);

View File

@@ -26,105 +26,108 @@ const {MarkerSvg} = goog.requireType('Blockly.blockRendering.MarkerSvg');
/**
* Class for a marker.
* This is used in keyboard navigation to save a location in the Blockly AST.
* @constructor
* @alias Blockly.Marker
*/
const Marker = function() {
class Marker {
/**
* The colour of the marker.
* @type {?string}
* @alias Blockly.Marker
*/
this.colour = null;
constructor() {
/**
* The colour of the marker.
* @type {?string}
*/
this.colour = null;
/**
* The current location of the marker.
* @type {ASTNode}
* @private
*/
this.curNode_ = null;
/**
* The object in charge of drawing the visual representation of the current
* node.
* @type {MarkerSvg}
* @private
*/
this.drawer_ = null;
/**
* The type of the marker.
* @type {string}
*/
this.type = 'marker';
}
/**
* The current location of the marker.
* @type {ASTNode}
* @private
* Sets the object in charge of drawing the marker.
* @param {MarkerSvg} drawer The object in charge of
* drawing the marker.
*/
this.curNode_ = null;
setDrawer(drawer) {
this.drawer_ = drawer;
}
/**
* The object in charge of drawing the visual representation of the current
* node.
* @type {MarkerSvg}
* @private
* Get the current drawer for the marker.
* @return {MarkerSvg} The object in charge of drawing
* the marker.
*/
this.drawer_ = null;
getDrawer() {
return this.drawer_;
}
/**
* The type of the marker.
* @type {string}
* Gets the current location of the marker.
* @return {ASTNode} The current field, connection, or block the marker
* is on.
*/
this.type = 'marker';
};
/**
* Sets the object in charge of drawing the marker.
* @param {MarkerSvg} drawer The object in charge of
* drawing the marker.
*/
Marker.prototype.setDrawer = function(drawer) {
this.drawer_ = drawer;
};
/**
* Get the current drawer for the marker.
* @return {MarkerSvg} The object in charge of drawing
* the marker.
*/
Marker.prototype.getDrawer = function() {
return this.drawer_;
};
/**
* Gets the current location of the marker.
* @return {ASTNode} The current field, connection, or block the marker
* is on.
*/
Marker.prototype.getCurNode = function() {
return this.curNode_;
};
/**
* Set the location of the marker and call the update method.
* Setting isStack to true will only work if the newLocation is the top most
* output or previous connection on a stack.
* @param {ASTNode} newNode The new location of the marker.
*/
Marker.prototype.setCurNode = function(newNode) {
const oldNode = this.curNode_;
this.curNode_ = newNode;
if (this.drawer_) {
this.drawer_.draw(oldNode, this.curNode_);
getCurNode() {
return this.curNode_;
}
};
/**
* Redraw the current marker.
* @package
*/
Marker.prototype.draw = function() {
if (this.drawer_) {
this.drawer_.draw(this.curNode_, this.curNode_);
/**
* Set the location of the marker and call the update method.
* Setting isStack to true will only work if the newLocation is the top most
* output or previous connection on a stack.
* @param {ASTNode} newNode The new location of the marker.
*/
setCurNode(newNode) {
const oldNode = this.curNode_;
this.curNode_ = newNode;
if (this.drawer_) {
this.drawer_.draw(oldNode, this.curNode_);
}
}
};
/**
* Hide the marker SVG.
*/
Marker.prototype.hide = function() {
if (this.drawer_) {
this.drawer_.hide();
/**
* Redraw the current marker.
* @package
*/
draw() {
if (this.drawer_) {
this.drawer_.draw(this.curNode_, this.curNode_);
}
}
};
/**
* Dispose of this marker.
*/
Marker.prototype.dispose = function() {
if (this.getDrawer()) {
this.getDrawer().dispose();
/**
* Hide the marker SVG.
*/
hide() {
if (this.drawer_) {
this.drawer_.hide();
}
}
};
/**
* Dispose of this marker.
*/
dispose() {
if (this.getDrawer()) {
this.getDrawer().dispose();
}
}
}
exports.Marker = Marker;

View File

@@ -17,7 +17,6 @@
*/
goog.module('Blockly.TabNavigateCursor');
const object = goog.require('Blockly.utils.object');
const {ASTNode} = goog.require('Blockly.ASTNode');
const {BasicCursor} = goog.require('Blockly.BasicCursor');
/* eslint-disable-next-line no-unused-vars */
@@ -26,32 +25,34 @@ const {Field} = goog.requireType('Blockly.Field');
/**
* A cursor for navigating between tab navigable fields.
* @constructor
* @extends {BasicCursor}
* @alias Blockly.TabNavigateCursor
*/
const TabNavigateCursor = function() {
TabNavigateCursor.superClass_.constructor.call(this);
};
object.inherits(TabNavigateCursor, BasicCursor);
/**
* Skip all nodes except for tab navigable fields.
* @param {?ASTNode} node The AST node to check whether it is valid.
* @return {boolean} True if the node should be visited, false otherwise.
* @override
*/
TabNavigateCursor.prototype.validNode_ = function(node) {
let isValid = false;
const type = node && node.getType();
if (node) {
const location = /** @type {Field} */ (node.getLocation());
if (type === ASTNode.types.FIELD && location && location.isTabNavigable() &&
location.isClickable()) {
isValid = true;
}
class TabNavigateCursor extends BasicCursor {
/**
* @alias Blockly.TabNavigateCursor
*/
constructor() {
super();
}
return isValid;
};
/**
* Skip all nodes except for tab navigable fields.
* @param {?ASTNode} node The AST node to check whether it is valid.
* @return {boolean} True if the node should be visited, false otherwise.
* @override
*/
validNode_(node) {
let isValid = false;
const type = node && node.getType();
if (node) {
const location = /** @type {Field} */ (node.getLocation());
if (type === ASTNode.types.FIELD && location &&
location.isTabNavigable() && location.isClickable()) {
isValid = true;
}
}
return isValid;
}
}
exports.TabNavigateCursor = TabNavigateCursor;