File tree 3 files changed +92
-2
lines changed
3 files changed +92
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -4,17 +4,23 @@ import { getLesson, getLessons } from "../../../data/lesson";
4
4
import getCourseConfig from "../../../data/course" ;
5
5
import Corner from "../../../components/corner" ;
6
6
import { Context } from "../../../context/headerContext" ;
7
+ import createCopyCodeFunctionality from "../../../data/copyCode" ;
7
8
8
9
export default function LessonSlug ( { post } ) {
9
10
const courseInfo = getCourseConfig ( ) ;
10
11
const [ _ , setHeader ] = useContext ( Context ) ;
12
+
11
13
useEffect ( ( ) => {
12
14
setHeader ( {
13
15
section : post . section ,
14
16
title : post . title ,
15
17
icon : post . icon ,
16
18
} ) ;
17
- return ( ) => setHeader ( { } ) ;
19
+ let elementsToClean = createCopyCodeFunctionality ( ) ;
20
+ return ( ) => {
21
+ setHeader ( { } ) ;
22
+ elementsToClean = [ ] ;
23
+ }
18
24
} , [ ] ) ;
19
25
20
26
const title = post . title
Original file line number Diff line number Diff line change @@ -530,4 +530,53 @@ pre code.hljs {
530
530
pre code ,
531
531
pre code .hljs {
532
532
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
+ }
You can’t perform that action at this time.
0 commit comments