All files / islands / CodeBlock.tsx

100.00% Branches 0/0
1.02% Lines 2/196
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


































































































































































































































































































































































import { useCopy } from "@hooks/useCopy.ts";
import { useEffect, useState } from "preact/hooks";

interface CodeBlockProps {
  // 支持两种模式:直接传入代码或文件路径
  code?: string;
  filePath?: string;
  language?: string;
  title?: string;
  showLineNumbers?: boolean;
  copyable?: boolean;
  className?: string;
}

export default function CodeBlock({
  code,
  filePath,
  language = "typescript",
  title,
  showLineNumbers = false,
  copyable = true,
  className = "",
}: CodeBlockProps) {
  const [fileContent, setFileContent] = useState<string>("");
  const [loading, setLoading] = useState<boolean>(false);
  const [loadError, setLoadError] = useState<string>("");

  // 获取实际要显示的代码内容
  const displayCode = code || fileContent;

  const { copy, copied, error } = useCopy({
    timeout: 2000,
    onSuccess: () => {
      console.log("代码已复制到剪贴板");
    },
    onError: (err) => {
      console.error("复制失败:", err);
    },
  });

  // 从文件路径加载代码内容
  useEffect(() => {
    if (filePath && !code) {
      setLoading(true);
      setLoadError("");

      // 使用fetch加载本地文件
      fetch(filePath)
        .then((response) => {
          if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${response.statusText}`);
          }
          return response.text();
        })
        .then((content) => {
          setFileContent(content);
          setLoading(false);
        })
        .catch((err) => {
          console.error("加载文件失败:", err);
          setLoadError(`加载文件失败: ${err.message}`);
          setFileContent(
            `// 无法加载文件: ${filePath}\n// 错误: ${err.message}`,
          );
          setLoading(false);
        });
    }
  }, [filePath, code]);

  // 自动检测语言类型(基于文件扩展名)
  const getLanguageFromPath = (path: string): string => {
    const ext = path.split(".").pop()?.toLowerCase();
    const languageMap: Record<string, string> = {
      "ts": "typescript",
      "tsx": "typescript",
      "js": "javascript",
      "jsx": "javascript",
      "py": "python",
      "rs": "rust",
      "go": "go",
      "java": "java",
      "cpp": "cpp",
      "c": "c",
      "css": "css",
      "scss": "scss",
      "html": "html",
      "json": "json",
      "md": "markdown",
      "yml": "yaml",
      "yaml": "yaml",
    };
    return languageMap[ext || ""] || language;
  };

  // 自动检测文件标题
  const getDisplayTitle = (): string => {
    if (title) return title;
    if (filePath) {
      return filePath.split("/").pop() || filePath;
    }
    return "";
  };

  // 自动检测语言
  const detectedLanguage = filePath ? getLanguageFromPath(filePath) : language;

  const handleCopy = () => {
    copy(displayCode);
  };

  const lines = displayCode.split("\n");

  return (
    <div
      className={`
      relative group
      bg-neutral-50 dark:bg-neutral-900 
      border border-neutral-200 dark:border-neutral-700
      rounded-2xl overflow-hidden
      shadow-colored hover:shadow-glow-lg
      transition-all duration-300
      ${className}
    `}
    >
      {/* 头部 */}
      {(getDisplayTitle() || copyable) && (
        <div className="
          flex items-center justify-between 
          px-4 py-3 
          bg-neutral-100 dark:bg-neutral-800
          border-b border-neutral-200 dark:border-neutral-700
        ">
          {getDisplayTitle() && (
            <div className="flex items-center gap-2">
              <div className="flex gap-1.5">
                <div className="w-3 h-3 bg-error-500 rounded-full"></div>
                <div className="w-3 h-3 bg-warning-500 rounded-full"></div>
                <div className="w-3 h-3 bg-success-500 rounded-full"></div>
              </div>
              <span className="text-sm font-medium text-neutral-700 dark:text-neutral-300 ml-2">
                {getDisplayTitle()}
              </span>
              <span className="text-xs text-neutral-600 dark:text-neutral-400 px-2 py-1 bg-neutral-200 dark:bg-neutral-700 rounded-md">
                {detectedLanguage}
              </span>
              {loading && (
                <span className="text-xs text-warning-600 dark:text-warning-400 px-2 py-1 bg-warning-100 dark:bg-warning-900/20 rounded-md animate-pulse">
                  加载中...
                </span>
              )}
              {loadError && (
                <span className="text-xs text-error-600 dark:text-error-400 px-2 py-1 bg-error-100 dark:bg-error-900/20 rounded-md">
                  加载失败
                </span>
              )}
            </div>
          )}

          {copyable && (
            <button
              onClick={handleCopy}
              disabled={loading || !displayCode}
              className="
                flex items-center gap-2 px-3 py-1.5
                text-neutral-600 dark:text-neutral-400 
                hover:text-neutral-900 dark:hover:text-white
                hover:bg-neutral-200 dark:hover:bg-neutral-700
                rounded-lg transition-all duration-200
                btn-animate magnetic-element cursor-none
                group/copy
                disabled:opacity-50 disabled:cursor-not-allowed
              "
              title={copied ? "已复制!" : "复制代码"}
            >
              {copied
                ? (
                  <>
                    <svg
                      className="w-4 h-4 text-success-400"
                      fill="currentColor"
                      viewBox="0 0 20 20"
                    >
                      <path
                        fillRule="evenodd"
                        d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                        clipRule="evenodd"
                      />
                    </svg>
                    <span className="text-xs text-success-400">已复制</span>
                  </>
                )
                : (
                  <>
                    <svg
                      className="w-4 h-4 group-hover/copy:scale-110 transition-transform"
                      fill="none"
                      stroke="currentColor"
                      viewBox="0 0 24 24"
                    >
                      <path
                        strokeLinecap="round"
                        strokeLinejoin="round"
                        strokeWidth="2"
                        d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"
                      />
                    </svg>
                    <span className="text-xs">复制</span>
                  </>
                )}
            </button>
          )}
        </div>
      )}

      {/* 代码内容 */}
      <div className="relative overflow-x-auto">
        {loading
          ? (
            // 加载状态
            <div className="p-8 text-center">
              <div className="inline-flex items-center gap-3 text-neutral-600 dark:text-neutral-400">
                <svg className="w-5 h-5 animate-spin" viewBox="0 0 24 24">
                  <circle
                    className="opacity-25"
                    cx="12"
                    cy="12"
                    r="10"
                    stroke="currentColor"
                    strokeWidth="4"
                    fill="none"
                  />
                  <path
                    className="opacity-75"
                    fill="currentColor"
                    d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
                  />
                </svg>
                <span>正在加载代码文件...</span>
              </div>
            </div>
          )
          : (
            <pre className="p-4 text-sm leading-relaxed">
            <code className="text-neutral-800 dark:text-neutral-100 font-mono">
              {showLineNumbers ? (
                <div className="flex">
                  <div className="
                    flex flex-col text-neutral-400 dark:text-neutral-500 text-xs
                    pr-4 border-r border-neutral-300 dark:border-neutral-700 mr-4
                    select-none
                  ">
                    {lines.map((_, index) => (
                      <span key={index} className="leading-relaxed">
                        {(index + 1).toString().padStart(2, ' ')}
                      </span>
                    ))}
                  </div>

                  <div className="flex-1 min-w-0">
                    {lines.map((line, index) => (
                      <div key={index} className="leading-relaxed">
                        {line || "\u00A0"}
                      </div>
                    ))}
                  </div>
                </div>
              ) : (
                <div className="whitespace-pre-wrap">
                  {displayCode || "// 暂无代码内容"}
                </div>
              )}
            </code>
            </pre>
          )}

        {/* 复制成功提示 */}
        {copied && (
          <div className="
            absolute top-2 right-2
            px-3 py-1.5 bg-success-500 text-white
            rounded-lg text-xs font-medium
            animate-fade-in shadow-lg
          ">
            ✓ 已复制到剪贴板
          </div>
        )}

        {/* 错误提示 */}
        {error && (
          <div className="
            absolute top-2 right-2
            px-3 py-1.5 bg-error-500 text-white
            rounded-lg text-xs font-medium
            animate-fade-in shadow-lg
          ">
            ✗ 复制失败
          </div>
        )}
      </div>

      {/* 装饰性渐变边框 */}
      <div className="
        absolute inset-0 rounded-2xl pointer-events-none
        bg-gradient-to-r from-primary-500/10 via-secondary-500/10 to-accent-500/10
        opacity-0 group-hover:opacity-100 transition-opacity duration-300
      ">
      </div>
    </div>
  );
}

