@@ -70,6 +70,7 @@ export class ActiveCode extends RunestoneBase {
7070 this . runButton = null ;
7171 this . enabledownload = $ ( orig ) . data ( "enabledownload" ) ;
7272 this . downloadButton = null ;
73+ this . resetButton = null ;
7374 this . saveButton = null ;
7475 this . loadButton = null ;
7576 this . outerDiv = null ;
@@ -167,7 +168,11 @@ export class ActiveCode extends RunestoneBase {
167168 } else if ( edmode === "octave" || edmode === "MATLAB" ) {
168169 edmode = "text/x-octave" ;
169170 }
170- var editor = CodeMirror ( codeDiv , {
171+
172+ if ( localStorage . getItem ( this . divid ) !== null )
173+ this . code = localStorage . getItem ( this . divid ) ;
174+
175+ var opts = {
171176 value : this . code ,
172177 lineNumbers : true ,
173178 mode : edmode ,
@@ -178,7 +183,9 @@ export class ActiveCode extends RunestoneBase {
178183 Tab : "indentMore" ,
179184 "Shift-Tab" : "indentLess" ,
180185 } ,
181- } ) ;
186+ }
187+ var editor = CodeMirror ( codeDiv , opts ) ;
188+
182189 // Make the editor resizable
183190 $ ( editor . getWrapperElement ( ) ) . resizable ( {
184191 resize : function ( ) {
@@ -242,7 +249,7 @@ export class ActiveCode extends RunestoneBase {
242249 }
243250
244251 async runButtonHandler ( ) {
245- // Disable the run button until the run is finished.
252+ // Disable the run button until the run is finished.
246253 this . runButton . disabled = true ;
247254 try {
248255 await this . runProg ( ) ;
@@ -272,6 +279,8 @@ export class ActiveCode extends RunestoneBase {
272279 this . runButton . onclick = this . runButtonHandler . bind ( this ) ;
273280 $ ( butt ) . attr ( "type" , "button" ) ;
274281
282+ this . addResetButton ( ctrlDiv ) ;
283+
275284 if ( this . enabledownload || eBookConfig . downloadsEnabled ) {
276285 this . addDownloadButton ( ctrlDiv ) ;
277286 }
@@ -348,6 +357,16 @@ export class ActiveCode extends RunestoneBase {
348357 $ ( butt ) . attr ( "type" , "button" ) ;
349358 }
350359
360+ addResetButton ( ctrlDiv ) {
361+ let butt = document . createElement ( "button" ) ;
362+ $ ( butt ) . text ( "Reset" ) ;
363+ $ ( butt ) . addClass ( "btn btn-default" ) ;
364+ $ ( butt ) . attr ( "type" , "button" ) ;
365+ ctrlDiv . appendChild ( butt ) ;
366+ this . resetButton = butt ;
367+ $ ( butt ) . click ( this . resetCode . bind ( this ) ) ;
368+ }
369+
351370 enableHideShow ( ctrlDiv ) {
352371 $ ( this . runButton ) . attr ( "disabled" , "disabled" ) ;
353372 let butt = document . createElement ( "button" ) ;
@@ -757,6 +776,11 @@ export class ActiveCode extends RunestoneBase {
757776 }
758777 }
759778
779+ resetCode ( ) {
780+ localStorage . removeItem ( this . divid ) ;
781+ window . location . reload ( ) ;
782+ }
783+
760784 async createGradeSummary ( ) {
761785 // get grade and comments for this assignment
762786 // get summary of all grades for this student
@@ -1190,7 +1214,7 @@ Yet another is that there is an internal error. The internal error message is:
11901214 }
11911215
11921216 logCurrentAnswer ( ) {
1193- this . logRunEvent ( {
1217+ let data = {
11941218 div_id : this . divid ,
11951219 code : this . editor . getValue ( ) ,
11961220 lang : this . language ,
@@ -1199,7 +1223,11 @@ Yet another is that there is an internal error. The internal error message is:
11991223 prefix : this . pretext ,
12001224 suffix : this . suffix ,
12011225 partner : this . partner ,
1202- } ) ; // Log the run event
1226+ } ;
1227+
1228+ localStorage . setItem ( this . divid , data [ 'code' ] ) ;
1229+
1230+ this . logRunEvent ( data ) ; // Log the run event
12031231 // If unit tests were run there will be a unit_results
12041232 if ( this . unit_results ) {
12051233 this . logBookEvent ( {
@@ -1285,6 +1313,9 @@ Yet another is that there is an internal error. The internal error message is:
12851313 } ) ;
12861314 Sk . divid = this . divid ;
12871315 Sk . logResults = logResults ;
1316+
1317+ localStorage . setItem ( this . divid , this . code ) ;
1318+
12881319 if ( this . graderactive && this . outerDiv . closest ( ".loading" ) ) {
12891320 Sk . gradeContainer = this . outerDiv . closest ( ".loading" ) . id ;
12901321 } else {
0 commit comments