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-29 23:54:39 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initSetPostLink() {
|
|
|
|
|
const postLinkContentDom = document.querySelector(
|
|
|
|
|
'.copyright-info-content .post-link .content'
|
|
|
|
|
)
|
|
|
|
|
postLinkContentDom && (postLinkContentDom.innerHTML = decodeURI(window.location.href))
|
2022-12-07 21:02:39 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
copyCopyrightInfo() {
|
|
|
|
|
const cicDom = document.querySelector('.copyright-info-content')
|
|
|
|
|
const copyDom = document.querySelector('.copy-copyright-info')
|
|
|
|
|
const copyIcon = copyDom.querySelector('i')
|
|
|
|
|
|
|
|
|
|
const ccLang = KEEP.language_copy_copyright
|
|
|
|
|
const colon = KEEP.hexo_config.language === 'en' ? ': ' : ':'
|
|
|
|
|
|
|
|
|
|
let isCopied = false
|
|
|
|
|
|
|
|
|
|
const setCopyDomContent = (class1, class2, content, copied) => {
|
|
|
|
|
if (copyIcon) {
|
|
|
|
|
copyIcon.classList.remove(class1)
|
|
|
|
|
copyIcon.classList.add(class2)
|
|
|
|
|
}
|
|
|
|
|
const tooltipDom = copyDom.querySelector('.tooltip-content')
|
|
|
|
|
tooltipDom && (tooltipDom.innerHTML = content)
|
|
|
|
|
isCopied = copied
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copyDom.addEventListener('click', () => {
|
|
|
|
|
if (!isCopied) {
|
|
|
|
|
const author = cicDom.querySelector('.post-author .content').innerHTML
|
|
|
|
|
const link = cicDom.querySelector('.post-link .content').innerHTML
|
|
|
|
|
const tgtTxt = `${ccLang.author}${colon}${author}\n${ccLang.link}${colon}${link}`
|
|
|
|
|
navigator.clipboard.writeText(tgtTxt).then(() => {
|
|
|
|
|
setCopyDomContent('fa-copy', 'fa-check', ccLang.copied, true)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
copyDom.addEventListener('mouseleave', () => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setCopyDomContent('fa-check', 'fa-copy', ccLang.copy, false)
|
|
|
|
|
}, 500)
|
|
|
|
|
})
|
2022-10-11 12:37:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-12 15:55:47 +08:00
|
|
|
|
KEEP.utils.postHelper.initSetPostToolsLeft()
|
2022-10-29 23:54:39 +08:00
|
|
|
|
if (KEEP.theme_config.toc?.enable === true) {
|
2022-10-29 00:16:15 +08:00
|
|
|
|
KEEP.utils.postHelper.initToggleToc()
|
|
|
|
|
}
|
2022-10-29 23:54:39 +08:00
|
|
|
|
if (KEEP.theme_config.comment?.enable === true) {
|
2022-10-29 00:16:15 +08:00
|
|
|
|
KEEP.utils.postHelper.goToComments()
|
|
|
|
|
KEEP.utils.postHelper.watchPostCommentsCount()
|
|
|
|
|
}
|
2022-10-29 23:54:39 +08:00
|
|
|
|
if (KEEP.theme_config.post?.copyright_info === true) {
|
|
|
|
|
KEEP.utils.postHelper.initSetPostLink()
|
2022-12-07 21:02:39 +08:00
|
|
|
|
KEEP.utils.postHelper.copyCopyrightInfo()
|
2022-10-29 23:54:39 +08:00
|
|
|
|
}
|
2022-10-11 12:37:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-29 23:54:39 +08:00
|
|
|
|
if (KEEP.theme_config.pjax?.enable === true && KEEP.utils) {
|
2022-10-11 12:37:06 +08:00
|
|
|
|
initToggleShowToc()
|
|
|
|
|
} else {
|
|
|
|
|
window.addEventListener('DOMContentLoaded', initToggleShowToc)
|
|
|
|
|
}
|