forked from shuyu-labs/WebCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePreviewModal.razor
More file actions
639 lines (585 loc) · 28.1 KB
/
CodePreviewModal.razor
File metadata and controls
639 lines (585 loc) · 28.1 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components
@using WebCodeCli.Helpers
@inject IJSRuntime JSRuntime
@inject NavigationManager NavigationManager
@if (_isVisible)
{
<div class="fixed inset-0 z-[80] overflow-hidden" @onclick="HandleBackdropClick">
<div class="flex items-center justify-center min-h-screen px-4 pt-4 pb-20 text-center sm:block sm:p-0">
<!-- 背景遮罩 -->
<div class="fixed inset-0 transition-opacity bg-black/50 backdrop-blur-sm" aria-hidden="true"></div>
<!-- 居中技巧 -->
<span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">​</span>
<!-- 模态框内容 -->
<div class="inline-block w-full max-w-6xl overflow-hidden text-left align-middle transition-all transform bg-white rounded-2xl shadow-2xl border border-gray-200"
@onclick:stopPropagation="true">
<!-- 头部 -->
<div class="flex items-center justify-between px-6 py-4 border-b border-gray-200 bg-white">
<div class="flex items-center gap-3 flex-1 min-w-0">
@((MarkupString)GetFileTypeIcon())
<div class="flex-1 min-w-0">
<h3 class="text-lg font-bold text-gray-900 truncate">@_fileName</h3>
<p class="text-xs text-gray-600 truncate">@_filePath</p>
</div>
</div>
<div class="flex items-center gap-2 ml-4">
@if (!string.IsNullOrEmpty(_languageLabel))
{
<span class="@GetLabelColorClass() px-3 py-1 text-xs font-bold text-white rounded-full shadow-sm">@_languageLabel</span>
}
<span class="text-xs text-gray-600 font-medium">@_fileSize</span>
</div>
</div>
<!-- 工具栏 -->
<div class="flex items-center justify-between px-6 py-3 bg-gray-50 border-b border-gray-200">
<div class="flex items-center gap-2">
@if (_previewType == PreviewType.Code)
{
<button type="button"
@onclick="CopyCode"
@onclick:stopPropagation="true"
@onclick:preventDefault="true"
class="btn-primary text-sm py-2 px-4 flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="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"></path>
</svg>
@(_copied ? "✓ 已复制" : "复制代码")
</button>
}
@if (_previewType == PreviewType.Office)
{
<button type="button"
@onclick="OpenInNewWindow"
@onclick:stopPropagation="true"
class="btn-primary text-sm py-2 px-4 flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"></path>
</svg>
在新窗口打开
</button>
}
<button type="button"
@onclick="DownloadFile"
@onclick:stopPropagation="true"
class="btn-secondary text-sm py-2 px-4 flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path>
</svg>
下载文件
</button>
</div>
<button type="button" @onclick="Close" @onclick:stopPropagation="true" class="text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-lg p-2 transition-all">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<!-- 内容区域 - 根据文件类型显示不同预览 -->
@if (_previewType == PreviewType.Office)
{
<!-- Office文档预览区域 - 使用 FilePreviewPanel 组件 -->
<div class="max-h-[70vh] overflow-hidden">
<FilePreviewPanel
PreviewType="FilePreviewPanel.FilePreviewType.Office"
FileName="@_fileName"
OfficePreviewUrl="@_officePreviewUrl"
OfficePreviewError="@_officePreviewError"
IsLoadingOffice="@_isLoadingPreview"
MaxHeight="max-h-[70vh]"
ContainerClass="h-full" />
</div>
<!-- 底部信息栏 -->
<div class="flex items-center justify-between px-6 py-3 bg-gray-50 border-t border-gray-200 text-xs text-gray-700 font-medium">
<div class="flex items-center gap-2">
@((MarkupString)GetOfficeTypeIcon())
<span>@FilePreviewPanel.GetOfficeTypeName(System.IO.Path.GetExtension(_fileName))</span>
</div>
<div class="flex items-center gap-2 text-blue-600">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 01-9 9m9-9a9 9 0 00-9-9m9 9H3m9 9a9 9 0 01-9-9m9 9c1.657 0 3-4.03 3-9s-1.343-9-3-9m0 18c-1.657 0-3-4.03-3-9s1.343-9 3-9m-9 9a9 9 0 019-9"></path>
</svg>
<span>Microsoft Office 在线预览</span>
</div>
</div>
}
else if (_previewType == PreviewType.Unknown)
{
<!-- 不支持预览的文件类型 -->
<div class="h-[50vh] flex flex-col items-center justify-center bg-gray-50 p-8">
<div class="w-20 h-20 bg-gray-200 rounded-full flex items-center justify-center mb-4">
<svg class="w-10 h-10 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
</svg>
</div>
<h4 class="text-lg font-bold text-gray-800 mb-2">无法预览此文件类型</h4>
<p class="text-gray-600 text-center max-w-md mb-4">此文件格式不支持在线预览,请下载后使用本地应用程序打开。</p>
<button @onclick="DownloadFile" class="btn-primary py-2 px-4 flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path>
</svg>
下载文件
</button>
</div>
}
else
{
<div class="max-h-[70vh] overflow-hidden">
<FilePreviewPanel
PreviewType="@ConvertToFilePreviewType(_previewType)"
FileName="@_fileName"
FileContent="@_fileContent"
Language="@_language"
ImageUrl="@_imageDataUrl"
PdfDataUrl="@_pdfDataUrl"
IsLoading="@_isLoadingPreview"
ErrorMessage="@_officePreviewError"
CodeElementId="code-preview-content"
ShowLineNumbers="false"
MaxHeight="max-h-[70vh]"
AutoApplyHighlight="true"
ContainerClass="h-full" />
</div>
}
<!-- 底部信息栏 -->
@if (_previewType == PreviewType.Code)
{
<div class="flex items-center justify-between px-6 py-3 bg-gray-50 border-t border-gray-200 text-xs text-gray-700 font-medium">
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path>
</svg>
共 @_lineCount 行
</div>
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"></path>
</svg>
编码: UTF-8
</div>
</div>
}
else if (_previewType == PreviewType.Image)
{
<div class="flex items-center justify-center px-6 py-3 bg-gray-50 border-t border-gray-200 text-xs text-gray-700 font-medium">
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
</svg>
图片预览
</div>
</div>
}
else if (_previewType == PreviewType.Pdf)
{
<div class="flex items-center justify-center px-6 py-3 bg-gray-50 border-t border-gray-200 text-xs text-gray-700 font-medium">
<div class="flex items-center gap-2">
<svg class="w-4 h-4 text-red-500" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4z" clip-rule="evenodd"></path>
</svg>
PDF 文档预览
</div>
</div>
}
</div>
</div>
</div>
}
@code {
// 预览类型枚举
private enum PreviewType
{
Code, // 代码/文本文件
Image, // 图片文件
Office, // Office文档(Word/Excel/PowerPoint)
Pdf, // PDF文件
Unknown // 不支持预览
}
// 会话ID,用于生成文件访问URL
[Parameter] public string SessionId { get; set; } = string.Empty;
private bool _isVisible = false;
private string _fileName = string.Empty;
private string _filePath = string.Empty;
private string _fileContent = string.Empty;
private string _language = "plaintext";
private string _languageLabel = string.Empty;
private string _fileSize = string.Empty;
private int _lineCount = 0;
private bool _copied = false;
private byte[]? _fileBytes;
// 预览相关
private PreviewType _previewType = PreviewType.Code;
private string _imageDataUrl = string.Empty;
private string _pdfDataUrl = string.Empty;
private string _officePreviewUrl = string.Empty;
private string _officePreviewError = string.Empty;
private bool _isLoadingPreview = false;
private string _currentSessionId = string.Empty;
/// <summary>
/// 显示预览模态框
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="filePath">文件相对路径</param>
/// <param name="content">文件文本内容(可选)</param>
/// <param name="fileBytes">文件字节内容(可选)</param>
/// <param name="sessionId">会话ID(用于生成文件URL)</param>
public async Task ShowAsync(string fileName, string filePath, string content, byte[]? fileBytes = null, string? sessionId = null)
{
_fileName = fileName;
_filePath = filePath;
_fileContent = content;
_fileBytes = fileBytes;
_copied = false;
_officePreviewError = string.Empty;
_isLoadingPreview = false;
_currentSessionId = sessionId ?? SessionId;
// 计算文件大小
var actualSize = fileBytes?.Length ?? content.Length;
_fileSize = CalculateFileSize(actualSize);
// 根据文件扩展名确定预览类型
var extension = System.IO.Path.GetExtension(fileName).ToLower();
_previewType = GetPreviewType(extension);
// 根据预览类型进行初始化
switch (_previewType)
{
case PreviewType.Code:
(_language, _languageLabel) = GetLanguageFromExtension(extension);
_lineCount = content.Split('\n').Length;
break;
case PreviewType.Image:
_languageLabel = GetImageLabel(extension);
if (fileBytes != null)
{
var mimeType = GetMimeType(extension);
_imageDataUrl = $"data:{mimeType};base64,{Convert.ToBase64String(fileBytes)}";
}
break;
case PreviewType.Pdf:
_languageLabel = "PDF";
if (fileBytes != null)
{
_pdfDataUrl = $"data:application/pdf;base64,{Convert.ToBase64String(fileBytes)}";
}
break;
case PreviewType.Office:
_languageLabel = GetOfficeLabel(extension);
await InitializeOfficePreview(extension);
break;
default:
_languageLabel = extension.TrimStart('.').ToUpper();
break;
}
_isVisible = true;
StateHasChanged();
// 如果是代码预览,应用语法高亮
if (_previewType == PreviewType.Code)
{
await Task.Delay(50);
try
{
await JSRuntime.InvokeVoidAsync("applyCodeHighlight");
}
catch (Exception ex)
{
Console.WriteLine($"应用语法高亮失败: {ex.Message}");
}
}
}
/// <summary>
/// 初始化Office在线预览
/// </summary>
private async Task InitializeOfficePreview(string extension)
{
_isLoadingPreview = true;
StateHasChanged();
try
{
// 获取当前应用的基础URL
var baseUri = NavigationManager.BaseUri.TrimEnd('/');
// 构建文件的公开访问URL
var encodedFilePath = Uri.EscapeDataString(_filePath);
var fileUrl = $"{baseUri}/api/workspace/{_currentSessionId}/files/{encodedFilePath}";
// 使用 FilePreviewPanel 的静态方法生成预览 URL
var (previewUrl, errorMessage) = FilePreviewPanel.GenerateOfficePreviewUrl(fileUrl, baseUri);
_officePreviewUrl = previewUrl;
_officePreviewError = errorMessage;
}
catch (Exception ex)
{
_officePreviewError = $"初始化预览失败: {ex.Message}";
}
finally
{
_isLoadingPreview = false;
}
}
/// <summary>
/// 在新窗口打开Office预览
/// </summary>
private async Task OpenInNewWindow()
{
if (!string.IsNullOrEmpty(_officePreviewUrl))
{
await JSRuntime.InvokeVoidAsync("window.open", _officePreviewUrl, "_blank");
}
}
/// <summary>
/// 获取预览类型
/// </summary>
private PreviewType GetPreviewType(string extension)
{
return extension.ToLower() switch
{
// 图片文件
".png" or ".jpg" or ".jpeg" or ".gif" or ".webp" or ".bmp" or ".svg" or ".ico"
=> PreviewType.Image,
// PDF文件
".pdf" => PreviewType.Pdf,
// Office文档 - Word
".doc" or ".docx" or ".docm" or ".dotx" or ".dotm" or ".odt"
=> PreviewType.Office,
// Office文档 - Excel
".xls" or ".xlsx" or ".xlsm" or ".xlsb" or ".xltx" or ".xltm" or ".csv" or ".ods"
=> PreviewType.Office,
// Office文档 - PowerPoint
".ppt" or ".pptx" or ".pptm" or ".potx" or ".potm" or ".ppsx" or ".ppsm" or ".odp"
=> PreviewType.Office,
// 代码/文本文件
".html" or ".htm" or ".css" or ".js" or ".ts" or ".jsx" or ".tsx" or ".json" or
".xml" or ".cs" or ".java" or ".py" or ".rb" or ".php" or ".go" or ".rs" or
".sql" or ".sh" or ".bash" or ".ps1" or ".yaml" or ".yml" or ".md" or ".vue" or
".razor" or ".cpp" or ".cc" or ".c" or ".swift" or ".kt" or ".dart" or ".r" or
".scala" or ".dockerfile" or ".txt" or ".log" or ".config" or ".ini" or ".env" or
".gitignore" or ".editorconfig" or ".prettierrc" or ".eslintrc" or ".babelrc" or
".csproj" or ".sln" or ".props" or ".targets"
=> PreviewType.Code,
// 压缩文件和其他二进制文件
".zip" or ".rar" or ".7z" or ".tar" or ".gz" or ".exe" or ".dll" or ".so" or ".bin"
=> PreviewType.Unknown,
// 默认尝试作为代码/文本处理
_ => PreviewType.Code
};
}
/// <summary>
/// 获取文件类型图标
/// </summary>
private string GetFileTypeIcon()
{
var extension = System.IO.Path.GetExtension(_fileName).ToLower();
return FileIconHelper.GetLargeFileIcon(extension);
}
/// <summary>
/// 获取标签颜色类
/// </summary>
private string GetLabelColorClass()
{
var extension = System.IO.Path.GetExtension(_fileName).ToLower();
return extension switch
{
".doc" or ".docx" or ".docm" or ".dotx" or ".dotm" or ".odt" => "bg-blue-600",
".xls" or ".xlsx" or ".xlsm" or ".xlsb" or ".xltx" or ".xltm" or ".csv" or ".ods" => "bg-green-600",
".ppt" or ".pptx" or ".pptm" or ".potx" or ".potm" or ".ppsx" or ".ppsm" or ".odp" => "bg-orange-500",
".pdf" => "bg-red-500",
".png" or ".jpg" or ".jpeg" or ".gif" or ".webp" or ".bmp" or ".svg" or ".ico" => "bg-pink-500",
_ => "bg-gray-900"
};
}
/// <summary>
/// 获取Office类型图标
/// </summary>
private string GetOfficeTypeIcon()
{
var extension = System.IO.Path.GetExtension(_fileName).ToLower();
return extension switch
{
".doc" or ".docx" or ".docm" or ".dotx" or ".dotm" or ".odt" =>
"<svg class=\"w-4 h-4 text-blue-600\" fill=\"currentColor\" viewBox=\"0 0 24 24\"><path d=\"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z\"/></svg>",
".xls" or ".xlsx" or ".xlsm" or ".xlsb" or ".xltx" or ".xltm" or ".csv" or ".ods" =>
"<svg class=\"w-4 h-4 text-green-600\" fill=\"currentColor\" viewBox=\"0 0 24 24\"><path d=\"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z\"/></svg>",
".ppt" or ".pptx" or ".pptm" or ".potx" or ".potm" or ".ppsx" or ".ppsm" or ".odp" =>
"<svg class=\"w-4 h-4 text-orange-600\" fill=\"currentColor\" viewBox=\"0 0 24 24\"><path d=\"M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6z\"/></svg>",
_ => "<svg class=\"w-4 h-4 text-gray-600\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z\"></path></svg>"
};
}
/// <summary>
/// 获取Office标签
/// </summary>
private string GetOfficeLabel(string extension)
{
return extension.ToLower() switch
{
".doc" or ".docx" or ".docm" or ".dotx" or ".dotm" => "Word",
".odt" => "ODT",
".xls" or ".xlsx" or ".xlsm" or ".xlsb" or ".xltx" or ".xltm" => "Excel",
".csv" => "CSV",
".ods" => "ODS",
".ppt" or ".pptx" or ".pptm" or ".potx" or ".potm" or ".ppsx" or ".ppsm" => "PowerPoint",
".odp" => "ODP",
_ => extension.TrimStart('.').ToUpper()
};
}
/// <summary>
/// 获取图片标签
/// </summary>
private string GetImageLabel(string extension)
{
return extension.ToLower() switch
{
".png" => "PNG",
".jpg" or ".jpeg" => "JPEG",
".gif" => "GIF",
".webp" => "WebP",
".bmp" => "BMP",
".svg" => "SVG",
".ico" => "ICO",
_ => "Image"
};
}
private void Close()
{
_isVisible = false;
_imageDataUrl = string.Empty;
_pdfDataUrl = string.Empty;
_officePreviewUrl = string.Empty;
_officePreviewError = string.Empty;
StateHasChanged();
}
private void HandleBackdropClick()
{
Close();
}
private async Task CopyCode()
{
try
{
// 使用统一的剪贴板函数(含回退方案)
var success = await JSRuntime.InvokeAsync<bool>("clipboardCopy.copyText", _fileContent);
if (success)
{
_copied = true;
StateHasChanged();
// 2秒后恢复按钮文本
await Task.Delay(2000);
_copied = false;
StateHasChanged();
}
else
{
Console.WriteLine("复制失败:剪贴板操作返回 false");
}
}
catch (Exception ex)
{
Console.WriteLine($"复制失败: {ex.Message}");
}
}
private async Task DownloadFile()
{
try
{
if (_fileBytes != null)
{
var base64 = Convert.ToBase64String(_fileBytes);
var mimeType = GetMimeType(System.IO.Path.GetExtension(_fileName));
await JSRuntime.InvokeVoidAsync("downloadBase64File", _fileName, base64, mimeType);
}
}
catch (Exception ex)
{
Console.WriteLine($"下载失败: {ex.Message}");
}
}
private string CalculateFileSize(int byteCount)
{
if (byteCount < 1024)
return $"{byteCount} B";
else if (byteCount < 1024 * 1024)
return $"{byteCount / 1024.0:F2} KB";
else
return $"{byteCount / (1024.0 * 1024.0):F2} MB";
}
private (string language, string label) GetLanguageFromExtension(string extension)
{
return extension switch
{
".html" or ".htm" => ("html", "HTML"),
".css" => ("css", "CSS"),
".js" => ("javascript", "JavaScript"),
".ts" => ("typescript", "TypeScript"),
".jsx" => ("jsx", "JSX"),
".tsx" => ("tsx", "TSX"),
".json" => ("json", "JSON"),
".xml" => ("xml", "XML"),
".cs" => ("csharp", "C#"),
".java" => ("java", "Java"),
".py" => ("python", "Python"),
".rb" => ("ruby", "Ruby"),
".php" => ("php", "PHP"),
".go" => ("go", "Go"),
".rs" => ("rust", "Rust"),
".sql" => ("sql", "SQL"),
".sh" or ".bash" => ("bash", "Bash"),
".ps1" => ("powershell", "PowerShell"),
".yaml" or ".yml" => ("yaml", "YAML"),
".md" => ("markdown", "Markdown"),
".vue" => ("markup", "Vue"),
".razor" => ("cshtml", "Razor"),
".cpp" or ".cc" => ("cpp", "C++"),
".c" => ("c", "C"),
".swift" => ("swift", "Swift"),
".kt" => ("kotlin", "Kotlin"),
".dart" => ("dart", "Dart"),
".r" => ("r", "R"),
".scala" => ("scala", "Scala"),
".dockerfile" => ("docker", "Docker"),
_ => ("plaintext", "Text")
};
}
private string GetMimeType(string extension)
{
return extension.ToLower() switch
{
".html" or ".htm" => "text/html",
".css" => "text/css",
".js" => "text/javascript",
".json" => "application/json",
".xml" => "application/xml",
".txt" => "text/plain",
".md" => "text/markdown",
".png" => "image/png",
".jpg" or ".jpeg" => "image/jpeg",
".gif" => "image/gif",
".webp" => "image/webp",
".bmp" => "image/bmp",
".svg" => "image/svg+xml",
".ico" => "image/x-icon",
".pdf" => "application/pdf",
".doc" => "application/msword",
".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".xls" => "application/vnd.ms-excel",
".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".ppt" => "application/vnd.ms-powerpoint",
".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
".csv" => "text/csv",
".zip" => "application/zip",
".rar" => "application/x-rar-compressed",
".7z" => "application/x-7z-compressed",
_ => "application/octet-stream"
};
}
/// <summary>
/// 将内部 PreviewType 转换为 FilePreviewPanel.FilePreviewType
/// </summary>
private FilePreviewPanel.FilePreviewType ConvertToFilePreviewType(PreviewType type)
{
return type switch
{
PreviewType.Code => FilePreviewPanel.FilePreviewType.Code,
PreviewType.Image => FilePreviewPanel.FilePreviewType.Image,
PreviewType.Pdf => FilePreviewPanel.FilePreviewType.Pdf,
PreviewType.Office => FilePreviewPanel.FilePreviewType.Unknown, // Office 类型需要特殊处理
PreviewType.Unknown => FilePreviewPanel.FilePreviewType.Unknown,
_ => FilePreviewPanel.FilePreviewType.None
};
}
}