Refactor autosave draft script and add error handling

This commit is contained in:
andrigamerita 2025-06-07 15:35:01 +02:00 committed by GitHub
commit a0b8059647
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 35 deletions

View file

@ -181,4 +181,8 @@ pre code {
display: flex;
align-items: center;
display: none;
}
.notice.error {
background-color: #fdd;
}

View file

@ -145,6 +145,7 @@ $( function() {
<?php if (isset($error)) { ?>
<div class="error-message"><?php echo $error ?></div>
<?php } ?>
<div class="notice error" id="response-error"></div>
<div class="notice" id="response"></div>
<div class="row">
<div class="hide-button" style="margin-bottom:1em;width:100%;text-align:right;"><button type="button" title="<?php echo i18n('Focus_mode');?>" id="hideButton" class="note-btn btn btn-sm <?php echo ((config('admin.theme') === 'light' || is_null(config('admin.theme'))) ? "btn-light" : "btn-dark");?>" style="width:38px;height:38px;font-size:18px;" ><i class="fa fa-eye" aria-hidden="true"></i></button></div>

View file

@ -1,43 +1,29 @@
const response = document.getElementById("response");
function hideError() {
$("#response-error").fadeOut(600, function() {
$("#response-error").css("display", "none");
});
}
function updateData() {
var title = $("#pTitle").val();
var url = $("#pURL").val();
var content = $("#wmd-input").val();
var description = $("#pMeta").val();
var tag = $("#pTag").val();
var category = $("#pCategory").val();
var posttype = $("#pType").val();
var pimage = $("#pImage").val();
var paudio = $("#pAudio").val();
var pvideo = $("#pVideo").val();
var pquote = $("#pQuote").val();
var plink = $("#pLink").val();
var pDate = $("#pDate").val();
var pTime = $("#pTime").val();
var oldfile = $("#oldfile").val();
var dateTime = pDate + " " + pTime;
var autoSave = 'autoSave';
// Prepare data to send to PHP
var data = {
title: title,
url: url,
content: content,
description: description,
tag: tag,
category: category,
posttype: posttype,
pimage: pimage,
paudio: paudio,
pvideo: pvideo,
pquote: pquote,
plink: plink,
dateTime: dateTime,
autoSave: autoSave,
title: $("#pTitle").val(),
url: $("#pURL").val(),
content: $("#wmd-input").val(),
description: $("#pMeta").val(),
tag: $("#pTag").val(),
category: $("#pCategory").val(),
posttype: $("#pType").val(),
pimage: $("#pImage").val(),
paudio: $("#pAudio").val(),
pvideo: $("#pVideo").val(),
pquote: $("#pQuote").val(),
plink: $("#pLink").val(),
dateTime: $("#pDate").val() + " " + $("#pTime").val(),
autoSave: 'autoSave',
addEdit: addEdit,
oldfile: oldfile,
parent_page: parent_page
oldfile: $("#oldfile").val(),
parent_page: parent_page
};
$.ajax({
@ -47,6 +33,7 @@ function updateData() {
success: function(response) {
$("#response").html(response.message);
$("#oldfile").val(response.file);
hideError();
$("#response").fadeIn(600, function() {
$("#response").css("display", "block");
});
@ -55,6 +42,13 @@ function updateData() {
$("#response").css("display", "none");
});
}, 6000);
},
error: function(response) {
$("#response-error").html("Error in Autosaving: " + response.statusText);
$("#response-error").fadeIn(600, function() {
$("#response-error").css("display", "block");
});
setTimeout(hideError, saveInterval/2);
}
});
}