Skip to content

Commit 0e50842

Browse files
committed
Merge branch 'carlosperate-blockly_zoom'
2 parents d2bee2d + f735434 commit 0e50842

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

python-main.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ is attached to the div with the referenced id.
1414
function pythonEditor(id) {
1515
// An object that encapsulates the behaviour of the editor.
1616
editor = {};
17+
editor.initialFontSize = 22;
18+
editor.fontSizeStep = 4;
1719

1820
// Represents the ACE based editor.
1921
var ACE = ace.edit(id); // The editor is in the tag with the referenced id.
@@ -24,6 +26,7 @@ function pythonEditor(id) {
2426
ACE.getSession().setMode("ace/mode/python"); // We're editing Python.
2527
ACE.getSession().setTabSize(4); // Tab=4 spaces.
2628
ACE.getSession().setUseSoftTabs(true); // Tabs are really spaces.
29+
ACE.setFontSize(editor.initialFontSize);
2730
editor.ACE = ACE;
2831

2932
// Gets the textual content of the editor (i.e. what the user has written).
@@ -202,32 +205,50 @@ function web_editor(config) {
202205

203206
// Get the font size of the text currently displayed in the editor.
204207
function getFontSize() {
205-
return parseInt($('#editor').css('font-size'));
208+
return EDITOR.ACE.getFontSize();
206209
}
207210

208211
// Set the font size of the text currently displayed in the editor.
209212
function setFontSize(size) {
210-
$('#editor').css('font-size', size + 'px');
213+
EDITOR.ACE.setFontSize(size);
211214
}
212215

213216
// Sets up the zoom-in functionality.
214217
function zoomIn() {
218+
var continueZooming = true;
219+
// Change editor zoom
215220
var fontSize = getFontSize();
216-
fontSize += 8;
217-
if(fontSize > 46) {
218-
fontSize = 46;
221+
fontSize += EDITOR.fontSizeStep;
222+
var fontSizeLimit = EDITOR.initialFontSize + (EDITOR.fontSizeStep * 6);
223+
if (fontSize > fontSizeLimit) {
224+
fontSize = fontSizeLimit;
225+
continueZooming = false;
219226
}
220227
setFontSize(fontSize);
228+
// Change Blockly zoom
229+
var workspace = Blockly.getMainWorkspace();
230+
if (workspace && continueZooming) {
231+
Blockly.getMainWorkspace().zoomCenter(1);
232+
}
221233
};
222234

223235
// Sets up the zoom-out functionality.
224236
function zoomOut() {
237+
var continueZooming = true;
238+
// Change editor zoom
225239
var fontSize = getFontSize();
226-
fontSize -= 8;
227-
if(fontSize < 22) {
228-
fontSize = 22;
240+
fontSize -= EDITOR.fontSizeStep;
241+
var fontSizeLimit = EDITOR.initialFontSize - (EDITOR.fontSizeStep * 3);
242+
if(fontSize < fontSizeLimit) {
243+
fontSize = fontSizeLimit;
244+
continueZooming = false;
229245
}
230246
setFontSize(fontSize);
247+
// Change Blockly zoom
248+
var workspace = Blockly.getMainWorkspace();
249+
if (workspace && continueZooming) {
250+
Blockly.getMainWorkspace().zoomCenter(-1);
251+
}
231252
};
232253

233254
// Checks for feature flags in the config object and shows/hides UI
@@ -371,8 +392,18 @@ function web_editor(config) {
371392
blockly.css('width', '33%');
372393
blockly.css('height', '100%');
373394
if(blockly.find('div.injectionDiv').length === 0) {
395+
// Calculate initial zoom level
396+
var zoomScaleSteps = 0.2;
397+
var fontSteps = (getFontSize() - EDITOR.initialFontSize) / EDITOR.fontSizeStep;
398+
var zoomLevel = (fontSteps * zoomScaleSteps) + 1.0;
374399
var workspace = Blockly.inject('blockly', {
375-
toolbox: document.getElementById('blockly-toolbox')
400+
toolbox: document.getElementById('blockly-toolbox'),
401+
zoom: {
402+
controls: false,
403+
wheel: false,
404+
startScale: zoomLevel,
405+
scaleSpeed: zoomScaleSteps + 1.0
406+
}
376407
});
377408
function myUpdateFunction(event) {
378409
var code = Blockly.Python.workspaceToCode(workspace);

0 commit comments

Comments
 (0)