perf: optimize pjax

This commit is contained in:
XPoet 2021-01-05 15:39:21 +08:00
parent 0ea7753fe7
commit a5806ad20a
4 changed files with 26 additions and 21 deletions

View File

@ -8,16 +8,16 @@
'.pjax',
],
history: true,
cacheBust: false,
currentUrlFullReload: false,
timeout: 0,
scrollTo: false,
scrollRestoration: false,
debug: false,
cacheBust: false,
// analytics: false,
// currentUrlFullReload: false,
// scrollRestoration: false,
// scrollTo: true,
// timeout: 0,
});
window.addEventListener('pjax:complete', () => {
KEEP.refresh();
pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
});

View File

@ -20,19 +20,21 @@ KEEP.initHeaderShrink = () => {
},
initMenuBarButton() {
document.querySelector('.menu-bar').addEventListener('click', () => {
document.body.classList.toggle('header-drawer-show');
});
},
toggleHeaderDrawerShow() {
const domList = [document.querySelector('.window-mask'), document.querySelector('.menu-bar')];
initWindowMask() {
document.querySelector('.window-mask').addEventListener('click', () => {
if (KEEP.theme_config.pjax.enable === true) {
domList.push(...document.querySelectorAll('.header-drawer .drawer-menu-list .drawer-menu-item'));
}
domList.forEach(v => {
v.addEventListener('click', () => {
document.body.classList.toggle('header-drawer-show');
});
},
});
}
}
KEEP.utils.headerShrink.init();
KEEP.utils.headerShrink.initMenuBarButton();
KEEP.utils.headerShrink.initWindowMask();
KEEP.utils.headerShrink.headerShrink();
KEEP.utils.headerShrink.toggleHeaderDrawerShow();
}

View File

@ -37,7 +37,7 @@ function initLeftSideToggle() {
KEEP.utils.leftSideToggle.initToggleBarButton();
}
if (KEEP.theme_config.pjax && KEEP.utils) {
if (KEEP.theme_config.pjax.enable === true && KEEP.utils) {
initLeftSideToggle();
} else {
window.addEventListener('DOMContentLoaded', initLeftSideToggle);

View File

@ -2,6 +2,7 @@ function initTOC() {
KEEP.utils.navItems = document.querySelectorAll('.post-toc-wrap .post-toc li');
if (KEEP.utils.navItems.length > 0) {
KEEP.utils = {
...KEEP.utils,
@ -16,7 +17,7 @@ function initTOC() {
} else if (index > 0) {
index--;
}
KEEP.utils.activateNavByIndex(index);
this.activateNavByIndex(index);
},
registerSidebarTOC() {
@ -41,7 +42,7 @@ function initTOC() {
});
},
activateNavByIndex: function (index) {
activateNavByIndex(index) {
const target = document.querySelectorAll('.post-toc li a.nav-link')[index];
if (!target || target.classList.contains('active-current')) return;
@ -70,14 +71,16 @@ function initTOC() {
KEEP.utils.leftSideToggle.changePageLayoutWhenOpenToggle(KEEP.utils.leftSideToggle.isOpenPageAside);
}
}
KEEP.utils.showPageAsideWhenHasTOC();
KEEP.utils.registerSidebarTOC();
} else {
KEEP.utils.pageContainer_dom.removeChild(document.querySelector('.page-aside'));
}
}
if (KEEP.theme_config.pjax && KEEP.utils) {
if (KEEP.theme_config.pjax.enable === true && KEEP.utils) {
initTOC();
} else {
window.addEventListener('DOMContentLoaded', initTOC);