feat: handle absolute internal links for stacked notes
This commit is contained in:
@@ -9,18 +9,15 @@ import { getUrl } from "@/modules/atproto/getUrl"
|
||||
import { withATProtoImages } from "@/modules/atproto/withATProtoImages"
|
||||
import { getAuthor } from "@/modules/atproto/getAuthor"
|
||||
import { PublicNoteRecord } from "@/modules/atproto/publicNote.types"
|
||||
import { parseAtUri } from "@/modules/atproto/parseAtUri"
|
||||
|
||||
const props = defineProps<{
|
||||
atUri: string
|
||||
didrkey: string
|
||||
index: number
|
||||
title?: string
|
||||
}>()
|
||||
|
||||
const atUri = computed(() => props.atUri)
|
||||
const atUriProps = computed(() => parseAtUri(atUri.value))
|
||||
const did = computed(() => atUriProps.value.did)
|
||||
const rkey = computed(() => atUriProps.value.rkey)
|
||||
const didrkey = computed(() => props.didrkey)
|
||||
const did = computed(() => props.didrkey.split("-")[0])
|
||||
const rkey = computed(() => props.didrkey.split("-")[1])
|
||||
|
||||
const index = computed(() => props.index)
|
||||
|
||||
@@ -33,7 +30,7 @@ const className = computed(() => `stacked-note-${props.index}`)
|
||||
const titleClassName = computed(() => `title-${className.value}`)
|
||||
|
||||
const { scrollToFocusedNote } = useRouteQueryStackedNotes()
|
||||
const { listenToClick } = useATProtoLinks(className.value, atUri)
|
||||
const { listenToClick } = useATProtoLinks(className.value, didrkey)
|
||||
const { displayNoteOverlay } = useNoteOverlay(className.value, index)
|
||||
|
||||
const noteRecord = computedAsync(async () =>
|
||||
@@ -70,12 +67,12 @@ watch(
|
||||
:class="{
|
||||
[className]: true,
|
||||
overlay: displayNoteOverlay,
|
||||
[`note-${rkey}`]: true,
|
||||
[`note-${didrkey}`]: true,
|
||||
}"
|
||||
>
|
||||
<a
|
||||
class="title-stacked-note-link"
|
||||
@click.prevent="scrollToFocusedNote(rkey)"
|
||||
@click.prevent="scrollToFocusedNote(didrkey)"
|
||||
>
|
||||
<div
|
||||
class="title-stacked-note breadcrumbs text-sm"
|
||||
|
||||
@@ -3,6 +3,7 @@ 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 { router } from "@/router/router"
|
||||
|
||||
export const useATProtoLinks = (
|
||||
className: ComputedRef<string> | string,
|
||||
@@ -11,26 +12,50 @@ export const useATProtoLinks = (
|
||||
const { addStackedNote } = useRouteQueryStackedNotes()
|
||||
const linkNote = (event: Event) => {
|
||||
const target = event.target as HTMLElement
|
||||
const atUri = target.getAttribute("href")
|
||||
const href = target.getAttribute("href")
|
||||
|
||||
if (!atUri) {
|
||||
if (!href) {
|
||||
return
|
||||
}
|
||||
|
||||
if (atUri.startsWith("#")) {
|
||||
if (href.startsWith("#")) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
|
||||
if (isExternalLink(atUri)) {
|
||||
window.open(atUri, "_blank")
|
||||
if (isExternalLink(href)) {
|
||||
window.open(href, "_blank")
|
||||
return
|
||||
}
|
||||
const { rkey } = parseAtUri(atUri)
|
||||
|
||||
addStackedNote(toValue(currentAtUri) ?? "", atUri, rkey)
|
||||
if (href.startsWith(window.location.origin)) {
|
||||
const { params } = router.resolve(
|
||||
href.replace(window.location.origin, ""),
|
||||
)
|
||||
|
||||
if (!params.did || !params.rkey) {
|
||||
return
|
||||
}
|
||||
|
||||
const noteId = params.slug
|
||||
? `${params.did}-${params.rkey}-${params.slug}`
|
||||
: `${params.did}-${params.rkey}`
|
||||
|
||||
addStackedNote(toValue(currentAtUri) ?? "", noteId)
|
||||
return
|
||||
}
|
||||
|
||||
if (href.startsWith("at://")) {
|
||||
const { did, rkey } = parseAtUri(href)
|
||||
|
||||
addStackedNote(
|
||||
toValue(currentAtUri) ?? "",
|
||||
`${did}-${rkey}`,
|
||||
`${did}-${rkey}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const LINK_SELECTOR = `.${toValue(className)} a`
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export const LINKS = ['http://', 'https://']
|
||||
export const LINKS = ["http://", "https://"]
|
||||
|
||||
export const isExternalLink = (href: string) =>
|
||||
!href.startsWith(window.location.origin) &&
|
||||
LINKS.some((link) => href.startsWith(link))
|
||||
|
||||
@@ -133,7 +133,7 @@ watch(
|
||||
:key="stackedNote"
|
||||
class="note"
|
||||
:index="index"
|
||||
:at-uri="stackedNote"
|
||||
:didrkey="stackedNote"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user