-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.html
More file actions
919 lines (787 loc) · 30.2 KB
/
Copy pathinteractive.html
File metadata and controls
919 lines (787 loc) · 30.2 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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MD → HTML 交互式预览</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: -apple-system, "Microsoft YaHei", "PingFang SC", sans-serif;
background: #fafafa;
color: #333;
line-height: 1.8;
}
/* 顶部工具栏 */
.toolbar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 48px;
background: #fff;
border-bottom: 1px solid #e0e0e0;
display: flex;
align-items: center;
padding: 0 16px;
z-index: 100;
gap: 12px;
}
.toolbar-title {
font-weight: 600;
color: #333;
margin-right: auto;
}
.toolbar-btn {
padding: 6px 12px;
background: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
color: #555;
}
.toolbar-btn:hover {
background: #e8e8e8;
}
.toolbar-btn.active {
background: #4a90d9;
color: white;
border-color: #4a90d9;
}
.toolbar-btn.commit {
background: #4caf50;
color: white;
border-color: #4caf50;
}
.toolbar-btn.commit:hover {
background: #43a047;
}
.toolbar-btn.commit:disabled {
background: #ccc;
border-color: #ccc;
cursor: not-allowed;
}
.comment-count {
background: #ff9800;
color: white;
padding: 2px 8px;
border-radius: 12px;
font-size: 12px;
margin-left: 4px;
}
/* 主内容区 */
.main {
margin-top: 48px;
display: flex;
height: calc(100vh - 48px);
}
/* 预览区 */
.preview {
flex: 1;
overflow-y: auto;
}
#preview {
padding: 32px;
max-width: 900px;
margin: 0 auto;
}
/* 编辑区(默认隐藏) */
.editor {
display: none;
width: 50%;
border-right: 1px solid #ddd;
background: #fff;
flex-direction: column;
}
.editor.visible {
display: flex;
}
#editor {
flex: 1;
width: 100%;
padding: 16px;
border: none;
resize: none;
font-family: "Consolas", "Monaco", "Courier New", monospace;
font-size: 14px;
line-height: 1.6;
outline: none;
}
/* 折叠块样式 */
details {
margin: 16px 0;
padding: 12px 16px;
border: 1px solid #e0e0e0;
border-radius: 6px;
background: #fff;
position: relative;
}
details[open] {
background: #f9f9f9;
}
summary {
cursor: pointer;
font-weight: 600;
color: #2c5f8a;
padding: 4px 0;
display: flex;
align-items: center;
gap: 8px;
}
summary:hover {
color: #1a5a8a;
}
/* 评论按钮 */
.comment-btn {
margin-left: auto;
padding: 4px 8px;
background: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
color: #666;
display: flex;
align-items: center;
gap: 4px;
}
.comment-btn:hover {
background: #e8e8e8;
}
.comment-btn.has-comment {
background: #fff3e0;
border-color: #ff9800;
color: #e65100;
}
/* 评论区域 */
.comment-area {
margin-top: 12px;
padding-top: 12px;
border-top: 1px dashed #ddd;
display: none;
}
.comment-area.visible {
display: block;
}
.comment-input {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
resize: vertical;
min-height: 60px;
font-family: inherit;
}
.comment-input:focus {
outline: none;
border-color: #4a90d9;
}
.comment-actions {
margin-top: 8px;
display: flex;
gap: 8px;
}
.comment-save-btn {
padding: 6px 12px;
background: #4a90d9;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
}
.comment-save-btn:hover {
background: #357abd;
}
.comment-delete-btn {
padding: 6px 12px;
background: #f44336;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
}
.comment-delete-btn:hover {
background: #d32f2f;
}
/* 已保存的评论显示 */
.comment-display {
margin-top: 8px;
padding: 8px 12px;
background: #fff8e1;
border-left: 3px solid #ff9800;
border-radius: 0 4px 4px 0;
font-size: 14px;
color: #555;
}
.comment-display .comment-text {
margin-bottom: 4px;
}
.comment-display .comment-time {
font-size: 12px;
color: #999;
}
/* 渲染样式 */
#preview h1 {
color: #1a1a1a;
border-bottom: 2px solid #e0e0e0;
padding-bottom: 10px;
margin: 24px 0 16px;
}
#preview h2 {
color: #2c5f8a;
margin: 32px 0 12px;
}
#preview h3 {
color: #4a7fb5;
margin: 24px 0 8px;
}
#preview p {
margin: 12px 0;
}
#preview ul, #preview ol {
margin: 12px 0;
padding-left: 24px;
}
#preview li {
margin: 4px 0;
}
#preview code {
background: #f0f0f0;
padding: 2px 6px;
border-radius: 3px;
font-size: 0.9em;
font-family: "Consolas", "Monaco", monospace;
}
#preview pre {
background: #2d2d2d;
color: #f8f8f2;
padding: 16px;
border-radius: 6px;
overflow-x: auto;
margin: 16px 0;
}
#preview pre code {
background: none;
padding: 0;
color: inherit;
}
#preview blockquote {
border-left: 4px solid #4a90d9;
padding: 12px 16px;
margin: 16px 0;
background: #f0f7ff;
color: #555;
}
#preview table {
border-collapse: collapse;
width: 100%;
margin: 16px 0;
}
#preview th, #preview td {
border: 1px solid #ddd;
padding: 8px 12px;
text-align: left;
}
#preview th {
background: #f0f4f8;
font-weight: 600;
}
#preview tr:nth-child(even) {
background: #f9f9f9;
}
#preview .tip {
background: #e8f5e9;
border-left: 4px solid #4caf50;
padding: 12px 16px;
margin: 16px 0;
border-radius: 0 4px 4px 0;
}
#preview .warning {
background: #fff3e0;
border-left: 4px solid #ff9800;
padding: 12px 16px;
margin: 16px 0;
border-radius: 0 4px 4px 0;
}
#preview .danger {
background: #ffebee;
border-left: 4px solid #f44336;
padding: 12px 16px;
margin: 16px 0;
border-radius: 0 4px 4px 0;
}
#preview hr {
border: none;
border-top: 1px solid #e0e0e0;
margin: 24px 0;
}
#preview a {
color: #4a90d9;
text-decoration: none;
}
#preview a:hover {
text-decoration: underline;
}
/* 空状态提示 */
.empty-state {
text-align: center;
padding: 100px 20px;
color: #999;
}
.empty-state h2 {
color: #666;
margin-bottom: 16px;
}
.empty-state p {
margin: 8px 0;
}
.empty-state code {
background: #f0f0f0;
padding: 4px 8px;
border-radius: 4px;
font-size: 14px;
}
/* 提示消息 */
.toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 12px 24px;
border-radius: 6px;
font-size: 14px;
z-index: 1000;
opacity: 0;
transition: opacity 0.3s;
}
.toast.show {
opacity: 1;
}
.toast.success {
background: #4caf50;
}
.toast.error {
background: #f44336;
}
</style>
</head>
<body>
<div class="toolbar">
<span class="toolbar-title">📄 MD → HTML 交互式预览</span>
<span id="commentSummary" class="comment-count" style="display: none;">0 条评论</span>
<button class="toolbar-btn" id="toggleEditor" onclick="toggleEditor()">📝 编辑</button>
<button class="toolbar-btn" id="pasteBtn" onclick="pasteFromClipboard()">📋 粘贴 MD</button>
<button class="toolbar-btn" id="copyBtn" onclick="copyHtml()">📋 复制 HTML</button>
<button class="toolbar-btn" id="clearBtn" onclick="clearAllComments()">🗑️ 清空评论</button>
<button class="toolbar-btn commit" id="commitBtn" onclick="commitComments()" disabled>✅ Commit</button>
</div>
<div class="main">
<div class="editor" id="editorPanel">
<textarea id="editor" placeholder="在此输入 Markdown..."></textarea>
</div>
<div class="preview">
<div id="preview">
<div class="empty-state">
<h2>欢迎使用 MD → HTML 交互式预览</h2>
<p>点击「📝 编辑」按钮打开编辑器</p>
<p>或点击「📋 粘贴 MD」从剪贴板粘贴内容</p>
<p>或拖拽 .md 文件到此页面</p>
<hr style="margin: 24px auto; width: 200px;">
<p><strong>交互功能:</strong></p>
<p>展开折叠块 → 点击「💬 评论」→ 写评论</p>
<p>点击「✅ Commit」→ 生成反馈报告 → 粘贴给 Agent</p>
</div>
</div>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
// 简单的 MD 解析器(不依赖外部库)
function parseMarkdown(md) {
let html = md;
// 代码块
html = html.replace(/```(\w*)\n([\s\S]*?)```/g, '<pre><code class="language-$1">$2</code></pre>');
// 标题
html = html.replace(/^### (.+)$/gm, '<h3>$1</h3>');
html = html.replace(/^## (.+)$/gm, '<h2>$1</h2>');
html = html.replace(/^# (.+)$/gm, '<h1>$1</h1>');
// 粗体和斜体
html = html.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
html = html.replace(/\*(.+?)\*/g, '<em>$1</em>');
// 行内代码
html = html.replace(/`(.+?)`/g, '<code>$1</code>');
// 链接
html = html.replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2">$1</a>');
// 图片
html = html.replace(/!\[(.+?)\]\((.+?)\)/g, '<img src="$2" alt="$1">');
// 引用
html = html.replace(/^> (.+)$/gm, '<blockquote>$1</blockquote>');
// 分隔线
html = html.replace(/^---$/gm, '<hr>');
// 无序列表
html = html.replace(/^- (.+)$/gm, '<li>$1</li>');
html = html.replace(/(<li>.*<\/li>\n?)+/g, '<ul>$&</ul>');
// 有序列表
html = html.replace(/^\d+\. (.+)$/gm, '<li>$1</li>');
// 表格
html = html.replace(/^\|(.+)\|$/gm, (match, content) => {
const cells = content.split('|').map(c => c.trim());
if (cells.every(c => /^[-:]+$/.test(c))) {
return ''; // 分隔行
}
const tag = cells.length > 0 ? 'td' : 'td';
return '<tr>' + cells.map(c => `<${tag}>${c}</${tag}>`).join('') + '</tr>';
});
html = html.replace(/(<tr>.*<\/tr>\n?)+/g, '<table>$&</table>');
// 段落
html = html.replace(/\n\n/g, '</p><p>');
html = '<p>' + html + '</p>';
// 清理空段落
html = html.replace(/<p>\s*<\/p>/g, '');
html = html.replace(/<p>(<h[1-6]>)/g, '$1');
html = html.replace(/(<\/h[1-6]>)<\/p>/g, '$1');
html = html.replace(/<p>(<ul>)/g, '$1');
html = html.replace(/(<\/ul>)<\/p>/g, '$1');
html = html.replace(/<p>(<table>)/g, '$1');
html = html.replace(/(<\/table>)<\/p>/g, '$1');
html = html.replace(/<p>(<blockquote>)/g, '$1');
html = html.replace(/(<\/blockquote>)<\/p>/g, '$1');
html = html.replace(/<p>(<hr>)/g, '$1');
html = html.replace(/(<hr>)<\/p>/g, '$1');
html = html.replace(/<p>(<pre>)/g, '$1');
html = html.replace(/(<\/pre>)<\/p>/g, '$1');
html = html.replace(/<p>(<details>)/g, '$1');
html = html.replace(/(<\/details>)<\/p>/g, '$1');
return html;
}
const editor = document.getElementById('editor');
const preview = document.getElementById('preview');
const editorPanel = document.getElementById('editorPanel');
const commitBtn = document.getElementById('commitBtn');
const commentSummary = document.getElementById('commentSummary');
const toast = document.getElementById('toast');
// localStorage key
const STORAGE_KEY = 'md2html_comments';
// 渲染
function render() {
const md = editor.value.trim();
if (md) {
preview.innerHTML = parseMarkdown(md);
// 为所有 details 元素添加评论按钮
setupCommentButtons();
// 加载已保存的评论
loadComments();
} else {
preview.innerHTML = `
<div class="empty-state">
<h2>欢迎使用 MD → HTML 交互式预览</h2>
<p>点击「📝 编辑」按钮打开编辑器</p>
<p>或点击「📋 粘贴 MD」从剪贴板粘贴内容</p>
<p>或拖拽 .md 文件到此页面</p>
<hr style="margin: 24px auto; width: 200px;">
<p><strong>交互功能:</strong></p>
<p>展开折叠块 → 点击「💬 评论」→ 写评论</p>
<p>点击「✅ Commit」→ 生成反馈报告 → 粘贴给 Agent</p>
</div>
`;
}
}
// 为折叠块添加评论按钮
function setupCommentButtons() {
const details = preview.querySelectorAll('details');
details.forEach((detail, index) => {
// 确保有 ID
if (!detail.id) {
detail.id = `block-${String(index + 1).padStart(3, '0')}`;
}
const summary = detail.querySelector('summary');
if (summary && !summary.querySelector('.comment-btn')) {
const blockId = detail.id;
const blockTitle = summary.textContent.trim();
// 创建评论按钮
const commentBtn = document.createElement('button');
commentBtn.className = 'comment-btn';
commentBtn.innerHTML = `💬 <span class="comment-count">0</span>`;
commentBtn.onclick = (e) => {
e.stopPropagation();
toggleCommentArea(blockId);
};
summary.appendChild(commentBtn);
// 创建评论区域
const commentArea = document.createElement('div');
commentArea.className = 'comment-area';
commentArea.id = `comment-area-${blockId}`;
commentArea.innerHTML = `
<textarea class="comment-input" id="comment-input-${blockId}"
placeholder="写下你的评论..."></textarea>
<div class="comment-actions">
<button class="comment-save-btn" onclick="saveComment('${blockId}')">保存评论</button>
<button class="comment-delete-btn" onclick="deleteComment('${blockId}')"
style="display: none;">删除评论</button>
</div>
<div class="comment-display" id="comment-display-${blockId}" style="display: none;">
<div class="comment-text"></div>
<div class="comment-time"></div>
</div>
`;
detail.appendChild(commentArea);
}
});
}
// 切换评论区域显示
function toggleCommentArea(blockId) {
const commentArea = document.getElementById(`comment-area-${blockId}`);
if (commentArea) {
commentArea.classList.toggle('visible');
}
}
// 保存评论到 localStorage
function saveComment(blockId) {
const input = document.getElementById(`comment-input-${blockId}`);
const comment = input.value.trim();
if (!comment) {
showToast('请输入评论内容', 'error');
return;
}
// 获取块标题
const detail = document.getElementById(blockId);
const summary = detail.querySelector('summary');
const title = summary.textContent.replace('💬', '').replace(/\d+/g, '').trim();
// 保存到 localStorage
const comments = getComments();
comments[blockId] = {
title: title,
comment: comment,
timestamp: Date.now()
};
localStorage.setItem(STORAGE_KEY, JSON.stringify(comments));
// 更新 UI
updateCommentUI(blockId, comment);
updateCommitButton();
showToast('评论已保存', 'success');
}
// 删除评论
function deleteComment(blockId) {
const comments = getComments();
delete comments[blockId];
localStorage.setItem(STORAGE_KEY, JSON.stringify(comments));
// 更新 UI
const display = document.getElementById(`comment-display-${blockId}`);
const deleteBtn = document.querySelector(`#comment-area-${blockId} .comment-delete-btn`);
const input = document.getElementById(`comment-input-${blockId}`);
display.style.display = 'none';
deleteBtn.style.display = 'none';
input.value = '';
// 更新评论按钮
const detail = document.getElementById(blockId);
const commentBtn = detail.querySelector('.comment-btn');
commentBtn.classList.remove('has-comment');
commentBtn.querySelector('.comment-count').textContent = '0';
updateCommitButton();
showToast('评论已删除', 'success');
}
// 获取所有评论
function getComments() {
const data = localStorage.getItem(STORAGE_KEY);
return data ? JSON.parse(data) : {};
}
// 加载已保存的评论
function loadComments() {
const comments = getComments();
Object.keys(comments).forEach(blockId => {
const detail = document.getElementById(blockId);
if (detail) {
const { comment, timestamp } = comments[blockId];
updateCommentUI(blockId, comment, timestamp);
}
});
updateCommitButton();
}
// 更新评论 UI
function updateCommentUI(blockId, comment, timestamp) {
const display = document.getElementById(`comment-display-${blockId}`);
const deleteBtn = document.querySelector(`#comment-area-${blockId} .comment-delete-btn`);
const input = document.getElementById(`comment-input-${blockId}`);
const detail = document.getElementById(blockId);
const commentBtn = detail.querySelector('.comment-btn');
if (comment) {
display.querySelector('.comment-text').textContent = comment;
display.querySelector('.comment-time').textContent = timestamp
? new Date(timestamp).toLocaleString()
: new Date().toLocaleString();
display.style.display = 'block';
deleteBtn.style.display = 'inline-block';
input.value = comment;
// 更新评论按钮
commentBtn.classList.add('has-comment');
commentBtn.querySelector('.comment-count').textContent = '1';
} else {
display.style.display = 'none';
deleteBtn.style.display = 'none';
input.value = '';
// 更新评论按钮
commentBtn.classList.remove('has-comment');
commentBtn.querySelector('.comment-count').textContent = '0';
}
}
// 更新 Commit 按钮状态
function updateCommitButton() {
const comments = getComments();
const count = Object.keys(comments).length;
commitBtn.disabled = count === 0;
commentSummary.textContent = `${count} 条评论`;
commentSummary.style.display = count > 0 ? 'inline' : 'none';
}
// Commit 评论 - 生成报告并复制到剪贴板
function commitComments() {
const comments = getComments();
const blocks = Object.keys(comments);
if (blocks.length === 0) {
showToast('没有评论可提交', 'error');
return;
}
// 生成报告
let report = '# 用户反馈报告\n\n';
blocks.forEach(blockId => {
const { title, comment } = comments[blockId];
report += `## 块:${title}\n`;
report += `评论:${comment}\n\n`;
});
report += '---\n\n请根据以上反馈迭代内容。';
// 复制到剪贴板
navigator.clipboard.writeText(report).then(() => {
showToast(`✅ 已生成报告并复制到剪贴板(${blocks.length} 条评论)`, 'success');
}).catch(err => {
// 降级方案:使用 textarea
const textarea = document.createElement('textarea');
textarea.value = report;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
showToast(`✅ 已生成报告并复制到剪贴板(${blocks.length} 条评论)`, 'success');
});
}
// 清空所有评论
function clearAllComments() {
if (!confirm('确定要清空所有评论吗?')) {
return;
}
localStorage.removeItem(STORAGE_KEY);
// 重新渲染
render();
showToast('所有评论已清空', 'success');
}
// 切换编辑器显示
function toggleEditor() {
const btn = document.getElementById('toggleEditor');
if (editorPanel.classList.contains('visible')) {
editorPanel.classList.remove('visible');
btn.classList.remove('active');
btn.textContent = '📝 编辑';
} else {
editorPanel.classList.add('visible');
btn.classList.add('active');
btn.textContent = '✕ 关闭编辑';
editor.focus();
}
}
// 从剪贴板粘贴
async function pasteFromClipboard() {
try {
const text = await navigator.clipboard.readText();
editor.value = text;
render();
// 自动打开编辑器
if (!editorPanel.classList.contains('visible')) {
toggleEditor();
}
} catch (err) {
alert('无法读取剪贴板,请手动粘贴 (Ctrl+V)');
}
}
// 复制 HTML
function copyHtml() {
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Agent 输出</title>
<style>
body { font-family: -apple-system, "Microsoft YaHei", sans-serif; max-width: 900px; margin: 40px auto; padding: 0 20px; line-height: 1.8; color: #333; }
h1 { color: #1a1a1a; border-bottom: 2px solid #e0e0e0; padding-bottom: 10px; }
h2 { color: #2c5f8a; margin-top: 40px; }
h3 { color: #4a7fb5; }
code { background: #f0f0f0; padding: 2px 6px; border-radius: 3px; }
pre { background: #2d2d2d; color: #f8f8f2; padding: 16px; border-radius: 6px; overflow-x: auto; }
pre code { background: none; color: inherit; }
blockquote { border-left: 4px solid #4a90d9; padding: 12px 16px; background: #f0f7ff; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px 12px; }
th { background: #f0f4f8; }
details { margin: 16px 0; padding: 12px 16px; border: 1px solid #e0e0e0; border-radius: 6px; }
details[open] { background: #f9f9f9; }
summary { cursor: pointer; font-weight: 600; color: #2c5f8a; }
.tip { background: #e8f5e9; border-left: 4px solid #4caf50; padding: 12px 16px; border-radius: 0 4px 4px 0; }
.warning { background: #fff3e0; border-left: 4px solid #ff9800; padding: 12px 16px; border-radius: 0 4px 4px 0; }
.danger { background: #ffebee; border-left: 4px solid #f44336; padding: 12px 16px; border-radius: 0 4px 4px 0; }
hr { border: none; border-top: 1px solid #e0e0e0; margin: 24px 0; }
</style>
</head>
<body>
${preview.innerHTML}
</body>
</html>`;
navigator.clipboard.writeText(html).then(() => {
alert('HTML 已复制到剪贴板!');
});
}
// 显示提示消息
function showToast(message, type = 'success') {
toast.textContent = message;
toast.className = `toast ${type} show`;
setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}
// 拖拽文件
document.addEventListener('dragover', (e) => {
e.preventDefault();
});
document.addEventListener('drop', (e) => {
e.preventDefault();
const file = e.dataTransfer.files[0];
if (file && (file.name.endsWith('.md') || file.name.endsWith('.txt'))) {
const reader = new FileReader();
reader.onload = (event) => {
editor.value = event.target.result;
render();
// 自动打开编辑器
if (!editorPanel.classList.contains('visible')) {
toggleEditor();
}
};
reader.readAsText(file);
}
});
// 监听编辑器输入
editor.addEventListener('input', render);
// 监听 Ctrl+V 粘贴
document.addEventListener('paste', (e) => {
const text = e.clipboardData.getData('text');
if (text && !editorPanel.classList.contains('visible')) {
editor.value = text;
render();
}
});
// 初始化
render();
</script>
</body>
</html>