use requestAnimationFrame for search

This commit is contained in:
Mylloon 2024-12-12 17:14:39 +01:00
parent bf13da9178
commit 9b6d75b560
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -92,31 +92,35 @@ const deepestNodeOpened = (path, options) => {
}; };
const searchFiles = (query, parent, currentFile) => { const searchFiles = (query, parent, currentFile) => {
const children = parent.querySelectorAll("li"); // Prevent blocking the main thread
requestAnimationFrame(() => {
const children = parent.querySelectorAll("li");
if (query === "") { const normalizedQuery = query.toLowerCase().trim();
children.forEach((item) => { if (normalizedQuery === "") {
item.style.display = ""; children.forEach((item) => {
if ( item.style.display = "";
item.classList.contains("directory") && if (
!item.classList.contains("collapsed") item.classList.contains("directory") &&
) { !item.classList.contains("collapsed")
item.classList.add("collapsed"); ) {
} item.classList.add("collapsed");
}); }
});
uncollapse(currentFile); uncollapse(currentFile);
return; return;
}
for (const item of children) {
if (item.innerText.toLowerCase().includes(query.toLowerCase())) {
item.style.display = "";
uncollapse(item);
continue;
} }
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", () => { window.addEventListener("load", () => {