From 0a3379a8530f365e2bd85e8ea20a1dfc8126c39c Mon Sep 17 00:00:00 2001
From: Jacky Zhao <j.zhao2k19@gmail.com>
Date: Fri, 02 Feb 2024 18:10:25 +0000
Subject: [PATCH] fix(search): null checks and focus fixes
---
quartz/components/scripts/search.inline.ts | 54 ++++++++++++++++++++++++------------------------------
1 files changed, 24 insertions(+), 30 deletions(-)
diff --git a/quartz/components/scripts/search.inline.ts b/quartz/components/scripts/search.inline.ts
index 2924f39..1ecf62f 100644
--- a/quartz/components/scripts/search.inline.ts
+++ b/quartz/components/scripts/search.inline.ts
@@ -188,7 +188,7 @@
removeAllChildren(preview)
}
if (searchLayout) {
- searchLayout.style.visibility = "hidden"
+ searchLayout.classList.remove("display-results")
}
searchType = "basic" // reset search type after closing
@@ -228,7 +228,7 @@
// If search is active, then we will render the first result and display accordingly
if (!container?.classList.contains("active")) return
- else if (e.key === "Enter") {
+ if (e.key === "Enter") {
// If result has focus, navigate to that one, otherwise pick first result
if (results?.contains(document.activeElement)) {
const active = document.activeElement as HTMLInputElement
@@ -250,9 +250,9 @@
: (document.activeElement as HTMLInputElement | null)
const prevResult = currentResult?.previousElementSibling as HTMLInputElement | null
currentResult?.classList.remove("focus")
- await displayPreview(prevResult)
prevResult?.focus()
- currentHover = prevResult
+ if (prevResult) currentHover = prevResult
+ await displayPreview(prevResult)
}
} else if (e.key === "ArrowDown" || e.key === "Tab") {
e.preventDefault()
@@ -264,19 +264,9 @@
: (document.getElementsByClassName("result-card")[0] as HTMLInputElement | null)
const secondResult = firstResult?.nextElementSibling as HTMLInputElement | null
firstResult?.classList.remove("focus")
- await displayPreview(secondResult)
secondResult?.focus()
- currentHover = secondResult
- } else {
- // If an element in results-container already has focus, focus next one
- const active = currentHover
- ? currentHover
- : (document.activeElement as HTMLInputElement | null)
- active?.classList.remove("focus")
- const nextResult = active?.nextElementSibling as HTMLInputElement | null
- await displayPreview(nextResult)
- nextResult?.focus()
- currentHover = nextResult
+ if (secondResult) currentHover = secondResult
+ await displayPreview(secondResult)
}
}
}
@@ -325,9 +315,9 @@
currentHover?.classList.remove("focus")
currentHover?.blur()
const target = ev.target as HTMLInputElement
- await displayPreview(target)
currentHover = target
currentHover.classList.add("focus")
+ await displayPreview(target)
}
async function onMouseLeave(ev: MouseEvent) {
@@ -405,12 +395,11 @@
async function displayPreview(el: HTMLElement | null) {
if (!searchLayout || !enablePreview || !el || !preview) return
const slug = el.id as FullSlug
- el.classList.add("focus")
- previewInner = document.createElement("div")
- previewInner.classList.add("preview-inner")
const innerDiv = await fetchContent(slug).then((contents) =>
contents.flatMap((el) => [...highlightHTML(currentSearchTerm, el as HTMLElement).children]),
)
+ previewInner = document.createElement("div")
+ previewInner.classList.add("preview-inner")
previewInner.append(...innerDiv)
preview.replaceChildren(previewInner)
@@ -418,13 +407,13 @@
const highlights = [...preview.querySelectorAll(".highlight")].sort(
(a, b) => b.innerHTML.length - a.innerHTML.length,
)
- highlights[0]?.scrollIntoView()
+ highlights[0]?.scrollIntoView({ block: "start" })
}
async function onType(e: HTMLElementEventMap["input"]) {
if (!searchLayout || !index) return
currentSearchTerm = (e.target as HTMLInputElement).value
- searchLayout.style.visibility = currentSearchTerm === "" ? "hidden" : "visible"
+ searchLayout.classList.toggle("display-results", currentSearchTerm !== "")
searchType = currentSearchTerm.startsWith("#") ? "tags" : "basic"
let searchResults: FlexSearch.SimpleDocumentSearchResultSetUnit[]
@@ -464,8 +453,8 @@
searchBar?.addEventListener("input", onType)
window.addCleanup(() => searchBar?.removeEventListener("input", onType))
- await fillDocument(data)
registerEscapeHandler(container, hideSearch)
+ await fillDocument(data)
})
/**
@@ -475,13 +464,18 @@
*/
async function fillDocument(data: { [key: FullSlug]: ContentDetails }) {
let id = 0
+ const promises: Array<Promise<unknown>> = []
for (const [slug, fileData] of Object.entries<ContentDetails>(data)) {
- await index.addAsync(id++, {
- id,
- slug: slug as FullSlug,
- title: fileData.title,
- content: fileData.content,
- tags: fileData.tags,
- })
+ promises.push(
+ index.addAsync(id++, {
+ id,
+ slug: slug as FullSlug,
+ title: fileData.title,
+ content: fileData.content,
+ tags: fileData.tags,
+ }),
+ )
}
+
+ return await Promise.all(promises)
}
--
Gitblit v1.10.0