import * as Blockly from 'blockly/core';
import { MyCustomLabelField } from '../../custom_fields/labelFieldTest';
import '../../code_generators/orBlockCode';
Blockly.defineBlocksWithJsonArray([
{
type: 'or_block',
tooltip: 'verodern',
helpUrl: '',
message0: '%1 %2 %3 %4',
args0: [
{
type: 'input_value',
name: 'OR1INP',
check: 'Boolean',
align: 'RIGHT',
},
{
type: 'input_end_row',
name: 'END',
},
{
type: 'field_custom_label',
name: 'OR0',
align: 'CENTRE',
text: 'or',
},
{
type: 'input_value',
name: 'OR2INP',
check: 'Boolean',
align: 'RIGHT',
},
],
output: 'Boolean',
mutator: 'custom_or_mutator',
style: 'custom_or_style',
inputsInline: true,
},
{
type: 'or_start',
align: 'LEFT',
message0: 'Condition 1 or 2 %1',
args0: [
{
type: 'input_dummy',
name: 'STACK',
},
],
nextStatement: null,
enableContextMenu: false,
style: 'custom_or_style',
tooltip: '',
},
// Block representing the item statement in the STATE mutator.
{
type: 'or_item',
align: 'LEFT',
message0: '+ Condition',
previousStatement: null,
nextStatement: null,
enableContextMenu: false,
style: 'custom_or_style',
tooltip: '',
},
]);
const OR_MUTATOR_MIXIN = {
itemCount_: 0,
saveExtraState: function () {
return {
itemCount: this.itemCount_,
};
},
loadExtraState: function (state) {
this.itemCount_ = state['itemCount'] || 0;
this.updateShape_();
},
decompose: function (workspace) {
const containerBlock = workspace.newBlock('or_start');
containerBlock.initSvg();
let connection = containerBlock.nextConnection;
for (let i = 0; i < this.itemCount_; i++) {
const itemBlock = workspace.newBlock('or_item');
itemBlock.initSvg();
connection.connect(itemBlock.previousConnection);
connection = itemBlock.nextConnection;
}
return containerBlock;
},
compose: function (containerBlock) {
let clauseBlock = containerBlock.nextConnection.targetBlock();
// Count number of inputs.
this.itemCount_ = 0;
const valueConnections = [null];
while (clauseBlock && !clauseBlock.isInsertionMarker()) {
switch (clauseBlock.type) {
case 'or_item':
this.itemCount_++;
valueConnections.push(clauseBlock.valueConnection_);
break;
default:
throw TypeError('Unknown block type: ' + clauseBlock.type);
}
clauseBlock =
clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock();
}
this.updateShape_();
// Reconnect any child blocks.
this.reconnectChildBlocks_(valueConnections);
},
saveConnections: function (containerBlock) {
let clauseBlock = containerBlock.nextConnection.targetBlock();
let i = 1;
while (clauseBlock) {
switch (clauseBlock.type) {
case 'or_item': {
const inputItem = this.getInput('ORINP' + i);
clauseBlock.valueConnection_ =
inputItem && inputItem.connection.targetConnection;
i++;
break;
}
default:
throw TypeError('Unknown block type: ' + clauseBlock.type);
}
clauseBlock =
clauseBlock.nextConnection && clauseBlock.nextConnection.targetBlock();
}
},
updateShape_: function () {
// Delete everything.
for (let i = 1; this.getInput('ORINP' + i); i++) {
const inpConn = this.getInput('ORINP' + i).connection.targetConnection;
//delete compare Blocks here, when it's a inlineIfCompare with no changes
if (
inpConn &&
inpConn?.sourceBlock_?.type === 'inlineIfCompare' &&
!inpConn?.sourceBlock_.getInput('VAR').connection.targetConnection
) {
inpConn.sourceBlock_.dispose();
}
this.removeInput('OR' + i);
this.removeInput('ORINP' + i);
this.removeInput('END' + i);
}
// Rebuild block.
for (let i = 1; i <= this.itemCount_; i++) {
this.appendEndRowInput('END' + i);
this.appendDummyInput('OR' + i).appendField(new MyCustomLabelField('or'));
this.appendValueInput('ORINP' + i).setAlign(Blockly.inputs.Align.RIGHT);
}
},
reconnectChildBlocks_: function (valueConnections) {
for (let i = 1; i <= this.itemCount_; i++) {
valueConnections[i]?.reconnect(this, 'ORINP' + i);
}
//set compare Blocks here, when the input is empty
//Blockly.Events.setGroup(true);
for (let i = 1; i <= this.itemCount_; i++) {
const inp = this.getInput('ORINP' + i);
if (inp.connection.targetConnection) {
continue;
}
const compBlock = this.workspace.newBlock('inlineIfCompare');
compBlock.outputConnection.connect(inp.connection);
compBlock.initSvg();
compBlock.render();
}
//Blockly.Events.setGroup(false);
},
};
Blockly.Extensions.registerMutator(
'custom_or_mutator',
OR_MUTATOR_MIXIN,
null,
['or_item']
);