Skip to content

Commit cae7815

Browse files
committed
fix: [scripts] Add null check in highlight wrapper
This change enables re-using highlight wrapper for components without the toolbar and copy buttons e.g. slides. Remove commented code in copy to clipboard.
1 parent 627101a commit cae7815

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

assets/scripts/copy.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ const hiTextBlock = document.querySelectorAll(".highlight-wrapper");
22

33
hiTextBlock.forEach(function (hiTextBlock) {
44
const hiToolbar = hiTextBlock.querySelector(".highlight-toolbar");
5+
if (!hiToolbar) return;
6+
57
const hiText = hiTextBlock.querySelector(".highlight");
8+
if (!hiText) return;
9+
610
const copyButton = hiToolbar.querySelector(".js-btn-copy-code");
11+
if (!copyButton) return;
12+
713
copyButton.classList.remove("hide");
814

915
/* Borrowed from adityatelange/hugo-PaperMod theme. */
@@ -16,15 +22,6 @@ hiTextBlock.forEach(function (hiTextBlock) {
1622

1723
/* copy code in pre > code blocks */
1824
copyButton.addEventListener("click", () => {
19-
/* Disabled to keep line numbers inline.
20-
// Use clipboard API if available.
21-
if ("clipboard" in navigator) {
22-
navigator.clipboard.writeText(hiText.innerText);
23-
copyingDone();
24-
return;
25-
}
26-
*/
27-
2825
// Fallback to selection and copy
2926
const range = document.createRange();
3027
range.selectNodeContents(hiText);

0 commit comments

Comments
 (0)