Skip to content

Commit 386c3c9

Browse files
authored
feat(enhanced-readability): improve spotlight hover (#570)
1 parent c2f37b3 commit 386c3c9

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

packages/vitepress-plugin-enhanced-readabilities/src/client/components/SpotlightHoverBlock.vue

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,34 @@ function computeBoxStyles(bounding: {
5151
}
5252
5353
function findChildElementUnderVPDocElement(element: HTMLElement | null): HTMLElement | null {
54+
if (!element)
55+
return null
56+
5457
const rootContainer = document.querySelector<HTMLElement>('.VPDoc main .vp-doc > div')
55-
const allowedContainerTags = ['DIV', 'BLOCKQUOTE', 'UL', 'OL', 'LI']
58+
if (!rootContainer)
59+
return null
60+
61+
const allowedContainerTags = ['DIV', 'BLOCKQUOTE', 'DETAILS', 'FIGURE', 'UL', 'OL', 'TABLE', 'THEAD', 'TBODY']
5662
57-
function findValidAncestor(currentElement: HTMLElement | null): HTMLElement | null {
58-
if (!currentElement || !rootContainer || currentElement === rootContainer) return null
63+
function isAncestorValid(current: HTMLElement | null): boolean {
64+
if (!current || current === rootContainer)
65+
return false
5966
60-
const parent = currentElement.parentElement
67+
const parent = current.parentElement
6168
62-
if (!parent) return null
63-
if (parent === rootContainer) return currentElement
64-
if (!allowedContainerTags.includes(parent.tagName)) return findValidAncestor(parent)
65-
if (parent.tagName === 'LI' && currentElement === parent.firstElementChild) return findValidAncestor(parent)
69+
if (!parent)
70+
return false
71+
if (parent === rootContainer)
72+
return true
73+
if (!allowedContainerTags.includes(parent.tagName))
74+
return false
75+
if (parent.tagName === 'DIV' && parent.querySelector(':scope > pre'))
76+
return false
6677
67-
return findValidAncestor(parent) ? currentElement : null
78+
return isAncestorValid(parent)
6879
}
6980
70-
return findValidAncestor(element)
81+
return isAncestorValid(element) ? element : findChildElementUnderVPDocElement(element.parentElement)
7182
}
7283
7384
function watchHandler() {
@@ -89,13 +100,12 @@ function watchHandler() {
89100
for (let i = 0; i < lines; i++) {
90101
const top = i * lineHeight
91102
const height = lineHeight
92-
const left = val.offsetLeft
93103
const width = val.offsetWidth
94104
95105
if (relativeY >= top && relativeY < top + height) {
96106
boxStyles.value = computeBoxStyles({
97107
top: top + rect.top,
98-
left: left + rect.left,
108+
left: rect.left,
99109
width,
100110
height,
101111
})

0 commit comments

Comments
 (0)