opencompletion.com/templates/chat.html
Russell Ballestrini 175f49d135 make fenced code blocks work with username
modified:   app.py
	modified:   templates/chat.html
2023-11-01 08:30:14 -04:00

249 lines
7.6 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>
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
<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;
}
.message-wrapper {
display: flex;
align-items: start;
}
.message-wrapper button {
margin-top: 16px;
margin-right: 8px;
}
p {
margin-top: 8px;
margin-bottom: 8px;
}
</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 = '';
}
}
function deleteMessage(messageId, room) {
socket.emit('delete_message', {'message_id': messageId, 'room': room});
}
// 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 messageWrapper = document.createElement('div');
messageWrapper.className = 'message-wrapper';
messageWrapper.id = "message-" + data.id;
const newMessage = document.createElement('p');
newMessage.innerHTML = marked.marked(data.content);
// Check if the message is NOT a join notification
if (!data.content.includes('has joined the room.') && !data.content.includes('Processing...')) {
// Create the delete button
const deleteButton = document.createElement('button');
deleteButton.innerHTML = 'x';
deleteButton.onclick = () => deleteMessage(data.id, room);
messageWrapper.appendChild(deleteButton);
}
messageWrapper.appendChild(newMessage);
document.getElementById('chat').appendChild(messageWrapper);
newMessage.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight;
});
socket.on('previous_messages', (data) => {
const messageWrapper = document.createElement('div');
messageWrapper.className = 'message-wrapper';
messageWrapper.id = "message-" + data.id;
const newMessage = document.createElement('p');
newMessage.innerHTML = marked.marked(`${data.username}:\n\n${data.message}`);
// Create the delete button
const deleteButton = document.createElement('button');
deleteButton.innerHTML = 'x';
deleteButton.onclick = () => deleteMessage(data.id, room);
messageWrapper.appendChild(deleteButton);
messageWrapper.appendChild(newMessage);
document.getElementById('chat').appendChild(messageWrapper);
newMessage.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
});
socket.on('delete_processing_message', (msg_id) => {
const tempMessages = document.querySelectorAll("#message-null");
tempMessages.forEach((tempMessage) => {
tempMessage.remove();
});
// Clear the message buffer for the corresponding message ID
delete messageBuffers[msg_id];
});
// A dictionary to hold buffers for each message ID
const messageBuffers = {};
socket.on('message_chunk', (data) => {
const wrapperId = "message-" + data.id;
let messageWrapper = document.getElementById(wrapperId);
let targetMessageElement;
// If the message wrapper doesn't exist, it's an initial chunk
if (!messageWrapper) {
messageWrapper = document.createElement('div');
messageWrapper.className = 'message-wrapper';
messageWrapper.id = wrapperId;
document.getElementById('chat').appendChild(messageWrapper);
// Create the "x" button for deletion
const deleteButton = document.createElement('button');
deleteButton.innerHTML = 'x';
deleteButton.onclick = () => deleteMessage(data.id, room);
messageWrapper.appendChild(deleteButton);
// Create the p element to hold the message content
targetMessageElement = document.createElement('p');
messageWrapper.appendChild(targetMessageElement);
} else {
// If the wrapper already exists, get the p element inside it
targetMessageElement = messageWrapper.querySelector('p');
}
// 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;
});
socket.on('message_deleted', (data) => {
const messageElement = document.getElementById('message-' + data.message_id);
if (messageElement) {
messageElement.remove();
}
});
</script>
</body>
</html>