1
- /* global Opal */
2
1
import _ from 'lodash' ;
3
2
import Blockly from 'scratch-blocks' ;
4
3
import { RubyToBlocksConverterError } from './errors' ;
@@ -7,54 +6,53 @@ import {RubyToBlocksConverterError} from './errors';
7
6
* My Blocks converter
8
7
*/
9
8
const MyBlocksConverter = {
10
- // eslint-disable-next-line no-unused-vars
11
- onSend : function ( receiver , name , args , rubyBlockArgs , rubyBlock ) {
12
- let block ;
13
- if ( this . _isSelf ( receiver ) || receiver === Opal . nil ) {
14
- const procedure = this . _lookupProcedure ( name ) ;
15
- if ( procedure ) {
16
- if ( procedure . argumentIds . length === args . length ) {
17
- block = this . _createBlock ( 'procedures_call' , 'statement' , {
18
- mutation : {
19
- argumentids : JSON . stringify ( procedure . argumentIds ) ,
20
- children : [ ] ,
21
- proccode : procedure . procCode . join ( ' ' ) ,
22
- tagName : 'mutation' ,
23
- warp : 'false'
24
- }
25
- } ) ;
9
+ register : function ( converter ) {
10
+ // Register my-block handler for procedure calls
11
+ converter . registerCallMyBlock ( 'self' , params => {
12
+ const { name, args, procedure} = params ;
26
13
27
- if ( Object . prototype . hasOwnProperty . call ( this . _context . procedureCallBlocks , procedure . id ) ) {
28
- this . _context . procedureCallBlocks [ procedure . id ] . push ( block . id ) ;
29
- } else {
30
- this . _context . procedureCallBlocks [ procedure . id ] = [ block . id ] ;
31
- }
14
+ if ( procedure . argumentIds . length !== args . length ) return null ;
32
15
33
- args . forEach ( ( arg , i ) => {
34
- const argumentId = procedure . argumentIds [ i ] ;
35
- if ( this . _isFalseOrBooleanBlock ( arg ) ) {
36
- if ( procedure . argumentVariables [ i ] . isBoolean ||
37
- this . _changeToBooleanArgument ( procedure . argumentNames [ i ] ) ) {
38
- if ( ! this . _isFalse ( arg ) ) {
39
- this . _addInput ( block , argumentId , arg , null ) ;
40
- }
41
- return ;
42
- }
43
- }
44
- if ( ! procedure . argumentVariables [ i ] . isBoolean &&
45
- ( this . _isNumberOrBlock ( arg ) || this . _isStringOrBlock ( arg ) ) ) {
46
- this . _addTextInput ( block , argumentId , this . _isNumber ( arg ) ? arg . toString ( ) : arg , '' ) ;
47
- return ;
48
- }
49
- throw new RubyToBlocksConverterError (
50
- this . _context . currentNode ,
51
- `invalid type of My Block "${ name } " argument #${ i + 1 } `
52
- ) ;
53
- } ) ;
16
+ const block = converter . _createBlock ( 'procedures_call' , 'statement' , {
17
+ mutation : {
18
+ argumentids : JSON . stringify ( procedure . argumentIds ) ,
19
+ children : [ ] ,
20
+ proccode : procedure . procCode . join ( ' ' ) ,
21
+ tagName : 'mutation' ,
22
+ warp : 'false'
54
23
}
24
+ } ) ;
25
+
26
+ if ( Object . prototype . hasOwnProperty . call ( converter . _context . procedureCallBlocks , procedure . id ) ) {
27
+ converter . _context . procedureCallBlocks [ procedure . id ] . push ( block . id ) ;
28
+ } else {
29
+ converter . _context . procedureCallBlocks [ procedure . id ] = [ block . id ] ;
55
30
}
56
- }
57
- return block ;
31
+
32
+ args . forEach ( ( arg , i ) => {
33
+ const argumentId = procedure . argumentIds [ i ] ;
34
+ if ( converter . _isFalseOrBooleanBlock ( arg ) ) {
35
+ if ( procedure . argumentVariables [ i ] . isBoolean ||
36
+ converter . _changeToBooleanArgument ( procedure . argumentNames [ i ] ) ) {
37
+ if ( ! converter . _isFalse ( arg ) ) {
38
+ converter . _addInput ( block , argumentId , arg , null ) ;
39
+ }
40
+ return ;
41
+ }
42
+ }
43
+ if ( ! procedure . argumentVariables [ i ] . isBoolean &&
44
+ ( converter . _isNumberOrBlock ( arg ) || converter . _isStringOrBlock ( arg ) ) ) {
45
+ converter . _addTextInput ( block , argumentId , converter . _isNumber ( arg ) ? arg . toString ( ) : arg , '' ) ;
46
+ return ;
47
+ }
48
+ throw new RubyToBlocksConverterError (
49
+ converter . _context . currentNode ,
50
+ `invalid type of My Block "${ name } " argument #${ i + 1 } `
51
+ ) ;
52
+ } ) ;
53
+
54
+ return block ;
55
+ } ) ;
58
56
} ,
59
57
60
58
// eslint-disable-next-line no-unused-vars
@@ -112,7 +110,7 @@ const MyBlocksConverter = {
112
110
const originalName = n . toString ( ) ;
113
111
// Convert argument name to snake_case lowercase
114
112
const normalizedName = this . _toSnakeCaseLowercase ( originalName ) ;
115
-
113
+
116
114
procedure . argumentNames . push ( normalizedName ) ;
117
115
procedure . argumentVariables . push ( this . _lookupOrCreateVariable ( normalizedName ) ) ;
118
116
procedure . procCode . push ( '%s' ) ;
0 commit comments