Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat: add copy button to readonly code blocks (#4448)
Bohan Jianga5636f0ff42a
diff --git a/packages/views/editor/readonly-content.test.tsx b/packages/views/editor/readonly-content.test.tsxindex 0a00706dee..8e30d71b18 100644--- a/packages/views/editor/readonly-content.test.tsx+++ b/packages/views/editor/readonly-content.test.tsx@@ -276,6 +276,24 @@ describe("ReadonlyContent code styling", () => {expect(blockCode?.textContent?.trim()).toBe(token);});+ it("copies the whole fenced code block from the readonly toolbar", async () => {+ const writeText = vi.fn().mockResolvedValue(undefined);+ Object.defineProperty(navigator, "clipboard", {+ configurable: true,+ value: { writeText },+ });+ const source = ["pnpm install", "pnpm test"].join("\n");+ const { getByRole } = render(+ <ReadonlyContent content={["```bash", source, "```"].join("\n")} />,+ );++ fireEvent.click(getByRole("button", { name: "Copy code" }));++ await waitFor(() => {+ expect(writeText).toHaveBeenCalledWith(source);+ });+ });+it("keeps editor code literal by disabling font ligatures", () => {const codeCss = readFileSync("editor/styles/code.css", "utf8");diff --git a/packages/views/editor/readonly-content.tsx b/packages/views/editor/readonly-content.tsxindex d6846d5c3b..8aa255e5e7 100644--- a/packages/views/editor/readonly-content.tsx+++ b/packages/views/editor/readonly-content.tsx@@ -16,11 +16,12 @@* - Rendering mentions with the same IssueMentionCard component and .mention class*/-import { isValidElement, memo, useMemo, useRef } from "react";+import { isValidElement, memo, useMemo, useRef, useState } from "react";import ReactMarkdown, {defaultUrlTransform,type Components,} from "react-markdown";+import type { ReactNode } from "react";import rehypeKatex from "rehype-katex";import remarkBreaks from "remark-breaks";import remarkGfm from "remark-gfm";@@ -29,9 +30,12 @@ import rehypeRaw from "rehype-raw";import rehypeSanitize, { defaultSchema } from "rehype-sanitize";import { createLowlight, common } from "lowlight";import { toHtml } from "hast-util-to-html";+import { Check, Copy } from "lucide-react";import { cn } from "@multica/ui/lib/utils";+import { copyText } from "@multica/ui/lib/clipboard";import { useWorkspacePaths, useWorkspaceSlug } from "@multica/core/paths";import type { Attachment } from "@multica/core/types";+import { useT } from "../i18n";import { useNavigation } from "../navigation";import { IssueMentionCard } from "../issues/components/issue-mention-card";import { ProjectChip } from "../projects/components/project-chip";@@ -156,6 +160,56 @@ function ProjectMentionLink({ projectId, label }: { projectId: string; label?: s);}+function getTextContent(node: ReactNode): string {+ if (node == null || typeof node === "boolean") return "";+ if (typeof node === "string" || typeof node === "number") return String(node);+ if (Array.isArray(node)) return node.map(getTextContent).join("");+ if (isValidElement(node)) {+ const props = node.props as { children?: ReactNode };+ return getTextContent(props.children);+ }+ return "";+}++function ReadonlyCodeBlock({ children }: { children: ReactNode }) {+ const { t } = useT("editor");+ const [copied, setCopied] = useState(false);+ const code = useMemo(+ () => getTextContent(children).replace(/\n$/, ""),+ [children],+ );+ const copyLabel = t(($) => $.code_block.copy_code) || "Copy code";++ const handleCopy = async () => {+ if (!code) return;+ if (await copyText(code)) {+ setCopied(true);+ setTimeout(() => setCopied(false), 2000);+ }+ };++ return (+ <div className="code-block-wrapper group/code relative my-3">+ <div className="absolute top-0 right-0 z-10 flex items-center gap-1.5 px-2 py-1.5 opacity-0 transition-opacity group-hover/code:opacity-100 focus-within:opacity-100">+ <button+ type="button"+ onClick={handleCopy}+ className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"+ title={copyLabel}+ aria-label={copyLabel}+ >+ {copied ? (+ <Check className="h-3.5 w-3.5" />+ ) : (+ <Copy className="h-3.5 w-3.5" />+ )}+ </button>+ </div>+ <pre className="!m-0 pr-12">{children}</pre>+ </div>+ );+}+// Named component so it can call useWorkspaceSlug() — arrow function inlined// inside `components` below would still work, but extracting it keeps the// hook usage explicit and avoids hook-in-object-literal surprises.@@ -304,7 +358,7 @@ function buildComponents(): Partial<Components> {);},- // Pre — pass through (CSS handles styling via .rich-text-editor pre).+ // Pre — wrap regular code fences with copy chrome.// Special-case Mermaid / HtmlBlockPreview returned from the `code`// renderer above so the outer `<pre>` does not wrap them — this is the// standard two-layer pattern used to escape react-markdown's default@@ -325,7 +379,7 @@ function buildComponents(): Partial<Components> {return <>{children}</>;}}- return <pre>{children}</pre>;+ return <ReadonlyCodeBlock>{children}</ReadonlyCodeBlock>;},};}