From 7be993c0578f2e8ee7a16c409908a859675bb3af Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sat, 19 Oct 2024 23:54:09 +0530 Subject: [PATCH] feat: Add HTML preview to code blocks Adds an HTML preview feature to the code block component, allowing users to view the rendered output of their HTML code snippets. This improves the user experience by providing a more interactive and informative way to understand the code. --- src/components/Common/CodeBlock.tsx | 65 +++++++++++++++---- .../Option/Playground/PlaygroundChat.tsx | 2 +- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/components/Common/CodeBlock.tsx b/src/components/Common/CodeBlock.tsx index ea112f2..d48a699 100644 --- a/src/components/Common/CodeBlock.tsx +++ b/src/components/Common/CodeBlock.tsx @@ -1,6 +1,6 @@ -import { Tooltip } from "antd" -import { CheckIcon, ClipboardIcon } from "lucide-react" -import { FC, useState } from "react" +import { Tooltip, Modal } from "antd" +import { CheckIcon, ClipboardIcon, EyeIcon, Maximize2Icon } from "lucide-react" +import { FC, useState } from "react" import { useTranslation } from "react-i18next" import { Prism as SyntaxHighlighter } from "react-syntax-highlighter" import { coldarkDark } from "react-syntax-highlighter/dist/cjs/styles/prism" @@ -10,26 +10,47 @@ interface Props { value: string } -export const CodeBlock: FC =({ language, value }) => { +export const CodeBlock: FC = ({ language, value }) => { const [isBtnPressed, setIsBtnPressed] = useState(false) + const [previewVisible, setPreviewVisible] = useState(false) const { t } = useTranslation("common") + + const handleCopy = () => { + navigator.clipboard.writeText(value) + setIsBtnPressed(true) + setTimeout(() => { + setIsBtnPressed(false) + }, 4000) + } + + const handlePreview = () => { + setPreviewVisible(true) + } + + const handlePreviewClose = () => { + setPreviewVisible(false) + } + return ( <> -
+
{language}
+ {language.toLowerCase() === "html" && ( + + + + )}
+ {previewVisible && ( + +
+