@@ -10,8 +10,10 @@ Returns an object that defines the behaviour of the Python editor. The editor
1010is attached to the div with the referenced id.
1111*/
1212function 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).
116118See the comments in-line for more information.
117119*/
118120function 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 ( ) ;
0 commit comments