Saving the post content using localstorage

If a post is created and the browser window crashes or the Composer window is hard-closed in some other way, all content is lost. 
Content is now saved temporarily so that it can be displayed again when the browser is restarted.
This commit is contained in:
loma-one 2024-09-15 17:55:50 +02:00 committed by GitHub
parent 38ff423716
commit 9a224a8ca9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -127,33 +127,54 @@
// Set initial height // Set initial height
textarea.style.height = "auto"; textarea.style.height = "auto";
textarea.style.height = (textarea.scrollHeight) + "px"; textarea.style.height = (textarea.scrollHeight) + "px";
// Restore saved content
const savedContent = localStorage.getItem(`comment-edit-text-${textarea.id}`);
if (savedContent) {
textarea.value = savedContent;
textarea.style.height = "auto";
textarea.style.height = (textarea.scrollHeight) + "px";
}
}); });
}); });
function togglePermissions() { // Auto-save content to localStorage every 5 seconds
var permissionsSection = document.getElementById('permissions-section'); setInterval(() => {
if (permissionsSection.style.display === 'none' || permissionsSection.style.display === '') { var textareas = document.querySelectorAll(".expandable-textarea");
permissionsSection.style.display = 'block'; textareas.forEach(function(textarea) {
} else { localStorage.setItem(`comment-edit-text-${textarea.id}`, textarea.value);
permissionsSection.style.display = 'none'; });
} }, 5000);
}
// Warn the user before leaving the page function togglePermissions() {
var formSubmitting = false; var permissionsSection = document.getElementById('permissions-section');
if (permissionsSection.style.display === 'none' || permissionsSection.style.display === '') {
permissionsSection.style.display = 'block';
} else {
permissionsSection.style.display = 'none';
}
}
function setFormSubmitting() { // Warn the user before leaving the page
formSubmitting = true; var formSubmitting = false;
}
window.addEventListener("beforeunload", function (event) { function setFormSubmitting() {
if (!formSubmitting) { formSubmitting = true;
var confirmationMessage = 'Are you sure you want to reload the page? All unsaved changes will be lost.'; // Remove saved content from localStorage when form is submitted
event.returnValue = confirmationMessage; var textareas = document.querySelectorAll(".expandable-textarea");
return confirmationMessage; textareas.forEach(function(textarea) {
} localStorage.removeItem(`comment-edit-text-${textarea.id}`);
}); });
}
// Set the formSubmitting flag when the form is submitted window.addEventListener("beforeunload", function (event) {
document.getElementById('comment-edit-form-{{$id}}').addEventListener('submit', setFormSubmitting); if (!formSubmitting) {
var confirmationMessage = 'Are you sure you want to reload the page? All unsaved changes will be lost.';
event.returnValue = confirmationMessage;
return confirmationMessage;
}
});
// Set the formSubmitting flag when the form is submitted
document.getElementById('comment-edit-form-{{$id}}').addEventListener('submit', setFormSubmitting);
</script> </script>