mirror of
https://github.com/multica-ai/multica.git
synced 2026-07-25 12:05:06 +02:00
navigator.clipboard is only exposed in a secure context (https or localhost). On self-hosted instances served over plain http:// it is undefined, so every copy / "copy all" / export button silently failed and left the clipboard empty (GitHub #3781). Add a shared copyText(text): Promise<boolean> helper in @multica/ui/lib/clipboard that prefers the async Clipboard API and falls back to a hidden <textarea> + document.execCommand('copy') for non-secure contexts. Migrate all direct navigator.clipboard.writeText call sites (code blocks, agent transcript copy-all, token / webhook / issue-link copy, etc.) to it, gating success side-effects on the returned boolean, and remove the now-redundant copyMarkdown wrapper. Secure-context users keep the native path unchanged. MUL-3068 Co-authored-by: J <j@multica.ai> Co-authored-by: multica-agent <github@multica.ai>
142 lines
4.7 KiB
TypeScript
142 lines
4.7 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* HtmlBlockPreview — readonly rendering of fenced ```html code blocks.
|
|
*
|
|
* Default view is "preview" (iframe) per the V2 plan; user can flip to
|
|
* "source" to see the highlighted markup and Copy it. Maximize opens the
|
|
* same iframe in a full-screen Dialog.
|
|
*
|
|
* Mounted by ReadonlyContent's `code` renderer for `lang === "html"`. The
|
|
* `pre` renderer in ReadonlyContent recognizes this component by reference
|
|
* and unwraps it from the default `<pre>` envelope, matching the same
|
|
* two-layer trick already used for MermaidDiagram.
|
|
*
|
|
* NOT used in the editable Tiptap NodeView — that path must keep
|
|
* `<NodeViewContent as="code" />` so the user can continue typing.
|
|
*/
|
|
|
|
import { useState } from "react";
|
|
import {
|
|
Check,
|
|
Code as CodeIcon,
|
|
Copy,
|
|
Eye,
|
|
Maximize2,
|
|
} from "lucide-react";
|
|
import { cn } from "@multica/ui/lib/utils";
|
|
import { copyText } from "@multica/ui/lib/clipboard";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
} from "@multica/ui/components/ui/dialog";
|
|
import { useT } from "../i18n";
|
|
import { CodeBlockStatic } from "./code-block-static";
|
|
import { HtmlPreviewBody } from "./html-preview-body";
|
|
|
|
const CODE_BLOCK_IFRAME_HEIGHT = "h-[480px]";
|
|
|
|
// Label shown in the code-block header. Not a translatable string — it's a
|
|
// language identifier (matches the `lang === "html"` token below).
|
|
const HTML_LANGUAGE_LABEL = "html";
|
|
|
|
interface HtmlBlockPreviewProps {
|
|
html: string;
|
|
className?: string;
|
|
}
|
|
|
|
export function HtmlBlockPreview({ html, className }: HtmlBlockPreviewProps) {
|
|
const { t } = useT("editor");
|
|
const [view, setView] = useState<"preview" | "source">("preview");
|
|
const [copied, setCopied] = useState(false);
|
|
const [fullscreen, setFullscreen] = useState(false);
|
|
|
|
const handleCopy = async () => {
|
|
if (!html) return;
|
|
if (await copyText(html)) {
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 2000);
|
|
}
|
|
};
|
|
|
|
const toggleView = () =>
|
|
setView((v) => (v === "preview" ? "source" : "preview"));
|
|
|
|
return (
|
|
<div className={cn("code-block-wrapper group/code relative my-2", className)}>
|
|
<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"
|
|
>
|
|
<span className="text-xs text-muted-foreground select-none">{HTML_LANGUAGE_LABEL}</span>
|
|
<button
|
|
type="button"
|
|
onClick={toggleView}
|
|
className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
|
|
title={
|
|
view === "preview"
|
|
? t(($) => $.code_block.show_source)
|
|
: t(($) => $.code_block.show_preview)
|
|
}
|
|
aria-label={
|
|
view === "preview"
|
|
? t(($) => $.code_block.show_source)
|
|
: t(($) => $.code_block.show_preview)
|
|
}
|
|
>
|
|
{view === "preview" ? (
|
|
<CodeIcon className="h-3.5 w-3.5" />
|
|
) : (
|
|
<Eye className="h-3.5 w-3.5" />
|
|
)}
|
|
</button>
|
|
{view === "preview" && (
|
|
<button
|
|
type="button"
|
|
onClick={() => setFullscreen(true)}
|
|
className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground hover:bg-muted hover:text-foreground transition-colors"
|
|
title={t(($) => $.code_block.fullscreen)}
|
|
aria-label={t(($) => $.code_block.fullscreen)}
|
|
>
|
|
<Maximize2 className="h-3.5 w-3.5" />
|
|
</button>
|
|
)}
|
|
<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={t(($) => $.code_block.copy_code)}
|
|
aria-label={t(($) => $.code_block.copy_code)}
|
|
>
|
|
{copied ? (
|
|
<Check className="h-3.5 w-3.5" />
|
|
) : (
|
|
<Copy className="h-3.5 w-3.5" />
|
|
)}
|
|
</button>
|
|
</div>
|
|
{view === "preview" ? (
|
|
<HtmlPreviewBody
|
|
source={{ kind: "inline", html }}
|
|
title="HTML preview"
|
|
className={CODE_BLOCK_IFRAME_HEIGHT}
|
|
/>
|
|
) : (
|
|
<CodeBlockStatic language="xml" body={html} />
|
|
)}
|
|
<Dialog open={fullscreen} onOpenChange={setFullscreen}>
|
|
<DialogContent
|
|
className="!max-w-6xl !h-[min(90vh,calc(100vh-2rem))] w-full p-0 gap-0 overflow-hidden"
|
|
aria-label={t(($) => $.code_block.fullscreen)}
|
|
>
|
|
<HtmlPreviewBody
|
|
source={{ kind: "inline", html }}
|
|
title="HTML preview"
|
|
className="h-full w-full"
|
|
iframeClassName="rounded-none border-0"
|
|
/>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
);
|
|
}
|