// 简单的语法高亮函数 - 支持浅色和深色模式
function highlightSyntax(code: string, language: string): string {
  // 基础的语法高亮,可以根据需要扩展
  let highlighted = code;

  if (language === "typescript" || language === "javascript") {
    // 关键字 - 使用CSS变量支持主题切换
    highlighted = highlighted.replace(
      /\b(const|let|var|function|return|if|else|for|while|class|interface|type|import|export|from|default|async|await|try|catch|finally)\b/g,
      '<span class="text-purple-600 dark:text-purple-400 font-semibold">$1</span>',
    );

    // 字符串
    highlighted = highlighted.replace(
      /(["'`])((?:\\.|(?!\1)[^\\])*?)\1/g,
      '<span class="text-green-600 dark:text-green-400">$1$2$1</span>',
    );

    // 注释
    highlighted = highlighted.replace(
      /\/\/.*$/gm,
      '<span class="text-neutral-500 dark:text-neutral-400 italic">$&</span>',
    );

    // 数字
    highlighted = highlighted.replace(
      /\b\d+\.?\d*\b/g,
      '<span class="text-orange-600 dark:text-orange-400">$&</span>',
    );

    // 函数调用
    highlighted = highlighted.replace(
      /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*(?=\()/g,
      '<span class="text-blue-600 dark:text-blue-400">$1</span>',
    );

    // 对象属性
    highlighted = highlighted.replace(
      /\.([a-zA-Z_$][a-zA-Z0-9_$]*)/g,
      '.<span class="text-cyan-600 dark:text-cyan-400">$1</span>',
    );
  }

  return highlighted;
}