refacto: better const naming
This commit is contained in:
@@ -1,24 +1,24 @@
|
|||||||
import { ComputedRef, onUnmounted, Ref, toValue } from 'vue'
|
import { ComputedRef, onUnmounted, Ref, toValue } from "vue"
|
||||||
|
|
||||||
import { noteEventBus } from '@/bus/noteEventBus'
|
import { noteEventBus } from "@/bus/noteEventBus"
|
||||||
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
|
import { useUserRepoStore } from "@/modules/repo/store/userRepo.store"
|
||||||
import { isExternalLink } from '@/utils/link'
|
import { isExternalLink } from "@/utils/link"
|
||||||
|
|
||||||
export const useLinks = (
|
export const useLinks = (
|
||||||
className: ComputedRef<string> | string,
|
className: ComputedRef<string> | string,
|
||||||
sha?: Ref<string> | string
|
sha?: Ref<string> | string,
|
||||||
) => {
|
) => {
|
||||||
const store = useUserRepoStore()
|
const store = useUserRepoStore()
|
||||||
|
|
||||||
const linkNote: EventListener = (event) => {
|
const linkNote: EventListener = (event) => {
|
||||||
const target = event.target as HTMLElement
|
const target = event.target as HTMLElement
|
||||||
const href = target.getAttribute('href')
|
const href = target.getAttribute("href")
|
||||||
|
|
||||||
if (!href) {
|
if (!href) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (href.startsWith('#')) {
|
if (href.startsWith("#")) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ export const useLinks = (
|
|||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
|
|
||||||
if (isExternalLink(href)) {
|
if (isExternalLink(href)) {
|
||||||
window.open(href, '_blank')
|
window.open(href, "_blank")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,38 +34,38 @@ export const useLinks = (
|
|||||||
path: href,
|
path: href,
|
||||||
currentNoteSHA: toValue(sha),
|
currentNoteSHA: toValue(sha),
|
||||||
user: store.user,
|
user: store.user,
|
||||||
repo: store.repo
|
repo: store.repo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const selector = `.${toValue(className)} a`
|
const LINK_SELECTOR = `.${toValue(className)} a`
|
||||||
|
|
||||||
const removeListeners = () => {
|
const removeListeners = () => {
|
||||||
const elements = document.querySelectorAll(selector)
|
const elements = document.querySelectorAll(LINK_SELECTOR)
|
||||||
|
|
||||||
elements.forEach((element) => {
|
elements.forEach((element) => {
|
||||||
element.removeEventListener('click', linkNote)
|
element.removeEventListener("click", linkNote)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const listenToClick = () => {
|
const listenToClick = () => {
|
||||||
removeListeners()
|
removeListeners()
|
||||||
const elements = document.querySelectorAll(selector)
|
const elements = document.querySelectorAll(LINK_SELECTOR)
|
||||||
|
|
||||||
elements.forEach((element) => {
|
elements.forEach((element) => {
|
||||||
const href = element.getAttribute('href')
|
const href = element.getAttribute("href")
|
||||||
|
|
||||||
if (!href) {
|
if (!href) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isExternalLink(href)) {
|
if (isExternalLink(href)) {
|
||||||
element.classList.add('external-link')
|
element.classList.add("external-link")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
elements.forEach((element) => {
|
elements.forEach((element) => {
|
||||||
element.addEventListener('click', linkNote)
|
element.addEventListener("click", linkNote)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +74,6 @@ export const useLinks = (
|
|||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
listenToClick
|
listenToClick,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user