@@ -379,261 +379,27 @@ Blockly.WorkspaceAudio.prototype.preload = function() {
379
379
} ;
380
380
381
381
// https://groups.google.com/forum/#!msg/blockly/eS1V49pI9c8/VEh5UuUcBAAJ
382
- // Custom Variable Block with Download Context Menu
383
- Blockly . defineBlocksWithJsonArray ( [
384
- // BEGIN JSON EXTRACT
385
- // Block for variable getter.
386
- {
387
- type : 'variables_get' ,
388
- message0 : '%1' ,
389
- args0 : [
390
- {
391
- type : 'field_variable' ,
392
- name : 'VAR' ,
393
- variable : '%{BKY_VARIABLES_DEFAULT_NAME}' ,
394
- } ,
395
- ] ,
396
- colour : '#DEDEDE' ,
397
- output : null ,
398
- helpUrl : '%{BKY_VARIABLES_GET_HELPURL}' ,
399
- tooltip : '%{BKY_VARIABLES_GET_TOOLTIP}' ,
400
- extensions : [ 'customContextMenu_variableSetterGetter' ] ,
401
- } ,
402
- // Block for variable setter.
403
- {
404
- type : 'variables_set' ,
405
- message0 : '%{BKY_VARIABLES_SET}' ,
406
- args0 : [
407
- {
408
- type : 'field_variable' ,
409
- name : 'VAR' ,
410
- variable : '%{BKY_VARIABLES_DEFAULT_NAME}' ,
411
- } ,
412
- {
413
- type : 'input_value' ,
414
- name : 'VALUE' ,
415
- } ,
416
- ] ,
417
- colour : '#DEDEDE' ,
418
- previousStatement : null ,
419
- nextStatement : null ,
420
- tooltip : '%{BKY_VARIABLES_SET_TOOLTIP}' ,
421
- helpUrl : '%{BKY_VARIABLES_SET_HELPURL}' ,
422
- extensions : [ 'customContextMenu_variableSetterGetter' ] ,
423
- } ,
424
- ] ) ; // END JSON EXTRACT (Do not delete this comment.)
425
-
426
- /**
427
- * Mixin to add context menu items to create getter/setter blocks for this
428
- * setter/getter.
429
- * Used by blocks 'variables_set' and 'variables_get'.
430
- * @mixin
431
- * @augments Blockly.Block
432
- * @package
433
- * @readonly
434
- */
435
- Blockly . Constants . Variables . CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN = {
436
- /**
437
- * Add menu option to create getter/setter block for this setter/getter.
438
- * @param {!Array } options List of menu options to add to.
439
- * @this Blockly.Block
440
- */
441
- customContextMenu : function ( options ) {
442
- if ( ! this . isInFlyout ) {
443
- // Getter blocks have the option to create a setter block, and vice versa.
444
- if ( this . type == 'variables_get' ) {
445
- var opposite_type = 'variables_set' ;
446
- var contextMenuMsg = Blockly . Msg [ 'VARIABLES_GET_CREATE_SET' ] ;
447
- } else {
448
- var opposite_type = 'variables_get' ;
449
- var contextMenuMsg = Blockly . Msg [ 'VARIABLES_SET_CREATE_GET' ] ;
450
- }
451
-
452
- var option = { enabled : this . workspace . remainingCapacity ( ) > 0 } ;
453
- var name = this . getField ( 'VAR' ) . getText ( ) ;
454
- option . text = contextMenuMsg . replace ( '%1' , name ) ;
455
- var xmlField = document . createElement ( 'field' ) ;
456
- xmlField . setAttribute ( 'name' , 'VAR' ) ;
457
- xmlField . appendChild ( document . createTextNode ( name ) ) ;
458
- var xmlBlock = document . createElement ( 'block' ) ;
459
- xmlBlock . setAttribute ( 'type' , opposite_type ) ;
460
- xmlBlock . appendChild ( xmlField ) ;
461
- option . callback = Blockly . ContextMenu . callbackFactory ( this , xmlBlock ) ;
462
- options . push ( option ) ;
463
-
464
- const downloadOption = {
465
- text : translate ( 'Download' ) ,
466
- enabled : true ,
467
- callback : ( ) => {
468
- const xml = Blockly . Xml . textToDom (
469
- '<xml xmlns="http://www.w3.org/1999/xhtml" collection="false"></xml>'
470
- ) ;
471
- xml . appendChild ( Blockly . Xml . blockToDom ( this ) ) ;
472
- save ( 'binary-bot-block' , true , xml ) ;
473
- } ,
474
- } ;
475
- options . push ( downloadOption ) ;
476
- // Getter blocks have the option to rename or delete that variable.
477
- } else {
478
- if ( this . type == 'variables_get' || this . type == 'variables_get_reporter' ) {
479
- var renameOption = {
480
- text : Blockly . Msg . RENAME_VARIABLE ,
481
- enabled : true ,
482
- callback : Blockly . Constants . Variables . RENAME_OPTION_CALLBACK_FACTORY ( this ) ,
483
- } ;
484
- var name = this . getField ( 'VAR' ) . getText ( ) ;
485
- var deleteOption = {
486
- text : Blockly . Msg . DELETE_VARIABLE . replace ( '%1' , name ) ,
487
- enabled : true ,
488
- callback : Blockly . Constants . Variables . DELETE_OPTION_CALLBACK_FACTORY ( this ) ,
489
- } ;
490
- options . unshift ( renameOption ) ;
491
- options . unshift ( deleteOption ) ;
492
- }
493
- }
494
- } ,
382
+ const addDownloadOption = ( callback , options , block ) => {
383
+ options . push ( {
384
+ text : translate ( 'Download' ) ,
385
+ enabled : true ,
386
+ callback : ( ) => {
387
+ const xml = Blockly . Xml . textToDom ( '<xml xmlns="http://www.w3.org/1999/xhtml" collection="false"></xml>' ) ;
388
+ xml . appendChild ( Blockly . Xml . blockToDom ( block ) ) ;
389
+ save ( 'binary-bot-block' , true , xml ) ;
390
+ } ,
391
+ } ) ;
392
+ callback ( options ) ;
495
393
} ;
496
394
497
- Blockly . Extensions . registerMixin (
498
- 'customContextMenu_variableSetterGetter' ,
499
- Blockly . Constants . Variables . CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN
500
- ) ;
501
-
502
- // Custom Loop block with download Context Menu
503
- Blockly . defineBlocksWithJsonArray ( [
504
- // Block for 'for' loop.
505
- {
506
- type : 'controls_for' ,
507
- message0 : '%{BKY_CONTROLS_FOR_TITLE}' ,
508
- args0 : [
509
- {
510
- type : 'field_variable' ,
511
- name : 'VAR' ,
512
- variable : null ,
513
- } ,
514
- {
515
- type : 'input_value' ,
516
- name : 'FROM' ,
517
- check : 'Number' ,
518
- align : 'RIGHT' ,
519
- } ,
520
- {
521
- type : 'input_value' ,
522
- name : 'TO' ,
523
- check : 'Number' ,
524
- align : 'RIGHT' ,
525
- } ,
526
- {
527
- type : 'input_value' ,
528
- name : 'BY' ,
529
- check : 'Number' ,
530
- align : 'RIGHT' ,
531
- } ,
532
- ] ,
533
- message1 : '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1' ,
534
- args1 : [
535
- {
536
- type : 'input_statement' ,
537
- name : 'DO' ,
538
- } ,
539
- ] ,
540
- colour : '#DEDEDE' ,
541
- inputsInline : true ,
542
- previousStatement : null ,
543
- nextStatement : null ,
544
- style : 'loop_blocks' ,
545
- helpUrl : '%{BKY_CONTROLS_FOR_HELPURL}' ,
546
- extensions : [ 'customContextMenu_newGetVariableBlock' , 'controls_for_customTooltip' ] ,
547
- } ,
548
- // Block for 'for each' loop.
549
- {
550
- type : 'controls_forEach' ,
551
- message0 : '%{BKY_CONTROLS_FOREACH_TITLE}' ,
552
- args0 : [
553
- {
554
- type : 'field_variable' ,
555
- name : 'VAR' ,
556
- variable : null ,
557
- } ,
558
- {
559
- type : 'input_value' ,
560
- name : 'LIST' ,
561
- check : 'Array' ,
562
- } ,
563
- ] ,
564
- message1 : '%{BKY_CONTROLS_REPEAT_INPUT_DO} %1' ,
565
- args1 : [
566
- {
567
- type : 'input_statement' ,
568
- name : 'DO' ,
569
- } ,
570
- ] ,
571
- colour : '#DEDEDE' ,
572
- previousStatement : null ,
573
- nextStatement : null ,
574
- style : 'loop_blocks' ,
575
- helpUrl : '%{BKY_CONTROLS_FOREACH_HELPURL}' ,
576
- extensions : [ 'customContextMenu_newGetVariableBlock' , 'controls_forEach_customTooltip' ] ,
577
- } ,
578
- ] ) ;
579
-
580
- /**
581
- * Mixin to add a context menu item to create a 'variables_get' block.
582
- * Used by blocks 'controls_for' and 'controls_forEach'.
583
- * @mixin
584
- * @augments Blockly.Block
585
- * @package
586
- * @readonly
587
- */
588
- Blockly . Constants . Loops . CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN = {
589
- /**
590
- * Add context menu option to create getter block for the loop's variable.
591
- * (customContextMenu support limited to web BlockSvg.)
592
- * @param {!Array } options List of menu options to add to.
593
- * @this Blockly.Block
594
- */
595
- customContextMenu : function ( options ) {
596
- if ( this . isInFlyout ) {
597
- return ;
598
- }
599
- var variable = this . getField ( 'VAR' ) . getVariable ( ) ;
600
- var varName = variable . name ;
601
- if ( ! this . isCollapsed ( ) && varName != null ) {
602
- var option = { enabled : true } ;
603
- option . text = Blockly . Msg [ 'VARIABLES_SET_CREATE_GET' ] . replace ( '%1' , varName ) ;
604
- var xmlField = Blockly . Variables . generateVariableFieldDom ( variable ) ;
605
- var xmlBlock = document . createElement ( 'block' ) ;
606
- xmlBlock . setAttribute ( 'type' , 'variables_get' ) ;
607
- xmlBlock . appendChild ( xmlField ) ;
608
- option . callback = Blockly . ContextMenu . callbackFactory ( this , xmlBlock ) ;
609
- options . push ( option ) ;
610
- }
611
- const downloadOption = {
612
- text : translate ( 'Download' ) ,
613
- enabled : true ,
614
- callback : ( ) => {
615
- const xml = Blockly . Xml . textToDom (
616
- '<xml xmlns="http://www.w3.org/1999/xhtml" collection="false"></xml>'
617
- ) ;
618
- xml . appendChild ( Blockly . Xml . blockToDom ( this ) ) ;
619
- save ( 'binary-bot-block' , true , xml ) ;
620
- } ,
621
- } ;
622
- options . push ( downloadOption ) ;
623
- } ,
395
+ const originalCustomContextVarFn =
396
+ Blockly . Constants . Variables . CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN . customContextMenu ;
397
+ Blockly . Constants . Variables . CUSTOM_CONTEXT_MENU_VARIABLE_GETTER_SETTER_MIXIN . customContextMenu = function ( options ) {
398
+ addDownloadOption ( originalCustomContextVarFn . bind ( this ) , options , this ) ;
624
399
} ;
625
400
626
- Blockly . Extensions . registerMixin (
627
- 'customContextMenu_newGetVariableBlock' ,
628
- Blockly . Constants . Loops . CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN
629
- ) ;
630
-
631
- Blockly . Extensions . register (
632
- 'controls_for_customTooltip' ,
633
- Blockly . Extensions . buildTooltipWithFieldText ( '%{BKY_CONTROLS_FOR_TOOLTIP}' , 'VAR' )
634
- ) ;
635
-
636
- Blockly . Extensions . register (
637
- 'controls_forEach_customTooltip' ,
638
- Blockly . Extensions . buildTooltipWithFieldText ( '%{BKY_CONTROLS_FOREACH_TOOLTIP}' , 'VAR' )
639
- ) ;
401
+ const originalCustomContextLoopFn =
402
+ Blockly . Constants . Loops . CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN . customContextMenu ;
403
+ Blockly . Constants . Loops . CUSTOM_CONTEXT_MENU_CREATE_VARIABLES_GET_MIXIN . customContextMenu = function ( options ) {
404
+ addDownloadOption ( originalCustomContextLoopFn . bind ( this ) , options , this ) ;
405
+ } ;
0 commit comments