1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
import hljs from "highlight.js";
import { useEffect, useState } from "react";
import styled from "styled-components";
import { defaultBorderRadius, secondaryDark, vscBackground } from ".";
import { Clipboard, CheckCircle } from "@styled-icons/heroicons-outline";
import StyledCode from "./StyledCode";
const StyledPre = styled.pre`
overflow-y: scroll;
word-wrap: normal;
border: 0.5px solid gray;
border-radius: ${defaultBorderRadius};
background-color: ${secondaryDark};
position: relative;
`;
const CopyButtonDiv = styled.div`
position: absolute;
top: 4px;
right: 4px;
`;
const StyledCopyButton = styled.button<{ visible: boolean }>`
border: none;
background-color: transparent;
cursor: pointer;
visibility: ${(props) => (props.visible ? "visible" : "hidden")};
`;
function CopyButton(props: { textToCopy: string; visible: boolean }) {
const [hovered, setHovered] = useState<boolean>(false);
const [clicked, setClicked] = useState<boolean>(false);
return (
<>
<StyledCopyButton
onMouseEnter={() => {
setHovered(true);
}}
onMouseLeave={() => {
setHovered(false);
}}
visible={clicked || props.visible}
onClick={() => {
navigator.clipboard.writeText(props.textToCopy);
setClicked(true);
setTimeout(() => {
setClicked(false);
}, 2000);
}}
>
{clicked ? (
<CheckCircle color="#00ff00" size="1.5em" />
) : (
<Clipboard color={hovered ? "#00ff00" : "white"} size="1.5em" />
)}
</StyledCopyButton>
</>
);
}
function CodeBlock(props: { children: string; showCopy?: boolean }) {
const [result, setResult] = useState<AutoHighlightResult | undefined>(
undefined
);
const [highlightTimeout, setHighlightTimeout] = useState<
NodeJS.Timeout | undefined
>(undefined);
useEffect(() => {
// Don't highlight more than once every 100ms
if (highlightTimeout) {
return;
}
setHighlightTimeout(
setTimeout(() => {
const result = hljs.highlightAuto(props.children, [
"python",
"javascript",
"typescript",
"bash",
"html",
"css",
"json",
"yaml",
"markdown",
"sql",
"java",
"c",
"cpp",
"csharp",
"go",
"kotlin",
"php",
"ruby",
"rust",
"scala",
"swift",
"dart",
"haskell",
"perl",
"r",
"julia",
"objectivec",
"ocaml",
]);
setResult(result);
setHighlightTimeout(undefined);
}, 100)
);
}, [props.children]);
const [hovered, setHovered] = useState<boolean>(false);
return (
<StyledPre
onMouseEnter={() => {
setHovered(true);
}}
onMouseLeave={() => {
setHovered(false);
}}
>
<CopyButtonDiv>
<CopyButton
visible={hovered && (props.showCopy || true)}
textToCopy={props.children}
/>
</CopyButtonDiv>
<StyledCode language={result?.language}>{props.children}</StyledCode>
</StyledPre>
);
}
export default CodeBlock;
|