Skip to content

Commit

Permalink
fix: support loading stringified XML from JSOn
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Aug 31, 2023
1 parent d45c3d4 commit e98e862
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 3 deletions.
7 changes: 6 additions & 1 deletion plugins/block-dynamic-connection/src/dynamic_if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,12 @@ const DYNAMIC_IF_MIXIN = {
* @param state The state to apply to this block, ie the else if count
* and else state.
*/
loadExtraState: function(this: DynamicIfBlock, state: IfExtraState) {
loadExtraState: function(this: DynamicIfBlock, state: IfExtraState | string) {
if (typeof state === 'string') {
this.domToMutation(Blockly.utils.xml.textToDom(state));
return;
}

this.elseifCount = state['elseIfCount'] || 0;
this.elseCount = state['hasElse'] ? 1 : 0;
for (let i = 1; i <= this.elseifCount; i++) {
Expand Down
7 changes: 6 additions & 1 deletion plugins/block-dynamic-connection/src/dynamic_list_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ const DYNAMIC_LIST_CREATE_MIXIN = {
* @param state The state to apply to this block, ie the item count.
*/
loadExtraState: function(
this: DynamicListCreateBlock, state: {[x: string]: any}) {
this: DynamicListCreateBlock, state: ({[x: string]: any} | string)) {
if (typeof state === 'string') {
this.domToMutation(Blockly.utils.xml.textToDom(state));
return;
}

this.itemCount = state['itemCount'];
// minInputs are added automatically.
for (let i = this.minInputs; i < this.itemCount; i++) {
Expand Down
7 changes: 6 additions & 1 deletion plugins/block-dynamic-connection/src/dynamic_text_join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ const DYNAMIC_TEXT_JOIN_MIXIN = {
* @param state The state to apply to this block, ie the item count.
*/
loadExtraState: function(
this: DynamicTextJoinBlock, state: {[x: string]: any}) {
this: DynamicTextJoinBlock, state: ({[x: string]: any} | string)) {
if (typeof state === 'string') {
this.domToMutation(Blockly.utils.xml.textToDom(state));
return;
}

this.itemCount = state['itemCount'];
// minInputs are added automatically.
for (let i = this.minInputs; i < this.itemCount; i++) {
Expand Down
73 changes: 73 additions & 0 deletions plugins/block-dynamic-connection/test/dynamic_if.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,79 @@ suite('If block', function() {
block, [/IF0/, /DO0/, /IF1/, /DO1/, /ELSE/], 'dynamic_if');
},
},
{
title: 'one if one else with children - json with stringified old XML',
json: {
'type': 'dynamic_if',
'id': '1',
'extraState':
'<mutation inputs="1,2" else="true" next="3"></mutation>',
'inputs': {
'IF1': {
'block': {
'type': 'logic_boolean',
'id': '2',
'fields': {
'BOOL': 'TRUE',
},
},
},
'IF2': {
'block': {
'type': 'logic_boolean',
'id': '3',
'fields': {
'BOOL': 'TRUE',
},
},
},
'ELSE': {
'block': {
'type': 'text_print',
'id': '4',
},
},
},
},
expectedJson: {
'type': 'dynamic_if',
'id': '1',
'extraState': {
'elseIfCount': 1,
'hasElse': true,
},
'inputs': {
'IF0': {
'block': {
'type': 'logic_boolean',
'id': '2',
'fields': {
'BOOL': 'TRUE',
},
},
},
'IF1': {
'block': {
'type': 'logic_boolean',
'id': '3',
'fields': {
'BOOL': 'TRUE',
},
},
},
'ELSE': {
'block': {
'type': 'text_print',
'id': '4',
},
},
},
},
assertBlockStructure: (block) => {
assertBlockStructure(
block, [/IF0/, /DO0/, /IF1/, /DO1/, /ELSE/], 'dynamic_if');
},
},
];
testHelpers.runSerializationTestSuite(testCases);
});
95 changes: 95 additions & 0 deletions plugins/block-dynamic-connection/test/dynamic_list_create.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,101 @@ suite('List create block', function() {
assertBlockStructure(block, [/ADD0/, /ADD1/, /ADD2/, /ADD3/]);
},
},
{
title: 'multiple inputs with children - json with stringified old XML',
json: {
'type': 'dynamic_list_create',
'id': '1',
'extraState':
'<mutation inputs="ADD0,ADD1,ADD2,ADD3" next="4"></mutation>',
'inputs': {
'ADD0': {
'block': {
'type': 'text',
'id': '2',
'fields': {
'TEXT': 'a',
},
},
},
'ADD1': {
'block': {
'type': 'text',
'id': '3',
'fields': {
'TEXT': 'b',
},
},
},
'ADD2': {
'block': {
'type': 'text',
'id': '4',
'fields': {
'TEXT': 'c',
},
},
},
'ADD3': {
'block': {
'type': 'text',
'id': '5',
'fields': {
'TEXT': 'd',
},
},
},
},
},
expectedJson: {
'type': 'dynamic_list_create',
'id': '1',
'extraState': {
'itemCount': 4,
},
'inputs': {
'ADD0': {
'block': {
'type': 'text',
'id': '2',
'fields': {
'TEXT': 'a',
},
},
},
'ADD1': {
'block': {
'type': 'text',
'id': '3',
'fields': {
'TEXT': 'b',
},
},
},
'ADD2': {
'block': {
'type': 'text',
'id': '4',
'fields': {
'TEXT': 'c',
},
},
},
'ADD3': {
'block': {
'type': 'text',
'id': '5',
'fields': {
'TEXT': 'd',
},
},
},
},
},
assertBlockStructure: (block) => {
assertBlockStructure(block, [/ADD0/, /ADD1/, /ADD2/, /ADD3/]);
},
},
];
testHelpers.runSerializationTestSuite(testCases);
});
95 changes: 95 additions & 0 deletions plugins/block-dynamic-connection/test/dynamic_text_join.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,101 @@ suite('Text join block', function() {
assertBlockStructure(block, [/ADD0/, /ADD1/, /ADD2/, /ADD3/]);
},
},
{
title: 'multiple inputs with children - json with stringified old XML',
json: {
'type': 'dynamic_text_join',
'id': '1',
'extraState':
'<mutation inputs="ADD0,ADD1,ADD2,ADD3" next="4"></mutation>',
'inputs': {
'ADD0': {
'block': {
'type': 'text',
'id': '2',
'fields': {
'TEXT': 'a',
},
},
},
'ADD1': {
'block': {
'type': 'text',
'id': '3',
'fields': {
'TEXT': 'b',
},
},
},
'ADD2': {
'block': {
'type': 'text',
'id': '4',
'fields': {
'TEXT': 'c',
},
},
},
'ADD3': {
'block': {
'type': 'text',
'id': '5',
'fields': {
'TEXT': 'd',
},
},
},
},
},
expectedJson: {
'type': 'dynamic_text_join',
'id': '1',
'extraState': {
'itemCount': 4,
},
'inputs': {
'ADD0': {
'block': {
'type': 'text',
'id': '2',
'fields': {
'TEXT': 'a',
},
},
},
'ADD1': {
'block': {
'type': 'text',
'id': '3',
'fields': {
'TEXT': 'b',
},
},
},
'ADD2': {
'block': {
'type': 'text',
'id': '4',
'fields': {
'TEXT': 'c',
},
},
},
'ADD3': {
'block': {
'type': 'text',
'id': '5',
'fields': {
'TEXT': 'd',
},
},
},
},
},
assertBlockStructure: (block) => {
assertBlockStructure(block, [/ADD0/, /ADD1/, /ADD2/, /ADD3/]);
},
},
];
testHelpers.runSerializationTestSuite(testCases);
});

0 comments on commit e98e862

Please sign in to comment.