Skip to content

Commit

Permalink
fix: fix anchor click handling
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Mar 19, 2022
1 parent 42701f6 commit 07f05dd
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/renderer/plugins/markdown-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ function getElement (id: string) {
document.getElementById(encodeURIComponent(id.replace(/^h-/, '')))
}

function getAnchorElement (target: HTMLElement) {
let cur: HTMLElement | null = target
while (cur && cur.tagName !== 'A' && cur.tagName !== 'ARTICLE') {
cur = cur.parentElement
}

return cur?.tagName === 'A' ? <HTMLAnchorElement>cur : null
}

const handleLink = (link: HTMLAnchorElement, view: HTMLElement) => {
const { currentFile } = store.state
if (!currentFile) {
Expand Down Expand Up @@ -224,17 +233,13 @@ export default {
})

ctx.registerHook('VIEW_ELEMENT_CLICK', async ({ e, view }) => {
const target = e.target as HTMLElement
const anchorTarget = getAnchorElement(<HTMLElement>e.target)

const preventEvent = () => {
e.preventDefault()
e.stopPropagation()
return true
}

if (target.tagName === 'A' || target.parentElement?.tagName === 'A') {
if (handleLink(target as HTMLAnchorElement, view)) {
return preventEvent()
if (anchorTarget) {
if (handleLink(anchorTarget, view)) {
e.preventDefault()
e.stopPropagation()
return true
} else {
return true
}
Expand Down

0 comments on commit 07f05dd

Please sign in to comment.