opencompletion.com/templates/chat.html
Russell Ballestrini 31a2f306e1 allow edits of streamed message chunks
modified:   templates/chat.html
2023-12-12 11:28:46 -05:00

635 lines
23 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 for syntax highlighting -->
<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">
<!-- Include socket.io for real-time bidirectional event-based communication -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
<!-- Include marked.js for markdown parsing -->
<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>
<!-- Include DOMPurify to sanitize HTML and prevent XSS attacks -->
<script src="https://cdn.jsdelivr.net/npm/dompurify@2/dist/purify.min.js"></script>
<!-- Link to the favicon -->
<link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
<style>
/* Basic styling for the chat application */
html, body {
height: 100%;
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background-color: #f7f7f7;
display: grid;
place-items: center;
}
/* Styling for the chat container */
#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;
}
/* Styling for the chat area */
#chat {
overflow-y: auto;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
min-width: 400px;
}
/* Styling for the message input area */
#message, .message-edit {
width: 100%;
border: 1px solid #e1e1e1;
border-radius: 5px;
padding: 5px;
display: block;
}
/* Styling for the message div holding html/markdown content */
.message-content {
width: 100%;
}
/* Styling for individual message wrappers */
.message-wrapper {
display: flex;
align-items: start;
}
/* Styling for the delete button next to messages */
.message-wrapper button {
margin-top: 16px;
margin-right: 8px;
}
/* Styling for paragraphs, used for messages */
p {
margin-top: 8px;
margin-bottom: 8px;
}
/* Styling for the main container that holds the rooms list and chat */
.main-container {
display: grid;
grid-template-columns: 20% 80%; /* Adjust the 20% as needed */
width: 100%;
height: 90vh;
}
/* Styling for the rooms list */
#rooms-list {
border-right: 1px solid #e1e1e1;
overflow-y: auto;
}
/* Styling for the unordered list in the rooms list */
#rooms-list ul {
list-style: none; /* Removes default list styling */
padding: 0; /* Resets default padding */
margin: 0; /* Resets default margin */
}
/* Styling for list items in the rooms list */
#rooms-list li {
margin-bottom: 10px; /* Adds space between items */
padding: 5px; /* Adds padding inside each item */
border: 1px solid #e1e1e1; /* Adds a border around each item */
border-radius: 5px; /* Optional: Rounds the corners of the border */
}
/* Styling for links in the rooms list */
#rooms-list a {
text-decoration: none; /* Optional: Removes underline from links */
color: inherit; /* Optional: Ensures link color matches the text color */
}
.hljs {
position: relative;
padding-left: 46px !important;
counter-reset: line;
}
.hljs .line-numbers-rows {
position: absolute;
top: 0;
left: 0;
width: 3em; /* Adjust the width as needed */
letter-spacing: -1px;
border-right: 1px solid #ccc; /* Optional: adds a line to separate numbers */
text-align: right;
margin-top: 10px; /* Align with the code block */
color: #999;
pointer-events: none;
}
.hljs .line-numbers-rows span {
display: block;
counter-increment: line;
}
.hljs .line-numbers-rows span::before {
content: counter(line);
display: block;
padding-right: 0.8em; /* Adjust the padding as needed */
}
</style>
</head>
<body>
<div class="main-container">
<div id="rooms-list">
<ul>
<!-- Loop through rooms and create list items for each room -->
{% for room in rooms %}
<a href="{{ url_for('chat', room_name=room.name) }}?username={{ username }}">
<li data-room-id="{{ room.id }}">
<!-- Display the room title if available, otherwise the room name -->
<b>{{ room.name }}</b> {% if room.title %}<br />{{ room.title }} {% endif %}
</li>
</a>
{% endfor %}
</ul>
</div>
<div id="chat-container">
<!-- Chat area where messages will be displayed -->
<div id="chat"></div>
<!-- Form for sending messages -->
<form id="message-form">
<textarea id="message" rows="4" placeholder="Type your message..."></textarea>
</form>
</div>
</div>
<script>
// Connect to the server using socket.io with dynamic scheme (http or https)
const socket = io.connect(window.location.protocol + "//" + document.domain + ":" + location.port);
// Retrieve username and room name from the URL parameters
const urlParams = new URLSearchParams(window.location.search);
const username = urlParams.get("username");
const room_name = "{{ room_name }}";
// Configuration for DOMPurify to specify which tags and attributes are allowed
const dompurify_config = {
ADD_TAGS: ["iframe", "img"],
FORBID_TAGS: ["form"],
ALLOWED_ATTR: [
"src", "width", "height", "frameborder", "allowfullscreen",
"alt", "class", "title", "style"
]
};
// 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_name": room_name});
document.getElementById("message").value = "";
}
}
// Function to handle deleting a message
function deleteMessage(messageId, room_name) {
socket.emit("delete_message", {"message_id": messageId, "room_name": room_name});
}
// Event listener for form submission to send a message
document.getElementById("message-form").addEventListener("submit", (e) => {
e.preventDefault();
sendMessage();
});
// Event listener for the Enter key press in the textarea to send a message
document.getElementById("message").addEventListener("keydown", function(e) {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
// Socket event for updating the room title
socket.on("update_room_title", (data) => {
document.title = data.title; // Update the window's title
});
// Socket event to update the room title in the sidebar.
socket.on('update_room_list', function(updatedRoom) {
// Find the room list item by its data-room-id attribute
const roomListItem = document.querySelector(`#rooms-list li[data-room-id="${updatedRoom.id}"]`);
if (roomListItem) {
// Update the room list item's content with the new title
roomListItem.innerHTML = `<b>${updatedRoom.name}</b> ${updatedRoom.title ? '<br />' + updatedRoom.title : ''}`;
}
});
// Socket event when the user connects
socket.on("connect", () => {
socket.emit("join", {"username": username, "room_name": room_name});
});
// Socket event for receiving a new message
socket.on("message", (data) => {
const messageWrapper = document.createElement("div");
messageWrapper.className = "message-wrapper";
messageWrapper.id = "message-" + data.id;
const newMessage = document.createElement("div");
newMessage.className = "message-content";
// Check if the username is present and prepend it to the message content
const messageContent = data.username ? `**${data.username}:**\n\n${data.content}` : data.content;
// Check if the message starts with a base64 image tag
if (data.content.startsWith('<img src="data:image/jpeg;base64')) {
// Directly assign the message as innerHTML if it starts with a base64 image
newMessage.innerHTML = data.content;
} else {
// Otherwise, sanitize and process the message with marked
newMessage.innerHTML = DOMPurify.sanitize(marked.marked(messageContent), dompurify_config);
}
// Check if the message has an id which means we can delete it.
if (data.id) {
newMessage.dataset.rawMarkdown = data.content;
// Create the delete button
const deleteButton = document.createElement("button");
deleteButton.innerHTML = "x";
deleteButton.onclick = () => deleteMessage(data.id, room_name);
messageWrapper.appendChild(deleteButton);
// Create the edit button
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.className = "edit-button";
editButton.onclick = () => editMessage(data.id, newMessage, data.content);
messageWrapper.appendChild(editButton);
}
messageWrapper.appendChild(newMessage);
document.getElementById("chat").appendChild(messageWrapper);
// Apply syntax highlighting to code blocks within the message
newMessage.querySelectorAll("pre code").forEach((block) => {
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
addLineNumbers(block);
});
// Scroll to the bottom of the chat container
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
});
// Socket event for receiving previous messages
socket.on("previous_messages", (data) => {
const messageWrapper = document.createElement("div");
messageWrapper.className = "message-wrapper";
messageWrapper.id = "message-" + data.id;
const newMessage = document.createElement("div");
newMessage.className = "message-content";
// Check if the message contains a base64 image
if (data.message.startsWith('<img src="data:image/jpeg;base64')) {
// Directly assign the message as innerHTML if it's a base64 image
newMessage.innerHTML = data.message;
} else {
// Otherwise, sanitize and process the message with marked
newMessage.innerHTML = DOMPurify.sanitize(marked.marked(`**${data.username}:**\n\n${data.message}`), dompurify_config);
}
newMessage.dataset.rawMarkdown = data.message;
// Create the delete button
const deleteButton = document.createElement("button");
deleteButton.innerHTML = "x";
deleteButton.onclick = () => deleteMessage(data.id, room_name);
messageWrapper.appendChild(deleteButton);
// Create the edit button
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.className = "edit-button";
editButton.onclick = () => editMessage(data.id, newMessage);
messageWrapper.appendChild(editButton);
messageWrapper.appendChild(newMessage);
document.getElementById("chat").appendChild(messageWrapper);
// Apply syntax highlighting to code blocks within the message
newMessage.querySelectorAll("pre code").forEach((block) => {
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
addLineNumbers(block);
});
});
// Socket event for deleting a processing message
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 event for receiving chunks of a message
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 div element to hold the message content
targetMessageElement = document.createElement("div");
targetMessageElement.className = "message-content";
// Create the "x" button for deletion
const deleteButton = document.createElement("button");
deleteButton.innerHTML = "x";
deleteButton.onclick = () => deleteMessage(data.id, room_name);
messageWrapper.appendChild(deleteButton);
// Create the edit button
const editButton = document.createElement("button");
editButton.textContent = "Edit";
editButton.className = "edit-button";
editButton.onclick = () => editMessage(data.id, targetMessageElement);
messageWrapper.appendChild(editButton);
messageWrapper.appendChild(targetMessageElement);
} else {
// If the wrapper already exists, get the message-content div inside it
targetMessageElement = messageWrapper.querySelector(".message-content");
}
// 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
const sanitizedContent = DOMPurify.sanitize(marked.marked(messageBuffers[data.id]), dompurify_config);
targetMessageElement.innerHTML = sanitizedContent;
// Store the raw markdown in a data attribute for later use in editing
targetMessageElement.dataset.rawMarkdown = messageBuffers[data.id];
// Apply syntax highlighting to code blocks within the content
targetMessageElement.querySelectorAll("pre code").forEach((block) => {
addCopyButtonToCodeBlock(block);
hljs.highlightElement(block);
addLineNumbers(block);
});
// Scroll to the bottom of the chat container
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
});
// Socket event for when a message is deleted
socket.on("message_deleted", (data) => {
const messageElement = document.getElementById("message-" + data.message_id);
if (messageElement) {
messageElement.remove();
}
});
// Socket event for when a message is updated
socket.on("message_updated", (data) => {
// Find the existing message wrapper by ID
const messageWrapper = document.getElementById("message-" + data.message_id);
if (messageWrapper) {
// Find the specific element that contains the message content
const messageContentContainer = messageWrapper.querySelector(".message-content");
// Update the message content
if (data.content.startsWith('<img src="data:image/jpeg;base64')) {
// If it's a base64 image, set it directly
messageContentContainer.innerHTML = data.content;
} else {
// If it's not an image, sanitize and process the message with marked
messageContentContainer.innerHTML = DOMPurify.sanitize(marked.marked(data.content), dompurify_config);
}
// Apply syntax highlighting and other functionalities to code blocks within the message
messageContentContainer.querySelectorAll("pre code").forEach((block) => {
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
addLineNumbers(block);
});
}
});
// Function to enter edit mode
function editMessage(messageId, messageContentContainer) {
// Store the current HTML in a data attribute
messageContentContainer.dataset.originalHtml = messageContentContainer.innerHTML;
const rawMarkdown = messageContentContainer.dataset.rawMarkdown;
// Create a textarea for editing
const textarea = document.createElement("textarea");
textarea.value = rawMarkdown;
textarea.rows = 16;
textarea.className = "message-edit";
// Replace the message content with the textarea
messageContentContainer.innerHTML = '';
messageContentContainer.appendChild(textarea);
// Find the message wrapper to access the edit and save buttons
const messageWrapper = messageContentContainer.closest('.message-wrapper');
// Create a save button with the 'save-button' class
const saveButton = document.createElement("button");
saveButton.textContent = "Save";
saveButton.className = "save-button"; // Add the class here
saveButton.onclick = () => saveEditedMessage(messageId, textarea, messageContentContainer);
// Change the edit button to a cancel button
const editButton = messageWrapper.querySelector(".edit-button");
editButton.textContent = "Cancel";
editButton.onclick = () => cancelEdit(messageId, messageContentContainer);
// Append the save button next to the cancel button
editButton.after(saveButton);
}
// Function to save the edited message
function saveEditedMessage(messageId, textarea, messageContentContainer) {
// Get the updated markdown from the textarea
const updatedMarkdown = textarea.value;
// Emit the update_message event to the server
socket.emit("update_message", {
"message_id": messageId,
"content": updatedMarkdown,
"room_name": room_name
});
// Reset the edit button to its original state
const messageWrapper = messageContentContainer.closest('.message-wrapper');
const editButton = messageWrapper.querySelector(".edit-button");
editButton.textContent = 'Edit';
editButton.onclick = () => editMessage(messageId, messageContentContainer, updatedMarkdown);
// Remove the save button using the 'save-button' class
const saveButton = messageWrapper.querySelector(".save-button");
if (saveButton) {
saveButton.remove();
}
}
// Function to cancel the edit and revert changes
function cancelEdit(messageId, messageContentContainer) {
// Restore the original HTML of the message content from the data attribute
messageContentContainer.innerHTML = messageContentContainer.dataset.originalHtml;
// Reset the edit button to its original state
const messageWrapper = messageContentContainer.closest('.message-wrapper');
const editButton = messageWrapper.querySelector(".edit-button");
editButton.textContent = 'Edit';
editButton.onclick = () => editMessage(messageId, messageContentContainer, messageContentContainer.dataset.rawMarkdown);
// Remove the save button using the 'save-button' class
const saveButton = messageWrapper.querySelector(".save-button");
if (saveButton) {
saveButton.remove();
}
}
function truncateCodeBlock(block, maxLines = 100) {
// Split the content by new lines and check if it exceeds the maxLines
const lines = block.textContent.split('\n');
if (lines.length > maxLines) {
// Store the full content in a data attribute
block.dataset.fullContent = block.textContent;
// Truncate the displayed content
const truncatedText = lines.slice(0, maxLines).join('\n') + '\n...';
block.textContent = truncatedText;
// Create a button to expand the code block
const expandButton = document.createElement('button');
expandButton.textContent = 'Show More';
expandButton.onclick = function() {
// Restore the full content from the data attribute
block.textContent = block.dataset.fullContent;
// Reapply syntax highlighting
hljs.highlightElement(block);
addLineNumbers(block);
// Change the button text to "Show Less"
expandButton.textContent = 'Show Less';
// Change the onclick function to truncate the block again
expandButton.onclick = function() {
block.textContent = truncatedText;
// Reapply syntax highlighting
hljs.highlightElement(block);
addLineNumbers(block);
// Change the button text back to "Show More"
expandButton.textContent = 'Show More';
// Set the onclick function back to the original expand function
expandButton.onclick = originalExpandFunction;
};
};
// Keep a reference to the original expand function
const originalExpandFunction = expandButton.onclick;
// Insert the expand button after the code block
block.parentNode.insertBefore(expandButton, block.nextSibling);
}
}
// Modify the addCopyButtonToCodeBlock function to use the full content
function addCopyButtonToCodeBlock(block) {
// Check if the full content is stored in a data attribute, otherwise use textContent
const contentToCopy = block.dataset.fullContent || block.textContent;
// Create a button to copy the code block's content
const copyButton = document.createElement('button');
copyButton.textContent = 'Copy';
copyButton.classList.add('copy-button'); // Add a class for styling if needed
copyButton.onclick = function() {
// Copy the content to the clipboard
navigator.clipboard.writeText(contentToCopy).then(() => {
// Optionally, indicate that the text was copied
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = 'Copy';
}, 2000); // Reset button text after 2 seconds
}).catch(err => {
console.error('Error copying text: ', err);
});
};
// Insert the button before the code block
block.parentNode.insertBefore(copyButton, block);
}
function addLineNumbers(block) {
const lines = block.textContent.split('\n').length - 1;
const lineNumbersWrapper = document.createElement('div');
lineNumbersWrapper.className = 'line-numbers-rows';
for (let i = 0; i < lines; i++) {
lineNumbersWrapper.appendChild(document.createElement('span'));
}
block.appendChild(lineNumbersWrapper);
}
</script>
</body>
</html>