Skip to content

Commit

Permalink
UI: improve the performance of code syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
oobabooga committed May 7, 2024
1 parent 0b193b8 commit d5bde7b
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,21 @@ targetElement.addEventListener("scroll", function() {

// Create a MutationObserver instance
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
updateCssProperties();

const firstChild = targetElement.children[0];
if (firstChild.classList.contains("generating")) {
typing.parentNode.classList.add("visible-dots");
document.getElementById("stop").style.display = "flex";
document.getElementById("Generate").style.display = "none";
} else {
typing.parentNode.classList.remove("visible-dots");
document.getElementById("stop").style.display = "none";
document.getElementById("Generate").style.display = "flex";
}
updateCssProperties();

const firstChild = targetElement.children[0];
if (firstChild.classList.contains("generating")) {
typing.parentNode.classList.add("visible-dots");
document.getElementById("stop").style.display = "flex";
document.getElementById("Generate").style.display = "none";
} else {
typing.parentNode.classList.remove("visible-dots");
document.getElementById("stop").style.display = "none";
document.getElementById("Generate").style.display = "flex";
}

doSyntaxHighlighting();
});

doSyntaxHighlighting();

if(!isScrolled) {
targetElement.scrollTop = targetElement.scrollHeight;
Expand Down Expand Up @@ -215,6 +214,9 @@ function doSyntaxHighlighting() {
indexes.forEach((index) => {
const element = elements[index];

// Tag this element to prevent it from being highlighted twice
element.setAttribute("data-highlighted", "true");

// Perform syntax highlighting
const codeBlocks = element.querySelectorAll("pre code");

Expand All @@ -231,8 +233,6 @@ function doSyntaxHighlighting() {
],
});

// Tag this element to indicate it has been syntax highlighted
element.setAttribute("data-highlighted", "true");
});

observer.observe(targetElement, config);
Expand Down

0 comments on commit d5bde7b

Please sign in to comment.