From e9addc90ca7f3002602224f59472b671bcd98ef4 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 15 Jul 2020 14:36:37 +0800 Subject: [PATCH] Use `classList.replace` --- .github/ISSUE_TEMPLATE/bug-report.md | 2 +- .github/ISSUE_TEMPLATE/question.md | 2 +- source/js/next-boot.js | 3 +-- source/js/utils.js | 12 +++++++++--- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md index 3f71a37..75b71a3 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ b/.github/ISSUE_TEMPLATE/bug-report.md @@ -45,7 +45,7 @@ Please follow this Issue template to provide relevant information, such as sourc ``` ### Package dependencies Information - + ``` ``` diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index af6674c..c84a3b2 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -45,7 +45,7 @@ Please follow this Issue template to provide relevant information, such as sourc ``` ### Package dependencies Information - + ``` ``` diff --git a/source/js/next-boot.js b/source/js/next-boot.js index 8760529..64378da 100644 --- a/source/js/next-boot.js +++ b/source/js/next-boot.js @@ -59,8 +59,7 @@ NexT.boot.registerEvents = function() { translateY: [0, -20], complete : () => { // Prevent adding TOC to Overview if Overview was selected when close & open sidebar. - sidebar.classList.remove(activeClassName[1 - index]); - sidebar.classList.add(activeClassName[index]); + sidebar.classList.replace(activeClassName[1 - index], activeClassName[index]); window.anime({ duration, targets : panel[index], diff --git a/source/js/utils.js b/source/js/utils.js index c8cb2f4..68a30df 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -6,6 +6,14 @@ HTMLElement.prototype.wrap = function(wrapper) { wrapper.appendChild(this); }; +// https://caniuse.com/#feat=mdn-api_element_classlist_replace +if (typeof DOMTokenList.prototype.replace !== 'function') { + DOMTokenList.prototype.replace = function(remove, add) { + this.remove(remove); + this.add(add); + }; +} + NexT.utils = { /** @@ -66,9 +74,7 @@ NexT.utils = { document.querySelectorAll('figure.highlight').forEach(element => { element.querySelectorAll('.code .line span').forEach(span => { span.classList.forEach(name => { - // https://caniuse.com/#feat=mdn-api_element_classlist_replace - span.classList.remove(name); - span.classList.add(`hljs-${name}`); + span.classList.replace(name, `hljs-${name}`); }); }); if (!CONFIG.copycode) return;