Merge pull request #1051 from anaqreon/auto-save

Auto-save comment and post text
This commit is contained in:
git-marijus
2018-04-09 13:18:09 +02:00
committed by GitHub
6 changed files with 118 additions and 2 deletions

View File

@@ -166,6 +166,16 @@ function handle_comment_form(e) {
$('#' + commentElm).addClass('expanded').removeAttr('placeholder');
$('#' + commentElm).attr('tabindex','9');
$('#' + submitElm).attr('tabindex','10');
if(auto_save_draft) {
var commentBody = localStorage.getItem("comment_body");
if(commentBody && $('#' + commentElm).val() === '') {
$('#' + commentElm).val(commentBody);
}
} else {
localStorage.removeItem("comment_body");
}
form.find(':not(:visible)').show();
}
@@ -185,6 +195,30 @@ function handle_comment_form(e) {
form.find(':not(.comment-edit-text)').hide();
}
});
var commentSaveTimer = null;
var emptyCommentElm = form.find('.comment-edit-text').attr('id');
$(document).on('focusout','#' + emptyCommentElm,function(e){
if(commentSaveTimer)
clearTimeout(commentSaveTimer);
commentSaveChanges(true);
commentSaveTimer = null;
});
$(document).on('focusin','#' + emptyCommentElm,function(e){
commentSaveTimer = setTimeout(function () {
commentSaveChanges(false);
},10000);
});
function commentSaveChanges(isFinal = false) {
if(auto_save_draft) {
localStorage.setItem("comment_body", $('#' + emptyCommentElm).val());
if( !isFinal) {
commentSaveTimer = setTimeout(commentSaveChanges,10000);
}
}
}
}
function commentClose(obj, id) {
@@ -1106,6 +1140,7 @@ function post_comment(id) {
$("#comment-edit-form-" + id).serialize(),
function(data) {
if(data.success) {
localStorage.removeItem("comment_body");
$("#comment-edit-preview-" + id).hide();
$("#comment-edit-wrapper-" + id).hide();
$("#comment-edit-text-" + id).val('');