mirror of
https://github.com/ChenQihan666/MyBlog-Next.git
synced 2026-08-14 11:29:33 +08:00
refactor: update post detail script codes
Updates: - add optional chaining in possible null variables - remove unnecessary after-swap event listeners
This commit is contained in:
@@ -116,7 +116,7 @@ const layoutProps = {
|
|||||||
link.innerText = "#";
|
link.innerText = "#";
|
||||||
link.className = "heading-link hidden group-hover:inline-block ml-2";
|
link.className = "heading-link hidden group-hover:inline-block ml-2";
|
||||||
link.href = "#" + heading.id;
|
link.href = "#" + heading.id;
|
||||||
link.ariaHidden = true;
|
link.ariaHidden = "true";
|
||||||
heading.appendChild(link);
|
heading.appendChild(link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ const layoutProps = {
|
|||||||
codeBlock.appendChild(copyButton);
|
codeBlock.appendChild(copyButton);
|
||||||
|
|
||||||
// wrap codebock with relative parent element
|
// wrap codebock with relative parent element
|
||||||
codeBlock.parentNode.insertBefore(wrapper, codeBlock);
|
codeBlock?.parentNode?.insertBefore(wrapper, codeBlock);
|
||||||
wrapper.appendChild(codeBlock);
|
wrapper.appendChild(codeBlock);
|
||||||
|
|
||||||
copyButton.addEventListener("click", async () => {
|
copyButton.addEventListener("click", async () => {
|
||||||
@@ -150,9 +150,9 @@ const layoutProps = {
|
|||||||
|
|
||||||
async function copyCode(block, button) {
|
async function copyCode(block, button) {
|
||||||
let code = block.querySelector("code");
|
let code = block.querySelector("code");
|
||||||
let text = code.innerText;
|
let text = code?.innerText;
|
||||||
|
|
||||||
await navigator.clipboard.writeText(text);
|
await navigator.clipboard.writeText(text ?? "");
|
||||||
|
|
||||||
// visual feedback that task is completed
|
// visual feedback that task is completed
|
||||||
button.innerText = "Copied";
|
button.innerText = "Copied";
|
||||||
@@ -163,7 +163,6 @@ const layoutProps = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
attachCopyButtons();
|
attachCopyButtons();
|
||||||
document.addEventListener("astro:after-swap", attachCopyButtons);
|
|
||||||
|
|
||||||
/** Scrolls the document to the top when
|
/** Scrolls the document to the top when
|
||||||
* the "Back to Top" button is clicked. */
|
* the "Back to Top" button is clicked. */
|
||||||
@@ -174,5 +173,4 @@ const layoutProps = {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
backToTop();
|
backToTop();
|
||||||
document.addEventListener("astro:after-swap", backToTop);
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user