Skip to content

Commit d2d4941

Browse files
authored
Merge pull request #5 from btholt/copy-code-functionality
feature: copy code
2 parents 1139c34 + 04ff8d0 commit d2d4941

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

data/copyCode.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function createDOMElements() {
2+
const container = document.createElement("div");
3+
container.innerHTML =
4+
"<div class='tooltip-copy'><input type='submit' value='Copy' /></div>";
5+
container.className = "div-copy";
6+
return container;
7+
}
8+
9+
function attachCopyCodeFunctionality(div) {
10+
const elementsToClean = [];
11+
document
12+
.querySelectorAll("pre")
13+
.forEach(function createButtonAndAttachHandlers(pre) {
14+
let timeout = null;
15+
const copy = div.cloneNode(true);
16+
pre.appendChild(copy);
17+
elementsToClean.push(pre);
18+
19+
copy.onclick = function copyTextToClipboard() {
20+
navigator.clipboard.writeText(pre.textContent);
21+
copy.classList.add("clicked");
22+
clearTimeout(timeout);
23+
timeout = setTimeout(function hidePopup() {
24+
copy.classList.remove("clicked");
25+
}, 1500);
26+
};
27+
});
28+
29+
return elementsToClean;
30+
}
31+
32+
export default function createCopyCodeFunctionality() {
33+
const container = createDOMElements();
34+
return attachCopyCodeFunctionality(container);
35+
}

pages/lessons/[section]/[slug].js

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import { getLesson, getLessons } from "../../../data/lesson";
44
import getCourseConfig from "../../../data/course";
55
import Corner from "../../../components/corner";
66
import { Context } from "../../../context/headerContext";
7+
import createCopyCodeFunctionality from "../../../data/copyCode";
78

89
export default function LessonSlug({ post }) {
910
const courseInfo = getCourseConfig();
1011
const [_, setHeader] = useContext(Context);
12+
1113
useEffect(() => {
1214
setHeader({
1315
section: post.section,
1416
title: post.title,
1517
icon: post.icon,
1618
});
17-
return () => setHeader({});
19+
let elementsToClean = createCopyCodeFunctionality();
20+
return () => {
21+
setHeader({});
22+
elementsToClean = [];
23+
}
1824
}, []);
1925

2026
const title = post.title

styles/courses.css

+50-1
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,53 @@ pre code.hljs {
530530
pre code,
531531
pre code.hljs {
532532
display: block;
533-
}
533+
}
534+
535+
pre {
536+
position: relative;
537+
}
538+
539+
.div-copy {
540+
position: absolute;
541+
top: 0;
542+
right: 0;
543+
}
544+
545+
.div-copy .tooltip-copy::after {
546+
content: "";
547+
position: absolute;
548+
right: 100%;
549+
margin-right: -4px;
550+
top: 60%;
551+
transform: translateY(-50%);
552+
border-style: solid;
553+
border-width: 2px 2px 5px 8px;
554+
border-color: transparent transparent transparent #444;
555+
opacity: 0;
556+
transition: opacity .3s;
557+
}
558+
559+
.div-copy .tooltip-copy::before {
560+
content: "Copied";
561+
position: absolute;
562+
top: 60%;
563+
transform: translateY(-50%);
564+
right: 100%;
565+
margin-right: 5px;
566+
padding: 2px 7px;
567+
border-radius: 5px;
568+
background: #444;
569+
color: #fff;
570+
text-align: center;
571+
opacity: 0;
572+
transition: opacity .3s;
573+
}
574+
575+
.div-copy .tooltip-copy {
576+
margin-right: 0.3em;
577+
margin-top: 0.3em;
578+
}
579+
580+
.div-copy.clicked .tooltip-copy::before, .div-copy.clicked .tooltip-copy::after {
581+
opacity: 1;
582+
}

0 commit comments

Comments
 (0)