fix(photo-enhance): restore dead sliders in enhanceModal and harden save

- re-bind input listeners for #enhanceBrightness/#enhanceContrast/#enhanceSaturate/#enhanceDenoise/#enhanceSharp -> scheduleEnhancePreview (lost in f5910d9)
- new "Только эффекты" button in #cpuEnhanceModal footer: applies slider effects via CPU pipeline (mode=cpu, face=off) in one click, no photo-ai/model
- harden saveEnhance: null-guard enhanceEntryId/enhanceSourceImg/btn, AbortController timeouts on applyAiResult (15s), canvas upload (30s), refreshEntryCard; guard runPhotoAi btn.disabled
This commit is contained in:
dev
2026-10-17 12:27:13 +03:00
parent f5910d9524
commit 4216173d30
2 changed files with 77 additions and 13 deletions
+1
View File
@@ -480,6 +480,7 @@
<div class="card-foot cpu-enhance-foot">
<button type="button" class="btn-primary ghost" id="cpuEnhanceRestoreBtn" style="display:none" title="Вернуть исходную фотографию"><i data-lucide="undo-2"></i> Вернуть оригинал</button>
<button type="button" class="btn-primary ghost" id="cpuEnhanceCancelBtn">Закрыть</button>
<button type="button" class="btn-primary ghost" id="cpuEnhanceApplyCpuBtn" title="Сразу применить эффекты с ползунков на полном размере, без ИИ-обработки. Оригинал сохранится в .originals — его можно вернуть кнопкой «Вернуть оригинал»"><i data-lucide="sliders-horizontal"></i> <span id="cpuEnhanceApplyCpuLabel">Только эффекты</span></button>
<button type="button" class="btn-primary" id="cpuEnhanceActivateBtn" title="Запустить обработку и получить результат"><i data-lucide="zap"></i> Активировать</button>
<button type="button" class="btn-primary" id="cpuEnhanceApplyBtn" title="Сохранить результат к записи"><i data-lucide="check"></i> <span id="cpuEnhanceApplyLabel">Сохранить</span></button>
</div>
+74 -11
View File
@@ -1139,7 +1139,7 @@ async function runPhotoAi() {
if (!confirm(`ИИ-обработка фото: ${mode.title}. ${faceNote} Продолжить?`)) return;
enhanceSaving = true;
const btn = document.getElementById('enhanceAiBtn');
btn.disabled = true;
if (btn) btn.disabled = true;
try {
const res = await fetch(`${API}/api/entries/${entryId}/photo/enhance-ai`, {
method: 'POST',
@@ -1215,7 +1215,7 @@ async function runPhotoAi() {
alert(e.message);
} finally {
enhanceSaving = false;
btn.disabled = false;
if (btn) btn.disabled = false;
setBtnLabel(btn, 'ИИ');
}
}
@@ -1304,11 +1304,12 @@ function isDefaultEnhanceParams() {
return p.brightness === 100 && p.contrast === 100 && p.saturate === 100 && p.denoise === 0 && p.sharp === 0;
}
async function applyAiResult() {
async function applyAiResult(signal) {
if (!enhanceAiJobId || !enhanceEntryId) return false;
const res = await fetch(`${API}/api/entries/${enhanceEntryId}/photo/jobs/${enhanceAiJobId}/apply`, {
method: 'POST',
headers: hdr(),
signal,
});
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.error || 'Ошибка применения ИИ-результата');
@@ -1634,6 +1635,8 @@ function cpuEnhanceSyncPending() {
if (autoBtn) autoBtn.disabled = cpuEnhanceBusy;
const resetBtn = document.getElementById('cpuEnhanceResetBtn');
if (resetBtn) resetBtn.disabled = cpuEnhanceBusy;
const applyCpuBtn = document.getElementById('cpuEnhanceApplyCpuBtn');
if (applyCpuBtn) applyCpuBtn.disabled = cpuEnhanceBusy;
}
function cpuEnhanceMarkApplied() {
@@ -1804,6 +1807,8 @@ async function openCpuEnhance(id) {
cpuEnhanceClearError();
const applyLabel = document.getElementById('cpuEnhanceApplyLabel');
if (applyLabel) applyLabel.textContent = 'Сохранить';
const applyCpuLabel = document.getElementById('cpuEnhanceApplyCpuLabel');
if (applyCpuLabel) applyCpuLabel.textContent = 'Только эффекты';
const restoreBtn = document.getElementById('cpuEnhanceRestoreBtn');
if (restoreBtn) restoreBtn.style.display = e.photo_original_path ? '' : 'none';
const context = document.getElementById('cpuEnhanceContext');
@@ -2021,6 +2026,41 @@ async function cpuEnhanceApply() {
}
}
async function cpuEnhanceApplyCpuOnly() {
if (!cpuEnhanceEntryId || cpuEnhanceSaving) return;
const entryId = cpuEnhanceEntryId;
const label = document.getElementById('cpuEnhanceApplyCpuLabel');
cpuEnhanceSaving = true;
cpuEnhanceClearError();
if (label) label.textContent = 'Сохраняю…';
cpuEnhanceSetBusy(true);
cpuEnhanceSetLoading(true, 'Применение эффектов…');
try {
const res = await fetch(`${API}/api/entries/${entryId}/photo/enhance-cpu`, {
method: 'POST',
headers: hdrJson(),
body: JSON.stringify({
mode: 'cpu',
params: { ...cpuEnhanceReadParams(), preset: 'quality' },
face: 'off',
face_model: 'gfpgan',
}),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(data.error || 'Ошибка обработки');
showToast('Эффекты применены, ИИ не использовался');
closeCpuEnhance(true);
refreshEntryCard(entryId);
} catch (e) {
cpuEnhanceSetError(e.message);
showToast(e.message, true);
} finally {
cpuEnhanceSaving = false;
if (label) label.textContent = 'Только эффекты';
cpuEnhanceSetLoading(false);
}
}
function cpuEnhancePollAi(entryId, jobId) {
return new Promise((resolve, reject) => {
let done = false;
@@ -2351,18 +2391,25 @@ function autoEnhance() {
}
async function saveEnhance() {
if (!enhanceEntryId || !enhanceSourceImg || enhanceSaving) return;
if (enhanceSaving) return;
if (!enhanceEntryId) { alert('Запись не выбрана'); return; }
if (!enhanceSourceImg) { alert('Фото ещё не загружено — подождите'); return; }
await loadEnhanceEngine();
const btn = document.getElementById('enhanceSaveBtn');
enhanceSaving = true;
btn.disabled = true;
btn.textContent = 'Сохранение...';
if (btn) { btn.disabled = true; btn.textContent = 'Сохранение...'; }
const entryId = enhanceEntryId;
try {
let done = false;
let engineLabel = 'клиент';
if (enhanceAiResultPath && isDefaultEnhanceParams()) {
await applyAiResult();
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 15000);
try {
await applyAiResult(ctrl.signal);
} finally {
clearTimeout(timer);
}
engineLabel = 'ИИ';
done = true;
} else if (enhanceEngine !== 'client' && !enhanceAiResultPath) {
@@ -2396,23 +2443,33 @@ async function saveEnhance() {
const blob = await new Promise((resolve, reject) => canvas.toBlob(b => b ? resolve(b) : reject(new Error('toBlob failed')), 'image/jpeg', 0.92));
const fd = new FormData();
fd.append('photo', blob, 'enhanced.jpg');
const res = await fetch(`${API}/api/entries/${entryId}/photo/enhance`, { method: 'PUT', headers: hdr(), body: fd });
const ctrl = new AbortController();
const timer = setTimeout(() => ctrl.abort(), 30000);
let res;
try {
res = await fetch(`${API}/api/entries/${entryId}/photo/enhance`, { method: 'PUT', headers: hdr(), body: fd, signal: ctrl.signal });
} finally {
clearTimeout(timer);
}
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || 'Ошибка сохранения');
}
}
showToast(`Фото улучшено (${engineLabel}) и сохранено`);
closeEnhance();
refreshEntryCard(entryId);
try { closeEnhance(); } catch (e) { console.error('closeEnhance failed:', e); }
try { await refreshEntryCard(entryId); } catch (e) { console.error('refreshEntryCard failed:', e); }
} catch (e) {
alert(e.message);
console.error('saveEnhance failed:', e);
alert(e && e.message ? e.message : String(e));
} finally {
enhanceSaving = false;
if (btn && document.contains(btn)) {
btn.disabled = false;
btn.textContent = 'Применить';
}
}
}
async function aiCorrect() {
if (aiCorrecting) return;
@@ -3028,10 +3085,16 @@ async function applyPhotoJob(jobId) {
}
}
document.getElementById('enhanceModal').addEventListener('click', e => { if (e.target === e.currentTarget) closeEnhance(); });
document.getElementById('enhanceBrightness').addEventListener('input', scheduleEnhancePreview);
document.getElementById('enhanceContrast').addEventListener('input', scheduleEnhancePreview);
document.getElementById('enhanceSaturate').addEventListener('input', scheduleEnhancePreview);
document.getElementById('enhanceDenoise').addEventListener('input', scheduleEnhancePreview);
document.getElementById('enhanceSharp').addEventListener('input', scheduleEnhancePreview);
document.getElementById('cpuEnhanceModal').addEventListener('click', e => { if (e.target === e.currentTarget) closeCpuEnhance(); });
document.getElementById('cpuEnhanceCancelBtn').addEventListener('click', () => closeCpuEnhance());
document.getElementById('cpuEnhanceCloseX').addEventListener('click', () => closeCpuEnhance());
document.getElementById('cpuEnhanceApplyBtn').addEventListener('click', cpuEnhanceApply);
document.getElementById('cpuEnhanceApplyCpuBtn').addEventListener('click', cpuEnhanceApplyCpuOnly);
document.getElementById('cpuEnhancePreviewBtn').addEventListener('click', cpuEnhancePreview);
document.getElementById('cpuEnhanceActivateBtn').addEventListener('click', cpuEnhanceActivate);
document.getElementById('cpuZoomIn').addEventListener('click', () => cpuEnhanceZoomBy(1.25));