File size: 2,803 Bytes
461e15a
 
d840d7c
 
461e15a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d840d7c
 
 
461e15a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { useEffect, useState } from "react";
import { Check, Copy, Download } from "lucide-react";
import { SHIKI_THEMES } from "#shared/shiki-langs";
import { getEditorHighlighter, loadLanguage } from "../../editor/editor-highlighter";

interface EmbedCodeViewProps {
  html: string;
  filename: string;
  /** Rendered instead of the code when set (diff view). */
  children?: React.ReactNode;
}

// Above this size Shiki takes long enough to stall typing in the chat.
const MAX_HIGHLIGHT_CHARS = 200_000;

export function EmbedCodeView({ html, filename, children }: EmbedCodeViewProps) {
  const [highlighted, setHighlighted] = useState<string | null>(null);
  const [copied, setCopied] = useState(false);

  useEffect(() => {
    let cancelled = false;
    setHighlighted(null);
    if (!html || html.length > MAX_HIGHLIGHT_CHARS) return;
    getEditorHighlighter()
      .then(async (hl) => {
        await loadLanguage(hl, "html");
        if (cancelled) return;
        setHighlighted(
          hl.codeToHtml(html, {
            lang: "html",
            themes: { light: SHIKI_THEMES.light, dark: SHIKI_THEMES.dark },
            defaultColor: false,
          }),
        );
      })
      .catch(() => {});
    return () => {
      cancelled = true;
    };
  }, [html]);

  const copy = async () => {
    try {
      await navigator.clipboard.writeText(html);
      setCopied(true);
      setTimeout(() => setCopied(false), 1500);
    } catch {
      // Clipboard blocked (permissions, insecure context): nothing to do.
    }
  };

  const download = () => {
    const blob = new Blob([html], { type: "text/html" });
    const url = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = filename || "embed.html";
    a.click();
    setTimeout(() => URL.revokeObjectURL(url), 1000);
  };

  return (
    <div className="es-code">
      <div className="es-code__actions">
        <span className="es-code__meta">
          {html ? `${html.split("\n").length} lines` : "No code yet"}
        </span>
        <button type="button" className="embed-btn" onClick={copy} disabled={!html}>
          {copied ? <Check size={13} /> : <Copy size={13} />}
          {copied ? "Copied" : "Copy"}
        </button>
        <button type="button" className="embed-btn" onClick={download} disabled={!html}>
          <Download size={13} />
          Download
        </button>
      </div>
      <div className="es-code__body">
        {children ??
          (highlighted ? (
            <div className="es-code__shiki" dangerouslySetInnerHTML={{ __html: highlighted }} />
          ) : (
            <pre className="es-preview__pre">
              <code>{html || "// No chart HTML yet"}</code>
            </pre>
          ))}
      </div>
    </div>
  );
}