hexo-theme-keep/source/js/toggle-show-toc.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

/* global KEEP */
function initToggleShowToc() {
KEEP.utils.toggleShowTocHandler = {
postPageContainerDom: document.querySelector('.post-page-container'),
toggleShowTocBtnDom: document.querySelector('.toggle-show-toc'),
toggleShowTocIcon: document.querySelector('.toggle-show-toc i'),
isShowToc: false,
initToggle() {
this.toggleShowTocBtnDom &&
2022-10-11 12:42:11 +08:00
this.toggleShowTocBtnDom.addEventListener('click', () => {
this.isShowToc = !this.isShowToc
KEEP.styleStatus.isShowToc = this.isShowToc
KEEP.setStyleStatus()
this.handleToggle(this.isShowToc)
})
},
handleToggle(isOpen) {
if (isOpen) {
this.postPageContainerDom.classList.add('show-toc')
document.body.classList.add('has-toc')
} else {
this.postPageContainerDom.classList.remove('show-toc')
document.body.classList.remove('has-toc')
}
},
hasToc(isOpen) {
this.toggleShowTocBtnDom.style.display = 'flex'
this.isShowToc = isOpen
this.handleToggle(isOpen)
}
}
KEEP.utils.toggleShowTocHandler.initToggle()
}
if (KEEP.theme_config.pjax.enable === true && KEEP.utils) {
initToggleShowToc()
} else {
window.addEventListener('DOMContentLoaded', initToggleShowToc)
}