Skip to content

Commit e544299

Browse files
carlosperatemicrobit-carlos
authored andcommitted
Refactor the logic around Blockly into an "object".
The web_editor() function is only meant to connect the behaviour of the editor (and other components) to the DOM. With time this function ended up containing more and more logic outside of its original functionality. In this case we refactor out all the Blockly logic not relevant to the page UI.
1 parent 748f4ff commit e544299

File tree

1 file changed

+91
-43
lines changed

1 file changed

+91
-43
lines changed

python-main.js

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,85 @@ if (typeof module !== 'undefined' && module.exports) {
128128
global.pythonEditor = pythonEditor;
129129
}
130130

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+
131210
/*
132211
The following code contains the various functions that connect the behaviour of
133212
the editor to the DOM (web-page).
@@ -140,6 +219,8 @@ function web_editor(config) {
140219
// Global (useful for testing) instance of the ACE wrapper object
141220
window.EDITOR = pythonEditor('editor');
142221

222+
var BLOCKS = blocks();
223+
143224
// Indicates if there are unsaved changes to the content of the editor.
144225
var dirty = false;
145226

@@ -176,9 +257,8 @@ function web_editor(config) {
176257
}
177258
setFontSize(fontSize);
178259
// Change Blockly zoom
179-
var workspace = Blockly.getMainWorkspace();
180-
if (workspace && continueZooming) {
181-
Blockly.getMainWorkspace().zoomCenter(1);
260+
if (continueZooming) {
261+
BLOCKS.zoomIn();
182262
}
183263
}
184264

@@ -195,9 +275,8 @@ function web_editor(config) {
195275
}
196276
setFontSize(fontSize);
197277
// Change Blockly zoom
198-
var workspace = Blockly.getMainWorkspace();
199-
if (workspace && continueZooming) {
200-
Blockly.getMainWorkspace().zoomCenter(-1);
278+
if (continueZooming) {
279+
BLOCKS.zoomOut();
201280
}
202281
}
203282

@@ -206,25 +285,7 @@ function web_editor(config) {
206285
function setupFeatureFlags() {
207286
if(config.flags.blocks) {
208287
$("#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();
228289
}
229290
if(config.flags.snippets) {
230291
$("#command-snippet").removeClass('hidden');
@@ -488,28 +549,15 @@ function web_editor(config) {
488549
var zoomScaleSteps = 0.2;
489550
var fontSteps = (getFontSize() - EDITOR.initialFontSize) / EDITOR.fontSizeStep;
490551
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) {
502556
EDITOR.setCode(code);
503-
};
504-
// Resize blockly
505-
var element = document.getElementById('blockly');
506-
new ResizeSensor(element, function() {
507-
Blockly.svgResize(workspace);
508557
});
509-
workspace.addChangeListener(myUpdateFunction);
510558
}
511559
// Set editor to current state of blocks.
512-
EDITOR.setCode(Blockly.Python.workspaceToCode(workspace));
560+
EDITOR.setCode(BLOCKS.getCode());
513561
}
514562
}
515563

0 commit comments

Comments
 (0)