@@ -128,6 +128,85 @@ if (typeof module !== 'undefined' && module.exports) {
128
128
global . pythonEditor = pythonEditor ;
129
129
}
130
130
131
+ /*
132
+ * Returns an object to wrap around Blockly.
133
+ */
134
+ function blocks ( ) {
135
+ 'use strict' ;
136
+
137
+ var blocklyWrapper = { } ;
138
+ var resizeSensorInstance = null ;
139
+ // Stores the Blockly workspace created during injection
140
+ var workspace = null ;
141
+
142
+ blocklyWrapper . init = function ( ) {
143
+ // Lazy loading all the JS files
144
+ script ( 'blockly/blockly_compressed.js' ) ;
145
+ script ( 'blockly/blocks_compressed.js' ) ;
146
+ script ( 'blockly/python_compressed.js' ) ;
147
+ script ( 'microbit_blocks/blocks/microbit.js' ) ;
148
+ script ( 'microbit_blocks/generators/accelerometer.js' ) ;
149
+ script ( 'microbit_blocks/generators/buttons.js' ) ;
150
+ script ( 'microbit_blocks/generators/compass.js' ) ;
151
+ script ( 'microbit_blocks/generators/display.js' ) ;
152
+ script ( 'microbit_blocks/generators/image.js' ) ;
153
+ script ( 'microbit_blocks/generators/microbit.js' ) ;
154
+ script ( 'microbit_blocks/generators/music.js' ) ;
155
+ script ( 'microbit_blocks/generators/neopixel.js' ) ;
156
+ script ( 'microbit_blocks/generators/pins.js' ) ;
157
+ script ( 'microbit_blocks/generators/radio.js' ) ;
158
+ script ( 'microbit_blocks/generators/speech.js' ) ;
159
+ script ( 'microbit_blocks/generators/python.js' ) ;
160
+ script ( 'blockly/msg/js/en.js' ) ;
161
+ script ( 'microbit_blocks/messages/en/messages.js' ) ;
162
+ } ;
163
+
164
+ blocklyWrapper . inject = function ( blocklyElement , toolboxElement , zoomLevel , zoomScaleSteps ) {
165
+ workspace = Blockly . inject ( blocklyElement , {
166
+ toolbox : toolboxElement ,
167
+ zoom : {
168
+ controls : false ,
169
+ wheel : false ,
170
+ startScale : zoomLevel ,
171
+ scaleSpeed : zoomScaleSteps + 1.0
172
+ }
173
+ } ) ;
174
+ // Resize blockly
175
+ resizeSensorInstance = new ResizeSensor ( blocklyElement , function ( ) {
176
+ Blockly . svgResize ( workspace ) ;
177
+ } ) ;
178
+ } ;
179
+
180
+ blocklyWrapper . getCode = function ( ) {
181
+ return workspace ? Blockly . Python . workspaceToCode ( workspace ) : 'Blockly not injected' ;
182
+ } ;
183
+
184
+ blocklyWrapper . addCodeChangeListener = function ( callback ) {
185
+ if ( workspace ) {
186
+ workspace . addChangeListener ( function ( event ) {
187
+ var code = blocklyWrapper . getCode ( ) ;
188
+ callback ( code ) ;
189
+ } ) ;
190
+ } else {
191
+ throw new Error ( 'Trying to add a Blockly change listener before injection.' ) ;
192
+ }
193
+ } ;
194
+
195
+ blocklyWrapper . zoomIn = function ( ) {
196
+ if ( workspace ) {
197
+ Blockly . getMainWorkspace ( ) . zoomCenter ( 1 ) ;
198
+ }
199
+ } ;
200
+
201
+ blocklyWrapper . zoomOut = function ( ) {
202
+ if ( workspace ) {
203
+ Blockly . getMainWorkspace ( ) . zoomCenter ( - 1 ) ;
204
+ }
205
+ } ;
206
+
207
+ return blocklyWrapper ;
208
+ }
209
+
131
210
/*
132
211
The following code contains the various functions that connect the behaviour of
133
212
the editor to the DOM (web-page).
@@ -140,6 +219,8 @@ function web_editor(config) {
140
219
// Global (useful for testing) instance of the ACE wrapper object
141
220
window . EDITOR = pythonEditor ( 'editor' ) ;
142
221
222
+ var BLOCKS = blocks ( ) ;
223
+
143
224
// Indicates if there are unsaved changes to the content of the editor.
144
225
var dirty = false ;
145
226
@@ -176,9 +257,8 @@ function web_editor(config) {
176
257
}
177
258
setFontSize ( fontSize ) ;
178
259
// Change Blockly zoom
179
- var workspace = Blockly . getMainWorkspace ( ) ;
180
- if ( workspace && continueZooming ) {
181
- Blockly . getMainWorkspace ( ) . zoomCenter ( 1 ) ;
260
+ if ( continueZooming ) {
261
+ BLOCKS . zoomIn ( ) ;
182
262
}
183
263
}
184
264
@@ -195,9 +275,8 @@ function web_editor(config) {
195
275
}
196
276
setFontSize ( fontSize ) ;
197
277
// Change Blockly zoom
198
- var workspace = Blockly . getMainWorkspace ( ) ;
199
- if ( workspace && continueZooming ) {
200
- Blockly . getMainWorkspace ( ) . zoomCenter ( - 1 ) ;
278
+ if ( continueZooming ) {
279
+ BLOCKS . zoomOut ( ) ;
201
280
}
202
281
}
203
282
@@ -206,25 +285,7 @@ function web_editor(config) {
206
285
function setupFeatureFlags ( ) {
207
286
if ( config . flags . blocks ) {
208
287
$ ( "#command-blockly" ) . removeClass ( 'hidden' ) ;
209
- // Add includes
210
- script ( 'blockly/blockly_compressed.js' ) ;
211
- script ( 'blockly/blocks_compressed.js' ) ;
212
- script ( 'blockly/python_compressed.js' ) ;
213
- script ( 'microbit_blocks/blocks/microbit.js' ) ;
214
- script ( 'microbit_blocks/generators/accelerometer.js' ) ;
215
- script ( 'microbit_blocks/generators/buttons.js' ) ;
216
- script ( 'microbit_blocks/generators/compass.js' ) ;
217
- script ( 'microbit_blocks/generators/display.js' ) ;
218
- script ( 'microbit_blocks/generators/image.js' ) ;
219
- script ( 'microbit_blocks/generators/microbit.js' ) ;
220
- script ( 'microbit_blocks/generators/music.js' ) ;
221
- script ( 'microbit_blocks/generators/neopixel.js' ) ;
222
- script ( 'microbit_blocks/generators/pins.js' ) ;
223
- script ( 'microbit_blocks/generators/radio.js' ) ;
224
- script ( 'microbit_blocks/generators/speech.js' ) ;
225
- script ( 'microbit_blocks/generators/python.js' ) ;
226
- script ( 'blockly/msg/js/en.js' ) ;
227
- script ( 'microbit_blocks/messages/en/messages.js' ) ;
288
+ BLOCKS . init ( ) ;
228
289
}
229
290
if ( config . flags . snippets ) {
230
291
$ ( "#command-snippet" ) . removeClass ( 'hidden' ) ;
@@ -488,28 +549,15 @@ function web_editor(config) {
488
549
var zoomScaleSteps = 0.2 ;
489
550
var fontSteps = ( getFontSize ( ) - EDITOR . initialFontSize ) / EDITOR . fontSizeStep ;
490
551
var zoomLevel = ( fontSteps * zoomScaleSteps ) + 1.0 ;
491
- var workspace = Blockly . inject ( 'blockly' , {
492
- toolbox : document . getElementById ( 'blockly-toolbox' ) ,
493
- zoom : {
494
- controls : false ,
495
- wheel : false ,
496
- startScale : zoomLevel ,
497
- scaleSpeed : zoomScaleSteps + 1.0
498
- }
499
- } ) ;
500
- var myUpdateFunction = function ( event ) {
501
- var code = Blockly . Python . workspaceToCode ( workspace ) ;
552
+ var blocklyElement = document . getElementById ( 'blockly' ) ;
553
+ var toolboxElement = document . getElementById ( 'blockly-toolbox' ) ;
554
+ BLOCKS . inject ( blocklyElement , toolboxElement , zoomLevel , zoomScaleSteps ) ;
555
+ BLOCKS . addCodeChangeListener ( function ( code ) {
502
556
EDITOR . setCode ( code ) ;
503
- } ;
504
- // Resize blockly
505
- var element = document . getElementById ( 'blockly' ) ;
506
- new ResizeSensor ( element , function ( ) {
507
- Blockly . svgResize ( workspace ) ;
508
557
} ) ;
509
- workspace . addChangeListener ( myUpdateFunction ) ;
510
558
}
511
559
// Set editor to current state of blocks.
512
- EDITOR . setCode ( Blockly . Python . workspaceToCode ( workspace ) ) ;
560
+ EDITOR . setCode ( BLOCKS . getCode ( ) ) ;
513
561
}
514
562
}
515
563
0 commit comments