diff --git a/plugins/block-dynamic-connection/src/dynamic_list_create.ts b/plugins/block-dynamic-connection/src/dynamic_list_create.ts index e6dfc2b2cc..9346f39732 100644 --- a/plugins/block-dynamic-connection/src/dynamic_list_create.ts +++ b/plugins/block-dynamic-connection/src/dynamic_list_create.ts @@ -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']); @@ -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); } }, @@ -204,14 +203,26 @@ const DYNAMIC_LIST_CREATE_MIXIN = { this: DynamicListCreateBlock, targetConns: Array, ): 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; diff --git a/plugins/block-dynamic-connection/test/dynamic_list_create.mocha.js b/plugins/block-dynamic-connection/test/dynamic_list_create.mocha.js index c6a616243c..669f9036ae 100644 --- a/plugins/block-dynamic-connection/test/dynamic_list_create.mocha.js +++ b/plugins/block-dynamic-connection/test/dynamic_list_create.mocha.js @@ -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. @@ -257,7 +257,6 @@ suite.only('List create block', function() { }, { title: 'multiple non-sequential inputs with children - old serialization', - skip: true, xml: '\n' + diff --git a/plugins/dev-tools/src/playground/index.js b/plugins/dev-tools/src/playground/index.js index 3731a7bfb8..e8815046f0 100644 --- a/plugins/dev-tools/src/playground/index.js +++ b/plugins/dev-tools/src/playground/index.js @@ -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; };