File size: 1,800 Bytes
21d7fc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react';

export default function CopyCodeButton({ code }) {
  const [isCopied, setIsCopied] = useState(false);

  const handleCopyCode = async () => {
    try {
      // Replace local font with web-hosted OpenType font and adjust text positioning
      const modifiedCode = code
        .replace(
          /loadFont\(['"](?:\/)?fonts\/GoogleSans-Bold\.ttf['"]\)/,
          "loadFont('https://cdn.jsdelivr.net/npm/@fontsource/[email protected]/files/roboto-latin-700-normal.woff')"
        )
        .replace(
          /points = font\.textToPoints\(word, width\/2 - textW\/2, height\/2 \+ fontSize\/3, fontSize,/,
          "points = font.textToPoints(word, (width + textW) / 3.5, (height - fontSize) / 1.75, fontSize,"
        );
      
      await navigator.clipboard.writeText(modifiedCode);
      setIsCopied(true);
      setTimeout(() => setIsCopied(false), 2000);
    } catch (err) {
      console.error('Failed to copy code:', err);
    }
  };

  return (
    <button
      onClick={handleCopyCode}
      className="sticky bottom-4 flex items-center gap-2 px-3 py-2 bg-white rounded-xl shadow-sm hover:bg-gray-50 transition-colors"
      style={{
        position: 'sticky',
        bottom: '0rem',
        float: 'right',
        marginRight: '0rem',
        zIndex: 10
      }}
    >
      <svg
        width="16"
        height="16"
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      >
        <rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
        <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
      </svg>
      <span className="text-sm">{isCopied ? 'Copied!' : 'Copy Code'}</span>
    </button>
  );
}