Initial commit
This commit is contained in:
119
js/adminFormRestore.js
Normal file
119
js/adminFormRestore.js
Normal file
@@ -0,0 +1,119 @@
|
||||
(function($) {
|
||||
'use strict';
|
||||
|
||||
var STORAGE_PREFIX = 'thalim_form_restore_';
|
||||
var MAX_AGE_MS = 10 * 60 * 1000; // 10 min
|
||||
|
||||
function getPostId() {
|
||||
return $('#post_ID').val() || 'new';
|
||||
}
|
||||
|
||||
function getStorageKey() {
|
||||
return STORAGE_PREFIX + getPostId();
|
||||
}
|
||||
|
||||
function saveFormData() {
|
||||
var data = { timestamp: Date.now() };
|
||||
|
||||
var $title = $('#title');
|
||||
if ($title.length) data.title = $title.val();
|
||||
|
||||
var $content = $('#content');
|
||||
if ($content.length) {
|
||||
if (typeof tinyMCE !== 'undefined' && tinyMCE.get('content')) {
|
||||
data.content = tinyMCE.get('content').getContent();
|
||||
} else {
|
||||
data.content = $content.val();
|
||||
}
|
||||
}
|
||||
|
||||
$('[name^="pods_meta_"]').each(function() {
|
||||
data[this.name] = $(this).val();
|
||||
});
|
||||
|
||||
try {
|
||||
sessionStorage.setItem(getStorageKey(), JSON.stringify(data));
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function restoreFormData() {
|
||||
var navEntries = performance.getEntriesByType('navigation');
|
||||
if (!navEntries.length || navEntries[0].type !== 'back_forward') return;
|
||||
|
||||
var key = getStorageKey();
|
||||
var stored, data;
|
||||
try {
|
||||
stored = sessionStorage.getItem(key);
|
||||
if (!stored) return;
|
||||
data = JSON.parse(stored);
|
||||
} catch (e) { return; }
|
||||
|
||||
if (!data.timestamp || Date.now() - data.timestamp > MAX_AGE_MS) {
|
||||
try { sessionStorage.removeItem(key); } catch (e) {}
|
||||
return;
|
||||
}
|
||||
|
||||
// Restaurer titre
|
||||
if (data.title !== undefined) $('#title').val(data.title);
|
||||
|
||||
// Restaurer contenu (TinyMCE ou textarea brut)
|
||||
if (data.content !== undefined) {
|
||||
$('#content').val(data.content);
|
||||
if (typeof tinyMCE !== 'undefined') {
|
||||
var ed = tinyMCE.get('content');
|
||||
if (ed) {
|
||||
ed.setContent(data.content);
|
||||
} else {
|
||||
tinyMCE.on('AddEditor', function(e) {
|
||||
if (e.editor.id === 'content') {
|
||||
e.editor.on('init', function() {
|
||||
e.editor.setContent(data.content);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Restaurer champs Pods après rendu React
|
||||
var restorePods = function() {
|
||||
setTimeout(function() {
|
||||
var postId = getPostId();
|
||||
Object.keys(data).forEach(function(name) {
|
||||
if (!name.startsWith('pods_meta_')) return;
|
||||
var fieldName = name.replace(/^pods_meta_/, '');
|
||||
var value = data[name];
|
||||
var $el = $('[name="' + name + '"]');
|
||||
if ($el.length) $el.val(value).trigger('change');
|
||||
if (window.PodsDFV && postId) {
|
||||
try { window.PodsDFV.setFieldValue('post', postId, fieldName, value, 0); } catch (e) {}
|
||||
}
|
||||
});
|
||||
}, 300);
|
||||
};
|
||||
|
||||
if (document.readyState === 'complete') {
|
||||
restorePods();
|
||||
} else {
|
||||
$(window).on('load', restorePods);
|
||||
}
|
||||
|
||||
// Notice informative
|
||||
var $notice = $('<div class="notice notice-info is-dismissible"><p>Votre contenu a \u00e9t\u00e9 restaur\u00e9 suite \u00e0 une erreur de validation. V\u00e9rifiez les champs obligatoires avant de publier.</p></div>');
|
||||
$('#wpbody-content').find('.wrap').first().find('h1').after($notice);
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
if (window.pagenow !== 'post' && window.pagenow !== 'post-new') return;
|
||||
|
||||
// Nettoyage si sauvegarde réussie
|
||||
if ($('.notice-success, #message.updated').length) {
|
||||
try { sessionStorage.removeItem(getStorageKey()); } catch (e) {}
|
||||
return;
|
||||
}
|
||||
|
||||
$('#post').on('submit', saveFormData);
|
||||
restoreFormData();
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
Reference in New Issue
Block a user