"use client"; // Rendering helpers for the formal Israeli citation ("כללי הציטוט האחיד"). // // Backend stores the citation as Markdown: parties' names wrapped in // **double asterisks**, everything else regular. These helpers: // 1. Render the citation with for the bold ranges. // 2. Copy it to the clipboard as BOTH text/html (so Word/Docs paste // with bold preserved) and text/plain (which keeps the markers // so the markdown survives a plain-text paste). import { useState } from "react"; import { Check, Copy } from "lucide-react"; import { toast } from "sonner"; function parseSegments(md: string): Array<{ bold: boolean; text: string }> { const out: Array<{ bold: boolean; text: string }> = []; const re = /\*\*([^*]+)\*\*/g; let last = 0; let m: RegExpExecArray | null; while ((m = re.exec(md)) !== null) { if (m.index > last) out.push({ bold: false, text: md.slice(last, m.index) }); out.push({ bold: true, text: m[1] }); last = re.lastIndex; } if (last < md.length) out.push({ bold: false, text: md.slice(last) }); return out; } function escapeHtml(s: string): string { return s .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } export function FormattedCitation({ citation, className, }: { citation: string; className?: string; }) { const segments = parseSegments(citation); return ( {segments.map((s, i) => s.bold ? ( {s.text} ) : ( {s.text} ), )} ); } export function CitationCopyButton({ citation, size = "sm", }: { citation: string; size?: "sm" | "xs"; }) { const [copied, setCopied] = useState(false); async function handleCopy() { const segments = parseSegments(citation); const html = segments .map((s) => s.bold ? `${escapeHtml(s.text)}` : escapeHtml(s.text), ) .join(""); const wrappedHtml = `${html}`; try { const cb = navigator.clipboard; if (typeof ClipboardItem !== "undefined" && cb && "write" in cb) { const item = new ClipboardItem({ "text/html": new Blob([wrappedHtml], { type: "text/html" }), "text/plain": new Blob([citation], { type: "text/plain" }), }); await cb.write([item]); } else { await cb.writeText(citation); } setCopied(true); toast.success("המראה מקום הועתק (עם הדגשה לצדדים)"); window.setTimeout(() => setCopied(false), 1800); } catch (err) { console.error("citation copy failed", err); toast.error("העתקה נכשלה"); } } const dims = size === "xs" ? "h-7 w-7" : "h-8 w-8"; return ( ); }