Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/anchor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ const getCurrentAnchor = ({ vm, state, link, emit }) => {

const addObserver = ({ props, state }) => {
const { links } = props
const { intersectionObserver } = state
const { intersectionObserver, expandLink } = state
const observer = (list) => {
list.forEach(item => {
const link = item.link
expandLink[link] = item
const linkEl = document.querySelector(link)
linkEl && intersectionObserver.observe(linkEl)
if (item.children) {
observer(item.children)
} else {
const linkEl = document.querySelector(link)
linkEl && intersectionObserver.observe(linkEl)
}
})
}
Expand Down Expand Up @@ -123,18 +123,26 @@ export const unmounted = ({ state }) => () => {
}

export const onItersectionObserver = ({ vm, state, props, emit }) => () => {

const { expandLink, scrollContainer } = state
const containerTop = scrollContainer.getBoundingClientRect().top + 10
state.intersectionObserver = new IntersectionObserver((entries) => {
entries.forEach(item => {
const key = item.target.id
state.observerLinks[key] = item
})

for (let item of Object.values(state.observerLinks)) {
if (item.isIntersecting && item.intersectionRatio > 0) {
const link = `#${item.target.id}`
getCurrentAnchor({ vm, state, link, emit })
break
for (let key in state.observerLinks) {
if (Object.prototype.hasOwnProperty.call(state.observerLinks, key)) {
const item = state.observerLinks[key]
if (item.isIntersecting && item.intersectionRatio >= 0 && item.target.getBoundingClientRect().top < containerTop) {
const link = `#${item.target.id}`
if (!expandLink[link].children) {
getCurrentAnchor({ vm, state, link, emit })
break
} else {
getCurrentAnchor({ vm, state, link, emit })
}
}
}
}
})
Expand Down
1 change: 1 addition & 0 deletions src/anchor/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const renderless = (props, { onMounted, onUnmounted, onUpdated, reactive
const state = reactive({
currentLink: '',
observerLinks: {},
expandLink: {},
intersectionObserver: null,
scrollContainer: null,
currentHash: ''
Expand Down