-
-
Notifications
You must be signed in to change notification settings - Fork 791
/
Copy pathCodeBlock.tsx
340 lines (327 loc) · 9.66 KB
/
CodeBlock.tsx
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { useState } from 'react';
import Highlight from 'react-syntax-highlighter';
import Caption from '../Caption';
import IconClipboard from '../icons/Clipboard';
interface CodeBlockProps {
children?: string;
codeBlocks?: { code: string; title?: string; language?: string }[];
className?: string;
highlightClassName?: string;
highlightedLines?: number[];
language?: string;
hasWindow?: boolean;
showCopy?: boolean;
showCaption?: boolean;
caption?: string;
showLineNumbers?: boolean;
startingLineNumber?: number;
textSizeClassName?: string;
title?: string;
}
interface Theme {
[key: string]: {
display?: string;
background?: string;
color?: string;
fontWeight?: string | number;
backgroundColor?: string;
fontStyle?: string;
textDecoration?: string;
};
}
const theme: Theme = {
hljs: {
display: 'inline-block',
background: '#252f3f',
color: '#c0e2a3'
},
'hljs-subst': {
color: '#d6deeb'
},
'hljs-selector-tag': {
color: '#ff6363'
},
'hljs-selector-id': {
color: '#fad430',
fontWeight: 'bold'
},
'hljs-selector-class': {
color: '#7edcda'
},
'hljs-selector-attr': {
color: '#7edcda'
},
'hljs-selector-pseudo': {
color: '#74e287'
},
'hljs-addition': {
backgroundColor: 'rgba(163, 190, 140, 0.5)'
},
'hljs-deletion': {
backgroundColor: 'rgba(191, 97, 106, 0.5)'
},
'hljs-built_in': {
color: '#7edcda'
},
'hljs-type': {
color: '#7edcda'
},
'hljs-class': {
color: '#7edcda'
},
'hljs-function': {
color: '#74e287'
},
'hljs-function > .hljs-title': {
color: '#74e287'
},
'hljs-keyword': {
color: '#64a0dc'
},
'hljs-literal': {
color: '#64a0dc'
},
'hljs-symbol': {
color: '#64a0dc'
},
'hljs-number': {
color: '#d8da68'
},
'hljs-regexp': {
color: '#EBCB8B'
},
'hljs-string': {
color: '#c0e2a3',
fontWeight: '500'
},
'hljs-title': {
color: '#7edcda'
},
'hljs-params': {
color: '#d6deeb'
},
'hljs-bullet': {
color: '#64a0dc'
},
'hljs-code': {
color: '#7edcda'
},
'hljs-emphasis': {
fontStyle: 'italic'
},
'hljs-formula': {
color: '#7edcda'
},
'hljs-strong': {
fontWeight: 'bold'
},
'hljs-link:hover': {
textDecoration: 'underline'
},
'hljs-quote': {
color: '#797f8c'
},
'hljs-comment': {
color: '#797f8c'
},
'hljs-doctag': {
color: '#7edcda'
},
'hljs-$ref': {
color: 'yellow'
},
'hljs-meta': {
color: '#5E81AC'
},
'hljs-meta-keyword': {
color: '#5E81AC'
},
'hljs-meta-string': {
color: '#c0e2a3'
},
'hljs-attr': {
color: '#7edcda'
},
'hljs-attribute': {
color: '#d6deeb'
},
'hljs-builtin-name': {
color: '#64a0dc'
},
'hljs-name': {
color: '#64a0dc'
},
'hljs-section': {
color: '#74e287'
},
'hljs-tag': {
color: '#64a0dc'
},
'hljs-variable': {
color: '#d6deeb'
},
'hljs-template-variable': {
color: '#d6deeb'
},
'hljs-template-tag': {
color: '#5E81AC'
},
'yaml .hljs-meta': {
color: '#D08770'
}
};
/**
* @description This component displays code snippets with syntax highlighting.
*
* @param {CodeBlockProps} props - The component props.
* @param {string} props.children - The code snippet to be displayed.
* @param {Array<{ code: string; title?: string; language?: string }>} props.codeBlocks - An array of code blocks
* with optional titles and languages.
* @param {string} props.className - Additional CSS class for styling the CodeBlock component.
* @param {string} props.highlightClassName - Additional CSS class for styling the code highlighting area.
* @param {number[]} props.highlightedLines - An array of line numbers to be highlighted.
* @param {string} props.language - The programming language for syntax highlighting (default is 'yaml').
* @param {boolean} props.hasWindow - Indicates whether window controls should be displayed.
* @param {boolean} props.showCopy - Indicates whether the copy-to-clipboard button should be displayed.
* @param {boolean} props.showCaption - Indicates whether the caption should be displayed.
* @param {string} props.caption - The caption text to be displayed.
* @param {boolean} props.showLineNumbers - Indicates whether line numbers should be displayed.
* @param {number} props.startingLineNumber - The starting line number for line numbering.
* @param {string} props.textSizeClassName - Additional CSS class for controlling the text size.
* @param {string} props.title - The title of the code block (default is the specified language).
*/
export default function CodeBlock({
children = '',
codeBlocks,
className = '',
highlightClassName = '',
highlightedLines,
language = 'yaml',
hasWindow = false,
showCopy = true,
showCaption = true,
caption = '',
showLineNumbers = true,
startingLineNumber = 1,
textSizeClassName = 'text-xs'
}: CodeBlockProps): React.ReactNode {
const [activeBlock, setActiveBlock] = useState<number>(0);
const [showIsCopied, setShowIsCopied] = useState<boolean>(false);
// eslint-disable-next-line no-param-reassign
codeBlocks = codeBlocks && codeBlocks.length ? codeBlocks : [{ code: children.replace(/\n$/, '') }];
const tabItemsCommonClassNames =
'inline-block border-teal-300 py-1 px-2 mx-px cursor-pointer hover:text-teal-300 font-bold';
const tabItemsClassNames = `${tabItemsCommonClassNames} text-gray-300`;
const tabItemsActiveClassNames = `${tabItemsCommonClassNames} text-teal-300 border-b-2`;
/**
* @description This function handles the copy button click event by copying the active code block to the clipboard.
*/
function onClickCopy() {
// check if navigator with clipboard exists (fallback for older browsers)
if (navigator && navigator.clipboard && codeBlocks && codeBlocks[activeBlock]) {
navigator.clipboard.writeText(codeBlocks[activeBlock].code).then(() => {
setShowIsCopied(true);
setTimeout(() => {
setShowIsCopied(false);
}, 2000);
});
}
}
/**
* @description This function renders the syntax-highlighted code blocks.
*/
function renderHighlight() {
return (
<div className='h-full max-h-screen'>
{codeBlocks && codeBlocks.length > 1 && (
<div className='pb-3 pl-1 pt-0 text-xs'>
<nav>
<ul>
{codeBlocks?.map((block, index) => (
<li
key={index}
className={activeBlock === index ? tabItemsActiveClassNames : tabItemsClassNames}
onClick={() => setActiveBlock(index)}
>
{block.title || block.language}
</li>
))}
</ul>
</nav>
</div>
)}
<div className={`relative overflow-y-auto pr-8 ${highlightClassName}`}>
<Highlight
className={`pb-2 pt-px text-sm font-medium font-ligatures-contextual
${showLineNumbers ? 'ml-0' : 'ml-3'} ${textSizeClassName}`}
language={codeBlocks && codeBlocks[activeBlock].language ? codeBlocks[activeBlock].language : language}
style={theme}
showLineNumbers={showLineNumbers}
startingLineNumber={startingLineNumber}
lineNumberContainerStyle={{
paddingLeft: '0.5em',
background: '#252f3f'
}}
lineNumberStyle={(lineNumber: number) => {
const isHighlighted = highlightedLines && highlightedLines.includes(lineNumber);
return {
display: 'inline-block',
marginLeft: '16px',
paddingRight: '16px',
background: isHighlighted ? '#252f3f' : 'inherit',
color: isHighlighted ? '#A3ACAD' : '#8B9394'
};
}}
wrapLines={true}
lineProps={(lineNumber: number) => {
const isHighlighted = highlightedLines && highlightedLines.includes(lineNumber);
return {
className: `${isHighlighted ? 'bg-code-editor-dark-highlight block ml-10 w-full' : ''} pr-8`
};
}}
codeTagProps={{
className: 'mr-8'
}}
>
{codeBlocks ? [codeBlocks[activeBlock].code] : ''}
</Highlight>
</div>
</div>
);
}
return (
<>
<div className={`relative z-10 my-8 max-w-full overflow-auto rounded bg-code-editor-dark pt-2 ${className}`}>
{hasWindow && (
<div className='pb-2 pl-4'>
<span className='mr-2 inline-block size-2.5 rounded-full bg-mac-window-close'></span>
<span className='mr-2 inline-block size-2.5 rounded-full bg-mac-window-minimize'></span>
<span className='mr-2 inline-block size-2.5 rounded-full bg-mac-window-maximize'></span>
</div>
)}
{showCopy && (
<div className='z-10'>
<button
onClick={onClickCopy}
className='absolute right-2 top-1 z-50 cursor-pointer bg-code-editor-dark text-xs
text-gray-500 hover:text-gray-300 focus:outline-none'
title='Copy to clipboard'
data-test='copy-button'
>
{showIsCopied && (
<span className='mr-2 inline-block pl-2 pt-1' data-testid='clicked-text'>
Copied!
</span>
)}
<span className='inline-block pt-1'>
<IconClipboard className='-mt-0.5 inline-block size-4' />
</span>
</button>
</div>
)}
{renderHighlight()}
</div>
{showCaption && caption && <Caption>{caption}</Caption>}
</>
);
}