@@ -7,7 +7,11 @@ goog.require('Blockly.Colours');
77goog . require ( 'Blockly.ScratchBlocks.VerticalExtensions' ) ;
88
99Blockly . Blocks [ 'control_expandableCase' ] = {
10- init : function ( ) {
10+ /**
11+ * pm: Block for joining strings together (determined by user)
12+ * @this Blockly.Block
13+ */
14+ init : function ( ) {
1115 this . jsonInit ( {
1216 "message0" : '%1 %2' ,
1317 "args0" : [
@@ -23,6 +27,78 @@ Blockly.Blocks['control_expandableCase'] = {
2327 "category" : Blockly . Categories . control ,
2428 "extensions" : [ "colours_control" , "shape_case" ]
2529 } ) ;
30+
31+ this . cases_ = 0 ;
32+ if ( this . isInFlyout ) this . addCase ( ) ;
33+ } ,
34+
35+ fillInBlock : function ( connection ) {
36+ if ( connection . sourceBlock_ . isInsertionMarker_ ) return ;
37+ const block = this . workspace . newBlock ( 'text' ) ;
38+ // TODO text values are undefined/dont save
39+ block . setShadow ( true ) ;
40+ block . initSvg ( ) ;
41+ block . render ( false ) ;
42+ block . outputConnection . connect ( connection ) ;
43+ } ,
44+ addCase : function ( ) {
45+ this . appendDummyInput ( `BREAK${ this . cases_ } ` ) . appendField ( "case" ) ;
46+ const input = this . appendValueInput ( `CASE${ this . cases_ } ` ) ;
47+ this . fillInBlock ( input . connection ) ;
48+ this . appendStatementInput ( `STACKCASE${ this . cases_ } ` ) ;
49+ } ,
50+
51+ mutationToDom : function ( ) {
52+ // on save
53+ const container = document . createElement ( "mutation" ) ;
54+ let number = Number ( this . cases_ ) ;
55+ if ( isNaN ( number ) ) number = 1 ;
56+ container . setAttribute ( "casecount" , String ( number ) ) ;
57+ return container ;
58+ } ,
59+
60+ domToMutation : function ( xmlElement ) {
61+ // on load
62+ const inputCount = Number ( xmlElement . getAttribute ( "casecount" ) ) ;
63+ this . cases_ = isNaN ( inputCount ) ? 0 : inputCount ;
64+ for ( let i = 0 ; i < this . cases_ ; i ++ ) this . addCase ( ) ;
65+ // TODO white text blocks keep spawing for all expandables, i feel like this is just due to me doing it wrong
66+ // TODO stack values dont save
67+ /*queueMicrotask(() => {
68+ const connections = this.getConnections_();
69+ for (let i = 1; i < connections.length; i++) {
70+ const block = connections[i].targetBlock();
71+ if (!block) continue;
72+ if (
73+ !block.category_ && !block.isShadow() &&
74+ !block.type.startsWith("procedures_") && !block.type.startsWith("argument_")
75+ ) block.dispose();
76+ }
77+ });*/
78+ } ,
79+
80+ onExpandableButtonClicked_ : function ( isAdding ) {
81+ // Create an event group to keep field value and mutator in sync
82+ // Return null at the end because setValue is called here already.
83+ Blockly . Events . setGroup ( true ) ;
84+ var oldMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
85+ if ( isAdding ) {
86+ this . cases_ ++ ;
87+ this . addCase ( ) ;
88+ } else if ( this . cases_ > 1 ) {
89+ this . removeInput ( `CASE${ this . cases_ } ` ) ;
90+ this . removeInput ( `STACKCASE${ this . cases_ } ` ) ;
91+ this . removeInput ( `BREAK${ this . cases_ } ` ) ;
92+ this . cases_ -- ;
93+ }
94+ this . initSvg ( ) ;
95+ if ( this . rendered ) this . render ( ) ;
96+
97+ var newMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
98+ Blockly . Events . fire ( new Blockly . Events . BlockChange (
99+ this , 'mutation' , null , oldMutation , newMutation
100+ ) ) ;
101+ Blockly . Events . setGroup ( false ) ;
26102 }
27103} ;
28104
0 commit comments