Update script.js

This commit is contained in:
Voxel 2025-04-15 15:15:25 -04:00 committed by GitHub
parent d198ddddae
commit a44c8dfe82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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() { if (!file) {
localStorage.setItem('threads', JSON.stringify(threads)); alert('Please select an image to upload.');
return;
} }
function createThread() { const formData = new FormData();
const title = document.getElementById('thread-title').value; formData.append('file', file);
const content = document.getElementById('thread-content').value;
const imageInput = document.getElementById('thread-image');
const reader = new FileReader(); // Send the file to culiao.lol for upload
reader.onload = () => { fetch('https://culiao.lol/upload', {
const thread = { method: 'POST',
id: Date.now(), body: formData,
title, })
posts: [{ .then(response => response.json())
content, .then(data => {
image: reader.result || '', if (data.success) {
date: new Date().toLocaleString() const imageUrl = data.url; // Assuming the response includes the URL
}] alert('Image uploaded successfully! Image URL: ' + imageUrl);
}; addImageToPost(imageUrl);
threads.unshift(thread);
saveThreads();
renderThreads();
};
if (imageInput.files.length > 0) {
reader.readAsDataURL(imageInput.files[0]);
} else { } else {
reader.onload(); // No image console.error('Upload failed:', data);
alert('Image upload failed. Please try again.');
} }
})
document.getElementById('thread-title').value = ''; .catch(error => {
document.getElementById('thread-content').value = ''; console.error('Error uploading image:', error);
document.getElementById('thread-image').value = ''; alert('An error occurred during image upload.');
}
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 = `<h3>${thread.title}</h3>`;
thread.posts.forEach(post => {
const postDiv = document.createElement('div');
postDiv.className = 'reply';
postDiv.innerHTML = `
<p>${post.content}</p>
${post.image ? `<img src="${post.image}">` : ''}
<small>${post.date}</small>
`;
threadDiv.appendChild(postDiv);
});
const replyBox = document.createElement('div');
replyBox.className = 'emoji-wrap';
replyBox.innerHTML = `
<textarea placeholder="Write a reply..."></textarea><br>
<input type="file" class="reply-image"><br>
<button>Reply</button>
<button class="emoji-button" data-target="reply-${thread.id}">😊</button>
`;
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);
});
}); });
} }
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!");
}