Skip to content

Commit 7576d1e

Browse files
committed
Prevent duplication of editor content
By removing the content once added to the ACE editor, we prevent an issue with Turbolinks / Turbo duplicating the lines. This issue became apparent during the migration to Turbo, since its behavior changed in comparison to Turbolinks. See hotwired/turbo#1413
1 parent dd30889 commit 7576d1e

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

app/assets/javascripts/editor.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ $(document).on('turbolinks:load', function(event) {
1414
);
1515

1616
if ($('#editor').isPresent() && CodeOceanEditor && event.originalEvent.data.url.includes("/implement")) {
17-
// This call will (amon other things) initializeEditors and load the content except for the last line
18-
// It must not be called during page navigation. Otherwise, content will be duplicated!
19-
// Search for insertFullLines and Turbolinks reload / cache control
2017
CodeOceanEditor.initializeEverything();
2118
}
2219

app/assets/javascripts/editor/editor.js.erb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,15 @@ var CodeOceanEditor = {
286286
var content = $('.editor-content[data-file-id=' + file_id + ']');
287287
this.setActiveFile($(element).parent().data('filename'), file_id);
288288

289-
document.insertFullLines(0, content.text().split(/\n/));
290-
// remove last (empty) that is there by default line
291-
document.removeFullLines(document.getLength() - 1, document.getLength() - 1);
289+
const full_lines = content.text().split(/\n/);
290+
if (full_lines.length > 1 && full_lines[0] !== "") {
291+
document.insertFullLines(0, full_lines);
292+
// remove last (empty) that is there by default line
293+
document.removeFullLines(document.getLength() - 1, document.getLength() - 1);
294+
// remove content from DOM, so that it won't be readded during Turbo navigation
295+
content.text("");
296+
}
297+
292298
editor.setReadOnly($(element).parent().data('read-only') !== undefined);
293299
if (editor.getReadOnly()) {
294300
editor.setHighlightActiveLine(false);

0 commit comments

Comments
 (0)