Do not render copy message button on every Khoj thinking step

Only render copy chat message button once, after message text is rendered
This commit is contained in:
Debanjum Singh Solanky
2024-04-10 14:46:38 +05:30
parent cadeaac769
commit 804c04f7b9
2 changed files with 50 additions and 42 deletions

View File

@@ -300,7 +300,7 @@
renderMessage(message, by, dt, references);
}
function formatHTMLMessage(htmlMessage, raw=false) {
function formatHTMLMessage(htmlMessage, raw=false, willReplace=true) {
var md = window.markdownit();
let newHTML = htmlMessage;
@@ -326,6 +326,7 @@
element.className = "chat-message-text-response";
// Add a copy button to each chat message
if (willReplace === true) {
let copyButton = document.createElement('button');
copyButton.classList.add("copy-button");
copyButton.title = "Copy Message";
@@ -335,11 +336,12 @@
copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText);
element.append(copyButton);
}
// Get any elements with a class that starts with "language"
let codeBlockElements = element.querySelectorAll('[class^="language-"]');
// For each element, add a parent div with the class "programmatic-output"
codeBlockElements.forEach((codeElement) => {
codeBlockElements.forEach((codeElement, key) => {
// Create the parent div
let parentDiv = document.createElement('div');
parentDiv.classList.add("programmatic-output");
@@ -348,6 +350,7 @@
// Move the code element into the parent div
parentDiv.appendChild(codeElement);
// Add a copy button to each element
if (willReplace === true) {
let copyButton = document.createElement('button');
copyButton.classList.add("copy-button");
copyButton.title = "Copy Code";
@@ -357,6 +360,7 @@
copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText);
codeElement.prepend(copyButton);
}
});
// Get all code elements that have no class.

View File

@@ -327,7 +327,7 @@ To get started, just start typing below. You can also type / to see a list of co
renderMessage(message, by, dt, references);
}
function formatHTMLMessage(htmlMessage, raw=false) {
function formatHTMLMessage(htmlMessage, raw=false, willReplace=true) {
var md = window.markdownit();
let newHTML = htmlMessage;
@@ -352,7 +352,8 @@ To get started, just start typing below. You can also type / to see a list of co
element.innerHTML = newHTML;
element.className = "chat-message-text-response";
// Add a copy button to each chat message
// Add a copy button to each chat message, if it doesn't already exist
if (willReplace === true) {
let copyButton = document.createElement('button');
copyButton.classList.add("copy-button");
copyButton.title = "Copy Message";
@@ -362,6 +363,7 @@ To get started, just start typing below. You can also type / to see a list of co
copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText);
element.append(copyButton);
}
// Get any elements with a class that starts with "language"
let codeBlockElements = element.querySelectorAll('[class^="language-"]');
@@ -374,7 +376,8 @@ To get started, just start typing below. You can also type / to see a list of co
codeElement.parentNode.insertBefore(parentDiv, codeElement);
// Move the code element into the parent div
parentDiv.appendChild(codeElement);
// Add a copy button to each element
// Add a copy button to each code block, if it doesn't already exist
if (willReplace === true) {
let copyButton = document.createElement('button');
copyButton.classList.add("copy-button");
copyButton.title = "Copy Code";
@@ -384,6 +387,7 @@ To get started, just start typing below. You can also type / to see a list of co
copyButton.appendChild(copyIcon);
copyButton.addEventListener('click', copyParentText);
codeElement.prepend(copyButton);
}
});
// Get all code elements that have no class.
@@ -589,7 +593,7 @@ To get started, just start typing below. You can also type / to see a list of co
if (replace) {
newResponseElement.innerHTML = "";
}
newResponseElement.appendChild(formatHTMLMessage(rawResponse));
newResponseElement.appendChild(formatHTMLMessage(rawResponse, false, replace));
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
}