Skip to content

Commit

Permalink
Use addEventListener to prevent accidential unloading (as in leaving …
Browse files Browse the repository at this point in the history
…the page)

Known Issue: Still not working reliable in Safari.
  • Loading branch information
redimp committed Aug 2, 2024
1 parent 645021c commit 6ef7b5c
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions otterwiki/templates/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,22 @@ <h5 class="sidebar-title"><a class="sidebar-title-link" href="{{ url_for('attach
bottom_panel,
]; // document.querySelector("#editor_block");
const preview_block = document.querySelector("#preview_block");
/* prevent user leaving with unsaved changes */
window.onbeforeunload = function(e) {
e = e || window.event;
/*
prevent user leaving with unsaved changes
Known Issue: this doesn't work reliable in Safari
*/
window.addEventListener('beforeunload', function (e) {
// Safari logs the event, but the preventDefault() only
// works the first time it is called.
// console.log(e);
if (!cm_editor.doc.isClean()) {
// will not be shown in modern browsers
return "You have unsaved changes.";
e.preventDefault();
// Chrome requires returnValue to be set.
e.returnValue = '';
return e;
}
};
return null;
});
/* save */
document.getElementById('saveform').onsubmit = function() {
const content_editor = cm_editor.getValue();
Expand Down

0 comments on commit 6ef7b5c

Please sign in to comment.