Skip to content

Commit e06bc18

Browse files
Minor refactor to decouple hex generation from button actions.
This way features that need to hook to this functionality only need to fo a function call with less chance for merge conflicts. .
1 parent d2b620b commit e06bc18

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

editor.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@
6969
</head>
7070
<body>
7171
<script type="application/javascript">
72-
<!-- VERSION INFORMATION -->
73-
EDITOR_VERSION = "1.1.2";
74-
UPY_VERSION = "1.0.1";
72+
// VERSION INFORMATION
73+
EDITOR_VERSION = "1.1.2";
74+
UPY_VERSION = "1.0.1";
7575
</script>
76-
76+
7777
<script id="load-template" type="x-tmpl-mustache">
7878
<h2><i class="fa fa-upload"></i> <strong>{{ title }}</strong></h2>
7979
<div class="load-drag-target" id="load-drag-target">
@@ -107,7 +107,6 @@ <h2><i class="fa fa-cogs"></i> <strong>{{ title }}</strong></h2>
107107
</tr>
108108
{{/snippets}}
109109
</table>
110-
111110
</script>
112111
<script id="share-template" type="x-tmpl-mustache">
113112
<h2><i class="fa fa-share-alt"></i> <strong>{{ title }}</strong></h2>

python-main.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ Returns an object that defines the behaviour of the Python editor. The editor
1010
is attached to the div with the referenced id.
1111
*/
1212
function pythonEditor(id) {
13+
'use strict';
14+
1315
// An object that encapsulates the behaviour of the editor.
14-
editor = {};
16+
var editor = {};
1517
editor.initialFontSize = 22;
1618
editor.fontSizeStep = 4;
1719

@@ -56,8 +58,8 @@ function pythonEditor(id) {
5658
// Triggers a snippet by name in the editor.
5759
editor.triggerSnippet = function(snippet) {
5860
var snippetManager = ace.require("ace/snippets").snippetManager;
59-
var snippet = snippetManager.snippetNameMap.python[snippet];
60-
if(snippet) {
61+
snippet = snippetManager.snippetNameMap.python[snippet];
62+
if (snippet) {
6163
snippetManager.insertSnippet(ACE, snippet.content);
6264
}
6365
};
@@ -116,6 +118,10 @@ the editor to the DOM (web-page).
116118
See the comments in-line for more information.
117119
*/
118120
function web_editor(config) {
121+
'use strict';
122+
123+
// Instance of the pythonEditor object (the ACE text editor wrapper)
124+
var EDITOR = pythonEditor('editor');
119125

120126
// Indicates if there are unsaved changes to the content of the editor.
121127
var dirty = false;
@@ -208,7 +214,7 @@ function web_editor(config) {
208214
return encodeURIComponent(f) + "=true";
209215
}).join("&");
210216
helpAnchor.attr("href", helpAnchor.attr("href") + "?" + featureQueryString);
211-
};
217+
}
212218

213219
// Update the docs link to append MicroPython version
214220
var docsAnchor = $("#docs-link");
@@ -221,7 +227,6 @@ function web_editor(config) {
221227
// Set version in document title
222228
document.title = document.title + ' ' + EDITOR_VERSION;
223229
// Setup the Ace editor.
224-
EDITOR = pythonEditor('editor');
225230
if(message.n && message.c && message.s) {
226231
var template = $('#decrypt-template').html();
227232
Mustache.parse(template);
@@ -309,13 +314,25 @@ function web_editor(config) {
309314
$("#command-download").focus();
310315
}
311316

317+
// Generates the text for a hex file with MicroPython and the user code
318+
function generateFullHexStr() {
319+
var firmware = $("#firmware").text();
320+
var fullHexStr = '';
321+
try {
322+
fullHexStr = EDITOR.getHexFile(firmware);
323+
} catch(e) {
324+
// We generate a user readable error here to be caught and displayed
325+
throw new Error(config.translate.alerts.length);
326+
}
327+
return fullHexStr;
328+
}
329+
312330
// This function describes what to do when the download button is clicked.
313331
function doDownload() {
314-
var firmware = $("#firmware").text();
315332
try {
316-
var output = EDITOR.getHexFile(firmware);
333+
var output = generateFullHexStr();
317334
} catch(e) {
318-
alert(config.translate.alerts.length);
335+
alert('Error:\n' + e.message);
319336
return;
320337
}
321338
var ua = navigator.userAgent.toLowerCase();

tests/spec/python-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("An editor for MicroPython on the BBC micro:bit:", function() {
4343
it("A tab is the same as 4 spaces.", function() {
4444
var editor = pythonEditor('editor');
4545
expect(editor.ACE.getOption('tabSize')).toBe(4);
46-
})
46+
});
4747

4848
it("A tab is 'soft'.", function() {
4949
var editor = pythonEditor('editor');

0 commit comments

Comments
 (0)