import { IconCheck, IconClipboard, IconDownload, IconCaretRight } from '@tabler/icons-react'; import { FC, memo, useEffect, useRef, useState } from 'react'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { oneDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'; import { useTranslation } from 'next-i18next'; import { generateRandomString, programmingLanguages, } from '@/utils/app/codeblock'; interface Props { language: string; value: string; } const IframeContent: FC = () => { useEffect(() => { window.addEventListener("message", ({ data: { code, language } }) => { if (language === 'html') { document.body.innerHTML = code; } else if (language === 'javascript') { eval(code); } }); }, []); return null; }; export const CodeBlock: FC = memo(({ language, value }) => { const { t } = useTranslation('markdown'); const [isCopied, setIsCopied] = useState(false); const [isRunning, setIsRunning] = useState(false); const iframeRef = useRef(null); const toggleRunCode = () => { setIsRunning(!isRunning); }; useEffect(() => { if (isRunning && iframeRef.current) { iframeRef.current.contentWindow?.postMessage({ code: value, language }, "*"); } }, [isRunning, value, language]); const copyToClipboard = () => { if (!navigator.clipboard || !navigator.clipboard.writeText) { return; } navigator.clipboard.writeText(value).then(() => { setIsCopied(true); setTimeout(() => { setIsCopied(false); }, 2000); }); }; const downloadAsFile = () => { const fileExtension = programmingLanguages[language] || '.file'; const suggestedFileName = `file-${generateRandomString( 3, true, )}${fileExtension}`; const fileName = window.prompt( t('Enter file name') || '', suggestedFileName, ); if (!fileName) { // user pressed cancel on prompt return; } const blob = new Blob([value], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.download = fileName; link.href = url; link.style.display = 'none'; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(url); }; return (
{language}
{(language === 'html' || language === 'javascript') && ( )}
{value} {isRunning && ( )}
); }); CodeBlock.displayName = 'CodeBlock';