opencompletion.com/templates/chat.html
Russell Ballestrini 5abd5fa516 add message ids to frontend p
modified:   app.py
	modified:   templates/chat.html
2023-10-29 18:00:48 -04:00

191 lines
5.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatroom</title>
<!-- Include highlight.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
<!-- Include a highlight.js theme (replace 'default' with your preferred theme) -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/9.1.2/marked.min.js" integrity="sha512-rfX4p3RNnxdwLT3wWP1K0NR3ztTobn+sISlT9WhxDDK00zNYbQ6MCHA5OHm0hqKAzEMXYCgFrp8iY/ER5MkXqA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: grid;
place-items: center;
}
#chat-container {
display: grid;
grid-template-rows: 1fr auto;
width: 100%;
height: 90vh;
background-color: #ffffff;
border-radius: 5px;
padding: 15px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
}
#chat {
overflow-y: auto;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
min-width: 400px;
}
#message {
width: 100%;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 5px;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="chat"></div>
<form id="message-form">
<textarea id="message" rows="4" placeholder="Type your message..."></textarea>
</form>
</div>
<script>
const socket = io.connect('http://' + document.domain + ':' + location.port);
const urlParams = new URLSearchParams(window.location.search);
const username = urlParams.get('username');
const room = '{{ room }}';
// Function to handle sending the message
function sendMessage() {
const message = document.getElementById('message').value;
if (message.trim() !== "") { // Ensure we're not sending empty messages
socket.emit('message', {'username': username, 'message': message, 'room': room});
document.getElementById('message').value = '';
}
}
// Event listener for form submission
document.getElementById('message-form').addEventListener('submit', (e) => {
e.preventDefault();
sendMessage();
});
// Event listener for the Enter key press in the textarea
document.getElementById('message').addEventListener('keydown', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
socket.on('connect', () => {
socket.emit('join', {username: username, room: room});
});
socket.on('message', (data) => {
const newMessage = document.createElement('p');
// Set the message ID as the id attribute
newMessage.id = "message-" + data.id; // Prefixing with "message-" to ensure the ID starts with a letter
// Convert the message content to markdown and set it as the innerHTML
newMessage.innerHTML = marked.marked(data.content);
// Append the new message to the chat container
document.getElementById('chat').appendChild(newMessage);
// Apply syntax highlighting to any code blocks within the message
newMessage.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
// Scroll to the bottom of the chat container
document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight;
});
socket.on('previous_messages', (data) => {
const chat = document.getElementById('chat');
// Create a new message element
const newMessage = document.createElement('p');
// Set the ID attribute for the message
newMessage.id = "message-" + data.id;
// Set the inner content for the message
newMessage.innerHTML = `${data.username}: ${marked.marked(data.message)}`;
// Append the new message to the chat container
chat.appendChild(newMessage);
// Apply syntax highlighting to any code blocks within the message
newMessage.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
});
socket.on('delete_processing_message', (data) => {
const tempMessage = document.getElementById("processing");
if (tempMessage) {
tempMessage.remove();
}
});
// A dictionary to hold buffers for each message ID
const messageBuffers = {};
socket.on('message_chunk', (data) => {
const messageId = "message-" + data.id;
let targetMessageElement = document.getElementById(messageId);
// If the target message element doesn't exist, it's an initial chunk
if (!targetMessageElement) {
targetMessageElement = document.createElement('p');
targetMessageElement.id = messageId;
document.getElementById('chat').appendChild(targetMessageElement);
}
// If the message buffer for this ID doesn't exist, create it
if (!messageBuffers[data.id]) {
messageBuffers[data.id] = "";
}
// Append the chunk to the buffer
messageBuffers[data.id] += data.content;
// Process the entire buffer with marked and set it as the content of the target element
targetMessageElement.innerHTML = marked.marked(messageBuffers[data.id]);
// Apply syntax highlighting to code blocks within the content
targetMessageElement.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
// Scroll to the bottom of the chat container
document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight;
});
</script>
</body>
</html>