@@ -1101,12 +1101,33 @@ var CodeOceanEditor = {
11011101 this . initializeDeadlines ( ) ;
11021102 CodeOceanEditorTips . initializeEventHandlers ( ) ;
11031103
1104- window . addEventListener ( "turbo:visit" , App . synchronized_editor ?. disconnect . bind ( App . synchronized_editor ) ) ;
1105- window . addEventListener ( "beforeunload" , App . synchronized_editor ?. disconnect . bind ( App . synchronized_editor ) ) ;
1104+ $ ( document ) . one ( "turbo:visit" , this . unloadEverything . bind ( this , App . synchronized_editor ) ) ;
1105+ $ ( window ) . one ( "beforeunload" , this . unloadEverything . bind ( this , App . synchronized_editor ) ) ;
11061106
1107- window . addEventListener ( "turbo:visit" , this . autosaveIfChanged . bind ( this ) ) ;
1108- window . addEventListener ( "beforeunload" , this . autosaveIfChanged . bind ( this ) ) ;
11091107 // create autosave when the editor is opened the first time
11101108 this . autosave ( ) ;
1109+ } ,
1110+
1111+ unloadEverything : function ( ) {
1112+ App . synchronized_editor ?. disconnect ( ) ;
1113+ this . autosaveIfChanged ( ) ;
1114+ this . cacheEditorContent ( ) ;
1115+ this . teardownEventHandlers ( ) ;
1116+ this . destroyEditors ( ) ;
1117+ } ,
1118+
1119+ cacheEditorContent : function ( ) {
1120+ // Persist the content of the editors in a hidden textarea to enable Turbo caching.
1121+ // In this case, we iterate over _all_ editors, not just writable ones.
1122+ for ( const [ file_id , editor ] of this . editor_for_file ) {
1123+ const file_content = editor . getValue ( ) ;
1124+ const editorContent = $ ( `.editor-content[data-file-id='${ file_id } ']` ) ;
1125+ editorContent . text ( file_content ) ;
1126+ }
1127+ } ,
1128+
1129+ destroyEditors : function ( ) {
1130+ CodeOceanEditor . editors . forEach ( editor => editor . destroy ( ) ) ;
1131+ CodeOceanEditor . editors = [ ] ;
11111132 }
11121133} ;
0 commit comments