From 9b6d75b560c5f912d60b7c5595d0cf5e22e88e6f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 12 Dec 2024 17:14:39 +0100 Subject: [PATCH] use requestAnimationFrame for search --- static/js/cours.js | 50 +++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/static/js/cours.js b/static/js/cours.js index 6537fad..8800b14 100644 --- a/static/js/cours.js +++ b/static/js/cours.js @@ -92,31 +92,35 @@ const deepestNodeOpened = (path, options) => { }; const searchFiles = (query, parent, currentFile) => { - const children = parent.querySelectorAll("li"); + // Prevent blocking the main thread + requestAnimationFrame(() => { + const children = parent.querySelectorAll("li"); - if (query === "") { - children.forEach((item) => { - item.style.display = ""; - if ( - item.classList.contains("directory") && - !item.classList.contains("collapsed") - ) { - item.classList.add("collapsed"); - } - }); - - uncollapse(currentFile); - return; - } - - for (const item of children) { - if (item.innerText.toLowerCase().includes(query.toLowerCase())) { - item.style.display = ""; - uncollapse(item); - continue; + const normalizedQuery = query.toLowerCase().trim(); + if (normalizedQuery === "") { + children.forEach((item) => { + item.style.display = ""; + if ( + item.classList.contains("directory") && + !item.classList.contains("collapsed") + ) { + item.classList.add("collapsed"); + } + }); + uncollapse(currentFile); + return; } - item.style.display = "none"; - } + + for (const item of children) { + const matches = item.innerText.toLowerCase().includes(normalizedQuery); + if (matches) { + item.style.display = ""; + uncollapse(item); + continue; + } + item.style.display = "none"; + } + }); }; window.addEventListener("load", () => {