@@ -57,6 +57,170 @@ const fetchProjectToken = projectId => {
57
57
} ) ;
58
58
} ;
59
59
60
+ function protobufToJson ( buffer ) {
61
+ const message = Project . decode ( buffer ) ;
62
+ const json = Project . toObject ( message ) ;
63
+
64
+ const newJson = {
65
+ targets : [ ] ,
66
+ monitors : [ ] ,
67
+ extensionData : { } ,
68
+ extensions : json . extensions ,
69
+ extensionURLs : { } ,
70
+ meta : {
71
+ semver : json . metaSemver ,
72
+ vm : json . metaVm ,
73
+ agent : json . metaAgent || ""
74
+ } ,
75
+ customFonts : json . fonts
76
+ } ;
77
+
78
+ for ( const target of json . targets ) {
79
+ let newTarget = {
80
+ isStage : target . isStage || false ,
81
+ name : target . name ,
82
+ variables : { } ,
83
+ lists : { } ,
84
+ broadcasts : { } ,
85
+ customVars : [ ] ,
86
+ blocks : { } ,
87
+ comments : { } ,
88
+ currentCostume : target . currentCostume ,
89
+ costumes : [ ] ,
90
+ sounds : [ ] ,
91
+ id : target . id ,
92
+ volume : target . volume ,
93
+ layerOrder : target . layerOrder ,
94
+ tempo : target . tempo ,
95
+ videoTransparency : target . videoTransparency ,
96
+ videoState : target . videoState ,
97
+ textToSpeechLanguage : target . textToSpeechLanguage || null ,
98
+ visible : target . visible ,
99
+ x : target . x ,
100
+ y : target . y ,
101
+ size : target . size ,
102
+ direction : target . direction ,
103
+ draggable : target . draggable ,
104
+ rotationStyle : target . rotationStyle
105
+ } ;
106
+
107
+ if ( newTarget . isStage ) {
108
+ delete newTarget . visible , delete newTarget . size , delete newTarget . direction , delete newTarget . draggable , delete newTarget . rotationStyle ;
109
+ }
110
+
111
+ for ( const variable in target . variables ) {
112
+ newTarget . variables [ variable ] = [ target . variables [ variable ] . name , target . variables [ variable ] . value ] ;
113
+ }
114
+
115
+ for ( const list in target . lists ) {
116
+ newTarget . lists [ list ] = [ target . lists [ list ] . name , target . lists [ list ] . value || [ ] ] ;
117
+ }
118
+
119
+ for ( const broadcast in target . broadcasts ) {
120
+ newTarget . broadcasts [ broadcast ] = target . broadcasts [ broadcast ] ;
121
+ }
122
+
123
+ for ( const customVar in target . customVars ) {
124
+ newTarget . customVars . push ( target . customVars [ customVar ] ) ;
125
+ }
126
+
127
+ for ( const block in target . blocks ) {
128
+ if ( target . blocks [ block ] . is_variable_reporter ) {
129
+ newTarget . blocks [ block ] = [
130
+ target . blocks [ block ] . varReporterBlock . first_num ,
131
+ target . blocks [ block ] . varReporterBlock . name ,
132
+ target . blocks [ block ] . varReporterBlock . id ,
133
+ target . blocks [ block ] . varReporterBlock . second_num ,
134
+ target . blocks [ block ] . varReporterBlock . third_num ,
135
+ ]
136
+ continue ;
137
+ }
138
+
139
+ newTarget . blocks [ block ] = {
140
+ opcode : target . blocks [ block ] . opcode ,
141
+ next : target . blocks [ block ] . next || null ,
142
+ parent : target . blocks [ block ] . parent || null ,
143
+ inputs : { } ,
144
+ fields : { } ,
145
+ shadow : target . blocks [ block ] . shadow ,
146
+ topLevel : target . blocks [ block ] . topLevel ,
147
+ x : target . blocks [ block ] . x ,
148
+ y : target . blocks [ block ] . y
149
+ }
150
+
151
+ if ( target . blocks [ block ] . mutation ) {
152
+ newTarget . blocks [ block ] . mutation = {
153
+ tagName : target . blocks [ block ] . mutation . tagName ,
154
+ proccode : target . blocks [ block ] . mutation . proccode ,
155
+ argumentids : target . blocks [ block ] . mutation . argumentids ,
156
+ argumentnames : target . blocks [ block ] . mutation . argumentnames ,
157
+ argumentdefaults : target . blocks [ block ] . mutation . argumentdefaults ,
158
+ warp : target . blocks [ block ] . mutation . warp ,
159
+ returns : target . blocks [ block ] . mutation . _returns ,
160
+ edited : target . blocks [ block ] . mutation . edited ,
161
+ optype : target . blocks [ block ] . mutation . optype ,
162
+ color : target . blocks [ block ] . mutation . color ,
163
+ hasnext : target . blocks [ block ] . next ? true : false ,
164
+ children : [ ]
165
+ }
166
+ }
167
+
168
+ for ( const input in target . blocks [ block ] . inputs ) {
169
+ newTarget . blocks [ block ] . inputs [ input ] = JSON . parse ( target . blocks [ block ] . inputs [ input ] ) ;
170
+ }
171
+
172
+ for ( const field in target . blocks [ block ] . fields ) {
173
+ newTarget . blocks [ block ] . fields [ field ] = JSON . parse ( target . blocks [ block ] . fields [ field ] ) ;
174
+ }
175
+ }
176
+
177
+ for ( const comment in target . comments ) {
178
+ newTarget . comments [ comment ] = target . comments [ comment ] ;
179
+ }
180
+
181
+ for ( const costume in target . costumes ) {
182
+ newTarget . costumes [ costume ] = target . costumes [ costume ] ;
183
+ }
184
+
185
+ for ( const sound in target . sounds ) {
186
+ newTarget . sounds [ sound ] = target . sounds [ sound ] ;
187
+ }
188
+
189
+ newJson . targets . push ( newTarget ) ;
190
+ }
191
+
192
+ for ( const monitor in json . monitors ) {
193
+ let newMonitor = {
194
+ id : json . monitors [ monitor ] . id ,
195
+ mode : json . monitors [ monitor ] . mode ,
196
+ opcode : json . monitors [ monitor ] . opcode ,
197
+ params : json . monitors [ monitor ] . params ,
198
+ spriteName : json . monitors [ monitor ] . spriteName || null ,
199
+ value : json . monitors [ monitor ] . value ,
200
+ width : json . monitors [ monitor ] . width ,
201
+ height : json . monitors [ monitor ] . height ,
202
+ x : json . monitors [ monitor ] . x ,
203
+ y : json . monitors [ monitor ] . y ,
204
+ visible : json . monitors [ monitor ] . visible ,
205
+ sliderMin : json . monitors [ monitor ] . sliderMin ,
206
+ sliderMax : json . monitors [ monitor ] . sliderMax ,
207
+ isDiscrete : json . monitors [ monitor ] . isDiscrete
208
+ }
209
+
210
+ newJson . monitors . push ( newMonitor ) ;
211
+ }
212
+
213
+ for ( const extensionData in json . extensionData ) {
214
+ newJson . extensionData [ extensionData ] = JSON . parse ( json . extensionData [ extensionData ] ) ;
215
+ }
216
+
217
+ for ( const extensionURL in json . extensionURLs ) {
218
+ newJson . extensionURLs [ extensionURL ] = json . extensionURLs [ extensionURL ] ;
219
+ }
220
+
221
+ return newJson ;
222
+ }
223
+
60
224
/* Higher Order Component to provide behavior for loading projects by id. If
61
225
* there's no id, the default project is loaded.
62
226
* @param {React.Component } WrappedComponent component to receive projectData prop
0 commit comments