diff --git a/.changeset/nervous-eels-look.md b/.changeset/nervous-eels-look.md new file mode 100644 index 00000000..96c8bb76 --- /dev/null +++ b/.changeset/nervous-eels-look.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Added copy button to code blocks. diff --git a/webview-ui/src/components/common/MarkdownBlock.tsx b/webview-ui/src/components/common/MarkdownBlock.tsx index bacf9dc2..c2b989c8 100644 --- a/webview-ui/src/components/common/MarkdownBlock.tsx +++ b/webview-ui/src/components/common/MarkdownBlock.tsx @@ -1,4 +1,4 @@ -import React, { memo, useEffect } from "react" +import React, { memo, useEffect, useRef, useState } from "react" import type { ComponentProps } from "react" import { useRemark } from "react-remark" import rehypeHighlight, { Options } from "rehype-highlight" @@ -91,15 +91,34 @@ const remarkPreventBoldFilenames = () => { } } +import { VSCodeButton } from "@vscode/webview-ui-toolkit/react" + +const CopyButton = styled(VSCodeButton)` + position: absolute; + top: 5px; + right: 5px; + z-index: 1; + opacity: 0; +` + +const CodeBlockContainer = styled.div` + position: relative; + + &:hover ${CopyButton} { + opacity: 1; + } +` + const StyledMarkdown = styled.div` pre { background-color: ${CODE_BLOCK_BG_COLOR}; border-radius: 3px; - margin: 13x 0; + margin: 13px 0; padding: 10px 10px; max-width: calc(100vw - 20px); overflow-x: auto; overflow-y: hidden; + padding-right: 70px; } pre > code { @@ -197,8 +216,42 @@ const StyledPre = styled.pre<{ theme: any }>` .join("")} ` +const PreWithCopyButton = ({ + children, + theme, + ...preProps +}: { theme: Record } & React.HTMLAttributes) => { + const preRef = useRef(null) + const [copied, setCopied] = useState(false) + + const handleCopy = () => { + if (preRef.current) { + const codeElement = preRef.current.querySelector("code") + const textToCopy = codeElement ? codeElement.textContent : preRef.current.textContent + + if (!textToCopy) return + navigator.clipboard.writeText(textToCopy).then(() => { + setCopied(true) + setTimeout(() => setCopied(false), 1500) + }) + } + } + + return ( + + + + + + {children} + + + ) +} + const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => { const { theme } = useExtensionState() + const [reactContent, setMarkdown] = useRemark({ remarkPlugins: [ remarkPreventBoldFilenames, @@ -223,7 +276,7 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => { ], rehypeReactOptions: { components: { - pre: ({ node, children, ...preProps }: any) => { + pre: ({ children, ...preProps }: React.HTMLAttributes) => { if (Array.isArray(children) && children.length === 1 && React.isValidElement(children[0])) { const child = children[0] as React.ReactElement<{ className?: string }> if (child.props?.className?.includes("language-mermaid")) { @@ -231,9 +284,9 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => { } } return ( - + {children} - + ) }, code: (props: ComponentProps<"code">) => { @@ -253,7 +306,7 @@ const MarkdownBlock = memo(({ markdown }: MarkdownBlockProps) => { }, [markdown, setMarkdown, theme]) return ( -
+
{reactContent}
)