Skip to content

Commit 5d13a4d

Browse files
committed
Enable proper Turbo caching for /implement
1 parent 12ab4e6 commit 5d13a4d

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

app/assets/javascripts/editor/editor.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

app/views/exercises/implement.html.slim

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
- content_for :head do
2-
// Force a full page reload, see https://turbo.hotwired.dev/handbook/drive#ensuring-specific-pages-trigger-a-full-reload.
3-
Otherwise, lti_parameters might be nil
4-
meta name='turbo-cache-control' content='no-cache'
52
- append_stylesheet_pack_tag("multilang_#{I18n.locale}")
63

74
#editor-column

0 commit comments

Comments
 (0)