Skip to content

Commit

Permalink
fix: rebuilding behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Aug 31, 2023
1 parent b895605 commit be8951a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
29 changes: 20 additions & 9 deletions plugins/block-dynamic-connection/src/dynamic_list_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const DYNAMIC_LIST_CREATE_MIXIN = {

this.setHelpUrl(Blockly.Msg['LISTS_CREATE_WITH_HELPURL']);
this.setStyle('list_blocks');
this.appendValueInput('ADD0')
.appendField(Blockly.Msg['LISTS_CREATE_WITH_INPUT_WITH']);
this.addFirstInput();
for (let i = 1; i < this.minInputs; i++) this.appendValueInput(`ADD${i}`);
this.setOutput(true, 'Array');
this.setTooltip(Blockly.Msg['LISTS_CREATE_WITH_TOOLTIP']);
Expand Down Expand Up @@ -159,16 +158,16 @@ const DYNAMIC_LIST_CREATE_MIXIN = {
const targetConns =
this.removeUnnecessaryEmptyConns(
this.inputList.map((i) => i.connection?.targetConnection));
this.deleteDynamicInputs();
this.tearDownBlock();
this.addItemInputs(targetConns);
this.itemCount = targetConns.length;
},

/**
* Deletes all inputs except for the first one, which is static.
* Deletes all inputs on the block so it can be rebuilt.
*/
deleteDynamicInputs(this: DynamicListCreateBlock): void {
for (let i = this.inputList.length - 1; i >= 1; i--) {
tearDownBlock(this: DynamicListCreateBlock): void {
for (let i = this.inputList.length - 1; i >= 0; i--) {
this.removeInput(this.inputList[i].name);
}
},
Expand Down Expand Up @@ -204,14 +203,26 @@ const DYNAMIC_LIST_CREATE_MIXIN = {
this: DynamicListCreateBlock,
targetConns: Array<Blockly.Connection | undefined | null>,
): void {
for (let i = 0; i < targetConns.length; i++) {
let input = this.getInput(`ADD${i}`);
if (!input) input = this.appendValueInput(`ADD${i}`);
const input = this.addFirstInput();
const firstConn = targetConns[0];
if (firstConn) input.connection?.connect(firstConn);

for (let i = 1; i < targetConns.length; i++) {
const input = this.appendValueInput(`ADD${i}`);

const targetConn = targetConns[i];
if (targetConn) input.connection?.connect(targetConn);
}
},

/**
* Adds the top input with the label to this block.
* @returns The added input.
*/
addFirstInput(this: DynamicListCreateBlock): Blockly.Input {
return this.appendValueInput('ADD0')
.appendField(Blockly.Msg['LISTS_CREATE_WITH_INPUT_WITH']);
},
};

Blockly.Blocks['dynamic_list_create'] = DYNAMIC_LIST_CREATE_MIXIN;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {overrideOldBlockDefinitions} = require('../src/index');

const assert = chai.assert;

suite.only('List create block', function() {
suite('List create block', function() {
/**
* Asserts that the list create block has the expected inputs.
* @param {!Blockly.Block} block The block to check.
Expand Down Expand Up @@ -257,7 +257,6 @@ suite.only('List create block', function() {
},
{
title: 'multiple non-sequential inputs with children - old serialization',
skip: true,
xml:
'<block xmlns="https://developers.google.com/blockly/xml"' +
' type="dynamic_list_create" id="1">\n' +
Expand Down
3 changes: 3 additions & 0 deletions plugins/dev-tools/src/playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,14 @@ function registerEditorCommands(editor, playground) {
const loadXml = () => {
const xml = editor.getModel().getValue();
const workspace = playground.getWorkspace();
Blockly.Events.disable();
try {
Blockly.Xml.domToWorkspace(Blockly.utils.xml.textToDom(xml), workspace);
} catch (e) {
// If this fails that's fine.
return false;
} finally {
Blockly.Events.enable();
}
return true;
};
Expand Down

0 comments on commit be8951a

Please sign in to comment.