12
12
import type { IAnchorRenderlessParams , IAnchorLinkItem } from '@/types'
13
13
import { addClass , removeClass } from '../common/deps/dom'
14
14
15
+ const getEleMentBySelect = ( parent , selector ) =>
16
+ selector ?. startsWith ( '#' ) ? document . getElementById ( 'selector' ) : parent . querySelector ( selector )
17
+
15
18
export const setFixAnchor =
16
19
( { vm, props } : Pick < IAnchorRenderlessParams , 'vm' | 'props' > ) =>
17
20
( ) => {
@@ -25,7 +28,7 @@ export const setFixAnchor =
25
28
const setMarkClass = ( { state, props } : Pick < IAnchorRenderlessParams , 'state' | 'props' > ) => {
26
29
const { scrollContainer } = state
27
30
const { markClass } = props
28
- const activeContentEl = scrollContainer . querySelector ( `${ state . currentLink } ` )
31
+ const activeContentEl = getEleMentBySelect ( scrollContainer , `${ state . currentLink } ` )
29
32
if ( markClass ) {
30
33
addClass ( activeContentEl , markClass )
31
34
setTimeout ( ( ) => {
@@ -95,7 +98,7 @@ const addObserver = ({ props, state }: Pick<IAnchorRenderlessParams, 'props' | '
95
98
list . forEach ( ( item ) => {
96
99
const link = item . link
97
100
expandLink [ link ] = item
98
- const linkEl = document . querySelector ( link )
101
+ const linkEl = getEleMentBySelect ( document , link )
99
102
linkEl && intersectionObserver . observe ( linkEl )
100
103
if ( item . children ) {
101
104
observer ( item . children )
@@ -127,13 +130,13 @@ const setChildOffsetTop = ({ state, props }: Pick<IAnchorRenderlessParams, 'stat
127
130
if ( ! props . links ?. length ) {
128
131
return
129
132
}
130
- state . childOffsetTop = document . querySelector ( props . links [ 0 ] . link ) ?. offsetTop || 0
133
+ state . childOffsetTop = getEleMentBySelect ( document , props . links [ 0 ] . link ) ?. offsetTop || 0
131
134
}
132
135
133
136
export const getContainer =
134
137
( { props } : Pick < IAnchorRenderlessParams , 'props' > ) =>
135
138
( ) : Element =>
136
- ( props . containerId && document . querySelector ( props . containerId ) ) || document . body
139
+ ( props . containerId && getEleMentBySelect ( document , props . containerId ) ) || document . body
137
140
138
141
export const mounted =
139
142
( { state, api, props, nextTick } : Pick < IAnchorRenderlessParams , 'state' | 'api' | 'props' | 'nextTick' > ) =>
@@ -234,7 +237,7 @@ export const linkClick =
234
237
setMarkClass ( { state, props } )
235
238
236
239
if ( scrollContainer && scrollContainer !== document . body && ! isChangeHash ) {
237
- const linkEl = scrollContainer . querySelector ( item . link ) as HTMLElement
240
+ const linkEl = getEleMentBySelect ( scrollContainer , item . link ) as HTMLElement
238
241
const top =
239
242
linkEl ?. getBoundingClientRect ( ) . top -
240
243
scrollContainer . getBoundingClientRect ( ) . top +
0 commit comments