Skip to content

Commit 2268177

Browse files
authored
live_tests.js -- Expandable Polishing (1/2), JWK cant listen nor code.
1 parent 2d540e4 commit 2268177

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

blocks_vertical/live_tests.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ Blockly.Blocks['control_dualblock'] = {
322322
};
323323

324324
Blockly.Blocks['operators_expandablejoininputs'] = {
325+
/*
326+
default block texts
327+
*/
328+
messageList: ["apple", "banana", "pear", "orange", "mango", "strawberry", "pineapple", "grape", "kiwi"],
329+
325330
/**
326331
* @this Blockly.Block
327332
*/
@@ -342,14 +347,31 @@ Blockly.Blocks['operators_expandablejoininputs'] = {
342347
"extensions": ["colours_operators", "output_string"]
343348
});
344349

345-
this.inputs_ = 0;
350+
this.inputs_ = 2;
351+
if (this.isInFlyout) {
352+
for (let i = 0; i < this.inputs_; i++) {
353+
const input = this.appendValueInput(`INPUT${i + 1}`);
354+
this.fillInBlock(input.connection, i);
355+
}
356+
}
357+
},
358+
359+
fillInBlock: function (connection, index) {
360+
if (connection.sourceBlock_.isInsertionMarker_) return;
361+
const block = this.workspace.newBlock('text');
362+
const text = this.messageList[index];
363+
block.setFieldValue(text ? text : "...", "TEXT");
364+
block.setShadow(true);
365+
block.initSvg();
366+
block.render(true);
367+
block.outputConnection.connect(connection);
346368
},
347369

348370
mutationToDom: function () {
349371
// on save
350372
const container = document.createElement("mutation");
351373
let number = Number(this.inputs_);
352-
if (isNaN(number)) number = 0;
374+
if (isNaN(number)) number = 1;
353375
container.setAttribute("inputcount", String(number));
354376
return container;
355377
},
@@ -359,37 +381,36 @@ Blockly.Blocks['operators_expandablejoininputs'] = {
359381
const inputCount = Number(xmlElement.getAttribute("inputcount"));
360382
this.inputs_ = isNaN(inputCount) ? 0 : inputCount;
361383
for (let i = 0; i < this.inputs_; i++) {
362-
let input = this.appendValueInput(`INPUT${i + 1}`)
384+
const input = this.appendValueInput(`INPUT${i + 1}`);
385+
this.fillInBlock(input.connection, i);
363386
}
387+
queueMicrotask(() => {
388+
const connections = this.getConnections_();
389+
for (let i = 1; i < connections.length; i++) {
390+
const block = connections[i].targetBlock();
391+
if (!block) continue;
392+
if (!block.category_ && !block.isShadow()) block.dispose();
393+
}
394+
});
364395
},
365396

366397
onExpandableButtonClicked_: function (isAdding) {
367398
// Create an event group to keep field value and mutator in sync
368399
// Return null at the end because setValue is called here already.
369400
Blockly.Events.setGroup(true);
370401
var oldMutation = Blockly.Xml.domToText(this.mutationToDom());
371-
if (!isAdding) {
402+
if (isAdding) {
403+
this.inputs_++;
404+
const number = this.inputs_;
405+
const newInput = this.appendValueInput(`INPUT${number}`);
406+
this.fillInBlock(newInput.connection, number - 1);
407+
} else if (this.inputs_ > 1) {
372408
const number = this.inputs_;
373409
this.removeInput(`INPUT${number}`);
374410
this.inputs_--;
375-
if (this.inputs_ < 0) {
376-
this.inputs_ = 0;
377-
}
378-
} else {
379-
this.inputs_++;
380-
const number = this.inputs_;
381-
let input = this.appendValueInput(`INPUT${number}`)
382-
let shadow = this.workspace.newBlock('text')
383-
shadow.setFieldValue('text', 'TEXT')
384-
shadow.setShadow(true)
385-
shadow.initSvg()
386-
shadow.render()
387-
input.connection.connect(shadow.outputConnection)
388411
}
389412
this.initSvg();
390-
if (this.rendered) {
391-
this.render();
392-
}
413+
if (this.rendered) this.render();
393414

394415
var newMutation = Blockly.Xml.domToText(this.mutationToDom());
395416
Blockly.Events.fire(new Blockly.Events.BlockChange(this,

0 commit comments

Comments
 (0)