URLs are now /pub/<base32id>/rkey instead of /pub/did:plc:<base32id>/rkey. Non-plc DIDs keep their method prefix (e.g. web:example.com).
102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
import { ComputedRef, onUnmounted, Ref, toValue } from "vue"
|
|
|
|
import { isExternalLink } from "@/utils/link"
|
|
import { useRouteQueryStackedNotes } from "@/hooks/useRouteQueryStackedNotes.hook"
|
|
import { parseAtUri } from "@/modules/atproto/parseAtUri"
|
|
import { toShortDid } from "@/modules/atproto/shortDid"
|
|
import { router } from "@/router/router"
|
|
|
|
export const useATProtoLinks = (
|
|
className: ComputedRef<string> | string,
|
|
currentAtUri?: Ref<string> | string,
|
|
) => {
|
|
const { addStackedNote } = useRouteQueryStackedNotes()
|
|
const linkNote = (event: Event) => {
|
|
const target = event.target as HTMLElement
|
|
const href = target.getAttribute("href")
|
|
|
|
if (!href) {
|
|
return
|
|
}
|
|
|
|
if (href.startsWith("#")) {
|
|
return
|
|
}
|
|
|
|
event.preventDefault()
|
|
event.stopPropagation()
|
|
|
|
if (isExternalLink(href)) {
|
|
window.open(href, "_blank")
|
|
return
|
|
}
|
|
|
|
if (href.startsWith(window.location.origin)) {
|
|
const { params } = router.resolve(
|
|
href.replace(window.location.origin, ""),
|
|
)
|
|
|
|
if (!params.shortDid || !params.rkey) {
|
|
return
|
|
}
|
|
|
|
const noteId = params.slug
|
|
? `${params.shortDid}-${params.rkey}-${params.slug}`
|
|
: `${params.shortDid}-${params.rkey}`
|
|
|
|
addStackedNote(
|
|
toValue(currentAtUri) ?? "",
|
|
noteId,
|
|
`${params.shortDid}-${params.rkey}`,
|
|
)
|
|
return
|
|
}
|
|
|
|
if (href.startsWith("at://")) {
|
|
const { did, rkey } = parseAtUri(href)
|
|
const noteId = `${toShortDid(did)}-${rkey}`
|
|
|
|
addStackedNote(toValue(currentAtUri) ?? "", noteId)
|
|
}
|
|
}
|
|
|
|
const LINK_SELECTOR = `.${toValue(className)} a`
|
|
|
|
const removeListeners = () => {
|
|
const elements = document.querySelectorAll(LINK_SELECTOR)
|
|
|
|
elements.forEach((element) => {
|
|
element.removeEventListener("click", linkNote)
|
|
})
|
|
}
|
|
|
|
const listenToClick = () => {
|
|
removeListeners()
|
|
const elements = document.querySelectorAll(LINK_SELECTOR)
|
|
|
|
elements.forEach((element) => {
|
|
const href = element.getAttribute("href")
|
|
|
|
if (!href) {
|
|
return
|
|
}
|
|
|
|
if (isExternalLink(href)) {
|
|
element.classList.add("external-link")
|
|
}
|
|
})
|
|
|
|
elements.forEach((element) => {
|
|
element.addEventListener("click", linkNote)
|
|
})
|
|
}
|
|
|
|
onUnmounted(() => {
|
|
removeListeners()
|
|
})
|
|
|
|
return {
|
|
listenToClick,
|
|
}
|
|
}
|