STA App

nt.getElementById('msgs'); b.innerHTML=""; s.forEach(d=>{ const m=d.data(); let c = m.text; if(m.type==='poll') c=`
📊 ${m.q}
1. ${m.o1}
2. ${m.o2}
`; if(m.type==='sticker') c=``; if(m.type==='audio') c=``; b.innerHTML+=`
${c}
`; }); b.scrollTop=b.scrollHeight; }); }; window.closeRoom = () => document.getElementById('room').classList.add('hidden'); window.send = async(type, content) => { let txt = content || document.getElementById('m-inp').value; if(!txt && type==='text') return; // Compression for stickers to fit in Firestore (temp fix) await addDoc(collection(db,"messages"),{chatId:curId, text:txt, uid:user.uid, type:type, createdAt:serverTimestamp()}); if(type==='text') document.getElementById('m-inp').value=""; }; // --- STICKER MAKER LOGIC --- let cvs = document.getElementById('editor'); let ctx = cvs.getContext('2d'); let currentImg = new Image(); window.openStickerMaker = () => document.getElementById('sticker-modal').classList.remove('hidden'); window.closeMaker = () => document.getElementById('sticker-modal').classList.add('hidden'); window.handleImage = (inp) => { const f = inp.files[0]; const r = new FileReader(); r.onload = (e) => { currentImg.src = e.target.result; currentImg.onload = () => window.draw(); }; r.readAsDataURL(f); }; window.draw = () => { // Clear ctx.clearRect(0, 0, cvs.width, cvs.height); // Draw Image with Zoom const zoom = document.getElementById('zoom-sl').value; const w = 300 * zoom; const h = (300 / currentImg.width * currentImg.height) * zoom; const x = (300 - w) / 2; const y = (300 - h) / 2; ctx.drawImage(currentImg, x, y, w, h); // Draw Text const txt = document.getElementById('txt-in').value; const size = document.getElementById('size-sl').value; const col = document.getElementById('col-in').value; ctx.font = `bold ${size}px Arial`; ctx.fillStyle = col; ctx.textAlign = "center"; ctx.strokeStyle = "black"; ctx.lineWidth = 3; ctx.strokeText(txt, 150, 280); // Bottom center ctx.fillText(txt, 150, 280); }; window.finishSticker = () => { const data = cvs.toDataURL('image/jpeg', 0.5); // Compress to 50% window.send('sticker', data); closeMaker(); }; // --- POLLS & AUDIO --- window.sendPoll = async() => { const q=prompt("Question?"), o1=prompt("Opt1"), o2=prompt("Opt2"); if(q) await addDoc(collection(db,"messages"),{chatId:curId, type:'poll', q:q, o1:o1, o2:o2, uid:user.uid, createdAt:serverTimestamp()}); }; window.recAudio = async() => { alert("Recording... Click OK to stop & send."); const stream = await navigator.mediaDevices.getUserMedia({audio:true}); const rec = new MediaRecorder(stream); const chunks = []; rec.ondataavailable = e => chunks.push(e.data); rec.start(); setTimeout(()=>{ rec.stop(); rec.onstop = () => { const blob = new Blob(chunks, {type:'audio/webm'}); const reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = () => window.send('audio', reader.result); }; }, 3000); // Auto stop after 3 sec (Demo) };