mirror of
https://github.com/google/blockly.git
synced 2026-01-26 18:20:10 +01:00
* 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>
707 lines
22 KiB
TypeScript
707 lines
22 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2011 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Components for creating connections between blocks.
|
|
*/
|
|
|
|
/**
|
|
* Components for creating connections between blocks.
|
|
* @class
|
|
*/
|
|
import * as goog from '../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.Connection');
|
|
|
|
// Unused import preserved for side-effects. Remove if unneeded.
|
|
// import './constants.js';
|
|
|
|
import type {Block} from './block.js';
|
|
import {ConnectionType} from './connection_type.js';
|
|
import type {BlockMove} from './events/events_block_move.js';
|
|
import * as eventUtils from './events/utils.js';
|
|
import type {Input} from './input.js';
|
|
import type {IASTNodeLocationWithBlock} from './interfaces/i_ast_node_location_with_block.js';
|
|
import type {IConnectionChecker} from './interfaces/i_connection_checker.js';
|
|
import * as blocks from './serialization/blocks.js';
|
|
import * as Xml from './xml.js';
|
|
|
|
|
|
/**
|
|
* Class for a connection between blocks.
|
|
* @alias Blockly.Connection
|
|
*/
|
|
export class Connection implements IASTNodeLocationWithBlock {
|
|
/** Constants for checking whether two connections are compatible. */
|
|
static CAN_CONNECT = 0;
|
|
static REASON_SELF_CONNECTION = 1;
|
|
static REASON_WRONG_TYPE = 2;
|
|
static REASON_TARGET_NULL = 3;
|
|
static REASON_CHECKS_FAILED = 4;
|
|
static REASON_DIFFERENT_WORKSPACES = 5;
|
|
static REASON_SHADOW_PARENT = 6;
|
|
static REASON_DRAG_CHECKS_FAILED = 7;
|
|
static REASON_PREVIOUS_AND_OUTPUT = 8;
|
|
|
|
protected sourceBlock_: Block;
|
|
|
|
/** Connection this connection connects to. Null if not connected. */
|
|
targetConnection: Connection|null = null;
|
|
|
|
/**
|
|
* Has this connection been disposed of?
|
|
* @internal
|
|
*/
|
|
disposed = false;
|
|
|
|
/** List of compatible value types. Null if all types are compatible. */
|
|
// AnyDuringMigration because: Type 'null' is not assignable to type 'any[]'.
|
|
private check_: AnyDuringMigration[] = null as AnyDuringMigration;
|
|
|
|
/** DOM representation of a shadow block, or null if none. */
|
|
// AnyDuringMigration because: Type 'null' is not assignable to type
|
|
// 'Element'.
|
|
private shadowDom_: Element = null as AnyDuringMigration;
|
|
|
|
/**
|
|
* Horizontal location of this connection.
|
|
* @internal
|
|
*/
|
|
x = 0;
|
|
|
|
/**
|
|
* Vertical location of this connection.
|
|
* @internal
|
|
*/
|
|
y = 0;
|
|
|
|
private shadowState_: blocks.State|null = null;
|
|
|
|
/**
|
|
* @param source The block establishing this connection.
|
|
* @param type The type of the connection.
|
|
*/
|
|
constructor(source: Block, public type: number) {
|
|
this.sourceBlock_ = source;
|
|
}
|
|
|
|
/**
|
|
* Connect two connections together. This is the connection on the superior
|
|
* block.
|
|
* @param childConnection Connection on inferior block.
|
|
*/
|
|
protected connect_(childConnection: Connection) {
|
|
const INPUT = ConnectionType.INPUT_VALUE;
|
|
const parentConnection = this;
|
|
const parentBlock = parentConnection.getSourceBlock();
|
|
const childBlock = childConnection.getSourceBlock();
|
|
|
|
// Make sure the childConnection is available.
|
|
if (childConnection.isConnected()) {
|
|
childConnection.disconnect();
|
|
}
|
|
|
|
// Make sure the parentConnection is available.
|
|
let orphan;
|
|
if (parentConnection.isConnected()) {
|
|
const shadowState = parentConnection.stashShadowState_();
|
|
const target = parentConnection.targetBlock();
|
|
if (target!.isShadow()) {
|
|
target!.dispose(false);
|
|
} else {
|
|
parentConnection.disconnect();
|
|
orphan = target;
|
|
}
|
|
parentConnection.applyShadowState_(shadowState);
|
|
}
|
|
|
|
// Connect the new connection to the parent.
|
|
let event;
|
|
if (eventUtils.isEnabled()) {
|
|
event =
|
|
new (eventUtils.get(eventUtils.BLOCK_MOVE))!(childBlock) as BlockMove;
|
|
}
|
|
connectReciprocally(parentConnection, childConnection);
|
|
childBlock.setParent(parentBlock);
|
|
if (event) {
|
|
event.recordNew();
|
|
eventUtils.fire(event);
|
|
}
|
|
|
|
// Deal with the orphan if it exists.
|
|
if (orphan) {
|
|
const orphanConnection = parentConnection.type === INPUT ?
|
|
orphan.outputConnection :
|
|
orphan.previousConnection;
|
|
const connection = Connection.getConnectionForOrphanedConnection(
|
|
childBlock, (orphanConnection));
|
|
if (connection) {
|
|
orphanConnection.connect(connection);
|
|
} else {
|
|
orphanConnection.onFailedConnect(parentConnection);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dispose of this connection and deal with connected blocks.
|
|
* @internal
|
|
*/
|
|
dispose() {
|
|
// isConnected returns true for shadows and non-shadows.
|
|
if (this.isConnected()) {
|
|
// Destroy the attached shadow block & its children (if it exists).
|
|
this.setShadowStateInternal_();
|
|
|
|
const targetBlock = this.targetBlock();
|
|
if (targetBlock) {
|
|
// Disconnect the attached normal block.
|
|
targetBlock.unplug();
|
|
}
|
|
}
|
|
|
|
this.disposed = true;
|
|
}
|
|
|
|
/**
|
|
* Get the source block for this connection.
|
|
* @return The source block.
|
|
*/
|
|
getSourceBlock(): Block {
|
|
return this.sourceBlock_;
|
|
}
|
|
|
|
/**
|
|
* Does the connection belong to a superior block (higher in the source
|
|
* stack)?
|
|
* @return True if connection faces down or right.
|
|
*/
|
|
isSuperior(): boolean {
|
|
return this.type === ConnectionType.INPUT_VALUE ||
|
|
this.type === ConnectionType.NEXT_STATEMENT;
|
|
}
|
|
|
|
/**
|
|
* Is the connection connected?
|
|
* @return True if connection is connected to another connection.
|
|
*/
|
|
isConnected(): boolean {
|
|
return !!this.targetConnection;
|
|
}
|
|
|
|
/**
|
|
* Get the workspace's connection type checker object.
|
|
* @return The connection type checker for the source block's workspace.
|
|
* @internal
|
|
*/
|
|
getConnectionChecker(): IConnectionChecker {
|
|
return this.sourceBlock_.workspace!.connectionChecker;
|
|
}
|
|
|
|
/**
|
|
* Called when an attempted connection fails. NOP by default (i.e. for
|
|
* headless workspaces).
|
|
* @param _otherConnection Connection that this connection failed to connect
|
|
* to.
|
|
* @internal
|
|
*/
|
|
onFailedConnect(_otherConnection: Connection) {}
|
|
// NOP
|
|
|
|
/**
|
|
* Connect this connection to another connection.
|
|
* @param otherConnection Connection to connect to.
|
|
* @return Whether the the blocks are now connected or not.
|
|
*/
|
|
connect(otherConnection: Connection): boolean {
|
|
if (this.targetConnection === otherConnection) {
|
|
// Already connected together. NOP.
|
|
return true;
|
|
}
|
|
|
|
const checker = this.getConnectionChecker();
|
|
if (checker.canConnect(this, otherConnection, false)) {
|
|
const eventGroup = eventUtils.getGroup();
|
|
if (!eventGroup) {
|
|
eventUtils.setGroup(true);
|
|
}
|
|
// Determine which block is superior (higher in the source stack).
|
|
if (this.isSuperior()) {
|
|
// Superior block.
|
|
this.connect_(otherConnection);
|
|
} else {
|
|
// Inferior block.
|
|
otherConnection.connect_(this);
|
|
}
|
|
if (!eventGroup) {
|
|
eventUtils.setGroup(false);
|
|
}
|
|
}
|
|
|
|
return this.isConnected();
|
|
}
|
|
|
|
/** Disconnect this connection. */
|
|
disconnect() {
|
|
const otherConnection = this.targetConnection;
|
|
if (!otherConnection) {
|
|
throw Error('Source connection not connected.');
|
|
}
|
|
if (otherConnection.targetConnection !== this) {
|
|
throw Error('Target connection not connected to source connection.');
|
|
}
|
|
let parentBlock;
|
|
let childBlock;
|
|
let parentConnection;
|
|
if (this.isSuperior()) {
|
|
// Superior block.
|
|
parentBlock = this.sourceBlock_;
|
|
childBlock = otherConnection.getSourceBlock();
|
|
parentConnection = this;
|
|
} else {
|
|
// Inferior block.
|
|
parentBlock = otherConnection.getSourceBlock();
|
|
childBlock = this.sourceBlock_;
|
|
parentConnection = otherConnection;
|
|
}
|
|
|
|
const eventGroup = eventUtils.getGroup();
|
|
if (!eventGroup) {
|
|
eventUtils.setGroup(true);
|
|
}
|
|
this.disconnectInternal_(parentBlock, childBlock);
|
|
if (!childBlock.isShadow()) {
|
|
// If we were disconnecting a shadow, no need to spawn a new one.
|
|
parentConnection.respawnShadow_();
|
|
}
|
|
if (!eventGroup) {
|
|
eventUtils.setGroup(false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Disconnect two blocks that are connected by this connection.
|
|
* @param parentBlock The superior block.
|
|
* @param childBlock The inferior block.
|
|
*/
|
|
protected disconnectInternal_(parentBlock: Block, childBlock: Block) {
|
|
let event;
|
|
if (eventUtils.isEnabled()) {
|
|
event =
|
|
new (eventUtils.get(eventUtils.BLOCK_MOVE))!(childBlock) as BlockMove;
|
|
}
|
|
const otherConnection = this.targetConnection;
|
|
if (otherConnection) {
|
|
otherConnection.targetConnection = null;
|
|
}
|
|
this.targetConnection = null;
|
|
childBlock.setParent(null);
|
|
if (event) {
|
|
event.recordNew();
|
|
eventUtils.fire(event);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Respawn the shadow block if there was one connected to the this connection.
|
|
*/
|
|
protected respawnShadow_() {
|
|
// Have to keep respawnShadow_ for backwards compatibility.
|
|
this.createShadowBlock_(true);
|
|
}
|
|
|
|
/**
|
|
* Returns the block that this connection connects to.
|
|
* @return The connected block or null if none is connected.
|
|
*/
|
|
targetBlock(): Block|null {
|
|
if (this.isConnected()) {
|
|
return this.targetConnection?.getSourceBlock() ?? null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Function to be called when this connection's compatible types have changed.
|
|
*/
|
|
protected onCheckChanged_() {
|
|
// The new value type may not be compatible with the existing connection.
|
|
if (this.isConnected() &&
|
|
(!this.targetConnection ||
|
|
!this.getConnectionChecker().canConnect(
|
|
this, this.targetConnection, false))) {
|
|
const child = this.isSuperior() ? this.targetBlock() : this.sourceBlock_;
|
|
child!.unplug();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Change a connection's compatibility.
|
|
* @param check Compatible value type or list of value types. Null if all
|
|
* types are compatible.
|
|
* @return The connection being modified (to allow chaining).
|
|
*/
|
|
setCheck(check: string|string[]|null): Connection {
|
|
if (check) {
|
|
// Ensure that check is in an array.
|
|
if (!Array.isArray(check)) {
|
|
check = [check];
|
|
}
|
|
this.check_ = check;
|
|
this.onCheckChanged_();
|
|
} else {
|
|
// AnyDuringMigration because: Type 'null' is not assignable to type
|
|
// 'any[]'.
|
|
this.check_ = null as AnyDuringMigration;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Get a connection's compatibility.
|
|
* @return List of compatible value types.
|
|
* Null if all types are compatible.
|
|
*/
|
|
getCheck(): AnyDuringMigration[]|null {
|
|
return this.check_;
|
|
}
|
|
|
|
/**
|
|
* Changes the connection's shadow block.
|
|
* @param shadowDom DOM representation of a block or null.
|
|
*/
|
|
setShadowDom(shadowDom: Element|null) {
|
|
this.setShadowStateInternal_({shadowDom});
|
|
}
|
|
|
|
/**
|
|
* Returns the xml representation of the connection's shadow block.
|
|
* @param returnCurrent If true, and the shadow block is currently attached to
|
|
* this connection, this serializes the state of that block and returns it
|
|
* (so that field values are correct). Otherwise the saved shadowDom is
|
|
* just returned.
|
|
* @return Shadow DOM representation of a block or null.
|
|
*/
|
|
getShadowDom(returnCurrent?: boolean): Element|null {
|
|
return returnCurrent && this.targetBlock()!.isShadow() ?
|
|
Xml.blockToDom((this.targetBlock() as Block)) as Element :
|
|
this.shadowDom_;
|
|
}
|
|
|
|
/**
|
|
* Changes the connection's shadow block.
|
|
* @param shadowState An state represetation of the block or null.
|
|
*/
|
|
setShadowState(shadowState: blocks.State|null) {
|
|
this.setShadowStateInternal_({shadowState});
|
|
}
|
|
|
|
/**
|
|
* Returns the serialized object representation of the connection's shadow
|
|
* block.
|
|
* @param returnCurrent If true, and the shadow block is currently attached to
|
|
* this connection, this serializes the state of that block and returns it
|
|
* (so that field values are correct). Otherwise the saved state is just
|
|
* returned.
|
|
* @return Serialized object representation of the block, or null.
|
|
*/
|
|
getShadowState(returnCurrent?: boolean): blocks.State|null {
|
|
if (returnCurrent && this.targetBlock() && this.targetBlock()!.isShadow()) {
|
|
return blocks.save(this.targetBlock() as Block);
|
|
}
|
|
return this.shadowState_;
|
|
}
|
|
|
|
/**
|
|
* Find all nearby compatible connections to this connection.
|
|
* Type checking does not apply, since this function is used for bumping.
|
|
*
|
|
* Headless configurations (the default) do not have neighboring connection,
|
|
* and always return an empty list (the default).
|
|
* {@link Blockly.RenderedConnection} overrides this behavior with a list
|
|
* computed from the rendered positioning.
|
|
* @param _maxLimit The maximum radius to another connection.
|
|
* @return List of connections.
|
|
* @internal
|
|
*/
|
|
neighbours(_maxLimit: number): Connection[] {
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Get the parent input of a connection.
|
|
* @return The input that the connection belongs to or null if no parent
|
|
* exists.
|
|
* @internal
|
|
*/
|
|
getParentInput(): Input|null {
|
|
let parentInput = null;
|
|
const inputs = this.sourceBlock_.inputList;
|
|
for (let i = 0; i < inputs.length; i++) {
|
|
if (inputs[i].connection === this) {
|
|
parentInput = inputs[i];
|
|
break;
|
|
}
|
|
}
|
|
return parentInput;
|
|
}
|
|
|
|
/**
|
|
* This method returns a string describing this Connection in developer terms
|
|
* (English only). Intended to on be used in console logs and errors.
|
|
* @return The description.
|
|
*/
|
|
toString(): string {
|
|
const block = this.sourceBlock_;
|
|
if (!block) {
|
|
return 'Orphan Connection';
|
|
}
|
|
let msg;
|
|
if (block.outputConnection === this) {
|
|
msg = 'Output Connection of ';
|
|
} else if (block.previousConnection === this) {
|
|
msg = 'Previous Connection of ';
|
|
} else if (block.nextConnection === this) {
|
|
msg = 'Next Connection of ';
|
|
} else {
|
|
let parentInput = null;
|
|
for (let i = 0, input; input = block.inputList[i]; i++) {
|
|
if (input.connection === this) {
|
|
parentInput = input;
|
|
break;
|
|
}
|
|
}
|
|
if (parentInput) {
|
|
msg = 'Input "' + parentInput.name + '" connection on ';
|
|
} else {
|
|
console.warn('Connection not actually connected to sourceBlock_');
|
|
return 'Orphan Connection';
|
|
}
|
|
}
|
|
return msg + block.toDevString();
|
|
}
|
|
|
|
/**
|
|
* Returns the state of the shadowDom_ and shadowState_ properties, then
|
|
* temporarily sets those properties to null so no shadow respawns.
|
|
* @return The state of both the shadowDom_ and shadowState_ properties.
|
|
*/
|
|
private stashShadowState_():
|
|
{shadowDom: Element|null, shadowState: blocks.State|null} {
|
|
const shadowDom = this.getShadowDom(true);
|
|
const shadowState = this.getShadowState(true);
|
|
// Set to null so it doesn't respawn.
|
|
// AnyDuringMigration because: Type 'null' is not assignable to type
|
|
// 'Element'.
|
|
this.shadowDom_ = null as AnyDuringMigration;
|
|
this.shadowState_ = null;
|
|
return {shadowDom, shadowState};
|
|
}
|
|
|
|
/**
|
|
* Reapplies the stashed state of the shadowDom_ and shadowState_ properties.
|
|
* @param param0 The state to reapply to the shadowDom_ and shadowState_
|
|
* properties.
|
|
*/
|
|
private applyShadowState_({shadowDom, shadowState}: {
|
|
shadowDom: Element|null,
|
|
shadowState: blocks.State|null
|
|
}) {
|
|
// AnyDuringMigration because: Type 'Element | null' is not assignable to
|
|
// type 'Element'.
|
|
this.shadowDom_ = shadowDom as AnyDuringMigration;
|
|
this.shadowState_ = shadowState;
|
|
}
|
|
|
|
/**
|
|
* Sets the state of the shadow of this connection.
|
|
* @param param0 The state to set the shadow of this connection to.
|
|
*/
|
|
private setShadowStateInternal_({shadowDom = null, shadowState = null}: {
|
|
shadowDom?: Element|null,
|
|
shadowState?: blocks.State|null
|
|
} = {}) {
|
|
// One or both of these should always be null.
|
|
// If neither is null, the shadowState will get priority.
|
|
// AnyDuringMigration because: Type 'Element | null' is not assignable to
|
|
// type 'Element'.
|
|
this.shadowDom_ = shadowDom as AnyDuringMigration;
|
|
this.shadowState_ = shadowState;
|
|
|
|
const target = this.targetBlock();
|
|
if (!target) {
|
|
this.respawnShadow_();
|
|
if (this.targetBlock() && this.targetBlock()!.isShadow()) {
|
|
this.serializeShadow_(this.targetBlock());
|
|
}
|
|
} else if (target.isShadow()) {
|
|
target.dispose(false);
|
|
this.respawnShadow_();
|
|
if (this.targetBlock() && this.targetBlock()!.isShadow()) {
|
|
this.serializeShadow_(this.targetBlock());
|
|
}
|
|
} else {
|
|
const shadow = this.createShadowBlock_(false);
|
|
this.serializeShadow_(shadow);
|
|
if (shadow) {
|
|
shadow.dispose(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a shadow block based on the current shadowState_ or shadowDom_.
|
|
* shadowState_ gets priority.
|
|
* @param attemptToConnect Whether to try to connect the shadow block to this
|
|
* connection or not.
|
|
* @return The shadow block that was created, or null if both the shadowState_
|
|
* and shadowDom_ are null.
|
|
*/
|
|
private createShadowBlock_(attemptToConnect: boolean): Block|null {
|
|
const parentBlock = this.getSourceBlock();
|
|
const shadowState = this.getShadowState();
|
|
const shadowDom = this.getShadowDom();
|
|
if (!parentBlock.workspace || !shadowState && !shadowDom) {
|
|
return null;
|
|
}
|
|
|
|
let blockShadow;
|
|
if (shadowState) {
|
|
blockShadow = blocks.appendInternal(shadowState, parentBlock.workspace, {
|
|
parentConnection: attemptToConnect ? this : undefined,
|
|
isShadow: true,
|
|
recordUndo: false,
|
|
});
|
|
return blockShadow;
|
|
}
|
|
|
|
if (shadowDom) {
|
|
blockShadow = Xml.domToBlock(shadowDom, parentBlock.workspace);
|
|
if (attemptToConnect) {
|
|
if (this.type === ConnectionType.INPUT_VALUE) {
|
|
if (!blockShadow.outputConnection) {
|
|
throw new Error('Shadow block is missing an output connection');
|
|
}
|
|
if (!this.connect(blockShadow.outputConnection)) {
|
|
throw new Error('Could not connect shadow block to connection');
|
|
}
|
|
} else if (this.type === ConnectionType.NEXT_STATEMENT) {
|
|
if (!blockShadow.previousConnection) {
|
|
throw new Error('Shadow block is missing previous connection');
|
|
}
|
|
if (!this.connect(blockShadow.previousConnection)) {
|
|
throw new Error('Could not connect shadow block to connection');
|
|
}
|
|
} else {
|
|
throw new Error(
|
|
'Cannot connect a shadow block to a previous/output connection');
|
|
}
|
|
}
|
|
return blockShadow;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Saves the given shadow block to both the shadowDom_ and shadowState_
|
|
* properties, in their respective serialized forms.
|
|
* @param shadow The shadow to serialize, or null.
|
|
*/
|
|
private serializeShadow_(shadow: Block|null) {
|
|
if (!shadow) {
|
|
return;
|
|
}
|
|
this.shadowDom_ = Xml.blockToDom(shadow) as Element;
|
|
this.shadowState_ = blocks.save(shadow);
|
|
}
|
|
|
|
/**
|
|
* Returns the connection (starting at the startBlock) which will accept
|
|
* the given connection. This includes compatible connection types and
|
|
* connection checks.
|
|
* @param startBlock The block on which to start the search.
|
|
* @param orphanConnection The connection that is looking for a home.
|
|
* @return The suitable connection point on the chain of blocks, or null.
|
|
*/
|
|
static getConnectionForOrphanedConnection(
|
|
startBlock: Block, orphanConnection: Connection): Connection|null {
|
|
if (orphanConnection.type === ConnectionType.OUTPUT_VALUE) {
|
|
return getConnectionForOrphanedOutput(
|
|
startBlock, orphanConnection.getSourceBlock());
|
|
}
|
|
// Otherwise we're dealing with a stack.
|
|
const connection = startBlock.lastConnectionInStack(true);
|
|
const checker = orphanConnection.getConnectionChecker();
|
|
if (connection && checker.canConnect(orphanConnection, connection, false)) {
|
|
return connection;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update two connections to target each other.
|
|
* @param first The first connection to update.
|
|
* @param second The second connection to update.
|
|
*/
|
|
function connectReciprocally(first: Connection, second: Connection) {
|
|
if (!first || !second) {
|
|
throw Error('Cannot connect null connections.');
|
|
}
|
|
first.targetConnection = second;
|
|
second.targetConnection = first;
|
|
}
|
|
/**
|
|
* Returns the single connection on the block that will accept the orphaned
|
|
* block, if one can be found. If the block has multiple compatible connections
|
|
* (even if they are filled) this returns null. If the block has no compatible
|
|
* connections, this returns null.
|
|
* @param block The superior block.
|
|
* @param orphanBlock The inferior block.
|
|
* @return The suitable connection point on 'block', or null.
|
|
*/
|
|
function getSingleConnection(block: Block, orphanBlock: Block): Connection|
|
|
null {
|
|
let foundConnection = null;
|
|
const output = orphanBlock.outputConnection;
|
|
const typeChecker = output.getConnectionChecker();
|
|
|
|
for (let i = 0, input; input = block.inputList[i]; i++) {
|
|
const connection = input.connection;
|
|
if (connection && typeChecker.canConnect(output, connection, false)) {
|
|
if (foundConnection) {
|
|
return null; // More than one connection.
|
|
}
|
|
foundConnection = connection;
|
|
}
|
|
}
|
|
return foundConnection;
|
|
}
|
|
|
|
/**
|
|
* Walks down a row a blocks, at each stage checking if there are any
|
|
* connections that will accept the orphaned block. If at any point there
|
|
* are zero or multiple eligible connections, returns null. Otherwise
|
|
* returns the only input on the last block in the chain.
|
|
* Terminates early for shadow blocks.
|
|
* @param startBlock The block on which to start the search.
|
|
* @param orphanBlock The block that is looking for a home.
|
|
* @return The suitable connection point on the chain of blocks, or null.
|
|
*/
|
|
function getConnectionForOrphanedOutput(
|
|
startBlock: Block, orphanBlock: Block): Connection|null {
|
|
let newBlock = startBlock;
|
|
let connection;
|
|
while (connection = getSingleConnection((newBlock), orphanBlock)) {
|
|
// AnyDuringMigration because: Type 'Block | null' is not assignable to
|
|
// type 'Block'.
|
|
newBlock = connection.targetBlock() as AnyDuringMigration;
|
|
if (!newBlock || newBlock.isShadow()) {
|
|
return connection;
|
|
}
|
|
}
|
|
return null;
|
|
}
|