render(); } function updateLike(id){ const arr = loadPosts(); const idx = arr.findIndex(x=>x.id===id); if(idx===-1) return; arr[idx].liked = !arr[idx].liked; arr[idx].likes = (arr[idx].likes||0) + (arr[idx].liked ? 1 : -1); savePosts(arr); render(); } function deletePost(id){ let arr = loadPosts(); arr = arr.filter(x=>x.id !== id); savePosts(arr); render(); } function copyPost(id){ const arr = loadPosts(); const p = arr.find(x=>x.id===id); if(!p) return; navigator.clipboard?.writeText(p.content).then(()=>alert('Copiado!')); } postsEl.addEventListener('click', (e)=>{ const btn = e.target.closest('button'); if(!btn) return; const act = btn.getAttribute('data-act'); const id = btn.getAttribute('data-id'); if(act==='like') updateLike(id); if(act==='delete') { if(confirm('Excluir este post?')) deletePost(id); } if(act==='copy') copyPost(id); }); postBtn.addEventListener('click', ()=>{ const txt = input.value; if(!txt.trim()) return alert('Escreve alguma coisa primeiro!'); createPost(txt); input.value=''; chars.textContent='0'; }); // helpers function escapeHtml(s){ return String(s).replace(/&/g,'&').replace(//g,'>') } function linkify(text){ // transforma # e urls simples return text.replace(/(https?:\/\/[^\s]+)/g, '$1') .replace(/#(\w+)/g, '#$1'); } // init render(); // small sample posts if empty (uncomment to seed) // if(loadPosts().length===0){ savePosts([{id:'p_1',author:'Amiga Dev',handle:'@amigadev',content:'Bem-vinda ao microblog!','likes':1,'time':Date.now()-60000}]); render(); }