8000 fix: have inputs construct connections by BeksOmega · Pull Request #7116 · google/blockly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: have inputs construct connections #7116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,13 +1569,7 @@ export class Block implements IASTNodeLocation, IDeletable {
* @returns The input object created.
*/
appendValueInput(name: string): Input {
return this.appendInput(
new ValueInput(
name,
this,
this.makeConnection_(ConnectionType.INPUT_VALUE)
)
);
return this.appendInput(new ValueInput(name, this));
}

/**
Expand All @@ -1587,13 +1581,7 @@ export class Block implements IASTNodeLocation, IDeletable {
*/
appendStatementInput(name: string): Input {
this.statementInputCount++;
return this.appendInput(
new StatementInput(
name,
this,
this.makeConnection_(ConnectionType.NEXT_STATEMENT)
)
);
return this.appendInput(new StatementInput(name, this));
}

/**
Expand Down Expand Up @@ -1633,7 +1621,7 @@ export class Block implements IASTNodeLocation, IDeletable {
false
);
if (!inputConstructor) return null;
return this.appendInput(new inputConstructor(name, this, null));
return this.appendInput(new inputConstructor(name, this));
}

/**
Expand Down Expand Up @@ -2310,8 +2298,9 @@ export class Block implements IASTNodeLocation, IDeletable {
*
* @param type The type of the connection to create.
* @returns A new connection of the specified type.
* @internal
*/
protected makeConnection_(type: number): Connection {
makeConnection_(type: ConnectionType): Connection {
return new Connection(this, type);
}

Expand Down
3 changes: 2 additions & 1 deletion core/block_svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,9 @@ export class BlockSvg
*
* @param type The type of the connection to create.
* @returns A new connection of the specified type.
* @internal
*/
protected override makeConnection_(type: number): RenderedConnection {
override makeConnection_(type: ConnectionType): RenderedConnection {
return new RenderedConnection(this, type);
}

Expand Down
2 changes: 1 addition & 1 deletion core/inputs/dummy_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class DummyInput extends Input {
* @param block The block containing this input.
*/
constructor(public name: string, block: Block) {
super(name, block, null);
super(name, block);
}
}
24 changes: 16 additions & 8 deletions core/inputs/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '../field_label.js';
import type {Block} from '../block.js';
import type {BlockSvg} from '../block_svg.js';
import type {Connection} from '../connection.js';
import type {ConnectionType} from '../connection_type.js';
import type {Field} from '../field.js';
import * as fieldRegistry from '../field_registry.js';
import type {RenderedConnection} from '../rendered_connection.js';
Expand All @@ -36,19 +37,14 @@ export class Input {

public readonly type: inputTypes = inputTypes.CUSTOM;

public connection: Connection | null = null;

/**
* @param name Language-neutral identifier which may used to find this input
* again.
* @param sourceBlock The block containing this input.
* @param connection Optional connection for this input. If this is a custom
* input, `null` will always be passed, and then the subclass can
* optionally construct a connection.
*/
constructor(
public name: string,
private sourceBlock: Block,
public connection: Connection | null
) {}
constructor(public name: string, private sourceBlock: Block) {}

/**
* Get the source block for this input.
Expand Down Expand Up @@ -298,6 +294,18 @@ export class Input {
this.connection.dispose();
}
}

/**
* Constructs a connection based on the type of this input's source block.
* Properly handles constructing headless connections for headless blocks
* and rendered connections for rendered blocks.
*
* @returns a connection of the given type, which is either a headless
* or rendered connection, based on the type of this input's source block.
*/
protected makeConnection(type: ConnectionType): Connection {
return this.sourceBlock.makeConnection_(type);
}
}

export namespace Input {
Expand Down
15 changes: 7 additions & 8 deletions core/inputs/statement_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@

import type {Block} from '../block.js';
import type {Connection} from '../connection.js';
import {ConnectionType} from '../connection_type.js';
import {Input} from './input.js';
import {inputTypes} from './input_types.js';

/** Represents an input on a block with a statement connection. */
export class StatementInput extends Input {
readonly type = inputTypes.STATEMENT;

public connection: Connection;

/**
* @param name Language-neutral identifier which may used to find this input
* again.
* @param block The block containing this input.
* @param connection The statement connection for this input.
*/
constructor(
public name: string,
block: Block,
public connection: Connection
) {
constructor(public name: string, block: Block) {
// Errors are maintained for people not using typescript.
if (!name) throw new Error('Statement inputs must have a non-empty name');
if (!connection) throw new Error('Value inputs must have a connection');
super(name, block, connection);

super(name, block);
this.connection = this.makeConnection(ConnectionType.NEXT_STATEMENT);
}
}
13 changes: 4 additions & 9 deletions core/inputs/value_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import type {Block} from '../block.js';
import type {Connection} from '../connection.js';
import {ConnectionType} from '../connection_type.js';
import {Input} from './input.js';
import {inputTypes} from './input_types.js';

Expand All @@ -17,16 +17,11 @@ export class ValueInput extends Input {
* @param name Language-neutral identifier which may used to find this input
* again.
* @param block The block containing this input.
5DEA * @param connection The value connection for this input.
*/
constructor(
public name: string,
block: Block,
public connection: Connection
) {
constructor(public name: string, block: Block) {
// Errors are maintained for people not using typescript.
if (!name) throw new Error('Value inputs must have a non-empty name');
if (!connection) throw new Error('Value inputs must have a connection');
super(name, block, connection);
super(name, block);
this.connection = this.makeConnection(ConnectionType.INPUT_VALUE);
}
}
0