Skip to content

Commit

Permalink
fix: fully tear down dynamic block before rebuilding (google#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Sep 28, 2023
1 parent c496d12 commit 689bac9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
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;
4 changes: 1 addition & 3 deletions plugins/block-dynamic-connection/src/dynamic_list_create.ts
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

0 comments on commit 689bac9

Please sign in to comment.