modified: templates/chat.html

This commit is contained in:
Russell Ballestrini 2023-12-02 19:21:20 -05:00
parent f1277e5504
commit 1b0de77a88

View file

@ -228,9 +228,9 @@ socket.on("message", (data) => {
// Apply syntax highlighting to code blocks within the message
newMessage.querySelectorAll("pre code").forEach((block) => {
hljs.highlightElement(block);
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
});
// Scroll to the bottom of the chat container
@ -258,9 +258,9 @@ socket.on("previous_messages", (data) => {
// Apply syntax highlighting to code blocks within the message
newMessage.querySelectorAll("pre code").forEach((block) => {
hljs.highlightElement(block);
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
});
});
@ -317,9 +317,9 @@ socket.on("message_chunk", (data) => {
// Apply syntax highlighting to code blocks within the content
targetMessageElement.querySelectorAll("pre code").forEach((block) => {
hljs.highlightElement(block);
addCopyButtonToCodeBlock(block);
truncateCodeBlock(block);
hljs.highlightElement(block);
});
// Scroll to the bottom of the chat container
@ -334,6 +334,7 @@ socket.on("message_deleted", (data) => {
}
});
function truncateCodeBlock(block, maxLines = 100) {
// Split the content by new lines and check if it exceeds the maxLines
const lines = block.textContent.split('\n');
@ -351,6 +352,8 @@ function truncateCodeBlock(block, maxLines = 100) {
expandButton.onclick = function() {
// Restore the full content from the data attribute
block.textContent = block.dataset.fullContent;
// Reapply syntax highlighting
hljs.highlightElement(block);
// Remove the expand button
expandButton.remove();
};
@ -360,6 +363,7 @@ function truncateCodeBlock(block, maxLines = 100) {
}
}
// 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