Skip to content
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

fix: dynamic if teardown #1886

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
37 changes: 22 additions & 15 deletions plugins/block-dynamic-connection/src/dynamic_if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ interface CaseInputPair {

/* eslint-disable @typescript-eslint/naming-convention */
const DYNAMIC_IF_MIXIN = {
/** Minimum number of inputs for this block. */
/**
* Minimum number of inputs for this block.
* @deprecated This is unused.
*/
minInputs: 1,

/** Count of else-if cases. */
Expand All @@ -47,13 +50,7 @@ const DYNAMIC_IF_MIXIN = {
init(this: DynamicIfBlock): void {
this.setHelpUrl(Blockly.Msg['CONTROLS_IF_HELPURL']);
this.setStyle('logic_blocks');

this.appendValueInput('IF0')
.setCheck('Boolean')
.appendField(Blockly.Msg['CONTROLS_IF_MSG_IF'], 'if');
this.appendStatementInput('DO0')
.appendField(Blockly.Msg['CONTROLS_IF_MSG_THEN']);

this.addFirstCase();
this.setNextStatement(true);
this.setPreviousStatement(true);
this.setTooltip(Blockly.Msg['LISTS_CREATE_WITH_TOOLTIP']);
Expand Down Expand Up @@ -231,8 +228,9 @@ const DYNAMIC_IF_MIXIN = {
const targetCaseConns = this.collectTargetCaseConns();
const targetElseConn = this.getInput('ELSE')?.connection?.targetConnection;

this.deleteDynamicInputs();
this.tearDownBlock();

this.addFirstCase();
this.addCaseInputs(targetCaseConns);
if (targetElseConn) this.addElseInput(targetElseConn);

Expand Down Expand Up @@ -260,12 +258,9 @@ const DYNAMIC_IF_MIXIN = {
return targetConns;
},

/**
* Deletes all of the inputs on this block except for the default "if"
* and "do" inputs.
*/
deleteDynamicInputs(this: DynamicIfBlock): void {
for (let i = this.inputList.length - 1; i >= 2; i--) {
/** Deletes all inputs on the block so it can be rebuilt. */
tearDownBlock(this: DynamicIfBlock): void {
for (let i = this.inputList.length - 1; i >= 0; i--) {
this.removeInput(this.inputList[i].name);
}
},
Expand Down Expand Up @@ -301,6 +296,18 @@ const DYNAMIC_IF_MIXIN = {
.appendField(Blockly.Msg['CONTROLS_IF_MSG_ELSE'])
.connection?.connect(targetElseConn);
},

/**
* Adds the first 'IF' and 'DO' inputs and their associated labels to this
* block.
*/
addFirstCase(this: DynamicIfBlock): void {
this.appendValueInput('IF0')
.setCheck('Boolean')
.appendField(Blockly.Msg['CONTROLS_IF_MSG_IF'], 'if');
this.appendStatementInput('DO0')
.appendField(Blockly.Msg['CONTROLS_IF_MSG_THEN']);
},
};

Blockly.Blocks['dynamic_if'] = DYNAMIC_IF_MIXIN;
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ const DYNAMIC_LIST_CREATE_MIXIN = {
this.itemCount = targetConns.length;
},

/**
* Deletes all inputs on the block so it can be rebuilt.
*/
/** Deletes all inputs on the block so it can be rebuilt. */
tearDownBlock(this: DynamicListCreateBlock): void {
for (let i = this.inputList.length - 1; i >= 0; i--) {
this.removeInput(this.inputList[i].name);
Expand Down