diff --git a/forum/script.js b/forum/script.js index f2a4a9a..eff5cea 100644 --- a/forum/script.js +++ b/forum/script.js @@ -1,123 +1,50 @@ -let threads = JSON.parse(localStorage.getItem('threads') || '[]'); +function uploadImage() { + const fileInput = document.getElementById('image-upload'); + const file = fileInput.files[0]; -function saveThreads() { - localStorage.setItem('threads', JSON.stringify(threads)); -} - -function createThread() { - const title = document.getElementById('thread-title').value; - const content = document.getElementById('thread-content').value; - const imageInput = document.getElementById('thread-image'); - - const reader = new FileReader(); - reader.onload = () => { - const thread = { - id: Date.now(), - title, - posts: [{ - content, - image: reader.result || '', - date: new Date().toLocaleString() - }] - }; - threads.unshift(thread); - saveThreads(); - renderThreads(); - }; - - if (imageInput.files.length > 0) { - reader.readAsDataURL(imageInput.files[0]); - } else { - reader.onload(); // No image + if (!file) { + alert('Please select an image to upload.'); + return; } - document.getElementById('thread-title').value = ''; - document.getElementById('thread-content').value = ''; - document.getElementById('thread-image').value = ''; -} + const formData = new FormData(); + formData.append('file', file); -function replyToThread(threadId, content, imageFile) { - const thread = threads.find(t => t.id === threadId); - const reader = new FileReader(); - - reader.onload = () => { - thread.posts.push({ - content, - image: reader.result || '', - date: new Date().toLocaleString() - }); - saveThreads(); - renderThreads(); - }; - - if (imageFile) { - reader.readAsDataURL(imageFile); - } else { - reader.onload(); - } -} - -function renderThreads() { - const container = document.getElementById('threads'); - container.innerHTML = ''; - - threads.forEach(thread => { - const threadDiv = document.createElement('div'); - threadDiv.className = 'thread'; - - threadDiv.innerHTML = `

${thread.title}

`; - - thread.posts.forEach(post => { - const postDiv = document.createElement('div'); - postDiv.className = 'reply'; - postDiv.innerHTML = ` -

${post.content}

- ${post.image ? `` : ''} - ${post.date} - `; - threadDiv.appendChild(postDiv); - }); - - const replyBox = document.createElement('div'); - replyBox.className = 'emoji-wrap'; - replyBox.innerHTML = ` -
-
- - - `; - - const textarea = replyBox.querySelector('textarea'); - textarea.id = `reply-${thread.id}`; - - replyBox.querySelector('button').onclick = () => { - const text = textarea.value; - const file = replyBox.querySelector('.reply-image').files[0]; - replyToThread(thread.id, text, file); - }; - - threadDiv.appendChild(replyBox); - container.appendChild(threadDiv); - }); - - attachEmojiPickers(); -} - -function attachEmojiPickers() { - const buttons = document.querySelectorAll('.emoji-button'); - buttons.forEach(button => { - const picker = new EmojiButton({ theme: 'auto' }); - const targetId = button.getAttribute('data-target'); - - picker.on('emoji', emoji => { - const textarea = document.getElementById(targetId); - textarea.value += emoji; - }); - - button.addEventListener('click', () => { - picker.togglePicker(button); - }); + // Send the file to culiao.lol for upload + fetch('https://culiao.lol/upload', { + method: 'POST', + body: formData, + }) + .then(response => response.json()) + .then(data => { + if (data.success) { + const imageUrl = data.url; // Assuming the response includes the URL + alert('Image uploaded successfully! Image URL: ' + imageUrl); + addImageToPost(imageUrl); + } else { + console.error('Upload failed:', data); + alert('Image upload failed. Please try again.'); + } + }) + .catch(error => { + console.error('Error uploading image:', error); + alert('An error occurred during image upload.'); }); } -renderThreads(); +function addImageToPost(imageUrl) { + const postContent = document.getElementById('post-content'); + postContent.value += `\n![Image](${imageUrl})`; // Add image URL to post content +} + +function submitPost() { + const postContent = document.getElementById('post-content').value; + if (!postContent) { + alert("Please write something before submitting."); + return; + } + + // Here you can process the post content and submit it to your forum (e.g., save it to localStorage or display on the page) + console.log("Post submitted: " + postContent); + alert("Post submitted!"); +}