Jacky Zhao
2024-08-09 9acaa1c8ac8c8afd3fa08d3b1f58a60006fcfc6f
quartz/components/scripts/popover.inline.ts
@@ -3,7 +3,7 @@
const p = new DOMParser()
async function mouseEnterHandler(
  this: HTMLLinkElement,
  this: HTMLAnchorElement,
  { clientX, clientY }: { clientX: number; clientY: number },
) {
  const link = this
@@ -33,7 +33,7 @@
  thisUrl.hash = ""
  thisUrl.search = ""
  const targetUrl = new URL(link.href)
  const hash = targetUrl.hash
  const hash = decodeURIComponent(targetUrl.hash)
  targetUrl.hash = ""
  targetUrl.search = ""
@@ -47,8 +47,8 @@
  }
  if (!response) return
  const contentType = response.headers.get("Content-Type")
  const contentTypeCategory = contentType?.split("/")[0] ?? "text"
  const [contentType] = response.headers.get("Content-Type")!.split(";")
  const [contentTypeCategory, typeInfo] = contentType.split("/")
  const popoverElement = document.createElement("div")
  popoverElement.classList.add("popover")
@@ -56,19 +56,27 @@
  popoverInner.classList.add("popover-inner")
  popoverElement.appendChild(popoverInner)
  popoverInner.dataset.contentType = contentTypeCategory
  popoverInner.dataset.contentType = contentType ?? undefined
  switch (contentTypeCategory) {
    case "image":
      const img = document.createElement("img")
      response.blob().then((blob) => {
        img.src = URL.createObjectURL(blob)
      })
      img.src = targetUrl.toString()
      img.alt = targetUrl.pathname
      popoverInner.appendChild(img)
      break
    case "application":
      switch (typeInfo) {
        case "pdf":
          const pdf = document.createElement("iframe")
          pdf.src = targetUrl.toString()
          popoverInner.appendChild(pdf)
          break
        default:
          break
      }
      break
    default:
      const contents = await response.text()
      const html = p.parseFromString(contents, "text/html")
@@ -92,7 +100,7 @@
}
document.addEventListener("nav", () => {
  const links = [...document.getElementsByClassName("internal")] as HTMLLinkElement[]
  const links = [...document.getElementsByClassName("internal")] as HTMLAnchorElement[]
  for (const link of links) {
    link.addEventListener("mouseenter", mouseEnterHandler)
    window.addCleanup(() => link.removeEventListener("mouseenter", mouseEnterHandler))