From ffd13f1ee9b31e1fa50ac82e98d3f8edf988fd65 Mon Sep 17 00:00:00 2001 From: halfwit Date: Sat, 28 Sep 2024 14:12:37 -0700 Subject: [PATCH 1/3] Alert the user if they are leaving unsaved changes in the editor --- wwwroot/src/js/wiki.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/wwwroot/src/js/wiki.js b/wwwroot/src/js/wiki.js index fff20de..647e350 100644 --- a/wwwroot/src/js/wiki.js +++ b/wwwroot/src/js/wiki.js @@ -131,23 +131,58 @@ function buildQuery(input) return output.join('&'); } +function hasChanges(changed, formData) { + var Form = document.getElementById('TheInternet'); + return changed || (new FormData(Form) !== formData); +} + $(document).ready(function() { // Populate saved name from local storage var name = localStorage.getItem('name'); + // Check if the title is "Preview:", which means changes are likely pending + var title = document.querySelector('div.title'); + var changed = title && title.textContent.trim().startsWith('Preview:'); + // Stash the form so we can use it to compare for changes later + var Form = document.getElementById('TheInternet'); + var formData = new FormData(Form); if(name) { $('#Name').value(name); } + $('a').on('click', function(e) { + if (hasChanges(changed, formData)) { + if (!confirm('You have unsaved changes. Are you sure you want to leave?')) { + e.preventDefault(); + } else { + changed = false; + } + } + }); + + $('#TheInternet').on('change input', function () { + changed = true; + }); + $('#TheInternet').on('submit', function() { + // Update our stashed form data, and remove the `changed` guard var Form = document.getElementById('TheInternet'); - Form.action = Form.action + 'edit'; + formData = new FormData(Form); + changed = false; + Form.action = Form.action + 'edit'; // Save current name in local storage var name = $('#Name').value(); localStorage.setItem('name', name); + return true; + }); + + // Preview should justwerk + $('input[value="Preview"]').on('click', function(e) { + changed = false; + return true; }); $('.navbox').on('click', function(event) From 3071e1e5c4016c968084f3e8823216ed5a8144a1 Mon Sep 17 00:00:00 2001 From: halfwit Date: Sat, 28 Sep 2024 17:30:29 -0700 Subject: [PATCH 2/3] Make sure we turn off the beforeunload, as sometimes it fires with the scoped variables wrong --- wwwroot/src/js/wiki.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/wwwroot/src/js/wiki.js b/wwwroot/src/js/wiki.js index 647e350..49c6971 100644 --- a/wwwroot/src/js/wiki.js +++ b/wwwroot/src/js/wiki.js @@ -157,11 +157,22 @@ $(document).ready(function() if (!confirm('You have unsaved changes. Are you sure you want to leave?')) { e.preventDefault(); } else { + // Override function to make sure it doesn't fire anyways + $(wnidow).on('beforeunload', function (e) {}); changed = false; } } }); + $(window).on('beforeunload', function(e) { + if (hasChanges(changed, formData)) { + var confirmationMessage = 'You have unsaved changes. Are you sure you want to leave?'; + (e || window.event).returnValue = confirmationMessage; + return confirmationMessage; + } + }); + + $('#TheInternet').on('change input', function () { changed = true; }); From accdace4e00ed31303717ab2711433194fb07c2e Mon Sep 17 00:00:00 2001 From: halfwit Date: Sat, 28 Sep 2024 17:33:31 -0700 Subject: [PATCH 3/3] Small typo --- wwwroot/src/js/wiki.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wwwroot/src/js/wiki.js b/wwwroot/src/js/wiki.js index 49c6971..3489d64 100644 --- a/wwwroot/src/js/wiki.js +++ b/wwwroot/src/js/wiki.js @@ -158,7 +158,7 @@ $(document).ready(function() e.preventDefault(); } else { // Override function to make sure it doesn't fire anyways - $(wnidow).on('beforeunload', function (e) {}); + $(window).on('beforeunload', function (e) {}); changed = false; } }