@@ -467,8 +467,7 @@ function web_editor(config) {
467467 // Sets up the file system and adds the initial main.py
468468 function setupFilesystem ( ) {
469469 micropythonFs = new microbitFs . MicropythonFsHex ( $ ( '#firmware' ) . text ( ) ) ;
470- // Get initial main.py
471- micropythonFs . write ( 'main.py' , EDITOR . getCode ( ) ) ; // Add main.py
470+ micropythonFs . write ( 'main.py' , EDITOR . getCode ( ) ) ; // Add main.py
472471 }
473472
474473 // Based on the Python code magic comment it detects a module
@@ -645,7 +644,7 @@ function web_editor(config) {
645644 }
646645
647646 // Regenerate the table showing the file list and call for the storage bar to be updated
648- var updateFileTables = function ( ) {
647+ function updateFileTables ( ) {
649648 // Delete the current table body content and add rows file by file
650649 $ ( '.fs-file-list table tbody' ) . empty ( ) ;
651650 micropythonFs . ls ( ) . forEach ( function ( filename ) {
@@ -681,7 +680,7 @@ function web_editor(config) {
681680 } ) ;
682681 } ) ;
683682 updateStorageBar ( ) ;
684- } ;
683+ }
685684
686685 // Generates the text for a hex file with MicroPython and the user code
687686 function generateFullHexStr ( ) {
@@ -726,7 +725,7 @@ function web_editor(config) {
726725
727726 // Describes what to do when the save/load button is clicked.
728727 function doFiles ( ) {
729- var template = $ ( '#load -template' ) . html ( ) ;
728+ var template = $ ( '#files -template' ) . html ( ) ;
730729 Mustache . parse ( template ) ;
731730 vex . open ( {
732731 content : Mustache . render ( template , config . translate . load ) ,
@@ -1104,7 +1103,7 @@ function web_editor(config) {
11041103 } ) ;
11051104 }
11061105
1107- function doSerial ( ) {
1106+ function doSerial ( ) {
11081107 // Hide terminal
11091108 if ( $ ( "#repl" ) . css ( 'display' ) != 'none' ) {
11101109 $ ( "#repl" ) . hide ( ) ;
@@ -1151,188 +1150,40 @@ function web_editor(config) {
11511150 }
11521151 }
11531152
1154- function setupHterm ( ) {
1155- if ( REPL == null ) {
1156- hterm . defaultStorage = new lib . Storage . Memory ( ) ;
1157-
1158- REPL = new hterm . Terminal ( "opt_profileName" ) ;
1159- REPL . options_ . cursorVisible = true ;
1160- REPL . prefs_ . set ( 'font-size' , 22 ) ;
1161-
1162- var daplinkReceived = false ;
1163-
1164- REPL . onTerminalReady = function ( ) {
1165- var io = REPL . io . push ( ) ;
1166-
1167- io . onVTKeystroke = function ( str ) {
1168- window . daplink . serialWrite ( str ) ;
1169- } ;
1170-
1171- io . sendString = function ( str ) {
1172- window . daplink . serialWrite ( str ) ;
1173- } ;
1174-
1175- io . onTerminalResize = function ( columns , rows ) {
1176- } ;
1177- } ;
1178-
1179- REPL . decorate ( document . querySelector ( '#repl' ) ) ;
1180- REPL . installKeyboard ( ) ;
1181-
1182- window . daplink . on ( DAPjs . DAPLink . EVENT_SERIAL_DATA , function ( data ) {
1183- REPL . io . print ( data ) ; // first byte of data is length
1184- daplinkReceived = true ;
1185- } ) ;
1186- }
1187-
1188- $ ( "#editor-container" ) . hide ( ) ;
1189- $ ( "#repl" ) . show ( ) ;
1190- $ ( "#request-repl" ) . show ( ) ;
1191-
1192- // Recalculate terminal height
1193- $ ( "#repl > iframe" ) . css ( "position" , "relative" ) ;
1194- $ ( "#repl" ) . attr ( "class" , "hbox flex1" ) ;
1195- REPL . prefs_ . set ( 'font-size' , getFontSize ( ) ) ;
1196-
1197- /* Don't do this automatically
1198- // Send ctrl-C to get the terminal up
1199- var attempt = 0;
1200- var getPrompt = setInterval(
1201- function(){
1202- daplink.serialWrite("\x03");
1203- console.log("Requesting REPL...");
1204- attempt++;
1205- if(attempt == 5 || daplinkReceived) clearInterval(getPrompt);
1206- }, 200);
1207- */
1208- }
1209-
1210- // Describes what to do when the filesystem button is clicked.
1211- function doFilesystem ( ) {
1212- // Update main.py in filesystem
1213- microbitFs . remove ( "main.py" ) ; // Remove existing
1214- microbitFs . write ( "main.py" , EDITOR . getCode ( ) ) ; // Add main.py
1215-
1216- // Create UI
1217- var template = $ ( '#filesystem-template' ) . html ( ) ;
1218- Mustache . parse ( template ) ;
1219- vex . open ( {
1220- content : Mustache . render ( template , config . translate . filesystem ) ,
1221- afterOpen : function ( vexContent ) {
1222- $ ( vexContent ) . find ( '#filesystem-drag-target' ) . on ( 'drag dragstart dragend dragover dragenter dragleave drop' , function ( e ) {
1223- e . preventDefault ( ) ;
1224- e . stopPropagation ( ) ;
1225- } )
1226- . on ( 'dragover dragenter' , function ( ) {
1227- $ ( '#filesystem-drag-target' ) . addClass ( 'is-dragover' ) ;
1228- } )
1229- . on ( 'dragleave dragend drop' , function ( ) {
1230- $ ( '#filesystem-drag-target' ) . removeClass ( 'is-dragover' ) ;
1231- } )
1232- . on ( 'drop' , function ( e ) {
1233- var files = e . originalEvent . dataTransfer . files ;
1234- doFilesystemAdd ( files ) ;
1235- } ) ;
1236- $ ( vexContent ) . find ( '#filesystem-form-form' ) . on ( 'submit' , function ( e ) {
1237- e . preventDefault ( ) ;
1238- e . stopPropagation ( ) ;
1239-
1240- var files = e . target [ 0 ] . files ;
1241- doFilesystemAdd ( files ) ;
1242-
1243- // Clear form input
1244- $ ( '#filesystem-form-form input[type=file]' ) . replaceWith ( $ ( '#filesystem-form-form input[type=file]' ) . val ( '' ) . clone ( true ) ) ;
1245-
1246- } ) ;
1247- }
1248- } )
1249- $ ( '.filesystem-toggle' ) . on ( 'click' , function ( e ) {
1250- $ ( '.filesystem-form' ) . toggle ( ) ;
1251- } ) ;
1252-
1253- Object . keys ( microbitFs . _files ) . forEach ( function ( key ) {
1254-
1255- var file = microbitFs . _files [ key ] ;
1256-
1257- var fileType = ( / [ . ] / . exec ( file . filename ) ) ? / [ ^ . ] + $ / . exec ( file . filename ) : "" ;
1153+ function setupHterm ( ) {
1154+ if ( REPL == null ) {
1155+ hterm . defaultStorage = new lib . Storage . Memory ( ) ;
1156+
1157+ REPL = new hterm . Terminal ( "opt_profileName" ) ;
1158+ REPL . options_ . cursorVisible = true ;
1159+ REPL . prefs_ . set ( 'font-size' , 22 ) ;
1160+ REPL . onTerminalReady = function ( ) {
1161+ var io = REPL . io . push ( ) ;
1162+ io . onVTKeystroke = function ( str ) {
1163+ window . daplink . serialWrite ( str ) ;
1164+ } ;
1165+ io . sendString = function ( str ) {
1166+ window . daplink . serialWrite ( str ) ;
1167+ } ;
1168+ io . onTerminalResize = function ( columns , rows ) {
1169+ } ;
1170+ } ;
1171+ REPL . decorate ( document . querySelector ( '#repl' ) ) ;
1172+ REPL . installKeyboard ( ) ;
12581173
1259- $ ( '.filesystem-drag-target table tbody' ) . append (
1260- '<tr><td>' + file . filename + '</td><td>' + fileType + '</td><td>' + ( file . _dataBytes . length / 1024 ) . toFixed ( 2 ) + ' kb</td><td>' + ( ( file . filename == 'main.py' ) ? '' : '<button id="' + file . filename + '" class="filesystem-remove-button">Remove</button>' ) + '</td></tr>'
1261- ) . on ( 'click' , function ( e ) {
1262- if ( $ ( e . target ) . hasClass ( "filesystem-remove-button" ) )
1263- {
1264- doFilesystemRemove ( e . target . id ) ;
1265- $ ( e . target ) . closest ( "tr" ) . remove ( ) ;
1266- }
1174+ window . daplink . on ( DAPjs . DAPLink . EVENT_SERIAL_DATA , function ( data ) {
1175+ REPL . io . print ( data ) ; // first byte of data is length
12671176 } ) ;
1268- } ) ;
1269-
1270- }
1271-
1272- // Function to remove a file
1273- function doFilesystemRemove ( name ) {
1274- return microbitFs . remove ( name ) ;
1275- }
1276-
1277- // Function for adding file to filesystem
1278- function doFilesystemAdd ( files ) {
1279-
1280- Array . from ( files ) . forEach ( function ( file ) {
1281- // Check if file already exists
1282- if ( microbitFs . exists ( file . name ) && file . name != "main.py" )
1283- {
1284- alert ( file . name + " already exists in the file system!" ) ;
1285- return ;
1286- }
1287-
1288- // Attempt to add file to FS
1289- var fileReader = new FileReader ( ) ;
1290- fileReader . onloadend = function ( e ) {
1291-
1292- var arrayBuffer = new Uint8Array ( e . target . result ) ;
1293-
1294- // Check if file is main.py
1295- if ( file . name == "main.py" )
1296- {
1297- if ( ! confirm ( "This will replace the code in the editor!" ) )
1298- return ;
1299-
1300- var utf8 = new TextDecoder ( "utf-8" ) . decode ( arrayBuffer ) ;
1301- console . log ( utf8 ) ;
1302- EDITOR . setCode ( utf8 ) ;
1303- }
1304-
1305- var fileType = $ ( '#file-type' ) . val ( ) ;
1306- microbitFs . write ( file . name , arrayBuffer ) ;
1307-
1308- // Check if the filesystem has run out of space
1309- try
1310- {
1311- microbitFs . getIntelHex ( ) ;
1312-
1313- // Update UI
1314- var fileType = ( / [ . ] / . exec ( file . name ) ) ? / [ ^ . ] + $ / . exec ( file . name ) : "" ;
1315-
1316- $ ( '.filesystem-drag-target table tbody' ) . append (
1317- '<tr><td>' + file . name + '</td><td>' + file . type + '</td><td>' + ( file . size / 1024 ) . toFixed ( 2 ) + ' kb</td><td>' + ( ( file . name == 'main.py' ) ? '' : '<button id="' + file . name + '" class="filesystem-remove-button">Remove</button>' ) + '</td></tr>'
1318- ) . on ( 'click' , function ( e ) {
1319- if ( $ ( e . target ) . hasClass ( "filesystem-remove-button" ) )
1320- {
1321- doFilesystemRemove ( e . target . id ) ;
1322- $ ( e . target ) . closest ( "tr" ) . remove ( ) ;
1323- }
1324- } ) ;
1325- } catch ( e ) {
1326- microbitFs . remove ( file . name ) ;
1327- alert ( "The file system does not have enough free space to add " + file . name ) ;
1328- return ; // Skip UI
1329- }
1330-
1331- } ;
1332- fileReader . readAsArrayBuffer ( file ) ;
1177+ }
13331178
1179+ $ ( "#editor-container" ) . hide ( ) ;
1180+ $ ( "#repl" ) . show ( ) ;
1181+ $ ( "#request-repl" ) . show ( ) ;
13341182
1335- } ) ;
1183+ // Recalculate terminal height
1184+ $ ( "#repl > iframe" ) . css ( "position" , "relative" ) ;
1185+ $ ( "#repl" ) . attr ( "class" , "hbox flex1" ) ;
1186+ REPL . prefs_ . set ( 'font-size' , getFontSize ( ) ) ;
13361187 }
13371188
13381189 function formatMenuContainer ( parentButtonId , containerId ) {
@@ -1360,9 +1211,9 @@ function web_editor(config) {
13601211 function setupButtons ( ) {
13611212 $ ( "#command-download" ) . click ( function ( ) {
13621213 if ( $ ( "#command-download" ) . length ) {
1363- doDownload ( ) ;
1214+ doDownload ( ) ;
13641215 } else {
1365- doFlash ( ) ;
1216+ doFlash ( ) ;
13661217 }
13671218 } ) ;
13681219 $ ( "#command-files" ) . click ( function ( ) {
@@ -1379,13 +1230,10 @@ function web_editor(config) {
13791230 } ) ;
13801231 $ ( "#command-connect" ) . click ( function ( ) {
13811232 if ( $ ( "#command-connect" ) . length ) {
1382- doConnect ( ) ;
1233+ doConnect ( ) ;
13831234 } else {
13841235 doDisconnect ( ) ;
1385- }
1386- } ) ;
1387- $ ( "#command-filesystem" ) . click ( function ( ) {
1388- doFilesystem ( ) ;
1236+ }
13891237 } ) ;
13901238 $ ( "#command-serial" ) . click ( function ( ) {
13911239 doSerial ( ) ;
@@ -1486,7 +1334,7 @@ function web_editor(config) {
14861334/*
14871335 * Function to close flash error box
14881336 */
1489- function flashErrorClose ( ) {
1337+ function flashErrorClose ( ) {
14901338 $ ( '#flashing-overlay-error' ) . html ( "" ) ;
14911339 $ ( '#flashing-overlay-container' ) . hide ( ) ;
14921340}
0 commit comments