refactor : IA éditeur — un seul bouton analyse+réécriture combinées
Un seul appel API retourne l'analyse critique ET la proposition d'article via le séparateur ===CRITIQUE===/===REWRITE===. Le panneau affiche les deux sections avec un bouton « Appliquer la proposition ». Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,36 +1,24 @@
|
||||
// ai-editor.js — boutons IA dans la sidebar éditeur
|
||||
// ai-editor.js — bouton IA dans la sidebar éditeur
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
var btnCritique = document.getElementById('btn-ai-critique');
|
||||
var btnRewrite = document.getElementById('btn-ai-rewrite');
|
||||
if (!btnCritique || !btnRewrite) return;
|
||||
var btnAnalyze = document.getElementById('btn-ai-analyze');
|
||||
if (!btnAnalyze) return;
|
||||
|
||||
var panel = document.getElementById('ai-result-panel');
|
||||
var labelEl = document.getElementById('ai-result-label');
|
||||
var resultEl = document.getElementById('ai-result-content');
|
||||
var btnApply = document.getElementById('btn-ai-apply');
|
||||
var btnClose = document.getElementById('btn-ai-close');
|
||||
var ta = document.getElementById('wz-content') || document.getElementById('content');
|
||||
var titleEl = document.getElementById('title');
|
||||
var panel = document.getElementById('ai-result-panel');
|
||||
var critiqueEl = document.getElementById('ai-critique-content');
|
||||
var rewriteEl = document.getElementById('ai-rewrite-content');
|
||||
var btnApply = document.getElementById('btn-ai-apply');
|
||||
var btnClose = document.getElementById('btn-ai-close');
|
||||
var ta = document.getElementById('wz-content') || document.getElementById('content');
|
||||
var titleEl = document.getElementById('title');
|
||||
|
||||
var lastRewrite = '';
|
||||
|
||||
function setLoading(btn, loading) {
|
||||
btn.disabled = loading;
|
||||
if (loading) {
|
||||
btn._origText = btn.textContent;
|
||||
btn.textContent = 'En cours…';
|
||||
} else {
|
||||
btn.textContent = btn._origText || btn.textContent;
|
||||
}
|
||||
}
|
||||
|
||||
async function callAi(action) {
|
||||
var btn = (action === 'critique') ? btnCritique : btnRewrite;
|
||||
|
||||
setLoading(btn, true);
|
||||
panel.style.display = 'none';
|
||||
btnApply.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
btnAnalyze.addEventListener('click', async function () {
|
||||
btnAnalyze.disabled = true;
|
||||
btnAnalyze._origText = btnAnalyze.textContent;
|
||||
btnAnalyze.textContent = 'En cours…';
|
||||
panel.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
|
||||
try {
|
||||
var titleVal = titleEl ? titleEl.value : '';
|
||||
@@ -39,57 +27,52 @@ document.addEventListener('DOMContentLoaded', function () {
|
||||
if (m) { titleVal = m[1].trim(); }
|
||||
}
|
||||
|
||||
var res = await fetch('/?action=ai_query', {
|
||||
var res = await fetch('/?action=ai_query', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams({
|
||||
action: action,
|
||||
action: 'analyze',
|
||||
title: titleVal,
|
||||
content: ta ? ta.value : '',
|
||||
}),
|
||||
});
|
||||
|
||||
var data = await res.json();
|
||||
|
||||
if (!data.ok) {
|
||||
labelEl.textContent = 'Erreur';
|
||||
resultEl.textContent = data.error || 'Erreur inconnue.';
|
||||
critiqueEl.textContent = data.error || 'Erreur inconnue.';
|
||||
rewriteEl.textContent = '';
|
||||
btnApply.style.display = 'none';
|
||||
} else {
|
||||
labelEl.textContent = (action === 'critique') ? 'Analyse critique' : 'Réécriture';
|
||||
resultEl.textContent = data.text;
|
||||
if (action === 'rewrite') {
|
||||
lastRewrite = data.text;
|
||||
btnApply.style.display = '';
|
||||
}
|
||||
critiqueEl.textContent = data.critique || '';
|
||||
rewriteEl.textContent = data.rewrite || '';
|
||||
lastRewrite = data.rewrite || '';
|
||||
btnApply.style.display = lastRewrite ? '' : 'none';
|
||||
}
|
||||
panel.style.display = '';
|
||||
} catch (e) {
|
||||
labelEl.textContent = 'Erreur';
|
||||
resultEl.textContent = 'Erreur de connexion.';
|
||||
panel.style.display = '';
|
||||
critiqueEl.textContent = 'Erreur de connexion.';
|
||||
rewriteEl.textContent = '';
|
||||
btnApply.style.display = 'none';
|
||||
panel.style.display = '';
|
||||
} finally {
|
||||
setLoading(btn, false);
|
||||
btnAnalyze.disabled = false;
|
||||
btnAnalyze.textContent = btnAnalyze._origText;
|
||||
}
|
||||
}
|
||||
|
||||
btnCritique.addEventListener('click', function () { callAi('critique'); });
|
||||
btnRewrite.addEventListener('click', function () { callAi('rewrite'); });
|
||||
});
|
||||
|
||||
btnApply.addEventListener('click', function () {
|
||||
if (!lastRewrite) return;
|
||||
if (!confirm("Remplacer le contenu de l’éditeur par la réécriture IA ?")) return;
|
||||
if (!confirm("Remplacer le contenu de l'éditeur par la proposition IA ?")) return;
|
||||
if (ta) {
|
||||
ta.value = lastRewrite;
|
||||
ta.dispatchEvent(new Event('input'));
|
||||
}
|
||||
panel.style.display = 'none';
|
||||
btnApply.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
panel.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
});
|
||||
|
||||
btnClose.addEventListener('click', function () {
|
||||
panel.style.display = 'none';
|
||||
btnApply.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
panel.style.display = 'none';
|
||||
lastRewrite = '';
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user