Skip to content

Commit b1bf9a3

Browse files
authored
Merge pull request #12 from xwcoder/hotfix/click-anchor
fix: capture the click event of anchor with child element
2 parents 0b53ae0 + 183f422 commit b1bf9a3

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/base/browser/dom.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const getAnchor = (el: HTMLElement): HTMLAnchorElement | null => {
2+
let parent: HTMLElement | null = el
3+
4+
while (parent && parent !== document.body) {
5+
if (parent instanceof HTMLAnchorElement) {
6+
return parent
7+
}
8+
9+
parent = parent.parentElement
10+
}
11+
12+
return null
13+
}

src/ui/apps/reader/MainPanel/Article/content.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { observer } from 'mobx-react-lite'
55
import { store } from '@/ui/store'
66
import { format as formatTime } from '@/base/common/date'
77
import { openExternal } from '@/base/browser/shell'
8+
import { getAnchor } from '@/base/browser/dom'
89

910
const { readerStore } = store
1011

@@ -81,14 +82,16 @@ function Content() {
8182

8283
useEffect(() => {
8384
const el = containerRef.current
85+
8486
const handler = (e: MouseEvent) => {
85-
const { target } = e
86-
if (!(target instanceof HTMLAnchorElement) || !target.href) {
87+
const anchor = getAnchor(e.target as HTMLElement)
88+
89+
if (!(anchor instanceof HTMLAnchorElement) || !anchor.href) {
8790
return
8891
}
8992

9093
e.preventDefault()
91-
openExternal(target.href)
94+
openExternal(anchor.href)
9295
}
9396

9497
el?.addEventListener('click', handler)

0 commit comments

Comments
 (0)