mirror of
https://github.com/google/blockly.git
synced 2026-01-07 17:10:11 +01:00
chore: Migrate core/ to Typescript, actually (#6299)
* fix: convert files to typescript * fix: add alias for AnyDuringMigration so that tsc will run * chore: format * chore: enable ts for the clang-format workflow (#6233) * chore: Restore @fileoverview comment locations (#6237) * chore: add declareModuleId (#6238) * fix: Revert comment change to app_controller.js (#6241) * fix: Add missing import goog statements (#6240) I've added the import statement immediately before the goog.declareModuleId calls that depend on it. There is an argument to be made that we should put the import statement in their normal place amongst any other imports, and move the declareModuleId statement to below the double blank line below the imports, but as these are so tightly coupled, replace the previous goog.module calls, and will both be deleted at the same time once the transition to TypeScript is fully complete I think it's fine (and certainly much easier) to do it this way. * chore: Fix whitespace (#6243) * fix: Remove spurious blank lines Remove extraneous blank lines introduced by deletion of 'use strict'; pragmas. Also fix the location of the goog.declareModuleId call in core/utils/array.ts. * fix: Add missing double-blank-line before body of modules Our convention is to have two blank lines between the imports (or module ID, if there are no imports) and the beginning of the body of the module. Enforce this. * fix: one addition format error for PR #6243 * fix(build): Skip npm prepare when running in CI (#6244) Have npm prepare do nothing when running in CI. We don't need to do any building, because npm test will build everything needed in the workflows in which it is run, and we don't want to build anything in other workflows because a tsc error would prevent those workflows from completing. * fix: re-add `@package` annotations as `@internal` annotations (#6232) * fix: add ~70% of internal attributes * fix: work on manually adding more @internal annotations * fix: add more manual internal annotations * fix: rename package typos to internal * fix: final manual fixes for internal annotations * chore: format * chore: make unnecessary multiline jsdoc a single line * fix: fix internal tags in serialization exceptions * fix: tsc errors picked up from develop (#6224) * fix: relative path for deprecation utils * fix: checking if properties exist in svg_math * fix: set all timeout PIDs to AnyDuringMigration * fix: make nullability errors explicity in block drag surface * fix: make null check in events_block_change explicit * fix: make getEventWorkspace_ internal so we can access it from CommentCreateDeleteHelper * fix: rename DIV -> containerDiv in tooltip * fix: ignore backwards compat check in category * fix: set block styles to AnyDuringMigration * fix: type typo in KeyboardShortcut * fix: constants name in row measurables * fix: typecast in mutator * fix: populateProcedures type of flattened array * fix: ignore errors related to workspace comment deserialization * chore: format files * fix: renaming imports missing file extensions * fix: remove check for sound.play * fix: temporarily remove bad requireType. All `export type` statements are stripped when tsc is run. This means that when we attempt to require BlockDefinition from the block files, we get an error because it does not exist. We decided to temporarily remove the require, because this will no longer be a problem when we conver the blocks to typescript, and everything gets compiled together. * fix: bad jsdoc in array * fix: silence missing property errors Closure was complaining about inexistant properties, but they actually do exist, they're just not being transpiled by tsc in a way that closure understands. I.E. if things are initialized in a function called by the constructor, rather than in a class field or in the custructor itself, closure would error. It would also error on enums, because they are transpiled to a weird IIFE. * fix: context menu action handler not knowing the type of this. this: TypeX information gets stripped when tsc is run, so closure could not know that this was not global. Fixed this by reorganizing to use the option object directly instead of passing it to onAction to be bound to this. * fix: readd getDeveloperVars checks (should not be part of migration) This was found because ALL_DEVELOPER_VARS_WARNINGS_BY_BLOCK_TYPE was no longer being accessed. * fix: silence closure errors about overriding supertype props We propertly define the overrides in typescript, but these get removed from the compiled output, so closure doesn't know they exist. * fix: silence globalThis errors this: TypeX annotations get stripped from the compiled output, so closure can't know that we're accessing the correct things. However, typescript makes sure that this always has the correct properties, so silencing this should be fine. * fix: bad jsdoc name * chore: attempt compiling with blockly.js * fix: attempt moving the import statement above the namespace line * chore: add todo comments to block def files * chore: remove todo from context menu * chore: add comments abotu disabled errors * chore: move comments back to their correct positions (#6249) * fix: work on fixing comments * chore: finish moving all comments * chore: format * chore: move some other messed up comments * chore: format * fix: Correct enum formatting, use merged `namespace`s for types that are class static members (#6246) * fix: formatting of enum KeyCodes * fix: Use merged namespace for ContextMenuRegistry static types - Create a namespace to be merged with the ContextMenuRegistry class containing the types that were formerly declared as static properties on that class. - Use type aliases to export them individually as well, for compatibility with the changes made by MigranTS (and/or @gonfunko) to how other modules in core/ now import these types. - Update renamings.json5 to reflect the availability of the direct exports for modules that import this module directly (though they are not available to, and will not be used by, code that imports only via blockly.js/blockly.ts.) * fix: Use merged namespace for Input.Align - Create a merged namespace for the Input.Align enum. - Use type/const aliases to export it as Input too. - Update renamings.json5 to reflect the availability of the direct export. * fix: Use merged namespace for Names.NameType - Create a merged namespace for the Names.NameType enum. - Use type/const aliases to export it as NameType too. - Update renamings.json5 to reflect the availability of the direct export. (This ought to have happened in an earlier version as it was already available by both routes.) * chore: Fix minor issues for PR #6246 - Use `Align` instead of `Input.Align` where possible. * fix(build): Suppress irrelevant JSC_UNUSED_LOCAL_ASSIGNMENT errors tsc generates code for merged namespaces that looks like: (function (ClassName) { let EnumName; (function (EnumName) { EnumName[EnumNameAlign["v1"] = 0] = "v1"; // etc. })(EnumName = ClassName.EnumName || (ClassName.EnumName = {})); })(ClassName || (ClassName = {})); and Closure Compiler complains about the fact that the EnumName let binding is initialised but never used. (It exists so that any other code that was in the namespace could see the enum.) Suppress this message, since it is not actionable and lint and/or tsc should tell us if we have actual unused variables in our .ts files. * chore(build): Suppress spurious warnings from closure-make-deps (#6253) A little bit of an ugly hack, but it works: pipe stderr through grep -v to suppress error output starting with "WARNING in". * fix: remaining enums that weren't properly exported (#6251) * fix: remaining enums that weren't properly exported * chore: format * fix: add enum value exports * chore: format * fix: properly export interfaces that were typedefs (#6250) * fix: properly export interfaces that were typedefs * fix: allowCollsion -> allowCollision * fix: convert unconverted enums * fix: enums that were/are instance properties * fix: revert changes to property enums * fix: renamed protected parameter properties (#6252) * fix: bad protected parameter properties * chore:format * fix: gesture constructor * fix: overridden properties that were renamed * refactor: Migrate `blockly.js` to TypeScript (#6261) * chore: Apply changes to blockly.js to blockly.ts * fix: Build using core/blockly.ts instead of .js Compiles and runs in compressed mode correctly! * fix(build): Don't depend on execSync running bash (#6262) For some reason on Github CI servers execSync uses /bin/sh, which is (on Ubuntu) dash rather than bash, and does not understand the pipefail option. So remove the grep pipe on stderr and just discard all error output at all. This is not ideal as errors in test deps will go unreported AND not even cause test failure, but it's not clear that it's worth investing more time to fix this at the moment. * chore: use `import type` where possible (#6279) * chore: automatically change imports to import types * chore: revert changes that actually need to be imports * chore: format * chore: add more import type statements based on importsNotUsedAsValues * chore: fix tsconfig * chore: add link to compiler issue * fix: add type information to blockly options (#6283) * fix: add type information to blockly options * chore: format * chore: remove erroneous comment * fix: bugs revealed by getting the built output working (#6282) * fix: types of compose and decompose in block * fix: workspace naming in toolbox * chore: add jsdoc * chore: restore registry comments to better positions * chore: pr comments' * fix(variables): Revert inadvertent change to allDeveloperVariables (#6290) It appears that a function call got modified incorrectly (probably in an effort to fix a typing issue). This fix trivially reverts the line in question to match the original JS version from develop. This causes the generator tests to pass. * fix: circular dependencies (#6281) * chore: fix circular dependencies w/ static workspace funcs * remove preserved imports that aren't currently necessary (probably) * fix circular dependency with workspaces and block using stub * fix dependency between variables and xml by moving function to utils * add stub for trashcan as well * fix line endings from rebase * fix goog/base order * add trashcan patch * fix: types of compose and decompose in block * fix: workspace naming in toolbox * chore: add jsdoc * chore: restore registry comments to better positions * chore: remove implementations in goog.js * chore: fix types of stubs * chore: remove added AnyDuringMigration casts * chore: remove modifications to xml and variables * chore: format * chore: remove event requirements in workspace comments * chore: fix circular dependency with xml and workspace comments * fixup remove ContextMenu import * chore: fix dependency between mutator and workspace * chore: break circular dependency between names and procedures * chore: get tests to run? * chore: pr comments' * chore: fix stubbing field registry fromJson * chore: fix spying on fire * chore: fix stubbing parts of connection checker * chore: fix stubbing dialog * chore: fix stubbing style * chore: fix spying on duplicate * chore: fix stubbing variables * chore: fix stubbing copy * chore: fix stubbing in workspace * chore: remove unnecessary stubs * chore: fix formatting * chore: fix other formatting * chore: add backwards compatible static properties to workspace * chore: move static type properties * chore: move and comment stubs * chore: add newlines at EOF * chore: improve errors for monkey patched functions * chore: update comment with a pointer to the doc * chore: update comment with a pointer to the doc * chore: format * chore: revert changes to playground used for testing (#6292) * chore: get mocha tests to pass. (#6291) * chore: fix undo and empty code blocks * chore: skip IE test * chore: fix gesture test * chore: fix replace message references test * chore: fix string table interpolation * chore: skip getById tests * chore: fix field tests * chore: fix console errors by making workspace nullable * chore: format * chore: fix definition overwrite warning * chore: update metadata * chore: temporarily modify the the advanced compile test * chore: fix gestures by fixing test instead Co-authored-by: Neil Fraser <fraser@google.com> Co-authored-by: Christopher Allen <cpcallen+git@google.com>
This commit is contained in:
@@ -8,31 +8,24 @@
|
||||
* @fileoverview The class representing an AST node.
|
||||
* Used to traverse the Blockly AST.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The class representing an AST node.
|
||||
* Used to traverse the Blockly AST.
|
||||
* @class
|
||||
*/
|
||||
goog.module('Blockly.ASTNode');
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.ASTNode');
|
||||
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Block} = goog.requireType('Blockly.Block');
|
||||
const {ConnectionType} = goog.require('Blockly.ConnectionType');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Connection} = goog.requireType('Blockly.Connection');
|
||||
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Field} = goog.requireType('Blockly.Field');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {IASTNodeLocationWithBlock} = goog.requireType('Blockly.IASTNodeLocationWithBlock');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {IASTNodeLocation} = goog.requireType('Blockly.IASTNodeLocation');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Input} = goog.requireType('Blockly.Input');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Workspace} = goog.requireType('Blockly.Workspace');
|
||||
import type {Block} from '../block.js';
|
||||
import type {Connection} from '../connection.js';
|
||||
import {ConnectionType} from '../connection_type.js';
|
||||
import type {Field} from '../field.js';
|
||||
import type {Input} from '../input.js';
|
||||
import type {IASTNodeLocation} from '../interfaces/i_ast_node_location.js';
|
||||
import type {IASTNodeLocationWithBlock} from '../interfaces/i_ast_node_location_with_block.js';
|
||||
import {Coordinate} from '../utils/coordinate.js';
|
||||
import type {Workspace} from '../workspace.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,15 +34,34 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
|
||||
* creating a node directly.
|
||||
* @alias Blockly.ASTNode
|
||||
*/
|
||||
class ASTNode {
|
||||
export class ASTNode {
|
||||
/**
|
||||
* @param {string} type The type of the location.
|
||||
* True to navigate to all fields. False to only navigate to clickable fields.
|
||||
*/
|
||||
static NAVIGATE_ALL_FIELDS = false;
|
||||
|
||||
/**
|
||||
* The default y offset to use when moving the cursor from a stack to the
|
||||
* workspace.
|
||||
*/
|
||||
private static readonly DEFAULT_OFFSET_Y: number = -20;
|
||||
private readonly type_: string;
|
||||
private readonly isConnection_: boolean;
|
||||
private readonly location_: IASTNodeLocation;
|
||||
|
||||
/** The coordinate on the workspace. */
|
||||
// AnyDuringMigration because: Type 'null' is not assignable to type
|
||||
// 'Coordinate'.
|
||||
private wsCoordinate_: Coordinate = null as AnyDuringMigration;
|
||||
|
||||
/**
|
||||
* @param type The type of the location.
|
||||
* Must be in ASTNode.types.
|
||||
* @param {!IASTNodeLocation} location The position in the AST.
|
||||
* @param {!ASTNode.Params=} opt_params Optional dictionary of options.
|
||||
* @param location The position in the AST.
|
||||
* @param opt_params Optional dictionary of options.
|
||||
* @alias Blockly.ASTNode
|
||||
*/
|
||||
constructor(type, location, opt_params) {
|
||||
constructor(type: string, location: IASTNodeLocation, opt_params?: Params) {
|
||||
if (!location) {
|
||||
throw Error('Cannot create a node without a location.');
|
||||
}
|
||||
@@ -57,41 +69,23 @@ class ASTNode {
|
||||
/**
|
||||
* The type of the location.
|
||||
* One of ASTNode.types
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
this.type_ = type;
|
||||
|
||||
/**
|
||||
* Whether the location points to a connection.
|
||||
* @type {boolean}
|
||||
* @private
|
||||
*/
|
||||
/** Whether the location points to a connection. */
|
||||
this.isConnection_ = ASTNode.isConnectionType_(type);
|
||||
|
||||
/**
|
||||
* The location of the AST node.
|
||||
* @type {!IASTNodeLocation}
|
||||
* @private
|
||||
*/
|
||||
/** The location of the AST node. */
|
||||
this.location_ = location;
|
||||
|
||||
/**
|
||||
* The coordinate on the workspace.
|
||||
* @type {Coordinate}
|
||||
* @private
|
||||
*/
|
||||
this.wsCoordinate_ = null;
|
||||
|
||||
this.processParams_(opt_params || null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the optional parameters.
|
||||
* @param {?ASTNode.Params} params The user specified parameters.
|
||||
* @private
|
||||
* @param params The user specified parameters.
|
||||
*/
|
||||
processParams_(params) {
|
||||
private processParams_(params: Params|null) {
|
||||
if (!params) {
|
||||
return;
|
||||
}
|
||||
@@ -104,37 +98,37 @@ class ASTNode {
|
||||
* Gets the value pointed to by this node.
|
||||
* It is the callers responsibility to check the node type to figure out what
|
||||
* type of object they get back from this.
|
||||
* @return {!IASTNodeLocation} The current field, connection, workspace, or
|
||||
* block the cursor is on.
|
||||
* @return The current field, connection, workspace, or block the cursor is
|
||||
* on.
|
||||
*/
|
||||
getLocation() {
|
||||
getLocation(): IASTNodeLocation {
|
||||
return this.location_;
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of the current location.
|
||||
* One of ASTNode.types
|
||||
* @return {string} The type of the location.
|
||||
* @return The type of the location.
|
||||
*/
|
||||
getType() {
|
||||
getType(): string {
|
||||
return this.type_;
|
||||
}
|
||||
|
||||
/**
|
||||
* The coordinate on the workspace.
|
||||
* @return {Coordinate} The workspace coordinate or null if the
|
||||
* location is not a workspace.
|
||||
* @return The workspace coordinate or null if the location is not a
|
||||
* workspace.
|
||||
*/
|
||||
getWsCoordinate() {
|
||||
getWsCoordinate(): Coordinate {
|
||||
return this.wsCoordinate_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the node points to a connection.
|
||||
* @return {boolean} [description]
|
||||
* @package
|
||||
* @return [description]
|
||||
* @internal
|
||||
*/
|
||||
isConnection() {
|
||||
isConnection(): boolean {
|
||||
return this.isConnection_;
|
||||
}
|
||||
|
||||
@@ -142,18 +136,18 @@ class ASTNode {
|
||||
* Given an input find the next editable field or an input with a non null
|
||||
* connection in the same block. The current location must be an input
|
||||
* connection.
|
||||
* @return {ASTNode} The AST node holding the next field or connection
|
||||
* or null if there is no editable field or input connection after the
|
||||
* given input.
|
||||
* @private
|
||||
* @return The AST node holding the next field or connection or null if there
|
||||
* is no editable field or input connection after the given input.
|
||||
*/
|
||||
findNextForInput_() {
|
||||
const location = /** @type {!Connection} */ (this.location_);
|
||||
private findNextForInput_(): ASTNode|null {
|
||||
const location = this.location_ as Connection;
|
||||
const parentInput = location.getParentInput();
|
||||
const block = parentInput.getSourceBlock();
|
||||
const curIdx = block.inputList.indexOf(parentInput);
|
||||
for (let i = curIdx + 1; i < block.inputList.length; i++) {
|
||||
const input = block.inputList[i];
|
||||
const block = parentInput!.getSourceBlock();
|
||||
// AnyDuringMigration because: Argument of type 'Input | null' is not
|
||||
// assignable to parameter of type 'Input'.
|
||||
const curIdx = block!.inputList.indexOf(parentInput as AnyDuringMigration);
|
||||
for (let i = curIdx + 1; i < block!.inputList.length; i++) {
|
||||
const input = block!.inputList[i];
|
||||
const fieldRow = input.fieldRow;
|
||||
for (let j = 0; j < fieldRow.length; j++) {
|
||||
const field = fieldRow[j];
|
||||
@@ -171,16 +165,14 @@ class ASTNode {
|
||||
/**
|
||||
* Given a field find the next editable field or an input with a non null
|
||||
* connection in the same block. The current location must be a field.
|
||||
* @return {ASTNode} The AST node pointing to the next field or
|
||||
* connection or null if there is no editable field or input connection
|
||||
* after the given input.
|
||||
* @private
|
||||
* @return The AST node pointing to the next field or connection or null if
|
||||
* there is no editable field or input connection after the given input.
|
||||
*/
|
||||
findNextForField_() {
|
||||
const location = /** @type {!Field} */ (this.location_);
|
||||
private findNextForField_(): ASTNode|null {
|
||||
const location = this.location_ as Field;
|
||||
const input = location.getParentInput();
|
||||
const block = location.getSourceBlock();
|
||||
const curIdx = block.inputList.indexOf(/** @type {!Input} */ (input));
|
||||
const curIdx = block.inputList.indexOf((input));
|
||||
let fieldIdx = input.fieldRow.indexOf(location) + 1;
|
||||
for (let i = curIdx; i < block.inputList.length; i++) {
|
||||
const newInput = block.inputList[i];
|
||||
@@ -203,17 +195,17 @@ class ASTNode {
|
||||
* Given an input find the previous editable field or an input with a non null
|
||||
* connection in the same block. The current location must be an input
|
||||
* connection.
|
||||
* @return {ASTNode} The AST node holding the previous field or
|
||||
* connection.
|
||||
* @private
|
||||
* @return The AST node holding the previous field or connection.
|
||||
*/
|
||||
findPrevForInput_() {
|
||||
const location = /** @type {!Connection} */ (this.location_);
|
||||
private findPrevForInput_(): ASTNode|null {
|
||||
const location = this.location_ as Connection;
|
||||
const parentInput = location.getParentInput();
|
||||
const block = parentInput.getSourceBlock();
|
||||
const curIdx = block.inputList.indexOf(parentInput);
|
||||
const block = parentInput!.getSourceBlock();
|
||||
// AnyDuringMigration because: Argument of type 'Input | null' is not
|
||||
// assignable to parameter of type 'Input'.
|
||||
const curIdx = block!.inputList.indexOf(parentInput as AnyDuringMigration);
|
||||
for (let i = curIdx; i >= 0; i--) {
|
||||
const input = block.inputList[i];
|
||||
const input = block!.inputList[i];
|
||||
if (input.connection && input !== parentInput) {
|
||||
return ASTNode.createInputNode(input);
|
||||
}
|
||||
@@ -231,15 +223,13 @@ class ASTNode {
|
||||
/**
|
||||
* Given a field find the previous editable field or an input with a non null
|
||||
* connection in the same block. The current location must be a field.
|
||||
* @return {ASTNode} The AST node holding the previous input or field.
|
||||
* @private
|
||||
* @return The AST node holding the previous input or field.
|
||||
*/
|
||||
findPrevForField_() {
|
||||
const location = /** @type {!Field} */ (this.location_);
|
||||
private findPrevForField_(): ASTNode|null {
|
||||
const location = this.location_ as Field;
|
||||
const parentInput = location.getParentInput();
|
||||
const block = location.getSourceBlock();
|
||||
const curIdx = block.inputList.indexOf(
|
||||
/** @type {!Input} */ (parentInput));
|
||||
const curIdx = block.inputList.indexOf((parentInput));
|
||||
let fieldIdx = parentInput.fieldRow.indexOf(location) - 1;
|
||||
for (let i = curIdx; i >= 0; i--) {
|
||||
const input = block.inputList[i];
|
||||
@@ -264,28 +254,25 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Navigate between stacks of blocks on the workspace.
|
||||
* @param {boolean} forward True to go forward. False to go backwards.
|
||||
* @return {ASTNode} The first block of the next stack or null if there
|
||||
* are no blocks on the workspace.
|
||||
* @private
|
||||
* @param forward True to go forward. False to go backwards.
|
||||
* @return The first block of the next stack or null if there are no blocks on
|
||||
* the workspace.
|
||||
*/
|
||||
navigateBetweenStacks_(forward) {
|
||||
private navigateBetweenStacks_(forward: boolean): ASTNode|null {
|
||||
let curLocation = this.getLocation();
|
||||
// TODO(#6097): Use instanceof checks to exit early for values of
|
||||
// curLocation that don't make sense.
|
||||
if ((/** @type {!IASTNodeLocationWithBlock} */ (curLocation))
|
||||
.getSourceBlock) {
|
||||
curLocation = /** @type {!IASTNodeLocationWithBlock} */ (curLocation)
|
||||
.getSourceBlock();
|
||||
if ((curLocation as IASTNodeLocationWithBlock).getSourceBlock) {
|
||||
curLocation = (curLocation as IASTNodeLocationWithBlock).getSourceBlock();
|
||||
}
|
||||
// TODO(#6097): Use instanceof checks to exit early for values of
|
||||
// curLocation that don't make sense.
|
||||
const curLocationAsBlock = /** @type {!Block} */ (curLocation);
|
||||
const curLocationAsBlock = curLocation as Block;
|
||||
if (!curLocationAsBlock || !curLocationAsBlock.workspace) {
|
||||
return null;
|
||||
}
|
||||
const curRoot = curLocationAsBlock.getRootBlock();
|
||||
const topBlocks = curRoot.workspace.getTopBlocks(true);
|
||||
const topBlocks = curRoot.workspace?.getTopBlocks(true) ?? [];
|
||||
for (let i = 0; i < topBlocks.length; i++) {
|
||||
const topBlock = topBlocks[i];
|
||||
if (curRoot.id === topBlock.id) {
|
||||
@@ -305,30 +292,26 @@ class ASTNode {
|
||||
* Finds the top most AST node for a given block.
|
||||
* This is either the previous connection, output connection or block
|
||||
* depending on what kind of connections the block has.
|
||||
* @param {!Block} block The block that we want to find the top
|
||||
* connection on.
|
||||
* @return {!ASTNode} The AST node containing the top connection.
|
||||
* @private
|
||||
* @param block The block that we want to find the top connection on.
|
||||
* @return The AST node containing the top connection.
|
||||
*/
|
||||
findTopASTNodeForBlock_(block) {
|
||||
private findTopASTNodeForBlock_(block: Block): ASTNode|null {
|
||||
const topConnection = getParentConnection(block);
|
||||
if (topConnection) {
|
||||
return /** @type {!ASTNode} */ (
|
||||
ASTNode.createConnectionNode(topConnection));
|
||||
return ASTNode.createConnectionNode(topConnection);
|
||||
} else {
|
||||
return /** @type {!ASTNode} */ (ASTNode.createBlockNode(block));
|
||||
return ASTNode.createBlockNode(block);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the AST node pointing to the input that the block is nested under or if
|
||||
* the block is not nested then get the stack AST node.
|
||||
* @param {Block} block The source block of the current location.
|
||||
* @return {ASTNode} The AST node pointing to the input connection or
|
||||
* the top block of the stack this block is in.
|
||||
* @private
|
||||
* @param block The source block of the current location.
|
||||
* @return The AST node pointing to the input connection or the top block of
|
||||
* the stack this block is in.
|
||||
*/
|
||||
getOutAstNodeForBlock_(block) {
|
||||
private getOutAstNodeForBlock_(block: Block): ASTNode|null {
|
||||
if (!block) {
|
||||
return null;
|
||||
}
|
||||
@@ -340,8 +323,11 @@ class ASTNode {
|
||||
// that input.
|
||||
if (topConnection && topConnection.targetConnection &&
|
||||
topConnection.targetConnection.getParentInput()) {
|
||||
// AnyDuringMigration because: Argument of type 'Input | null' is not
|
||||
// assignable to parameter of type 'Input'.
|
||||
return ASTNode.createInputNode(
|
||||
topConnection.targetConnection.getParentInput());
|
||||
topConnection.targetConnection.getParentInput() as
|
||||
AnyDuringMigration);
|
||||
} else {
|
||||
// Go to stack level if you are not underneath an input.
|
||||
return ASTNode.createStackNode(topBlock);
|
||||
@@ -350,13 +336,12 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Find the first editable field or input with a connection on a given block.
|
||||
* @param {!Block} block The source block of the current location.
|
||||
* @return {ASTNode} An AST node pointing to the first field or input.
|
||||
* @param block The source block of the current location.
|
||||
* @return An AST node pointing to the first field or input.
|
||||
* Null if there are no editable fields or inputs with connections on the
|
||||
* block.
|
||||
* @private
|
||||
*/
|
||||
findFirstFieldOrInput_(block) {
|
||||
private findFirstFieldOrInput_(block: Block): ASTNode|null {
|
||||
const inputs = block.inputList;
|
||||
for (let i = 0; i < inputs.length; i++) {
|
||||
const input = inputs[i];
|
||||
@@ -376,34 +361,33 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Finds the source block of the location of this node.
|
||||
* @return {Block} The source block of the location, or null if the node
|
||||
* is of type workspace.
|
||||
* @return The source block of the location, or null if the node is of type
|
||||
* workspace.
|
||||
*/
|
||||
getSourceBlock() {
|
||||
getSourceBlock(): Block|null {
|
||||
if (this.getType() === ASTNode.types.BLOCK) {
|
||||
return /** @type {Block} */ (this.getLocation());
|
||||
return this.getLocation() as Block;
|
||||
} else if (this.getType() === ASTNode.types.STACK) {
|
||||
return /** @type {Block} */ (this.getLocation());
|
||||
return this.getLocation() as Block;
|
||||
} else if (this.getType() === ASTNode.types.WORKSPACE) {
|
||||
return null;
|
||||
} else {
|
||||
return /** @type {IASTNodeLocationWithBlock} */ (this.getLocation())
|
||||
.getSourceBlock();
|
||||
return (this.getLocation() as IASTNodeLocationWithBlock).getSourceBlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the element to the right of the current element in the AST.
|
||||
* @return {ASTNode} An AST node that wraps the next field, connection,
|
||||
* block, or workspace. Or null if there is no node to the right.
|
||||
* @return An AST node that wraps the next field, connection, block, or
|
||||
* workspace. Or null if there is no node to the right.
|
||||
*/
|
||||
next() {
|
||||
next(): ASTNode|null {
|
||||
switch (this.type_) {
|
||||
case ASTNode.types.STACK:
|
||||
return this.navigateBetweenStacks_(true);
|
||||
|
||||
case ASTNode.types.OUTPUT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return ASTNode.createBlockNode(connection.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.FIELD:
|
||||
@@ -413,18 +397,18 @@ class ASTNode {
|
||||
return this.findNextForInput_();
|
||||
|
||||
case ASTNode.types.BLOCK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
const nextConnection = block.nextConnection;
|
||||
return ASTNode.createConnectionNode(nextConnection);
|
||||
}
|
||||
case ASTNode.types.PREVIOUS: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return ASTNode.createBlockNode(connection.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.NEXT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
const targetConnection = connection.targetConnection;
|
||||
return ASTNode.createConnectionNode(targetConnection);
|
||||
return ASTNode.createConnectionNode(targetConnection!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -434,13 +418,13 @@ class ASTNode {
|
||||
/**
|
||||
* Find the element one level below and all the way to the left of the current
|
||||
* location.
|
||||
* @return {ASTNode} An AST node that wraps the next field, connection,
|
||||
* workspace, or block. Or null if there is nothing below this node.
|
||||
* @return An AST node that wraps the next field, connection, workspace, or
|
||||
* block. Or null if there is nothing below this node.
|
||||
*/
|
||||
in() {
|
||||
in(): ASTNode|null {
|
||||
switch (this.type_) {
|
||||
case ASTNode.types.WORKSPACE: {
|
||||
const workspace = /** @type {!Workspace} */ (this.location_);
|
||||
const workspace = this.location_ as Workspace;
|
||||
const topBlocks = workspace.getTopBlocks(true);
|
||||
if (topBlocks.length > 0) {
|
||||
return ASTNode.createStackNode(topBlocks[0]);
|
||||
@@ -448,17 +432,17 @@ class ASTNode {
|
||||
break;
|
||||
}
|
||||
case ASTNode.types.STACK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
return this.findTopASTNodeForBlock_(block);
|
||||
}
|
||||
case ASTNode.types.BLOCK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
return this.findFirstFieldOrInput_(block);
|
||||
}
|
||||
case ASTNode.types.INPUT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
const targetConnection = connection.targetConnection;
|
||||
return ASTNode.createConnectionNode(targetConnection);
|
||||
return ASTNode.createConnectionNode(targetConnection!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,11 +451,11 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Find the element to the left of the current element in the AST.
|
||||
* @return {ASTNode} An AST node that wraps the previous field,
|
||||
* connection, workspace or block. Or null if no node exists to the left.
|
||||
* @return An AST node that wraps the previous field, connection, workspace or
|
||||
* block. Or null if no node exists to the left.
|
||||
* null.
|
||||
*/
|
||||
prev() {
|
||||
prev(): ASTNode|null {
|
||||
switch (this.type_) {
|
||||
case ASTNode.types.STACK:
|
||||
return this.navigateBetweenStacks_(false);
|
||||
@@ -486,12 +470,12 @@ class ASTNode {
|
||||
return this.findPrevForInput_();
|
||||
|
||||
case ASTNode.types.BLOCK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
const topConnection = getParentConnection(block);
|
||||
return ASTNode.createConnectionNode(topConnection);
|
||||
}
|
||||
case ASTNode.types.PREVIOUS: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
const targetConnection = connection.targetConnection;
|
||||
if (targetConnection && !targetConnection.getParentInput()) {
|
||||
return ASTNode.createConnectionNode(targetConnection);
|
||||
@@ -499,7 +483,7 @@ class ASTNode {
|
||||
break;
|
||||
}
|
||||
case ASTNode.types.NEXT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return ASTNode.createBlockNode(connection.getSourceBlock());
|
||||
}
|
||||
}
|
||||
@@ -510,13 +494,13 @@ class ASTNode {
|
||||
/**
|
||||
* Find the next element that is one position above and all the way to the
|
||||
* left of the current location.
|
||||
* @return {ASTNode} An AST node that wraps the next field, connection,
|
||||
* workspace or block. Or null if we are at the workspace level.
|
||||
* @return An AST node that wraps the next field, connection, workspace or
|
||||
* block. Or null if we are at the workspace level.
|
||||
*/
|
||||
out() {
|
||||
out(): ASTNode|null {
|
||||
switch (this.type_) {
|
||||
case ASTNode.types.STACK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
const blockPos = block.getRelativeToSurfaceXY();
|
||||
// TODO: Make sure this is in the bounds of the workspace.
|
||||
const wsCoordinate =
|
||||
@@ -524,7 +508,7 @@ class ASTNode {
|
||||
return ASTNode.createWorkspaceNode(block.workspace, wsCoordinate);
|
||||
}
|
||||
case ASTNode.types.OUTPUT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
const target = connection.targetConnection;
|
||||
if (target) {
|
||||
return ASTNode.createConnectionNode(target);
|
||||
@@ -532,23 +516,23 @@ class ASTNode {
|
||||
return ASTNode.createStackNode(connection.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.FIELD: {
|
||||
const field = /** @type {!Field} */ (this.location_);
|
||||
const field = this.location_ as Field;
|
||||
return ASTNode.createBlockNode(field.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.INPUT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return ASTNode.createBlockNode(connection.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.BLOCK: {
|
||||
const block = /** @type {!Block} */ (this.location_);
|
||||
const block = this.location_ as Block;
|
||||
return this.getOutAstNodeForBlock_(block);
|
||||
}
|
||||
case ASTNode.types.PREVIOUS: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return this.getOutAstNodeForBlock_(connection.getSourceBlock());
|
||||
}
|
||||
case ASTNode.types.NEXT: {
|
||||
const connection = /** @type {!Connection} */ (this.location_);
|
||||
const connection = this.location_ as Connection;
|
||||
return this.getOutAstNodeForBlock_(connection.getSourceBlock());
|
||||
}
|
||||
}
|
||||
@@ -558,11 +542,10 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Whether an AST node of the given type points to a connection.
|
||||
* @param {string} type The type to check. One of ASTNode.types.
|
||||
* @return {boolean} True if a node of the given type points to a connection.
|
||||
* @private
|
||||
* @param type The type to check. One of ASTNode.types.
|
||||
* @return True if a node of the given type points to a connection.
|
||||
*/
|
||||
static isConnectionType_(type) {
|
||||
private static isConnectionType_(type: string): boolean {
|
||||
switch (type) {
|
||||
case ASTNode.types.PREVIOUS:
|
||||
case ASTNode.types.NEXT:
|
||||
@@ -575,10 +558,10 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Create an AST node pointing to a field.
|
||||
* @param {Field} field The location of the AST node.
|
||||
* @return {ASTNode} An AST node pointing to a field.
|
||||
* @param field The location of the AST node.
|
||||
* @return An AST node pointing to a field.
|
||||
*/
|
||||
static createFieldNode(field) {
|
||||
static createFieldNode(field: Field): ASTNode|null {
|
||||
if (!field) {
|
||||
return null;
|
||||
}
|
||||
@@ -589,20 +572,25 @@ class ASTNode {
|
||||
* Creates an AST node pointing to a connection. If the connection has a
|
||||
* parent input then create an AST node of type input that will hold the
|
||||
* connection.
|
||||
* @param {Connection} connection This is the connection the node will
|
||||
* point to.
|
||||
* @return {ASTNode} An AST node pointing to a connection.
|
||||
* @param connection This is the connection the node will point to.
|
||||
* @return An AST node pointing to a connection.
|
||||
*/
|
||||
static createConnectionNode(connection) {
|
||||
static createConnectionNode(connection: Connection): ASTNode|null {
|
||||
if (!connection) {
|
||||
return null;
|
||||
}
|
||||
const type = connection.type;
|
||||
if (type === ConnectionType.INPUT_VALUE) {
|
||||
return ASTNode.createInputNode(connection.getParentInput());
|
||||
// AnyDuringMigration because: Argument of type 'Input | null' is not
|
||||
// assignable to parameter of type 'Input'.
|
||||
return ASTNode.createInputNode(
|
||||
connection.getParentInput() as AnyDuringMigration);
|
||||
} else if (
|
||||
type === ConnectionType.NEXT_STATEMENT && connection.getParentInput()) {
|
||||
return ASTNode.createInputNode(connection.getParentInput());
|
||||
// AnyDuringMigration because: Argument of type 'Input | null' is not
|
||||
// assignable to parameter of type 'Input'.
|
||||
return ASTNode.createInputNode(
|
||||
connection.getParentInput() as AnyDuringMigration);
|
||||
} else if (type === ConnectionType.NEXT_STATEMENT) {
|
||||
return new ASTNode(ASTNode.types.NEXT, connection);
|
||||
} else if (type === ConnectionType.OUTPUT_VALUE) {
|
||||
@@ -616,10 +604,10 @@ class ASTNode {
|
||||
/**
|
||||
* Creates an AST node pointing to an input. Stores the input connection as
|
||||
* the location.
|
||||
* @param {Input} input The input used to create an AST node.
|
||||
* @return {ASTNode} An AST node pointing to a input.
|
||||
* @param input The input used to create an AST node.
|
||||
* @return An AST node pointing to a input.
|
||||
*/
|
||||
static createInputNode(input) {
|
||||
static createInputNode(input: Input): ASTNode|null {
|
||||
if (!input || !input.connection) {
|
||||
return null;
|
||||
}
|
||||
@@ -628,10 +616,10 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Creates an AST node pointing to a block.
|
||||
* @param {Block} block The block used to create an AST node.
|
||||
* @return {ASTNode} An AST node pointing to a block.
|
||||
* @param block The block used to create an AST node.
|
||||
* @return An AST node pointing to a block.
|
||||
*/
|
||||
static createBlockNode(block) {
|
||||
static createBlockNode(block: Block): ASTNode|null {
|
||||
if (!block) {
|
||||
return null;
|
||||
}
|
||||
@@ -642,12 +630,12 @@ class ASTNode {
|
||||
* Create an AST node of type stack. A stack, represented by its top block, is
|
||||
* the set of all blocks connected to a top block, including the top
|
||||
* block.
|
||||
* @param {Block} topBlock A top block has no parent and can be found
|
||||
* in the list returned by workspace.getTopBlocks().
|
||||
* @return {ASTNode} An AST node of type stack that points to the top
|
||||
* block on the stack.
|
||||
* @param topBlock A top block has no parent and can be found in the list
|
||||
* returned by workspace.getTopBlocks().
|
||||
* @return An AST node of type stack that points to the top block on the
|
||||
* stack.
|
||||
*/
|
||||
static createStackNode(topBlock) {
|
||||
static createStackNode(topBlock: Block): ASTNode|null {
|
||||
if (!topBlock) {
|
||||
return null;
|
||||
}
|
||||
@@ -656,28 +644,27 @@ class ASTNode {
|
||||
|
||||
/**
|
||||
* Creates an AST node pointing to a workspace.
|
||||
* @param {!Workspace} workspace The workspace that we are on.
|
||||
* @param {Coordinate} wsCoordinate The position on the workspace
|
||||
* for this node.
|
||||
* @return {ASTNode} An AST node pointing to a workspace and a position
|
||||
* on the workspace.
|
||||
* @param workspace The workspace that we are on.
|
||||
* @param wsCoordinate The position on the workspace for this node.
|
||||
* @return An AST node pointing to a workspace and a position on the
|
||||
* workspace.
|
||||
*/
|
||||
static createWorkspaceNode(workspace, wsCoordinate) {
|
||||
static createWorkspaceNode(
|
||||
workspace: Workspace|null, wsCoordinate: Coordinate|null): ASTNode|null {
|
||||
if (!wsCoordinate || !workspace) {
|
||||
return null;
|
||||
}
|
||||
const params = {wsCoordinate: wsCoordinate};
|
||||
const params = {wsCoordinate};
|
||||
return new ASTNode(ASTNode.types.WORKSPACE, workspace, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an AST node for the top position on a block.
|
||||
* This is either an output connection, previous connection, or block.
|
||||
* @param {!Block} block The block to find the top most AST node on.
|
||||
* @return {ASTNode} The AST node holding the top most position on the
|
||||
* block.
|
||||
* @param block The block to find the top most AST node on.
|
||||
* @return The AST node holding the top most position on the block.
|
||||
*/
|
||||
static createTopNode(block) {
|
||||
static createTopNode(block: Block): ASTNode|null {
|
||||
let astNode;
|
||||
const topConnection = getParentConnection(block);
|
||||
if (topConnection) {
|
||||
@@ -689,59 +676,42 @@ class ASTNode {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
* wsCoordinate: Coordinate
|
||||
* }}
|
||||
*/
|
||||
ASTNode.Params;
|
||||
export namespace ASTNode {
|
||||
export interface Params {
|
||||
wsCoordinate: Coordinate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Object holding different types for an AST node.
|
||||
* @enum {string}
|
||||
*/
|
||||
ASTNode.types = {
|
||||
FIELD: 'field',
|
||||
BLOCK: 'block',
|
||||
INPUT: 'input',
|
||||
OUTPUT: 'output',
|
||||
NEXT: 'next',
|
||||
PREVIOUS: 'previous',
|
||||
STACK: 'stack',
|
||||
WORKSPACE: 'workspace',
|
||||
};
|
||||
export enum types {
|
||||
FIELD = 'field',
|
||||
BLOCK = 'block',
|
||||
INPUT = 'input',
|
||||
OUTPUT = 'output',
|
||||
NEXT = 'next',
|
||||
PREVIOUS = 'previous',
|
||||
STACK = 'stack',
|
||||
WORKSPACE = 'workspace',
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True to navigate to all fields. False to only navigate to clickable fields.
|
||||
* @type {boolean}
|
||||
*/
|
||||
ASTNode.NAVIGATE_ALL_FIELDS = false;
|
||||
export type Params = ASTNode.Params;
|
||||
// No need to export ASTNode.types from the module at this time because (1) it
|
||||
// wasn't automatically converted by the automatic migration script, (2) the
|
||||
// name doesn't follow the styleguide.
|
||||
|
||||
/**
|
||||
* The default y offset to use when moving the cursor from a stack to the
|
||||
* workspace.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
ASTNode.DEFAULT_OFFSET_Y = -20;
|
||||
|
||||
/**
|
||||
* Gets the parent connection on a block.
|
||||
* This is either an output connection, previous connection or undefined.
|
||||
* If both connections exist return the one that is actually connected
|
||||
* to another block.
|
||||
* @param {!Block} block The block to find the parent connection on.
|
||||
* @return {Connection} The connection connecting to the parent of the
|
||||
* block.
|
||||
* @private
|
||||
* @param block The block to find the parent connection on.
|
||||
* @return The connection connecting to the parent of the block.
|
||||
*/
|
||||
const getParentConnection = function(block) {
|
||||
function getParentConnection(block: Block): Connection {
|
||||
let topConnection = block.outputConnection;
|
||||
if (!topConnection ||
|
||||
(block.previousConnection && block.previousConnection.isConnected())) {
|
||||
block.previousConnection && block.previousConnection.isConnected()) {
|
||||
topConnection = block.previousConnection;
|
||||
}
|
||||
return topConnection;
|
||||
};
|
||||
|
||||
exports.ASTNode = ASTNode;
|
||||
}
|
||||
|
||||
@@ -8,42 +8,42 @@
|
||||
* @fileoverview The class representing a basic cursor.
|
||||
* Used to demo switching between different cursors.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The class representing a basic cursor.
|
||||
* Used to demo switching between different cursors.
|
||||
* @class
|
||||
*/
|
||||
goog.module('Blockly.BasicCursor');
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.BasicCursor');
|
||||
|
||||
const registry = goog.require('Blockly.registry');
|
||||
const {ASTNode} = goog.require('Blockly.ASTNode');
|
||||
const {Cursor} = goog.require('Blockly.Cursor');
|
||||
import * as registry from '../registry.js';
|
||||
|
||||
import {ASTNode} from './ast_node.js';
|
||||
import {Cursor} from './cursor.js';
|
||||
|
||||
|
||||
/**
|
||||
* Class for a basic cursor.
|
||||
* This will allow the user to get to all nodes in the AST by hitting next or
|
||||
* previous.
|
||||
* @extends {Cursor}
|
||||
* @alias Blockly.BasicCursor
|
||||
*/
|
||||
class BasicCursor extends Cursor {
|
||||
/**
|
||||
* @alias Blockly.BasicCursor
|
||||
*/
|
||||
export class BasicCursor extends Cursor {
|
||||
/** Name used for registering a basic cursor. */
|
||||
static readonly registrationName = 'basicCursor';
|
||||
|
||||
/** @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
|
||||
* @return The next node, or null if the current node is not set or there is
|
||||
* no next value.
|
||||
*/
|
||||
next() {
|
||||
override next(): ASTNode|null {
|
||||
const curNode = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
@@ -60,21 +60,19 @@ class BasicCursor extends Cursor {
|
||||
* 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
|
||||
* @return The next node, or null if the current node is not set or there is
|
||||
* no next value.
|
||||
*/
|
||||
in() {
|
||||
override in(): ASTNode|null {
|
||||
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
|
||||
* @return The previous node, or null if the current node is not set or there
|
||||
* is no previous value.
|
||||
*/
|
||||
prev() {
|
||||
override prev(): ASTNode|null {
|
||||
const curNode = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
@@ -91,11 +89,10 @@ class BasicCursor extends Cursor {
|
||||
* 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
|
||||
* @return The previous node, or null if the current node is not set or there
|
||||
* is no previous value.
|
||||
*/
|
||||
out() {
|
||||
override out(): ASTNode|null {
|
||||
return this.prev();
|
||||
}
|
||||
|
||||
@@ -103,13 +100,14 @@ class BasicCursor extends Cursor {
|
||||
* 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
|
||||
* @param node The current position in the AST.
|
||||
* @param isValid A function true/false depending on whether the given node
|
||||
* should be traversed.
|
||||
* @return The next node in the traversal.
|
||||
*/
|
||||
getNextNode_(node, isValid) {
|
||||
protected getNextNode_(
|
||||
node: ASTNode|null, isValid: (p1: ASTNode|null) => boolean): ASTNode
|
||||
|null {
|
||||
if (!node) {
|
||||
return null;
|
||||
}
|
||||
@@ -120,7 +118,9 @@ class BasicCursor extends Cursor {
|
||||
return this.getNextNode_(newNode, isValid);
|
||||
}
|
||||
const siblingOrParent = this.findSiblingOrParent_(node.out());
|
||||
if (isValid(siblingOrParent)) {
|
||||
// AnyDuringMigration because: Argument of type 'ASTNode | null' is not
|
||||
// assignable to parameter of type 'ASTNode'.
|
||||
if (isValid(siblingOrParent as AnyDuringMigration)) {
|
||||
return siblingOrParent;
|
||||
} else if (siblingOrParent) {
|
||||
return this.getNextNode_(siblingOrParent, isValid);
|
||||
@@ -132,18 +132,19 @@ class BasicCursor extends Cursor {
|
||||
* 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
|
||||
* @param node The current position in the AST.
|
||||
* @param isValid A function true/false depending on whether the given node
|
||||
* should be traversed.
|
||||
* @return The previous node in the traversal or null if no previous node
|
||||
* exists.
|
||||
*/
|
||||
getPreviousNode_(node, isValid) {
|
||||
protected getPreviousNode_(
|
||||
node: ASTNode|null, isValid: (p1: ASTNode|null) => boolean): ASTNode
|
||||
|null {
|
||||
if (!node) {
|
||||
return null;
|
||||
}
|
||||
let newNode = node.prev();
|
||||
let newNode: ASTNode|null = node.prev();
|
||||
|
||||
if (newNode) {
|
||||
newNode = this.getRightMostChild_(newNode);
|
||||
@@ -161,11 +162,10 @@ class BasicCursor extends Cursor {
|
||||
/**
|
||||
* 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
|
||||
* @param node The AST node to check whether it is valid.
|
||||
* @return True if the node should be visited, false otherwise.
|
||||
*/
|
||||
validNode_(node) {
|
||||
protected validNode_(node: ASTNode|null): boolean {
|
||||
let isValid = false;
|
||||
const type = node && node.getType();
|
||||
if (type === ASTNode.types.OUTPUT || type === ASTNode.types.INPUT ||
|
||||
@@ -178,12 +178,10 @@ class BasicCursor extends Cursor {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param node The current position in the AST.
|
||||
* @return The parent AST node or null if there are no valid parents.
|
||||
*/
|
||||
findSiblingOrParent_(node) {
|
||||
private findSiblingOrParent_(node: ASTNode|null): ASTNode|null {
|
||||
if (!node) {
|
||||
return null;
|
||||
}
|
||||
@@ -196,30 +194,21 @@ class BasicCursor extends Cursor {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param node The node to find the right most child of.
|
||||
* @return The right most child of the given node, or the node if no child
|
||||
* exists.
|
||||
*/
|
||||
getRightMostChild_(node) {
|
||||
if (!node.in()) {
|
||||
private getRightMostChild_(node: ASTNode|null): ASTNode|null {
|
||||
if (!node!.in()) {
|
||||
return node;
|
||||
}
|
||||
let newNode = node.in();
|
||||
while (newNode.next()) {
|
||||
let newNode = node!.in();
|
||||
while (newNode && newNode.next()) {
|
||||
newNode = newNode.next();
|
||||
}
|
||||
return this.getRightMostChild_(newNode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Name used for registering a basic cursor.
|
||||
* @const {string}
|
||||
*/
|
||||
BasicCursor.registrationName = 'basicCursor';
|
||||
|
||||
registry.register(
|
||||
registry.Type.CURSOR, BasicCursor.registrationName, BasicCursor);
|
||||
|
||||
exports.BasicCursor = BasicCursor;
|
||||
|
||||
@@ -8,45 +8,40 @@
|
||||
* @fileoverview The class representing a cursor.
|
||||
* Used primarily for keyboard navigation.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The class representing a cursor.
|
||||
* Used primarily for keyboard navigation.
|
||||
* @class
|
||||
*/
|
||||
goog.module('Blockly.Cursor');
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Cursor');
|
||||
|
||||
import * as registry from '../registry.js';
|
||||
|
||||
import {ASTNode} from './ast_node.js';
|
||||
import {Marker} from './marker.js';
|
||||
|
||||
const registry = goog.require('Blockly.registry');
|
||||
const {ASTNode} = goog.require('Blockly.ASTNode');
|
||||
const {Marker} = goog.require('Blockly.Marker');
|
||||
|
||||
/**
|
||||
* Class for a cursor.
|
||||
* A cursor controls how a user navigates the Blockly AST.
|
||||
* @extends {Marker}
|
||||
* @alias Blockly.Cursor
|
||||
*/
|
||||
class Cursor extends Marker {
|
||||
/**
|
||||
* @alias Blockly.Cursor
|
||||
*/
|
||||
export class Cursor extends Marker {
|
||||
override type = 'cursor';
|
||||
|
||||
/** @alias Blockly.Cursor */
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
/**
|
||||
* @override
|
||||
*/
|
||||
this.type = 'cursor';
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return The next element, or null if the current node is not set or there
|
||||
* is no next value.
|
||||
*/
|
||||
next() {
|
||||
next(): ASTNode|null {
|
||||
const curNode = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
@@ -67,12 +62,11 @@ class Cursor extends Marker {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return The in element, or null if the current node is not set or there is
|
||||
* no in value.
|
||||
*/
|
||||
in() {
|
||||
let curNode = this.getCurNode();
|
||||
in(): ASTNode|null {
|
||||
let curNode: ASTNode|null = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
}
|
||||
@@ -82,7 +76,7 @@ class Cursor extends Marker {
|
||||
curNode.getType() === ASTNode.types.OUTPUT) {
|
||||
curNode = curNode.next();
|
||||
}
|
||||
const newNode = curNode.in();
|
||||
const newNode = curNode?.in() ?? null;
|
||||
|
||||
if (newNode) {
|
||||
this.setCurNode(newNode);
|
||||
@@ -92,11 +86,10 @@ class Cursor extends Marker {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return The previous element, or null if the current node is not set or
|
||||
* there is no previous value.
|
||||
*/
|
||||
prev() {
|
||||
prev(): ASTNode|null {
|
||||
const curNode = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
@@ -117,11 +110,10 @@ class Cursor extends Marker {
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return The out element, or null if the current node is not set or there is
|
||||
* no out value.
|
||||
*/
|
||||
out() {
|
||||
out(): ASTNode|null {
|
||||
const curNode = this.getCurNode();
|
||||
if (!curNode) {
|
||||
return null;
|
||||
@@ -140,5 +132,3 @@ class Cursor extends Marker {
|
||||
}
|
||||
|
||||
registry.register(registry.Type.CURSOR, registry.DEFAULT, Cursor);
|
||||
|
||||
exports.Cursor = Cursor;
|
||||
|
||||
@@ -8,19 +8,19 @@
|
||||
* @fileoverview The class representing a marker.
|
||||
* Used primarily for keyboard navigation to show a marked location.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The class representing a marker.
|
||||
* Used primarily for keyboard navigation to show a marked location.
|
||||
* @class
|
||||
*/
|
||||
goog.module('Blockly.Marker');
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.Marker');
|
||||
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {ASTNode} = goog.requireType('Blockly.ASTNode');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {MarkerSvg} = goog.requireType('Blockly.blockRendering.MarkerSvg');
|
||||
import type {MarkerSvg} from '../renderers/common/marker_svg.js';
|
||||
|
||||
import type {ASTNode} from './ast_node.js';
|
||||
|
||||
|
||||
/**
|
||||
@@ -28,63 +28,50 @@ const {MarkerSvg} = goog.requireType('Blockly.blockRendering.MarkerSvg');
|
||||
* This is used in keyboard navigation to save a location in the Blockly AST.
|
||||
* @alias Blockly.Marker
|
||||
*/
|
||||
class Marker {
|
||||
export class Marker {
|
||||
/** The colour of the marker. */
|
||||
colour: string|null = null;
|
||||
|
||||
/** The current location of the marker. */
|
||||
// AnyDuringMigration because: Type 'null' is not assignable to type
|
||||
// 'ASTNode'.
|
||||
private curNode_: ASTNode = null as AnyDuringMigration;
|
||||
|
||||
/**
|
||||
* Constructs a new Marker instance.
|
||||
* The object in charge of drawing the visual representation of the current
|
||||
* node.
|
||||
*/
|
||||
constructor() {
|
||||
/**
|
||||
* The colour of the marker.
|
||||
* @type {?string}
|
||||
*/
|
||||
this.colour = null;
|
||||
// AnyDuringMigration because: Type 'null' is not assignable to type
|
||||
// 'MarkerSvg'.
|
||||
private drawer_: MarkerSvg = null as AnyDuringMigration;
|
||||
|
||||
/**
|
||||
* The current location of the marker.
|
||||
* @type {ASTNode}
|
||||
* @private
|
||||
*/
|
||||
this.curNode_ = null;
|
||||
/** The type of the marker. */
|
||||
type = 'marker';
|
||||
|
||||
/**
|
||||
* 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';
|
||||
}
|
||||
/** Constructs a new Marker instance. */
|
||||
constructor() {}
|
||||
|
||||
/**
|
||||
* Sets the object in charge of drawing the marker.
|
||||
* @param {MarkerSvg} drawer The object in charge of
|
||||
* drawing the marker.
|
||||
* @param drawer The object in charge of drawing the marker.
|
||||
*/
|
||||
setDrawer(drawer) {
|
||||
setDrawer(drawer: MarkerSvg) {
|
||||
this.drawer_ = drawer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current drawer for the marker.
|
||||
* @return {MarkerSvg} The object in charge of drawing
|
||||
* the marker.
|
||||
* @return The object in charge of drawing the marker.
|
||||
*/
|
||||
getDrawer() {
|
||||
getDrawer(): MarkerSvg {
|
||||
return this.drawer_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current location of the marker.
|
||||
* @return {ASTNode} The current field, connection, or block the marker
|
||||
* is on.
|
||||
* @return The current field, connection, or block the marker is on.
|
||||
*/
|
||||
getCurNode() {
|
||||
getCurNode(): ASTNode {
|
||||
return this.curNode_;
|
||||
}
|
||||
|
||||
@@ -92,9 +79,9 @@ class Marker {
|
||||
* 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.
|
||||
* @param newNode The new location of the marker.
|
||||
*/
|
||||
setCurNode(newNode) {
|
||||
setCurNode(newNode: ASTNode) {
|
||||
const oldNode = this.curNode_;
|
||||
this.curNode_ = newNode;
|
||||
if (this.drawer_) {
|
||||
@@ -104,7 +91,7 @@ class Marker {
|
||||
|
||||
/**
|
||||
* Redraw the current marker.
|
||||
* @package
|
||||
* @internal
|
||||
*/
|
||||
draw() {
|
||||
if (this.drawer_) {
|
||||
@@ -112,23 +99,17 @@ class Marker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide the marker SVG.
|
||||
*/
|
||||
/** Hide the marker SVG. */
|
||||
hide() {
|
||||
if (this.drawer_) {
|
||||
this.drawer_.hide();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispose of this marker.
|
||||
*/
|
||||
/** Dispose of this marker. */
|
||||
dispose() {
|
||||
if (this.getDrawer()) {
|
||||
this.getDrawer().dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.Marker = Marker;
|
||||
|
||||
@@ -8,38 +8,36 @@
|
||||
* @fileoverview The class representing a cursor that is used to navigate
|
||||
* between tab navigable fields.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The class representing a cursor that is used to navigate
|
||||
* between tab navigable fields.
|
||||
* @class
|
||||
*/
|
||||
goog.module('Blockly.TabNavigateCursor');
|
||||
import * as goog from '../../closure/goog/goog.js';
|
||||
goog.declareModuleId('Blockly.TabNavigateCursor');
|
||||
|
||||
const {ASTNode} = goog.require('Blockly.ASTNode');
|
||||
const {BasicCursor} = goog.require('Blockly.BasicCursor');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const {Field} = goog.requireType('Blockly.Field');
|
||||
import type {Field} from '../field.js';
|
||||
|
||||
import {ASTNode} from './ast_node.js';
|
||||
import {BasicCursor} from './basic_cursor.js';
|
||||
|
||||
|
||||
/**
|
||||
* A cursor for navigating between tab navigable fields.
|
||||
* @extends {BasicCursor}
|
||||
* @alias Blockly.TabNavigateCursor
|
||||
*/
|
||||
class TabNavigateCursor extends BasicCursor {
|
||||
export class TabNavigateCursor extends 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
|
||||
* @param node The AST node to check whether it is valid.
|
||||
* @return True if the node should be visited, false otherwise.
|
||||
*/
|
||||
validNode_(node) {
|
||||
override validNode_(node: ASTNode|null): boolean {
|
||||
let isValid = false;
|
||||
const type = node && node.getType();
|
||||
if (node) {
|
||||
const location = /** @type {Field} */ (node.getLocation());
|
||||
const location = node.getLocation() as Field;
|
||||
if (type === ASTNode.types.FIELD && location &&
|
||||
location.isTabNavigable() && location.isClickable()) {
|
||||
isValid = true;
|
||||
@@ -48,5 +46,3 @@ class TabNavigateCursor extends BasicCursor {
|
||||
return isValid;
|
||||
}
|
||||
}
|
||||
|
||||
exports.TabNavigateCursor = TabNavigateCursor;
|
||||
|
||||
Reference in New Issue
Block a user