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,9 +127,25 @@
// 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";
}
}); });
}); });
// Auto-save content to localStorage every 5 seconds
setInterval(() => {
var textareas = document.querySelectorAll(".expandable-textarea");
textareas.forEach(function(textarea) {
localStorage.setItem(`comment-edit-text-${textarea.id}`, textarea.value);
});
}, 5000);
function togglePermissions() { function togglePermissions() {
var permissionsSection = document.getElementById('permissions-section'); var permissionsSection = document.getElementById('permissions-section');
if (permissionsSection.style.display === 'none' || permissionsSection.style.display === '') { if (permissionsSection.style.display === 'none' || permissionsSection.style.display === '') {
@ -144,6 +160,11 @@
function setFormSubmitting() { function setFormSubmitting() {
formSubmitting = true; formSubmitting = true;
// Remove saved content from localStorage when form is submitted
var textareas = document.querySelectorAll(".expandable-textarea");
textareas.forEach(function(textarea) {
localStorage.removeItem(`comment-edit-text-${textarea.id}`);
});
} }
window.addEventListener("beforeunload", function (event) { window.addEventListener("beforeunload", function (event) {