|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {Children, cloneElement, ReactElement, useEffect, useRef, useState} from 'react'; |
| 4 | +import {Clipboard} from 'react-feather'; |
| 5 | +import styled from '@emotion/styled'; |
| 6 | + |
| 7 | +import {cleanCodeSnippet, useCleanSnippetInClipboard} from '../codeBlock'; |
| 8 | + |
| 9 | +type ChildrenItem = ReturnType<typeof Children.toArray>[number] | React.ReactNode; |
| 10 | + |
| 11 | +export function makeHighlightBlocks( |
| 12 | + children: React.ReactNode, |
| 13 | + language: string | undefined |
| 14 | +) { |
| 15 | + const items = Children.toArray(children); |
| 16 | + |
| 17 | + let highlightedLineElements: ReactElement[] = []; |
| 18 | + const highlightElementGroupCounter = 0; |
| 19 | + |
| 20 | + return items.reduce((arr: ChildrenItem[], child) => { |
| 21 | + if (typeof child !== 'object') { |
| 22 | + arr.push(child); |
| 23 | + return arr; |
| 24 | + } |
| 25 | + |
| 26 | + const element = child as ReactElement; |
| 27 | + const classes = element.props.className; |
| 28 | + |
| 29 | + const isCodeLine = classes && classes.includes('code-line'); |
| 30 | + if (!isCodeLine) { |
| 31 | + const updatedChild = cloneElement( |
| 32 | + child as ReactElement, |
| 33 | + {}, |
| 34 | + makeHighlightBlocks((child as ReactElement).props.children, language) |
| 35 | + ); |
| 36 | + arr.push(updatedChild); |
| 37 | + return arr; |
| 38 | + } |
| 39 | + |
| 40 | + const isHighlightedLine = isCodeLine && classes.includes('highlight-line'); |
| 41 | + |
| 42 | + if (isHighlightedLine) { |
| 43 | + highlightedLineElements.push(element); |
| 44 | + } else { |
| 45 | + if (highlightedLineElements.length > 0) { |
| 46 | + arr.push( |
| 47 | + <HighlightBlock groupId={highlightElementGroupCounter} language={language}> |
| 48 | + {...highlightedLineElements} |
| 49 | + </HighlightBlock> |
| 50 | + ); |
| 51 | + highlightedLineElements = []; |
| 52 | + } |
| 53 | + arr.push(child); |
| 54 | + } |
| 55 | + |
| 56 | + return arr; |
| 57 | + }, [] as ChildrenItem[]); |
| 58 | +} |
| 59 | + |
| 60 | +export function HighlightBlock({ |
| 61 | + children, |
| 62 | + groupId, |
| 63 | + language, |
| 64 | +}: { |
| 65 | + children: React.ReactNode; |
| 66 | + groupId: number; |
| 67 | + language: string | undefined; |
| 68 | +}) { |
| 69 | + const codeRef = useRef<HTMLDivElement>(null); |
| 70 | + |
| 71 | + useCleanSnippetInClipboard(codeRef, {language}); |
| 72 | + |
| 73 | + // Show the copy button after js has loaded |
| 74 | + // otherwise the copy button will not work |
| 75 | + const [showCopyButton, setShowCopyButton] = useState(false); |
| 76 | + useEffect(() => { |
| 77 | + setShowCopyButton(true); |
| 78 | + }, []); |
| 79 | + |
| 80 | + async function copyCodeOnClick() { |
| 81 | + if (codeRef.current === null) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const code = cleanCodeSnippet(codeRef.current.innerText, {language}); |
| 86 | + |
| 87 | + try { |
| 88 | + await navigator.clipboard.writeText(code); |
| 89 | + } catch (error) { |
| 90 | + // eslint-disable-next-line no-console |
| 91 | + console.error('Failed to copy:', error); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return ( |
| 96 | + <HighlightBlockContainer key={`highlight-block-${groupId}`}> |
| 97 | + <CodeLinesContainer ref={codeRef}>{children}</CodeLinesContainer> |
| 98 | + <ClipBoardContainer onClick={copyCodeOnClick}> |
| 99 | + {showCopyButton && <Clipboard size={14} opacity={70} />} |
| 100 | + </ClipBoardContainer> |
| 101 | + </HighlightBlockContainer> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +const HighlightBlockContainer = styled('div')` |
| 106 | + display: flex; |
| 107 | + flex-direction: row; |
| 108 | + justify-content: space-between; |
| 109 | + align-items: stretch; |
| 110 | + float: left; |
| 111 | + min-width: 100%; |
| 112 | + box-sizing: border-box; |
| 113 | + background-color: rgba(239, 239, 239, 0.06); |
| 114 | +`; |
| 115 | + |
| 116 | +const CodeLinesContainer = styled('div')` |
| 117 | + padding: 8px 0; |
| 118 | + width: calc(100% - 48px); |
| 119 | +`; |
| 120 | + |
| 121 | +const ClipBoardContainer = styled('div')` |
| 122 | + width: 48px; |
| 123 | + display: flex; |
| 124 | + justify-content: center; |
| 125 | + align-items: center; |
| 126 | + cursor: pointer; |
| 127 | + :hover { |
| 128 | + background-color: rgba(239, 239, 239, 0.1); |
| 129 | + } |
| 130 | + :active { |
| 131 | + background-color: rgba(239, 239, 239, 0.15); |
| 132 | + } |
| 133 | + transition: background-color 150ms linear; |
| 134 | +`; |
0 commit comments