@@ -28,257 +28,6 @@ Blockly.Blocks['test_spread'] = {
2828 } ,
2929} ;
3030
31- Blockly . Blocks [ 'operator_expandableBool' ] = {
32- /**
33- * pm: Block for performing multiple truth operations (determined by user)
34- * @this Blockly.Block
35- */
36- init : function ( ) {
37- this . jsonInit ( {
38- "message0" : '%1 %2' ,
39- "args0" : [
40- {
41- "type" : "field_expandable_remove" ,
42- "name" : "REMOVE"
43- } ,
44- {
45- "type" : "field_expandable_add" ,
46- "name" : "ADD"
47- }
48- ] ,
49- "category" : Blockly . Categories . operators ,
50- "extensions" : [ "colours_operators" , "output_boolean" ]
51- } ) ;
52-
53- this . inputs_ = 0 ;
54- this . expandable_ = true ;
55- } ,
56-
57- fillInBlock : Blockly . scratchBlocksUtils . generateMutatorShadow ,
58- menuGenerator : function ( ) {
59- const dropdown = new Blockly . FieldDropdown ( function ( ) {
60- return [
61- [ "and" , "a" ] , [ "or" , "o" ] , [ "xor" , "x" ] ,
62- [ "nand" , "n" ] , [ "nor" , "N" ] , [ "xnor" , "X" ]
63- ] ;
64- } ) ;
65- const ogSetValue = dropdown . setValue ;
66- dropdown . setValue = function ( value , omitMutation ) {
67- const srcBlock = this . sourceBlock_ ;
68- let oldMutation ;
69- if ( ! omitMutation ) oldMutation = Blockly . Xml . domToText ( srcBlock . mutationToDom ( ) ) ;
70-
71- ogSetValue . call ( this , value ) ;
72- if ( ! omitMutation ) {
73- const newMutation = Blockly . Xml . domToText ( srcBlock . mutationToDom ( ) ) ;
74- Blockly . Events . fire ( new Blockly . Events . BlockChange (
75- srcBlock , 'mutation' , null , oldMutation , newMutation
76- ) ) ;
77- }
78- }
79- return dropdown ;
80- } ,
81-
82- mutationToDom : function ( ) {
83- // on save
84- const container = document . createElement ( "mutation" ) ;
85- container . setAttribute ( "inputcount" , String ( this . inputs_ ) ) ;
86- const operations = [ ] ;
87- for ( var i = 1 ; i < this . inputList . length ; i ++ ) {
88- const input = this . inputList [ i ] ;
89- if ( input . fieldRow [ 0 ] ) operations . push ( input . fieldRow [ 0 ] . getValue ( ) ) ;
90- }
91-
92- container . setAttribute ( "menuvalues" , operations . join ( "" ) ) ;
93- container . setAttribute ( "optimize" , operations . every ( ( o ) => operations [ 0 ] === o ) ) ;
94- return container ;
95- } ,
96- domToMutation : function ( xmlElement ) {
97- // on load
98- const inputCount = Number ( xmlElement . getAttribute ( "inputcount" ) ) ;
99- const menuValues = String ( xmlElement . getAttribute ( "menuvalues" ) ) ;
100- this . inputs_ = isNaN ( inputCount ) ? 0 : inputCount ;
101-
102- let repeatPreventer = false ;
103- if ( this . inputList . length > 1 ) {
104- // this was a control z action
105-
106- if ( this . inputList . length - 1 === menuValues . length ) repeatPreventer = true ;
107- else {
108- const lastInput = this . inputList [ this . inputList . length - 1 ] ;
109- const innerBlock = lastInput . connection . targetBlock ( ) ;
110- if ( innerBlock . isShadow ( ) ) innerBlock . dispose ( ) ;
111- this . removeInput ( lastInput . name ) ;
112- return ;
113- }
114- }
115-
116- for ( let i = 0 ; i < this . inputs_ ; i ++ ) {
117- if ( repeatPreventer && this . getInput ( `BOOL${ i + 1 } ` ) ) continue ;
118-
119- const input = this . appendValueInput ( `BOOL${ i + 1 } ` ) . setCheck ( "Boolean" ) ;
120- if ( i > 0 ) {
121- const menu = input . appendField ( this . menuGenerator ( ) ) ;
122- menu . fieldRow [ 0 ] . setValue ( menuValues [ i - 1 ] ? menuValues [ i - 1 ] : "a" , true ) ;
123- }
124- // vm will automatically replace empty inputs with saved shadows
125- }
126- } ,
127-
128- onExpandableButtonClicked_ : function ( isAdding ) {
129- // Create an event group to keep field value and mutator in sync
130- // Return null at the end because setValue is called here already.
131- Blockly . Events . setGroup ( true ) ;
132- var oldMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
133- if ( isAdding ) {
134- this . inputs_ ++ ;
135- const number = this . inputs_ ;
136- const newInput = this . appendValueInput ( `BOOL${ number } ` ) . setCheck ( "Boolean" ) ;
137- if ( ! this . isInsertionMarker_ ) {
138- newInput . init ( ) ;
139- newInput . initOutlinePath ( this . svgGroup_ ) ;
140- newInput . outlinePath . setAttribute ( 'fill' , this . getColourTertiary ( ) ) ;
141- }
142- newInput . appendField ( this . menuGenerator ( ) ) ;
143- this . fillInBlock ( newInput . connection , "checkbox" ) ;
144- } else if ( this . inputs_ > 2 ) {
145- const number = this . inputs_ ;
146- this . removeInput ( `BOOL${ number } ` ) ;
147- this . inputs_ -- ;
148- }
149- this . initSvg ( ) ;
150- if ( this . rendered ) this . render ( ) ;
151-
152- const newMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
153- Blockly . Events . fire ( new Blockly . Events . BlockChange (
154- this , 'mutation' , null , oldMutation , newMutation
155- ) ) ;
156- Blockly . Events . setGroup ( false ) ;
157- }
158- } ;
159-
160- Blockly . Blocks [ 'operator_expandableCompare' ] = {
161- /**
162- * pm: Block for performing multiple comparisons (determined by user)
163- * @this Blockly.Block
164- */
165- init : function ( ) {
166- this . jsonInit ( {
167- "message0" : '%1 %2' ,
168- "args0" : [
169- {
170- "type" : "field_expandable_remove" ,
171- "name" : "REMOVE"
172- } ,
173- {
174- "type" : "field_expandable_add" ,
175- "name" : "ADD"
176- }
177- ] ,
178- "category" : Blockly . Categories . operators ,
179- "extensions" : [ "colours_operators" , "output_boolean" ]
180- } ) ;
181-
182- this . inputs_ = 0 ;
183- this . expandable_ = true ;
184- } ,
185-
186- fillInBlock : Blockly . scratchBlocksUtils . generateMutatorShadow ,
187- menuGenerator : function ( ) {
188- const dropdown = new Blockly . FieldDropdown ( function ( ) {
189- return [
190- [ ">" , "m" ] , [ "≥" , "M" ] , [ "<" , "l" ] , [ "≤" , "L" ] ,
191- [ "=" , "e" ] , [ "===" , "E" ] , [ "≠" , "n" ]
192- ] ;
193- } ) ;
194- const ogSetValue = dropdown . setValue ;
195- dropdown . setValue = function ( value , omitMutation ) {
196- const srcBlock = this . sourceBlock_ ;
197- let oldMutation ;
198- if ( ! omitMutation ) oldMutation = Blockly . Xml . domToText ( srcBlock . mutationToDom ( ) ) ;
199-
200- ogSetValue . call ( this , value ) ;
201- if ( ! omitMutation ) {
202- const newMutation = Blockly . Xml . domToText ( srcBlock . mutationToDom ( ) ) ;
203- Blockly . Events . fire ( new Blockly . Events . BlockChange (
204- srcBlock , 'mutation' , null , oldMutation , newMutation
205- ) ) ;
206- }
207- }
208- return dropdown ;
209- } ,
210-
211- mutationToDom : function ( ) {
212- // on save
213- const container = document . createElement ( "mutation" ) ;
214- container . setAttribute ( "inputcount" , String ( this . inputs_ ) ) ;
215- let orderedOperations = "" ;
216- for ( var i = 1 ; i < this . inputList . length ; i ++ ) {
217- const input = this . inputList [ i ] ;
218- if ( input . fieldRow [ 0 ] ) orderedOperations += input . fieldRow [ 0 ] . getValue ( ) ;
219- }
220- container . setAttribute ( "menuvalues" , orderedOperations ) ;
221- return container ;
222- } ,
223- domToMutation : function ( xmlElement ) {
224- // on load
225- const inputCount = Number ( xmlElement . getAttribute ( "inputcount" ) ) ;
226- const menuValues = String ( xmlElement . getAttribute ( "menuvalues" ) ) ;
227- this . inputs_ = isNaN ( inputCount ) ? 0 : inputCount ;
228-
229- let repeatPreventer = false ;
230- if ( this . inputList . length > 1 ) {
231- // this was a control z action
232-
233- if ( this . inputList . length - 1 === menuValues . length ) repeatPreventer = true ;
234- else {
235- const lastInput = this . inputList [ this . inputList . length - 1 ] ;
236- const innerBlock = lastInput . connection . targetBlock ( ) ;
237- if ( innerBlock . isShadow ( ) ) innerBlock . dispose ( ) ;
238- this . removeInput ( lastInput . name ) ;
239- return ;
240- }
241- }
242-
243- for ( let i = 0 ; i < this . inputs_ ; i ++ ) {
244- if ( repeatPreventer && this . getInput ( `INPUT${ i + 1 } ` ) ) continue ;
245-
246- const input = this . appendValueInput ( `INPUT${ i + 1 } ` ) ;
247- if ( i > 0 ) {
248- const menu = input . appendField ( this . menuGenerator ( ) ) ;
249- menu . fieldRow [ 0 ] . setValue ( menuValues [ i - 1 ] ? menuValues [ i - 1 ] : "m" , true ) ;
250- }
251- // vm will automatically replace empty inputs with saved shadows
252- }
253- } ,
254-
255- onExpandableButtonClicked_ : function ( isAdding ) {
256- // Create an event group to keep field value and mutator in sync
257- // Return null at the end because setValue is called here already.
258- Blockly . Events . setGroup ( true ) ;
259- var oldMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
260- if ( isAdding ) {
261- this . inputs_ ++ ;
262- const number = this . inputs_ ;
263- const newInput = this . appendValueInput ( `INPUT${ number } ` ) ;
264- newInput . appendField ( this . menuGenerator ( ) ) ;
265- this . fillInBlock ( newInput . connection , "text" , "" , "TEXT" ) ;
266- } else if ( this . inputs_ > 2 ) {
267- const number = this . inputs_ ;
268- this . removeInput ( `INPUT${ number } ` ) ;
269- this . inputs_ -- ;
270- }
271- this . initSvg ( ) ;
272- if ( this . rendered ) this . render ( ) ;
273-
274- const newMutation = Blockly . Xml . domToText ( this . mutationToDom ( ) ) ;
275- Blockly . Events . fire ( new Blockly . Events . BlockChange (
276- this , 'mutation' , null , oldMutation , newMutation
277- ) ) ;
278- Blockly . Events . setGroup ( false ) ;
279- }
280- } ;
281-
28231Blockly . Blocks [ 'field_textdropdown_test' ] = {
28332 init : function ( ) {
28433 this . jsonInit ( {
0 commit comments