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', '.pjax',
], ],
history: true, history: true,
cacheBust: false,
currentUrlFullReload: false,
timeout: 0,
scrollTo: false,
scrollRestoration: false,
debug: false, debug: false,
cacheBust: false,
// analytics: false,
// currentUrlFullReload: false,
// scrollRestoration: false,
// scrollTo: true,
// timeout: 0,
}); });
window.addEventListener('pjax:complete', () => { window.addEventListener('pjax:complete', () => {
KEEP.refresh(); KEEP.refresh();
pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script')); pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
}); });

View File

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

View File

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

View File

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