feat(digest): вставка картинок из буфера обмена (Ctrl+V) в модалке

- Обработчик paste на #digestBody собирает изображения из буфера
- Для существующего дайджеста — загружает картинки как вложения напрямую
- Для нового дайджеста — сначала авто-создаёт дайджест (doSaveDigest), затем вставляет картинки как вложения, не закрывая модалку
- Превью через кнопку выбора файла оставлено без изменений
This commit is contained in:
dev
2026-11-04 00:56:01 +03:00
parent f8eecde6fc
commit ab9052e88e
+73 -7
View File
@@ -255,15 +255,15 @@
}
}
async function saveDigest() {
async function doSaveDigest() {
const title = document.getElementById('digestTitle').value.trim();
const body = document.getElementById('digestBody').value.trim();
const isPinned = document.getElementById('digestPinned').checked;
const postAt = inputToIso(document.getElementById('digestPostAt').value);
const hideAt = inputToIso(document.getElementById('digestHideAt').value);
if (!title) { showToast('Введите заголовок', true); return; }
if (!body) { showToast('Введите текст сообщения', true); return; }
if (saving) return;
if (!title) { showToast('Введите заголовок', true); return null; }
if (!body) { showToast('Введите текст сообщения', true); return null; }
if (saving) return null;
const payload = { title: title, body: body, is_pinned: isPinned, preview_path: previewPath || null, post_at: postAt, hide_at: hideAt };
saving = true;
let res;
@@ -278,24 +278,89 @@
} catch (e) {
saving = false;
showToast('Ошибка сети', true);
return;
return null;
}
const data = await res.json().catch(function() { return {}; });
saving = false;
if (!res.ok) {
showToast(data.error || 'Ошибка при сохранении', true);
return;
return null;
}
const savedId = editingId || (data.item && data.item.id);
if (savedId) {
await uploadPreview(savedId);
previewFile = null;
}
showToast(editingId ? 'Сообщение обновлено' : 'Сообщение создано');
return savedId;
}
async function saveDigest() {
const savedId = await doSaveDigest();
if (savedId === null) return;
if (!editingId) {
showToast('Сообщение создано');
closeDigestModal();
await loadDigests();
return;
}
showToast('Сообщение обновлено');
closeDigestModal();
await loadDigests();
}
async function digestPaste(e) {
const m = document.getElementById('digestModal');
if (!m || !m.classList.contains('open')) return;
const active = document.activeElement;
if (!active || active.id !== 'digestBody') return;
const clip = e.clipboardData;
if (!clip) return;
const pasted = [];
for (const item of Array.from(clip.items || [])) {
if (item.kind !== 'file') continue;
const file = item.getAsFile();
if (file && /^image\//i.test(file.type || '')) pasted.push(file);
}
if (!pasted.length && clip.files && clip.files.length) {
for (const f of Array.from(clip.files)) {
if (/^image\//i.test(f.type || '')) pasted.push(f);
}
}
if (!pasted.length) return;
e.preventDefault();
let targetId = editingId;
if (!targetId) {
const saved = await doSaveDigest();
if (!saved) return;
targetId = saved;
editingId = targetId;
document.getElementById('digestImageAdd').style.display = '';
document.getElementById('digestImagesWrap').style.display = '';
}
let pending = pasted.length, failed = 0;
for (const file of pasted) {
const fd = new FormData();
fd.append('image', file);
try {
const res = await fetch(API + '/api/digests/' + targetId + '/images', {
method: 'POST', headers: hdr(), body: fd
});
if (!res.ok) { failed++; continue; }
const data = await res.json().catch(function() { return {}; });
if (data.item) digestImages.push(data.item);
} catch (err) { failed++; }
}
renderImageGallery();
await loadDigests();
if (pending) {
const ok = pending - failed;
if (failed) showToast('Вставлено ' + ok + ' из ' + pending + ' картинок', true);
else showToast('Картинок вставлено: ' + ok);
}
}
async function addPreviewFile() {
const input = document.getElementById('digestPreviewFile');
if (!input.files || !input.files.length) return;
@@ -416,6 +481,7 @@
document.getElementById('digestPreviewRemove').addEventListener('click', removePreview);
document.getElementById('digestImageAdd').addEventListener('click', addImages);
document.getElementById('digestImageFile').addEventListener('change', addImages);
document.getElementById('digestBody').addEventListener('paste', digestPaste);
document.getElementById('digestImagesWrap').addEventListener('click', function(e) {
const btn = e.target.closest('[data-action="del-image"]');
if (btn) removeImage(parseInt(btn.dataset.id, 10));