1212import type { IAnchorRenderlessParams , IAnchorLinkItem } from '@/types'
1313import { addClass , removeClass } from '../common/deps/dom'
1414
15+ const getEleMentBySelect = ( parent , selector ) =>
16+ selector ?. startsWith ( '#' ) ? document . getElementById ( 'selector' ) : parent . querySelector ( selector )
17+
1518export const setFixAnchor =
1619 ( { vm, props } : Pick < IAnchorRenderlessParams , 'vm' | 'props' > ) =>
1720 ( ) => {
@@ -25,7 +28,7 @@ export const setFixAnchor =
2528const setMarkClass = ( { state, props } : Pick < IAnchorRenderlessParams , 'state' | 'props' > ) => {
2629 const { scrollContainer } = state
2730 const { markClass } = props
28- const activeContentEl = scrollContainer . querySelector ( `${ state . currentLink } ` )
31+ const activeContentEl = getEleMentBySelect ( scrollContainer , `${ state . currentLink } ` )
2932 if ( markClass ) {
3033 addClass ( activeContentEl , markClass )
3134 setTimeout ( ( ) => {
@@ -95,7 +98,7 @@ const addObserver = ({ props, state }: Pick<IAnchorRenderlessParams, 'props' | '
9598 list . forEach ( ( item ) => {
9699 const link = item . link
97100 expandLink [ link ] = item
98- const linkEl = document . querySelector ( link )
101+ const linkEl = getEleMentBySelect ( document , link )
99102 linkEl && intersectionObserver . observe ( linkEl )
100103 if ( item . children ) {
101104 observer ( item . children )
@@ -127,13 +130,13 @@ const setChildOffsetTop = ({ state, props }: Pick<IAnchorRenderlessParams, 'stat
127130 if ( ! props . links ?. length ) {
128131 return
129132 }
130- state . childOffsetTop = document . querySelector ( props . links [ 0 ] . link ) ?. offsetTop || 0
133+ state . childOffsetTop = getEleMentBySelect ( document , props . links [ 0 ] . link ) ?. offsetTop || 0
131134}
132135
133136export const getContainer =
134137 ( { props } : Pick < IAnchorRenderlessParams , 'props' > ) =>
135138 ( ) : Element =>
136- ( props . containerId && document . querySelector ( props . containerId ) ) || document . body
139+ ( props . containerId && getEleMentBySelect ( document , props . containerId ) ) || document . body
137140
138141export const mounted =
139142 ( { state, api, props, nextTick } : Pick < IAnchorRenderlessParams , 'state' | 'api' | 'props' | 'nextTick' > ) =>
@@ -234,7 +237,7 @@ export const linkClick =
234237 setMarkClass ( { state, props } )
235238
236239 if ( scrollContainer && scrollContainer !== document . body && ! isChangeHash ) {
237- const linkEl = scrollContainer . querySelector ( item . link ) as HTMLElement
240+ const linkEl = getEleMentBySelect ( scrollContainer , item . link ) as HTMLElement
238241 const top =
239242 linkEl ?. getBoundingClientRect ( ) . top -
240243 scrollContainer . getBoundingClientRect ( ) . top +
0 commit comments