Skip to content

Commit

Permalink
🐛 Fix: take a dirty hack to fix the bug of APlayer and smoothScroll
Browse files Browse the repository at this point in the history
Fixed #292
  • Loading branch information
Lruihao committed Jul 24, 2024
1 parent 99dc099 commit dd33cab
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -1257,6 +1271,7 @@ class FixIt {
if (!this.config.encryption?.all) {
this.initToc();
this.initTocListener();
this.fixTocScroll();
}
this.onScroll();
this.onResize();
Expand Down

0 comments on commit dd33cab

Please sign in to comment.