From a5806ad20a1ea7d059bf714bd919dc3cc6672ce6 Mon Sep 17 00:00:00 2001 From: XPoet Date: Tue, 5 Jan 2021 15:39:21 +0800 Subject: [PATCH] perf: optimize pjax --- layout/_partial/pjax/pjax.ejs | 12 ++++++------ source/js/header-shrink.js | 24 +++++++++++++----------- source/js/left-side-toggle.js | 2 +- source/js/toc.js | 9 ++++++--- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/layout/_partial/pjax/pjax.ejs b/layout/_partial/pjax/pjax.ejs index 95aefa4..cec107d 100644 --- a/layout/_partial/pjax/pjax.ejs +++ b/layout/_partial/pjax/pjax.ejs @@ -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')); }); diff --git a/source/js/header-shrink.js b/source/js/header-shrink.js index c1c286d..0a81128 100644 --- a/source/js/header-shrink.js +++ b/source/js/header-shrink.js @@ -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', () => { - document.body.classList.toggle('header-drawer-show'); + 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(); } diff --git a/source/js/left-side-toggle.js b/source/js/left-side-toggle.js index 80770b0..0132efa 100644 --- a/source/js/left-side-toggle.js +++ b/source/js/left-side-toggle.js @@ -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); diff --git a/source/js/toc.js b/source/js/toc.js index bd4357b..17d6d45 100644 --- a/source/js/toc.js +++ b/source/js/toc.js @@ -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);