-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhtml-editor.html
More file actions
483 lines (419 loc) · 15.8 KB
/
html-editor.html
File metadata and controls
483 lines (419 loc) · 15.8 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#FFFFFF">
<meta name="color-scheme" content="light">
<title>HTML 编辑器</title>
<!-- 异步加载字体 -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&family=Roboto+Mono:wght@400&display=swap" rel="stylesheet" media="print" onload="this.media='all'">
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" rel="stylesheet" media="print" onload="this.media='all'">
<noscript>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" rel="stylesheet">
</noscript>
<style>
:root {
--md-sys-color-primary-container: #EADDFF;
--md-sys-color-on-primary-container: #21005D;
--md-sys-color-outline: #79747E;
--md-sys-color-surface-variant: #F3EDF7;
--md-sys-color-on-surface: #1D1B20;
--md-sys-typescale-body: 'Roboto', sans-serif;
--md-sys-typescale-code: 'Roboto Mono', monospace;
}
* {
-webkit-tap-highlight-color: transparent;
box-sizing: border-box;
}
@keyframes force-show {
0% { opacity: 0; }
100% { opacity: 1; }
}
html, body {
margin: 0;
padding: 0;
background-color: #FFFFFF;
color: #1D1B20;
height: 100dvh;
width: 100vw;
overflow: hidden;
display: flex;
flex-direction: column;
opacity: 0;
animation: force-show 0.5s ease-in 1.5s forwards;
}
body.js-loaded {
animation: none;
opacity: 1;
transition: opacity 0.4s ease;
}
.app-container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
position: relative;
max-width: none;
}
.editor-wrapper {
flex-grow: 1;
padding: 0;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
}
#visualEditor {
width: 100%;
height: 100%;
border: none;
outline: none;
padding: 16px;
padding-bottom: 180px; /* 增加底部留白,防止被面板遮挡 */
font-size: 16px;
line-height: 1.6;
color: #1D1B20;
resize: none;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
display: block;
font-family: var(--md-sys-typescale-body);
}
#visualEditor:empty:before {
content: attr(data-placeholder);
color: var(--md-sys-color-outline);
opacity: 0.6;
display: block;
}
/* 图片在编辑器中最大宽度限制,防止撑破布局 */
#visualEditor img {
max-width: 100%;
height: auto;
}
#codeEditor {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: none;
outline: none;
padding: 16px;
padding-top: 20px;
padding-bottom: 140px;
font-family: var(--md-sys-typescale-code);
white-space: pre;
font-size: 13px;
color: #333;
background-color: #FFFFFF !important;
resize: none;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
display: none;
z-index: 10;
}
.switch-container {
position: absolute;
bottom: 30px;
width: 100%;
display: flex;
justify-content: center;
pointer-events: none;
z-index: 20;
}
.segmented-switch {
pointer-events: auto;
display: flex;
background-color: var(--md-sys-color-surface-variant);
border-radius: 50px;
padding: 4px;
height: 56px;
box-shadow: 0 4px 8px rgba(0,0,0,0.12);
}
.segment-btn {
border: none;
background: transparent;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
cursor: pointer;
font-family: var(--md-sys-typescale-body);
font-size: 14px;
font-weight: 500;
color: #49454F;
padding: 0 24px;
border-radius: 50px;
min-width: 110px;
transition: all 0.2s ease;
}
.segment-btn.active {
background-color: var(--md-sys-color-primary-container);
color: var(--md-sys-color-on-primary-container);
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.material-symbols-outlined {
font-family: 'Material Symbols Outlined';
font-weight: normal;
font-style: normal;
font-size: 20px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
}
/* --- 新增:属性检查器面板样式 --- */
#inspectorPanel {
position: absolute;
bottom: 100px; /* 位于切换按钮上方 */
left: 16px;
right: 16px;
background-color: #2b2930; /* 深色背景,突出显示 */
color: #f2f0f4;
border-radius: 12px;
padding: 12px 16px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
z-index: 30;
display: none; /* 默认隐藏 */
flex-direction: column;
gap: 8px;
font-family: var(--md-sys-typescale-code);
font-size: 12px;
animation: slideUp 0.2s ease-out;
}
@keyframes slideUp {
from { transform: translateY(10px); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
.inspector-header {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid rgba(255,255,255,0.2);
padding-bottom: 8px;
margin-bottom: 4px;
}
.inspector-tag {
font-weight: bold;
color: #d0bcff;
text-transform: uppercase;
font-size: 14px;
}
.inspector-close {
background: none;
border: none;
color: rgba(255,255,255,0.7);
cursor: pointer;
padding: 4px;
margin: -4px;
}
.inspector-row {
display: flex;
flex-direction: column;
gap: 2px;
word-break: break-all;
}
.inspector-label {
color: rgba(255,255,255,0.6);
font-size: 10px;
}
.inspector-value {
color: #e6e1e5;
background: rgba(255,255,255,0.1);
padding: 4px 6px;
border-radius: 4px;
user-select: text; /* 允许复制 */
}
</style>
</head>
<body>
<div class="app-container">
<div class="editor-wrapper">
<div id="visualEditor" contenteditable="true" data-placeholder="在此粘贴内容..."></div>
<textarea id="codeEditor" spellcheck="false"></textarea>
<!-- 新增:属性检查器面板 -->
<div id="inspectorPanel">
<div class="inspector-header">
<span id="inspectorTag" class="inspector-tag">TAG</span>
<button id="inspectorClose" class="inspector-close">
<span class="material-symbols-outlined" style="font-size: 18px;">close</span>
</button>
</div>
<div id="inspectorContent">
<!-- 动态内容将插入这里 -->
</div>
</div>
</div>
<div class="switch-container">
<div class="segmented-switch">
<button id="btnVisual" class="segment-btn active">
<span class="material-symbols-outlined">visibility</span>
<span class="btn-text">预览</span>
</button>
<button id="btnCode" class="segment-btn">
<span class="material-symbols-outlined">code</span>
<span class="btn-text">代码</span>
</button>
</div>
</div>
</div>
<script>
// --- 1. 国际化 (i18n) 处理 ---
const userLang = navigator.language || navigator.userLanguage;
const isChinese = userLang.startsWith('zh');
const i18n = {
title: isChinese ? "HTML 编辑器" : "HTML Editor",
placeholder: isChinese ? "在此粘贴内容..." : "Paste content here...",
preview: isChinese ? "预览" : "Preview",
code: isChinese ? "代码" : "Code",
style: isChinese ? "样式" : "Style",
src: isChinese ? "图片源" : "Source",
href: isChinese ? "链接" : "Link",
class: isChinese ? "类名" : "Class"
};
(function() {
if (!isChinese) {
document.title = i18n.title;
const visualEditor = document.getElementById('visualEditor');
if (visualEditor) visualEditor.setAttribute('data-placeholder', i18n.placeholder);
const btnVisualText = document.querySelector('#btnVisual .btn-text');
if (btnVisualText) btnVisualText.textContent = i18n.preview;
const btnCodeText = document.querySelector('#btnCode .btn-text');
if (btnCodeText) btnCodeText.textContent = i18n.code;
document.documentElement.lang = "en";
}
})();
// --- 2. 智能显示逻辑 ---
function showApp() {
document.body.classList.add('js-loaded');
}
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(showApp).catch(showApp);
} else {
showApp();
}
// --- 3. 编辑器核心逻辑 ---
const visualEditor = document.getElementById('visualEditor');
const codeEditor = document.getElementById('codeEditor');
const btnVisual = document.getElementById('btnVisual');
const btnCode = document.getElementById('btnCode');
// 检查器相关元素
const inspectorPanel = document.getElementById('inspectorPanel');
const inspectorTag = document.getElementById('inspectorTag');
const inspectorContent = document.getElementById('inspectorContent');
const inspectorClose = document.getElementById('inspectorClose');
let isCodeView = false;
const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
// --- 新增:点击元素显示属性逻辑 ---
visualEditor.addEventListener('click', (e) => {
// 如果点击的是编辑器本身(空白处),则隐藏面板
if (e.target === visualEditor) {
inspectorPanel.style.display = 'none';
return;
}
// 如果是链接,阻止默认跳转行为,方便编辑和查看
if (e.target.tagName === 'A') {
e.preventDefault();
}
const target = e.target;
const tagName = target.tagName.toLowerCase();
// 准备显示的数据
let infoHtml = '';
// 1. 获取 src (针对图片)
if (target.hasAttribute('src')) {
infoHtml += createInspectorRow(i18n.src, target.getAttribute('src'));
}
// 2. 获取 href (针对链接)
if (target.hasAttribute('href')) {
infoHtml += createInspectorRow(i18n.href, target.getAttribute('href'));
}
// 3. 获取 style (内联样式)
if (target.hasAttribute('style')) {
infoHtml += createInspectorRow(i18n.style, target.getAttribute('style'));
}
// 4. 获取 class
if (target.className && typeof target.className === 'string') {
infoHtml += createInspectorRow(i18n.class, target.className);
}
// 如果没有任何特殊属性,显示提示或仅显示标签
if (infoHtml === '') {
// 也可以选择不显示面板,或者显示“无内联样式”
// 这里我们选择显示面板,方便用户确认选中了什么
infoHtml = `<div class="inspector-row"><span class="inspector-label">Info</span><span class="inspector-value">No inline styles</span></div>`;
}
// 更新 UI
inspectorTag.textContent = `<${tagName}>`;
inspectorContent.innerHTML = infoHtml;
inspectorPanel.style.display = 'flex';
});
// 辅助函数:创建面板行 HTML
function createInspectorRow(label, value) {
// 简单的转义防止 XSS
const safeValue = value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
return `
<div class="inspector-row">
<span class="inspector-label">${label}</span>
<span class="inspector-value">${safeValue}</span>
</div>
`;
}
// 关闭面板
inspectorClose.addEventListener('click', () => {
inspectorPanel.style.display = 'none';
});
// 切换到代码模式时隐藏面板
function hideInspector() {
inspectorPanel.style.display = 'none';
}
// --- 格式化 HTML ---
function formatHTML(html) {
const tab = ' ';
let result = '';
let indent = '';
html = html.replace(/[\r\n]+/g, ' ').replace(/>\s+</g, '><').trim();
const tokens = html.split(/(<[^>]+>)/g).filter(s => s.trim() !== '');
tokens.forEach(token => {
let isClosing = token.startsWith('</');
let isOpening = token.startsWith('<') && !isClosing;
if (!token.startsWith('<')) token = token.trim();
if (token.length === 0) return;
let tagName = '';
if (isOpening) {
const match = token.match(/^<([a-z0-9]+)/i);
if (match) tagName = match[1].toLowerCase();
}
if (isClosing) indent = indent.substring(tab.length);
result += indent + token + '\n';
if (isOpening && !voidTags.includes(tagName) && !token.includes('/>')) indent += tab;
});
return result.trim();
}
btnVisual.addEventListener('click', () => {
if (!isCodeView) return;
visualEditor.innerHTML = codeEditor.value;
codeEditor.style.display = 'none';
btnVisual.classList.add('active');
btnCode.classList.remove('active');
isCodeView = false;
});
btnCode.addEventListener('click', () => {
if (isCodeView) return;
hideInspector(); // 切换时隐藏面板
const rawHTML = visualEditor.innerHTML;
codeEditor.value = formatHTML(rawHTML);
codeEditor.style.display = 'block';
visualEditor.innerHTML = '';
btnCode.classList.add('active');
btnVisual.classList.remove('active');
isCodeView = true;
});
</script>
</body>
</html>