-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
317 lines (247 loc) · 10.8 KB
/
script.js
File metadata and controls
317 lines (247 loc) · 10.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
const dmp = new diff_match_patch();
let diffResults = [];
let currentDiffIndex = 0;
let versionAContent = '';
let versionBContent = '';
// Store the full diff list for accurate navigation and rendering
let fullDiffsForNavigation = [];
// --- Debounce Helper Function ---
function debounce(func, delay) {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
// --- Line Number and Scroll Helpers ---
function calculateLineAndCol(text, index) {
// Splits the text up to the index to find the line breaks
const lines = text.substring(0, index).split('\n');
const line = lines.length; // The line number is the count of lines + 1
const col = lines[lines.length - 1].length + 1; // Column is characters on the last line segment
return { line, col };
}
function updateLineNumbers(lineNumbersDivId, content) {
const lineNumbersDiv = document.getElementById(lineNumbersDivId);
const lines = content.split('\n');
let numbers = '';
const lineCount = (lines.length === 1 && lines[0].length === 0) ? 1 : lines.length;
for (let i = 1; i <= lineCount; i++) {
numbers += i + '<br>';
}
lineNumbersDiv.innerHTML = numbers;
}
// Line Numbers for Input Textareas
function updateInputLineNumbers() {
const contentA = document.getElementById('versionAInput').value;
const contentB = document.getElementById('versionBInput').value;
updateLineNumbers('inputLineNumbersA', contentA);
updateLineNumbers('inputLineNumbersB', contentB);
}
// Sync scrolling for the display editors (Code Editors)
function syncScrollDisplay(sourceId, targetId) {
const source = document.getElementById(sourceId);
const target = document.getElementById(targetId);
target.scrollTop = source.scrollTop;
document.getElementById('lineNumbersA').scrollTop = source.scrollTop;
document.getElementById('lineNumbersB').scrollTop = source.scrollTop;
}
// Sync scrolling for the input textareas and their line number panels
function syncScrollInput(sourceInputId, targetNumbersId) {
const source = document.getElementById(sourceInputId);
const target = document.getElementById(targetNumbersId);
target.scrollTop = source.scrollTop;
}
// --- Rendering and Diff Functions ---
function clearActiveDiffHighlight() {
document.querySelectorAll('.active-diff').forEach(el => {
el.classList.remove('active-diff');
});
}
function renderDiff(editorId, diffs, isVersionA) {
const editorDiv = document.getElementById(editorId);
let html = '';
let contentForLineNumbers = '';
for (const diff of diffs) {
const op = diff[0];
const text = diff[1];
let spanClass = '';
let showText = true;
if (op === 1) {
spanClass = 'added';
if (isVersionA) showText = false;
} else if (op === -1) {
spanClass = 'removed';
if (!isVersionA) showText = false;
}
// Replace spaces with non-breaking spaces and escape HTML
const formattedText = text.replace(/ /g, '\u00a0').replace(/</g, '<').replace(/>/g, '>');
// Wrap text in an inline-block span for correct highlight application
html += `<span class="${spanClass}">${showText ? formattedText : ''}</span>`;
if (showText) {
contentForLineNumbers += text;
}
}
editorDiv.innerHTML = html;
return contentForLineNumbers;
}
let lastDiffCount = 0;
function runDiff(event) {
versionAContent = document.getElementById('versionAInput').value;
versionBContent = document.getElementById('versionBInput').value;
if (!versionAContent && !versionBContent) {
document.getElementById('percentageDisplay').textContent = `0.00%`;
document.getElementById('lineDisplay').textContent = '-';
document.getElementById('colDisplay').textContent = '-';
document.getElementById('codeEditorA').innerHTML = '';
document.getElementById('codeEditorB').innerHTML = '';
updateLineNumbers('lineNumbersA', '');
updateLineNumbers('lineNumbersB', '');
lastDiffCount = 0;
clearActiveDiffHighlight();
return;
}
const diffs = dmp.diff_main(versionAContent, versionBContent);
dmp.diff_cleanupSemantic(diffs);
fullDiffsForNavigation = diffs;
const newDiffResults = diffs.filter(d => d[0] !== 0);
const diffsChanged = newDiffResults.length !== lastDiffCount;
lastDiffCount = newDiffResults.length;
diffResults = newDiffResults;
const contentA = renderDiff('codeEditorA', fullDiffsForNavigation, true);
const contentB = renderDiff('codeEditorB', fullDiffsForNavigation, false);
updateLineNumbers('lineNumbersA', contentA);
updateLineNumbers('lineNumbersB', contentB);
updateStats(fullDiffsForNavigation, versionAContent.length);
if (diffResults.length > 0) {
if (diffsChanged) {
currentDiffIndex = 0;
}
// Always call navigateDiff to re-apply the highlight and scroll
navigateDiff(0, true);
} else {
clearActiveDiffHighlight();
}
}
function updateStats(diffs, totalLengthA) {
let equalChars = 0;
let firstChangeIndexA = -1; // Character index in Version A where the first change occurs
let currentCharIndexA = 0;
for (const diff of diffs) {
const op = diff[0];
const text = diff[1];
if (op === 0) {
equalChars += text.length;
currentCharIndexA += text.length;
} else if (op === -1) {
if (firstChangeIndexA === -1) {
firstChangeIndexA = currentCharIndexA;
}
currentCharIndexA += text.length;
} else if (op === 1) {
if (firstChangeIndexA === -1) {
firstChangeIndexA = currentCharIndexA;
}
}
}
const totalChars = versionAContent.length + versionBContent.length;
const totalDiffLength = totalChars - (2 * equalChars);
const percentage = totalChars > 0 ? (totalDiffLength / totalChars) * 100 : 0;
document.getElementById('percentageDisplay').textContent = `${percentage.toFixed(2)}%`;
if (firstChangeIndexA !== -1) {
// Correctly calculate line and column based on the full content string
const { line, col } = calculateLineAndCol(versionAContent, firstChangeIndexA);
document.getElementById('lineDisplay').textContent = line;
document.getElementById('colDisplay').textContent = col;
} else {
document.getElementById('lineDisplay').textContent = '-';
document.getElementById('colDisplay').textContent = '-';
}
}
// FUNCTIONAL NAVIGATION: Scrolls the display panels to the change and applies purple highlight.
function navigateDiff(indexOffset) {
if (diffResults.length === 0) return;
// Update index based on offset, with loop-around logic
currentDiffIndex += indexOffset;
if (currentDiffIndex < 0) {
currentDiffIndex = diffResults.length - 1;
} else if (currentDiffIndex >= diffResults.length) {
currentDiffIndex = 0;
}
const targetChange = diffResults[currentDiffIndex];
let charIndexA = 0;
// Find the character index corresponding to the start of the current change
for (const diff of fullDiffsForNavigation) {
if (diff === targetChange) {
break;
}
if (diff[0] === 0 || diff[0] === -1) {
charIndexA += diff[1].length;
}
}
// --- Scrolling ---
const { line } = calculateLineAndCol(versionAContent, charIndexA);
const editorA = document.getElementById('codeEditorA');
const editorB = document.getElementById('codeEditorB');
const lineHeight = 18;
const scrollPosition = (line - 1) * lineHeight;
editorA.scrollTop = scrollPosition;
editorB.scrollTop = scrollPosition;
document.getElementById('lineNumbersA').scrollTop = scrollPosition;
document.getElementById('lineNumbersB').scrollTop = scrollPosition;
// --- Highlighting ---
clearActiveDiffHighlight();
const spansA = editorA.querySelectorAll('span');
const spansB = editorB.querySelectorAll('span');
let currentSpanIndex = 0;
for (let i = 0; i < fullDiffsForNavigation.length; i++) {
const diff = fullDiffsForNavigation[i];
if (diff === targetChange) {
// Apply the active highlight (purple) to the span in the respective editor
// Check span in version A (removed diffs)
if (diff[0] === -1 || diff[0] === 0) {
if (spansA[currentSpanIndex]) spansA[currentSpanIndex].classList.add('active-diff');
}
// Check span in version B (added diffs)
if (diff[0] === 1 || diff[0] === 0) {
if (spansB[currentSpanIndex]) spansB[currentSpanIndex].classList.add('active-diff');
}
break;
}
// The span index always increments for every diff block
currentSpanIndex++;
}
// Update status bar for navigation
document.getElementById('lineDisplay').textContent = line;
document.getElementById('colDisplay').textContent = calculateLineAndCol(versionAContent, charIndexA).col;
}
function clearEditor(version) {
const inputId = (version === 'A') ? 'versionAInput' : 'versionBInput';
document.getElementById(inputId).value = '';
runDiff(null);
}
// Apply the debounce wrapper to runDiff
const debouncedRunDiff = debounce(runDiff, 300);
// --- Event Listeners ---
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('nextBtn').addEventListener('click', () => navigateDiff(1));
document.getElementById('prevBtn').addEventListener('click', () => navigateDiff(-1));
// Input listeners: Update line numbers immediately, debounce the heavy diff calculation
document.getElementById('versionAInput').addEventListener('input', () => {
updateInputLineNumbers();
debouncedRunDiff();
});
document.getElementById('versionBInput').addEventListener('input', () => {
updateInputLineNumbers();
debouncedRunDiff();
});
// Listen to scroll events on the resizable textareas to sync line numbers
document.getElementById('versionAInput').addEventListener('scroll', () => syncScrollInput('versionAInput', 'inputLineNumbersA'));
document.getElementById('versionBInput').addEventListener('scroll', () => syncScrollInput('versionBInput', 'inputLineNumbersB'));
document.getElementById('clearA').addEventListener('click', () => clearEditor('A'));
document.getElementById('clearB').addEventListener('click', () => clearEditor('B'));
// Initial run on load
runDiff(null);
});