-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
790 lines (709 loc) · 24 KB
/
Copy pathscript.js
File metadata and controls
790 lines (709 loc) · 24 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
// 示例代码库
const examples = {
1: `<!DOCTYPE html>
<html>
<head>
<title>标题和段落示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f0f8ff;
}
h1 { color: #2c5aa0; }
h2 { color: #4a7bc8; }
h3 { color: #6a8fd8; }
p { line-height: 1.6; }
</style>
</head>
<body>
<h1>这是一个一级标题</h1>
<h2>这是一个二级标题</h2>
<h3>这是一个三级标题</h3>
<p>这是一个段落。HTML中的段落用于显示文本内容,可以包含多行文字。</p>
<p>这是另一个段落。每个段落都会自动换行并有一定的间距。</p>
</body>
</html>`,
2: `<!DOCTYPE html>
<html>
<head>
<title>图片和链接示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fff0f5;
}
.container {
text-align: center;
}
img {
max-width: 100%;
height: auto;
border-radius: 10px;
margin: 20px 0;
}
a {
color: #e91e63;
text-decoration: none;
font-weight: bold;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>图片和链接演示</h1>
<img src="https://picsum.photos/400/300?random=1" alt="示例风景图片">
<p>点击下面的链接访问学习资源:</p>
<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTML" target="_blank">MDN HTML文档</a>
<br>
<a href="https://www.w3schools.com/html/" target="_blank">W3Schools HTML教程</a>
</div>
</body>
</html>`,
3: `<!DOCTYPE html>
<html>
<head>
<title>表格示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f0fff0;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>HTML表格示例</h1>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
<th>职业</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>北京</td>
<td>前端开发</td>
</tr>
<tr>
<td>李四</td>
<td>30</td>
<td>上海</td>
<td>UI设计师</td>
</tr>
<tr>
<td>王五</td>
<td>28</td>
<td>深圳</td>
<td>全栈开发</td>
</tr>
</tbody>
</table>
</body>
</html>`,
4: `<!DOCTYPE html>
<html>
<head>
<title>列表示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fffaf0;
}
.list-container {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.list-box {
background: white;
padding: 20px;
margin: 10px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
flex: 1;
min-width: 300px;
}
ul { list-style-type: disc; }
ol { list-style-type: decimal; }
li { margin: 5px 0; }
</style>
</head>
<body>
<h1>HTML列表示例</h1>
<div class="list-container">
<div class="list-box">
<h3>无序列表</h3>
<ul>
<li>HTML基础</li>
<li>CSS样式</li>
<li>JavaScript交互</li>
<li>响应式设计</li>
</ul>
</div>
<div class="list-box">
<h3>有序列表</h3>
<ol>
<li>学习HTML标签</li>
<li>掌握CSS选择器</li>
<li>理解JavaScript语法</li>
<li>构建完整项目</li>
</ol>
</div>
</div>
</body>
</html>`,
5: `<!DOCTYPE html>
<html>
<head>
<title>文字样式示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #f8f8ff;
}
.text-sample {
margin: 15px 0;
padding: 10px;
border-left: 4px solid #6a5acd;
background: white;
}
.bold { font-weight: bold; color: #d35400; }
.italic { font-style: italic; color: #2980b9; }
.underline { text-decoration: underline; color: #27ae60; }
.highlight {
background-color: #fffacd;
padding: 2px 4px;
}
</style>
</head>
<body>
<h1>HTML文字样式示例</h1>
<div class="text-sample">
<p>这是<span class="bold">加粗</span>的文字效果</p>
<p>这是<span class="italic">斜体</span>的文字效果</p>
<p>这是<span class="underline">下划线</span>的文字效果</p>
<p>这是<span class="highlight">高亮</span>的文字效果</p>
<p>可以<span class="bold italic">同时使用</span>多种样式</p>
</div>
</body>
</html>`,
6: `<!DOCTYPE html>
<html>
<head>
<title>CSS样式示例</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
}
.card {
background: rgba(255,255,255,0.1);
padding: 25px;
margin: 15px 0;
border-radius: 15px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.2);
}
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #feca57);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-weight: bold;
}
.button {
background: linear-gradient(45deg, #ff6b6b, #feca57);
border: none;
padding: 12px 24px;
border-radius: 25px;
color: white;
font-weight: bold;
cursor: pointer;
transition: transform 0.2s;
}
.button:hover {
transform: translateY(-2px);
}
</style>
</head>
<body>
<div class="card">
<h1 class="gradient-text">CSS样式演示</h1>
<p>这个示例展示了现代CSS特性:</p>
<ul>
<li>渐变背景</li>
<li>毛玻璃效果</li>
<li>动画过渡</li>
<li>响应式布局</li>
</ul>
<button class="button">悬停效果按钮</button>
</div>
</body>
</html>`
};
const defaultCode = `
<!DOCTYPE html>
<html>
<head>
<title>我的第一个网页</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
}
.container {
background: rgba(255,255,255,0.1);
padding: 30px;
border-radius: 15px;
backdrop-filter: blur(10px);
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.card {
background: white;
color: #333;
padding: 20px;
margin: 10px 0;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
<div class="container">
<h1>欢迎学习HTML!</h1>
<div class="card">
<h2>HTML是什么?</h2>
<p>HTML是网页的基础构建语言,用于创建网页结构。</p>
</div>
<div class="card">
<h2>开始学习</h2>
<p>修改左侧代码,然后点击运行按钮查看效果!</p>
</div>
<div class="card">
<h2>参考示例</h2>
<p>点击导航栏学习示例参考学习!</p>
</div>
</div>
</body>
</html>`;
const theme_selector = document.getElementById('theme-selector');
// 主题切换功能
function setTheme(theme) {
document.body.className = document.body.className.replace(/theme-\w+/g, '');
document.body.classList.add(`theme-${theme}`);
localStorage.setItem('editorTheme', theme);
theme_selector.value = theme;
// 更新语法高亮
updateCodeDisplay();
}
// 主题选择器事件
theme_selector.addEventListener('change', function() {
setTheme(this.value);
});
const codeInput = document.getElementById('html-code');
const highlightOutput = document.getElementById('highlightOutput');
const charCount = document.getElementById('charCount');
const lineCount = document.getElementById('lineCount');
codeInput.addEventListener('input', updateHighlight);
codeInput.addEventListener('scroll', syncScroll);
codeInput.addEventListener('keydown', handleTab);
function syncScroll() {
highlightOutput.scrollTop = codeInput.scrollTop;
highlightOutput.scrollLeft = codeInput.scrollLeft;
}
function updateHighlight() {
var theme = 'theme-' + theme_selector.value;
const code = codeInput.value;
// 更新字符和行数统计
charCount.textContent = `字符数: ${code.length}`;
lineCount.textContent = `行数: ${code.split('\n').length}`;
try {
// 语法高亮处理
let highlighted = code
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n/g, '<br>')
.replace(/\t/g, ' ')
.replace(/ /g, ' ');
// 处理DOCTYPE声明
highlighted = highlighted.replace(
/<!DOCTYPE\s+([^&]*)>/gi,
'<<span class="doctype">!DOCTYPE $1</span>>'
);
// 处理HTML标签
highlighted = highlighted.replace(
/<(\/?)([a-zA-Z][a-zA-Z0-9-]*)(\s[^&]*?)?>/g,
function(match, slash, tagName, attributes) {
let result = `<<span class="html-tag">${slash}${tagName}</span>`;
if (attributes) {
// 处理属性
let attrText = attributes.replace(
/(\s)([a-zA-Z-][a-zA-Z0-9-:]*)=("([^"]*)"|'([^']*)'|([a-zA-Z0-9-]+))/g,
function(attrMatch, space, attrName, quote1, doubleValue, quote2, singleValue, unquotedValue) {
let value = doubleValue || singleValue || unquotedValue;
return `${space}<span class="html-attr">${attrName}</span>=<span class="html-value">"${value}"</span>`;
}
);
result += attrText;
}
result += '>';
return result;
}
);
// 处理HTML注释
highlighted = highlighted.replace(
/<!--([\s\S]*?)-->/g,
'<!--<span class="' + theme + ' html-comment">$1</span>-->'
);
// 处理CSS注释
highlighted = highlighted.replace(
/\/\*([\s\S]*?)\*\//g,
'<span class="' + theme + 'css-comment">/*$1*/</span>'
);
//highlighted = '<div class="' + theme + '"> ' + highlighted + '</div>';
highlightOutput.innerHTML = highlighted;
} catch (error) {
console.error('代码高亮错误:', error);
// 显示原始代码作为后备
highlightOutput.textContent = codeInput.value;
}
}
function handleTab(e) {
if (e.key === 'Tab') {
e.preventDefault();
const start = codeInput.selectionStart;
const end = codeInput.selectionEnd;
// 插入4个空格
codeInput.value = codeInput.value.substring(0, start) + ' ' + codeInput.value.substring(end);
// 设置光标位置
codeInput.selectionStart = codeInput.selectionEnd = start + 4;
updateHighlight();
}
}
function clearCode() {
codeInput.value = '';
updateHighlight();
}
// 添加拖拽调整功能
document.addEventListener('DOMContentLoaded', function() {
const resizer = document.getElementById('resizer');
const editorPanel = document.getElementById('editor-panel');
const previewPanel = document.getElementById('preview-panel');
const container = document.querySelector('.editor-container');
// 初始化编辑器高度
const codeEditor = document.getElementById('html-code');
codeEditor.style.height = 'auto';
codeEditor.style.height = (codeEditor.scrollHeight) + 'px';
// 运行初始代码
runCode();
setupEventListeners();
updateHighlight();
// 拖拽功能实现
let startX, startY, startWidth, startHeight;
function initDrag(e) {
startX = e.pageX;
startY = e.pageY;
startWidth = editorPanel.offsetWidth;
startHeight = editorPanel.offsetHeight;
document.addEventListener('mousemove', resize);
document.addEventListener('mouseup', stopResize);
document.addEventListener('touchmove', resize);
document.addEventListener('touchend', stopResize);
resizer.style.userSelect = 'none';
resizer.style.touchAction = 'none';
}
function resize(e) {
e.preventDefault();
if (window.innerWidth > 768) { // 桌面模式 - 水平拖拽
const width = startWidth + (e.pageX - startX);
// 限制最小宽度
if (width >= 300 && width <= container.offsetWidth - 300) {
editorPanel.style.width = width + 'px';
previewPanel.style.width = (container.offsetWidth - width - 8) + 'px';
}
} else { // 移动模式 - 垂直拖拽
const height = startHeight + (e.pageY - startY);
// 限制最小高度
if (height >= 200 && height <= container.offsetHeight - 200) {
editorPanel.style.height = height + 'px';
previewPanel.style.height = (container.offsetHeight - height - 8) + 'px';
}
}
}
function stopResize() {
document.removeEventListener('mousemove', resize);
document.removeEventListener('mouseup', stopResize);
document.removeEventListener('touchmove', resize);
document.removeEventListener('touchend', stopResize);
}
resizer.addEventListener('mousedown', initDrag);
resizer.addEventListener('touchstart', initDrag);
});
// 预览全屏功能
function openPreviewFullscreen() {
const modal = document.getElementById('previewModal');
const fullscreenFrame = document.getElementById('fullscreenPreview');
const modalTitle = document.getElementById('modalTitle');
const code = document.getElementById('html-code').value;
// 提取页面标题
const titleMatch = code.match(/<title>(.*?)<\/title>/i);
modalTitle.textContent = titleMatch ? titleMatch[1] : '网页预览';
// 设置iframe内容
fullscreenFrame.srcdoc = code;
// 显示模态框
modal.style.display = 'flex';
}
function closePreviewFullscreen() {
const modal = document.getElementById('previewModal');
modal.style.display = 'none';
}
// 点击模态框背景关闭
document.getElementById('previewModal').addEventListener('click', function(e) {
if (e.target === this) {
closePreviewFullscreen();
}
});
// 编辑区全屏功能
function toggleEditorFullscreen() {
const element = document.getElementById('editor-panel');
const isFullscreen = element.classList.contains('fullscreen');
updateHighlight();
if (isFullscreen) {
// 退出全屏
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
element.classList.remove('fullscreen');
// 恢复按钮
const closeBtn = document.getElementById('editor-fullscreen-close');
if (closeBtn) closeBtn.remove();
} else {
// 进入全屏
if (element.requestFullscreen) {
element.requestFullscreen();
} else if (element.webkitRequestFullscreen) {
element.webkitRequestFullscreen();
} else if (element.msRequestFullscreen) {
element.msRequestFullscreen();
}
element.classList.add('fullscreen');
}
}
// 下载代码功能
function downloadCode() {
const code = document.getElementById('html-code').value;
const blob = new Blob([code], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'my-html-page.html';
document.body.appendChild(a);
a.click();
// 清理
setTimeout(() => {
document.body.removeChild(a);
URL.revokeObjectURL(url);
}, 0);
showMessage('代码已下载', 'success');
}
// 页面加载时初始化
document.addEventListener('DOMContentLoaded', function() {
runCode(); // 初始运行示例代码
setupEventListeners();
updateHighlight();
});
// 添加代码格式化功能
function formatCode() {
const codeEditor = document.getElementById('html-code');
try {
// HTML格式化处理
let formatted = '';
let indent = 0;
const lines = codeEditor.value.split('\n');
lines.forEach(line => {
const trimmed = line.trim();
if (trimmed.startsWith('</')) indent = Math.max(0, indent - 4);
formatted += ' '.repeat(indent) + trimmed + '\n';
if (trimmed.startsWith('<') && !trimmed.startsWith('</') && !trimmed.endsWith('/>')) {
indent += 4;
}
});
codeEditor.value = formatted;
codeEditor.style.height = 'auto';
codeEditor.style.height = (codeEditor.scrollHeight) + 'px';
showMessage('代码已格式化', 'success');
} catch (error) {
showMessage('格式化失败: ' + error.message, 'error');
}
}
// 设置事件监听器
function setupEventListeners() {
const codeEditor = document.getElementById('html-code');
// 代码编辑器自动调整高度 + 自动保存
codeEditor.addEventListener('input', function() {
this.style.height = 'auto';
this.style.height = (this.scrollHeight) + 'px';
// 防抖处理:1秒内无输入才保存
clearTimeout(this.saveTimer);
this.saveTimer = setTimeout(() => {
localStorage.setItem('htmlLearningCode', this.value);
showMessage('代码已自动保存', 'info');
}, 1000);
});
// 快捷键支持
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'Enter') {
runCode();
e.preventDefault(); // 防止默认行为
}
});
}
// 显示指定区域
function showSection(section) {
// 隐藏所有区域
document.getElementById('editor-section').classList.add('hidden');
document.getElementById('examples-section').classList.add('hidden');
document.getElementById('resources-section').classList.add('hidden');
// 显示目标区域
const targetSection = document.getElementById(section + '-section');
targetSection.classList.remove('hidden');
targetSection.classList.add('fade-in');
}
// 运行代码
function runCode() {
const code = document.getElementById('html-code').value;
const preview = document.getElementById('preview');
try {
// 清空预览区域
preview.innerHTML = '';
// 创建iframe来安全运行代码
const iframe = document.createElement('iframe');
iframe.style.width = '100%';
iframe.style.height = '100%';
iframe.style.border = 'none';
iframe.onload = function() {
// 增加对文档对象的检查
const iframeWindow = iframe.contentWindow;
if (!iframeWindow) {
throw new Error("无法访问iframe窗口");
}
const iframeDoc = iframeWindow.document;
if (!iframeDoc) {
throw new Error("无法访问iframe文档");
}
iframeDoc.open();
iframeDoc.write(code);
iframeDoc.close();
};
// 先将iframe添加到DOM再加载内容
preview.appendChild(iframe);
// 保存代码到本地存储
localStorage.setItem('htmlLearningCode', code);
// 显示成功消息
showMessage('代码运行成功!', 'success');
} catch (error) {
showMessage('运行出错: ' + error.message, 'error');
}
}
// 重置代码
function resetCode() {
codeInput.value = defaultCode;
runCode();
updateHighlight();
showMessage('代码已重置为默认示例', 'info');
}
// 加载示例代码
function loadExample(exampleId) {
if (examples[exampleId]) {
document.getElementById('html-code').value = examples[exampleId];
runCode();
updateHighlight();
showSection('editor');
showMessage('示例代码已加载,点击运行查看效果', 'info');
}
}
// 显示消息提示
function showMessage(message, type) {
// 移除现有消息
const existingMessage = document.querySelector('.message-toast');
if (existingMessage) {
existingMessage.remove();
}
// 创建新消息
const messageDiv = document.createElement('div');
messageDiv.className = `message-toast fixed top-4 right-4 p-4 rounded-lg shadow-lg z-50 transition transform duration-300 ${
type === 'success' ? 'bg-green-500' :
type === 'error' ? 'bg-red-500' : 'bg-blue-500'
} text-white`;
messageDiv.textContent = message;
document.body.appendChild(messageDiv);
// 3秒后自动消失
setTimeout(() => {
messageDiv.style.transform = 'translateX(100%)';
setTimeout(() => messageDiv.remove(), 300);
}, 3000);
}
// 从本地存储加载代码
function loadFromStorage() {
const savedCode = localStorage.getItem('htmlLearningCode');
if (savedCode) {
document.getElementById('html-code').value = savedCode;
}
}
// 初始化本地存储功能
loadFromStorage();
updateHighlight();