From dd33cabe16a97fc62e899397385dd22f2a24f8b6 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Wed, 24 Jul 2024 15:30:27 +0800 Subject: [PATCH] :bug: Fix: take a dirty hack to fix the bug of APlayer and smoothScroll Fixed #292 --- assets/js/theme.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index d92c7e24..cb0150d2 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -499,16 +499,10 @@ class FixIt { * init table of contents */ initToc() { - let $tocCore = document.getElementById('TableOfContents'); + const $tocCore = document.getElementById('TableOfContents'); if ($tocCore === null) { return; } - // It's a dirty hack to fix the bug of APlayer, see https://github.com/hugo-fixit/FixIt/issues/292 - if (typeof APlayer === 'function') { - const $newTocCore = $tocCore.cloneNode(true); - $tocCore.parentElement.replaceChild($newTocCore, $tocCore); - $tocCore = $newTocCore; - } if (document.getElementById('toc-static').dataset.kept === 'true' || this.util.isTocStatic()) { const $tocContentStatic = document.getElementById('toc-content-static'); if ($tocCore.parentElement !== $tocContentStatic) { @@ -579,6 +573,25 @@ class FixIt { }, false); } + /** + * It's a dirty hack to fix the bug of APlayer and smoothScroll. + * see https://github.com/hugo-fixit/FixIt/issues/292 + */ + fixTocScroll() { + if (typeof APlayer === 'function') { + // remove APlayer click event listener of the toc link + let $tocCore = document.getElementById('TableOfContents'); + const $newTocCore = $tocCore.cloneNode(true); + $tocCore.parentElement.replaceChild($newTocCore, $tocCore); + $tocCore = $newTocCore; + // remove APlayer click event listener of the heading mark + this.util.forEach(document.querySelectorAll('.heading-mark'), ($headingMark) => { + const $newHeadingMark = $headingMark.cloneNode(true); + $headingMark.parentElement.replaceChild($newHeadingMark, $headingMark); + }); + } + } + initMath(target = document.body) { if (this.config.math) { renderMathInElement(target, this.config.math); @@ -1000,6 +1013,7 @@ class FixIt { this.initToc(); this.initTocListener(); this.initPangu(); + this.fixTocScroll(); this.util.forEach(document.querySelectorAll('.encrypted-hidden'), ($element) => { $element.classList.replace('encrypted-hidden', 'decrypted-shown'); }); @@ -1257,6 +1271,7 @@ class FixIt { if (!this.config.encryption?.all) { this.initToc(); this.initTocListener(); + this.fixTocScroll(); } this.onScroll(); this.onResize();