fix: ctrl + k breaking after page nav

This commit is contained in:
Jacky Zhao 2023-08-20 20:47:07 -07:00
parent d0f67d9935
commit 4e42d52e16

View file

@ -1,7 +1,7 @@
import { Document } from "flexsearch" import { Document } from "flexsearch"
import { ContentDetails } from "../../plugins/emitters/contentIndex" import { ContentDetails } from "../../plugins/emitters/contentIndex"
import { registerEscapeHandler, removeAllChildren } from "./util" import { registerEscapeHandler, removeAllChildren } from "./util"
import { FullSlug, getFullSlug, resolveRelative, simplifySlug } from "../../util/path" import { FullSlug, resolveRelative } from "../../util/path"
interface Item { interface Item {
id: number id: number
@ -58,12 +58,12 @@ function highlight(searchTerm: string, text: string, trim?: boolean) {
}) })
.join(" ") .join(" ")
return `${startIndex === 0 ? "" : "..."}${slice}${ return `${startIndex === 0 ? "" : "..."}${slice}${endIndex === tokenizedText.length - 1 ? "" : "..."
endIndex === tokenizedText.length - 1 ? "" : "..."
}` }`
} }
const encoder = (str: string) => str.toLowerCase().split(/([^a-z]|[^\x00-\x7F])/) const encoder = (str: string) => str.toLowerCase().split(/([^a-z]|[^\x00-\x7F])/)
let prevShortcutHandler: ((e: HTMLElementEventMap["keydown"]) => void) | undefined = undefined
document.addEventListener("nav", async (e: unknown) => { document.addEventListener("nav", async (e: unknown) => {
const currentSlug = (e as CustomEventMap["nav"]).detail.url const currentSlug = (e as CustomEventMap["nav"]).detail.url
@ -159,8 +159,12 @@ document.addEventListener("nav", async (e: unknown) => {
displayResults(finalResults) displayResults(finalResults)
} }
document.removeEventListener("keydown", shortcutHandler) if (prevShortcutHandler) {
document.removeEventListener("keydown", prevShortcutHandler)
}
document.addEventListener("keydown", shortcutHandler) document.addEventListener("keydown", shortcutHandler)
prevShortcutHandler = shortcutHandler
searchIcon?.removeEventListener("click", showSearch) searchIcon?.removeEventListener("click", showSearch)
searchIcon?.addEventListener("click", showSearch) searchIcon?.addEventListener("click", showSearch)
searchBar?.removeEventListener("input", onType) searchBar?.removeEventListener("input", onType)