-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-diff.html
More file actions
311 lines (270 loc) · 8.82 KB
/
Copy pathtext-diff.html
File metadata and controls
311 lines (270 loc) · 8.82 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文本差异对比</title>
<style>
body {
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
background-color: #f5f5f5;
min-height: 100vh;
box-sizing: border-box;
}
/* 返回按钮样式 */
.header {
display: flex;
align-items: center;
margin-bottom: 20px;
gap: 15px;
}
.back-button {
padding: 8px 15px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
display: flex;
align-items: center;
gap: 5px;
}
.back-button:hover {
background-color: #34495e;
}
.page-title {
font-size: 18px;
color: #2c3e50;
margin: 0;
}
.diff-container {
display: flex;
gap: 20px;
max-width: 1200px;
margin: 0 auto;
height: calc(100vh - 140px);
}
.text-area {
flex: 1;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
display: flex;
flex-direction: column;
height: 100%;
}
.text-area h3 {
margin-top: 0;
margin-bottom: 15px;
color: #2c3e50;
}
.diff-editor {
width: 100%;
flex: 1;
padding: 15px;
font-size: 14px;
line-height: 1.6;
border: 1px solid #ddd;
border-radius: 5px;
background-color: white;
overflow-y: auto;
white-space: pre-wrap;
word-wrap: break-word;
box-sizing: border-box;
}
.diff-editor div {
padding: 2px 5px;
margin: 0;
min-height: 1.6em;
white-space: pre-wrap;
word-wrap: break-word;
background-color: white;
}
.line-left {
background-color: #ffe6e6 !important;
}
.line-right {
background-color: #e6ffe6 !important;
}
.same-line {
background-color: #e6f3ff !important;
}
/* 底部按钮样式 */
.bottom-container {
text-align: center;
margin-top: 20px;
padding: 20px;
}
.compare-button {
padding: 12px 30px;
font-size: 16px;
background-color: #2c3e50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.compare-button:hover {
background-color: #34495e;
}
/* 添加说明样式 */
.instructions {
background-color: #f8f9fa;
padding: 15px 20px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
.instructions h3 {
margin: 0 0 10px 0;
color: #2c3e50;
font-size: 16px;
}
.color-example {
display: flex;
gap: 20px;
}
.color-item {
display: flex;
align-items: center;
gap: 8px;
}
.color-box {
width: 20px;
height: 20px;
border-radius: 4px;
}
.red-box {
background-color: #ffe6e6;
}
.green-box {
background-color: #e6ffe6;
}
.blue-box {
background-color: #e6f3ff;
}
</style>
</head>
<body>
<!-- 添加使用说明 -->
<div class="instructions">
<h3>使用说明:</h3>
<div class="color-example">
<div class="color-item">
<div class="color-box red-box"></div>
<span>左侧独有内容</span>
</div>
<div class="color-item">
<div class="color-box green-box"></div>
<span>右侧独有内容</span>
</div>
<div class="color-item">
<div class="color-box blue-box"></div>
<span>两侧相同内容</span>
</div>
</div>
</div>
<!-- 返回按钮和标题 -->
<div class="header">
<button class="back-button" onclick="window.location.href='aa.html'">
← 返回
</button>
<h2 class="page-title">文本差异对比</h2>
</div>
<div class="diff-container">
<div class="text-area">
<h3>文本 1</h3>
<div id="editor1" class="diff-editor" contenteditable="true" spellcheck="false"></div>
</div>
<div class="text-area">
<h3>文本 2</h3>
<div id="editor2" class="diff-editor" contenteditable="true" spellcheck="false"></div>
</div>
</div>
<!-- 添加底部按钮 -->
<div class="bottom-container">
<button class="compare-button" onclick="compareTexts()">开始对比</button>
</div>
<script>
const editor1 = document.getElementById('editor1');
const editor2 = document.getElementById('editor2');
function compareTexts() {
const lines1 = editor1.innerText.split('\n');
const lines2 = editor2.innerText.split('\n');
const lines2Set = new Set(lines2);
const lines1Set = new Set(lines1);
// 更新编辑器1的内容和样式
editor1.innerHTML = lines1.map(line => {
const div = document.createElement('div');
div.textContent = line || ' '; // 确保空行也有高度
if (lines2Set.has(line) && line.trim() !== '') {
div.className = 'same-line';
} else if (line.trim() !== '') {
div.className = 'line-left';
}
return div.outerHTML;
}).join('');
// 更新编辑器2的内容和样式
editor2.innerHTML = lines2.map(line => {
const div = document.createElement('div');
div.textContent = line || ' '; // 确保空行也有高度
if (lines1Set.has(line) && line.trim() !== '') {
div.className = 'same-line';
} else if (line.trim() !== '') {
div.className = 'line-right';
}
return div.outerHTML;
}).join('');
// 确保编辑器始终至少有一个div
if (!editor1.hasChildNodes()) {
editor1.innerHTML = '<div><br></div>';
}
if (!editor2.hasChildNodes()) {
editor2.innerHTML = '<div><br></div>';
}
}
// 处理按键事件,确保正常换行和对齐
function handleKeyPress(e) {
if (e.key === 'Enter') {
e.preventDefault();
const selection = window.getSelection();
const range = selection.getRangeAt(0);
// 创建新的div元素
const newDiv = document.createElement('div');
newDiv.innerHTML = '<br>';
// 获取当前行的div
let currentDiv = range.startContainer;
while (currentDiv && currentDiv.nodeName !== 'DIV') {
currentDiv = currentDiv.parentNode;
}
// 在当前行后插入新行
if (currentDiv && currentDiv.parentNode) {
currentDiv.parentNode.insertBefore(newDiv, currentDiv.nextSibling);
// 移动光标到新行
range.setStart(newDiv, 0);
range.setEnd(newDiv, 0);
selection.removeAllRanges();
selection.addRange(range);
}
}
}
editor1.addEventListener('keypress', handleKeyPress);
editor2.addEventListener('keypress', handleKeyPress);
// 监听粘贴事件
function handlePaste(e) {
e.preventDefault();
const text = (e.clipboardData || window.clipboardData).getData('text');
document.execCommand('insertText', false, text);
}
editor1.addEventListener('paste', handlePaste);
editor2.addEventListener('paste', handlePaste);
// 初始化编辑器
editor1.innerHTML = '<div><br></div>';
editor2.innerHTML = '<div><br></div>';
</script>
</body>
</html>