2022-10-11 12:37:06 +08:00
|
|
|
/* global KEEP */
|
|
|
|
|
|
|
|
function initToggleShowToc() {
|
2022-10-12 15:55:47 +08:00
|
|
|
KEEP.utils.postHelper = {
|
2022-10-11 12:37:06 +08:00
|
|
|
postPageContainerDom: document.querySelector('.post-page-container'),
|
|
|
|
toggleShowTocBtnDom: document.querySelector('.toggle-show-toc'),
|
|
|
|
toggleShowTocIcon: document.querySelector('.toggle-show-toc i'),
|
2022-10-12 15:55:47 +08:00
|
|
|
mainContentDom: document.querySelector('.main-content'),
|
2022-10-13 22:51:32 +08:00
|
|
|
postToolsDom: document.querySelector('.post-tools'),
|
|
|
|
goToCommentsDom: document.querySelector('.post-tools .go-to-comments'),
|
|
|
|
|
2022-10-11 12:37:06 +08:00
|
|
|
isShowToc: false,
|
|
|
|
|
2022-10-12 15:55:47 +08:00
|
|
|
initToggleToc() {
|
2022-10-11 12:37:06 +08:00
|
|
|
this.toggleShowTocBtnDom &&
|
2022-10-15 00:17:40 +08:00
|
|
|
this.toggleShowTocBtnDom.addEventListener('click', () => {
|
|
|
|
this.isShowToc = !this.isShowToc
|
|
|
|
KEEP.styleStatus.isShowToc = this.isShowToc
|
|
|
|
KEEP.setStyleStatus()
|
|
|
|
this.handleToggleToc(this.isShowToc)
|
|
|
|
})
|
2022-10-11 12:37:06 +08:00
|
|
|
},
|
|
|
|
|
2022-10-12 15:55:47 +08:00
|
|
|
handleToggleToc(isOpen) {
|
2022-10-11 12:37:06 +08:00
|
|
|
if (isOpen) {
|
|
|
|
this.postPageContainerDom.classList.add('show-toc')
|
2022-10-11 23:05:45 +08:00
|
|
|
document.body.classList.add('has-toc')
|
2022-10-11 12:37:06 +08:00
|
|
|
} else {
|
|
|
|
this.postPageContainerDom.classList.remove('show-toc')
|
2022-10-11 23:05:45 +08:00
|
|
|
document.body.classList.remove('has-toc')
|
2022-10-11 12:37:06 +08:00
|
|
|
}
|
2022-10-20 14:37:32 +08:00
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setPostToolsLeft()
|
|
|
|
}, 120)
|
2022-10-11 12:37:06 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
hasToc(isOpen) {
|
|
|
|
this.toggleShowTocBtnDom.style.display = 'flex'
|
|
|
|
this.isShowToc = isOpen
|
2022-10-12 15:55:47 +08:00
|
|
|
this.handleToggleToc(isOpen)
|
|
|
|
},
|
|
|
|
|
2022-10-20 14:37:32 +08:00
|
|
|
setPostToolsLeft(mcw) {
|
|
|
|
const mainContainerWidth = mcw
|
|
|
|
? mcw
|
|
|
|
: this.mainContentDom.getBoundingClientRect().width.toFixed(0)
|
2022-10-12 15:55:47 +08:00
|
|
|
let offsetX = 5
|
|
|
|
|
2022-10-20 14:37:32 +08:00
|
|
|
if (window.innerWidth <= 800) {
|
2022-10-12 15:55:47 +08:00
|
|
|
offsetX = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
this.postToolsDom.style.opacity = `1`
|
2022-10-20 14:37:32 +08:00
|
|
|
this.postToolsDom.style.left = `calc((100vw - ${mainContainerWidth}px) / 2 - ${offsetX}rem)`
|
2022-10-12 15:55:47 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
initSetPostToolsLeft() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setPostToolsLeft()
|
|
|
|
}, 150)
|
|
|
|
|
|
|
|
window.addEventListener('resize', () => {
|
|
|
|
this.setPostToolsLeft()
|
|
|
|
})
|
2022-10-13 22:51:32 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// go comment anchor
|
|
|
|
goToComments() {
|
2022-10-13 23:45:44 +08:00
|
|
|
const commentsAnchor = document.querySelector('#comments-anchor')
|
|
|
|
if (this.goToCommentsDom && commentsAnchor) {
|
|
|
|
this.goToCommentsDom.addEventListener('click', (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
let winScrollY = window.scrollY
|
|
|
|
winScrollY = winScrollY === 0 ? -20 : winScrollY
|
|
|
|
const offset = commentsAnchor.getBoundingClientRect().top + winScrollY
|
|
|
|
window.anime({
|
|
|
|
targets: document.scrollingElement,
|
2022-10-14 09:52:56 +08:00
|
|
|
duration: 300,
|
2022-10-13 23:45:44 +08:00
|
|
|
easing: 'linear',
|
|
|
|
scrollTop: offset,
|
|
|
|
complete: () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
KEEP.utils.pageTop_dom.classList.add('hide')
|
|
|
|
}, 150)
|
|
|
|
}
|
|
|
|
})
|
2022-10-13 22:51:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// watch comments count
|
|
|
|
watchPostCommentsCount() {
|
|
|
|
const commentsCountDom = this.postToolsDom.querySelector('.post-comments-count')
|
2022-10-20 17:07:41 +08:00
|
|
|
if (!commentsCountDom) return
|
2022-10-15 00:17:40 +08:00
|
|
|
const config = { attributes: true, childList: true }
|
2022-10-20 17:07:41 +08:00
|
|
|
|
2022-10-13 22:51:32 +08:00
|
|
|
const callback = function (mutationsList) {
|
|
|
|
mutationsList.forEach((item) => {
|
|
|
|
if (item.type === 'childList') {
|
|
|
|
const count = Number(item.target.innerHTML)
|
|
|
|
if (count > 0) {
|
|
|
|
commentsCountDom.style.display = 'flex'
|
|
|
|
if (count > 99) {
|
|
|
|
commentsCountDom.innerHTML = '99+'
|
|
|
|
observer.disconnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new MutationObserver(callback)
|
|
|
|
observer.observe(commentsCountDom, config)
|
2022-10-11 12:37:06 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 15:55:47 +08:00
|
|
|
KEEP.utils.postHelper.initToggleToc()
|
|
|
|
KEEP.utils.postHelper.initSetPostToolsLeft()
|
2022-10-13 22:51:32 +08:00
|
|
|
KEEP.utils.postHelper.goToComments()
|
|
|
|
KEEP.utils.postHelper.watchPostCommentsCount()
|
2022-10-11 12:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (KEEP.theme_config.pjax.enable === true && KEEP.utils) {
|
|
|
|
initToggleShowToc()
|
|
|
|
} else {
|
|
|
|
window.addEventListener('DOMContentLoaded', initToggleShowToc)
|
|
|
|
}
|