From d5bde7babc7a023bed87257a5ef5026a221d2e87 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Mon, 6 May 2024 16:51:05 -0700 Subject: [PATCH] UI: improve the performance of code syntax highlighting --- js/main.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/js/main.js b/js/main.js index 235d6dea26..72568e1b7a 100644 --- a/js/main.js +++ b/js/main.js @@ -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; @@ -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"); @@ -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);