feat: add report card screenshot functionality

- Add screenshot button in journal HTML
- Implement downloadReportCardScreenshot() with html-to-image
- Add styles for screenshot modal
- New dependency: html-to-image.js
This commit is contained in:
dev
2026-10-29 11:23:16 +03:00
parent dd934baf5d
commit a97455030a
3 changed files with 61 additions and 0 deletions
+51
View File
@@ -3034,6 +3034,57 @@ reportFieldsPanel.addEventListener('change', e => {
});
document.getElementById('reportFieldsAll').addEventListener('click', () => setReportCardFields(REPORT_CARD_FIELDS.map(f => f.key)));
document.getElementById('reportFieldsNone').addEventListener('click', () => setReportCardFields([]));
function reportScreenshotLabel() {
return document.querySelector('#reportScreenshotBtn .report-fields__shot-text');
}
async function downloadReportCardScreenshot() {
const modal = document.getElementById('reportCardModal');
const card = modal ? modal.querySelector('.student-card') : null;
const btn = document.getElementById('reportScreenshotBtn');
const label = reportScreenshotLabel();
if (!card || !modal.classList.contains('open') || !window.htmlToImage || !btn || btn.disabled) return;
const oldLabel = label ? label.textContent : '';
btn.disabled = true;
if (label) label.textContent = 'Готовим...';
const rect = card.getBoundingClientRect();
if (!rect.width || !rect.height) { btn.disabled = false; if (label) label.textContent = oldLabel; return; }
const holder = document.createElement('div');
holder.style.cssText = 'position:fixed;left:-10000px;top:0;';
const stage = document.createElement('div');
const clone = card.cloneNode(true);
clone.removeAttribute('id');
clone.querySelectorAll('[id]').forEach(n => n.removeAttribute('id'));
clone.style.boxShadow = '0 6px 16px rgba(39,91,160,.18)';
stage.style.cssText = `position:relative;left:0;top:0;width:${Math.ceil(rect.width) + 20}px;height:${Math.ceil(rect.height) + 20}px;padding:10px;box-sizing:border-box;background:linear-gradient(160deg,#E8F2FF 0%,#F6FAFF 55%,#D9EAFE 100%);border-radius:30px;`;
try {
stage.appendChild(clone);
holder.appendChild(stage);
document.body.appendChild(holder);
const blob = await window.htmlToImage.toBlob(stage, { pixelRatio: 2, skipFonts: true });
if (!blob) throw new Error('empty blob');
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const nm = (document.getElementById('reportCardName').textContent || '').trim().replace(/\s+/g, ' ').replace(/[\\/:*?"<>|]+/g, '').slice(0, 60);
a.href = url;
a.download = `card-${nm || 'student'}-${todayIso()}.png`;
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 4000);
showToast('Скриншот карточки сохранён');
} catch (err) {
console.error('report card screenshot failed', err);
showToast('Не удалось сделать скриншот', true);
} finally {
holder.remove();
btn.disabled = false;
if (label) label.textContent = oldLabel;
}
}
document.getElementById('reportScreenshotBtn').addEventListener('click', downloadReportCardScreenshot);
document.addEventListener('click', e => {
if (!reportFieldsRoot.contains(e.target)) setReportFieldsOpen(false);
});