-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
616 lines (523 loc) · 22.3 KB
/
Copy pathscripts.js
File metadata and controls
616 lines (523 loc) · 22.3 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
const BACKEND_URL = 'https://backend-compare.onrender.com';
async function apiCall(path, method = 'GET', payload = null) {
console.log(`🔄 Calling ${path}`);
const config = { method, credentials: 'include' };
if (payload) {
config.headers = { 'Content-Type': 'application/json' };
config.body = JSON.stringify(payload);
}
try {
const res = await fetch(BACKEND_URL + path, config);
console.log(`✅ ${path} status: ${res.status}`);
return res.ok ? await res.json() : {};
} catch (e) {
console.error(`❌ ${path} error:`, e);
return {};
}
}
window.addEventListener('load', async () => {
console.log('🚀 Text-Comparison loaded - starting cookie setup');
await apiCall('/set-session', 'POST', { userId: 'user_' + Date.now() });
const data = await apiCall('/get-session');
const ta = document.getElementById('text2');
if (data.user_text && ta) {
ta.value = data.user_text;
console.log('✅ Restored text from cookie!');
} else {
console.log('ℹ️ No saved text (first visit)');
}
if (ta) {
let timer;
ta.addEventListener('input', e => {
clearTimeout(timer);
timer = setTimeout(() => {
apiCall('/save-input', 'POST', { text: e.target.value });
console.log('💾 Auto-saved text');
}, 800);
});
}
});
document.addEventListener('DOMContentLoaded', (event) => {
// Set up PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js';
// Text comparison event listeners
document.getElementById('compareButton').addEventListener('click', () => {
const text1 = document.getElementById('text1').value;
const text2 = document.getElementById('text2').value;
const outputOriginal = document.getElementById('outputOriginalContent');
const outputModified = document.getElementById('outputModifiedContent');
const diff = getDiff(text1, text2);
outputOriginal.innerHTML = diff.originalHtml;
outputModified.innerHTML = diff.modifiedHtml;
updateHeaderStats(diff.stats);
});
document.getElementById('resetButton').addEventListener('click', () => {
document.getElementById('text1').value = '';
document.getElementById('text2').value = '';
document.getElementById('outputOriginalContent').innerHTML = '';
document.getElementById('outputModifiedContent').innerHTML = '';
resetHeaderStats();
});
// Mode toggle event listeners
document.getElementById('textModeBtn').addEventListener('click', () => {
setMode('text');
});
document.getElementById('pdfModeBtn').addEventListener('click', () => {
setMode('pdf');
});
// PDF event listeners
document.getElementById('pdfOriginal').addEventListener('change', handlePdfUpload('original'));
document.getElementById('pdfModified').addEventListener('change', handlePdfUpload('modified'));
document.getElementById('comparePdfButton').addEventListener('click', comparePdfs);
document.getElementById('resetPdfButton').addEventListener('click', resetPdfs);
document.getElementById('prevPage').addEventListener('click', () => changePage(-1));
document.getElementById('nextPage').addEventListener('click', () => changePage(1));
resetHeaderStats();
});
// Mode switching
function setMode(mode) {
const textSection = document.getElementById('textSection');
const pdfSection = document.getElementById('pdfSection');
const textModeBtn = document.getElementById('textModeBtn');
const pdfModeBtn = document.getElementById('pdfModeBtn');
if (mode === 'text') {
textSection.style.display = 'flex';
pdfSection.style.display = 'none';
textModeBtn.classList.add('active');
pdfModeBtn.classList.remove('active');
} else {
textSection.style.display = 'none';
pdfSection.style.display = 'flex';
textModeBtn.classList.remove('active');
pdfModeBtn.classList.add('active');
}
}
// PDF State
const pdfState = {
originalPdf: null,
modifiedPdf: null,
originalTextItems: [],
modifiedTextItems: [],
currentPage: 1,
totalPages: 1,
scale: 1.5,
comparisonDone: false
};
// Handle PDF upload
function handlePdfUpload(type) {
return async function(event) {
const file = event.target.files[0];
if (!file) return;
const filenameSpan = document.getElementById(type === 'original' ? 'pdfOriginalName' : 'pdfModifiedName');
filenameSpan.textContent = file.name;
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
if (type === 'original') {
pdfState.originalPdf = pdf;
} else {
pdfState.modifiedPdf = pdf;
}
// Reset comparison state when new PDF is loaded
pdfState.comparisonDone = false;
document.getElementById('pdfStatsContainer').style.display = 'none';
// Update total pages to min of both PDFs if both are loaded
updatePageCount();
// Render preview
if (type === 'original' && pdfState.originalPdf) {
await renderPdfPage(pdfState.originalPdf, 1, 'pdfCanvasOriginal', 'annotationOriginal');
} else if (type === 'modified' && pdfState.modifiedPdf) {
await renderPdfPage(pdfState.modifiedPdf, 1, 'pdfCanvasModified', 'annotationModified');
}
};
}
function updatePageCount() {
if (pdfState.originalPdf && pdfState.modifiedPdf) {
pdfState.totalPages = Math.max(pdfState.originalPdf.numPages, pdfState.modifiedPdf.numPages);
} else if (pdfState.originalPdf) {
pdfState.totalPages = pdfState.originalPdf.numPages;
} else if (pdfState.modifiedPdf) {
pdfState.totalPages = pdfState.modifiedPdf.numPages;
}
updatePageInfo();
}
function updatePageInfo() {
document.getElementById('pageInfo').textContent = `Page ${pdfState.currentPage} of ${pdfState.totalPages}`;
document.getElementById('prevPage').disabled = pdfState.currentPage <= 1;
document.getElementById('nextPage').disabled = pdfState.currentPage >= pdfState.totalPages;
}
async function changePage(delta) {
const newPage = pdfState.currentPage + delta;
if (newPage < 1 || newPage > pdfState.totalPages) return;
pdfState.currentPage = newPage;
updatePageInfo();
// Re-render current page
if (pdfState.originalPdf) {
await renderPdfPage(pdfState.originalPdf, pdfState.currentPage, 'pdfCanvasOriginal', 'annotationOriginal');
}
if (pdfState.modifiedPdf) {
await renderPdfPage(pdfState.modifiedPdf, pdfState.currentPage, 'pdfCanvasModified', 'annotationModified');
}
// Re-apply highlights if comparison was done
if (pdfState.comparisonDone) {
await applyHighlightsForCurrentPage();
}
}
// Render a PDF page to canvas
async function renderPdfPage(pdf, pageNum, canvasId, annotationId) {
const canvas = document.getElementById(canvasId);
const ctx = canvas.getContext('2d');
const annotationLayer = document.getElementById(annotationId);
const wrapper = canvas.parentElement;
if (pageNum > pdf.numPages) {
// Clear canvas if page doesn't exist
canvas.width = 0;
canvas.height = 0;
annotationLayer.innerHTML = '';
return null;
}
const page = await pdf.getPage(pageNum);
const viewport = page.getViewport({ scale: pdfState.scale });
canvas.width = viewport.width;
canvas.height = viewport.height;
// Set up wrapper and annotation layer dimensions to match canvas
wrapper.style.width = viewport.width + 'px';
wrapper.style.height = viewport.height + 'px';
annotationLayer.style.width = viewport.width + 'px';
annotationLayer.style.height = viewport.height + 'px';
await page.render({
canvasContext: ctx,
viewport: viewport
}).promise;
return { page, viewport };
}
// Extract text with coordinates from a PDF using PDF.js text layer approach
async function extractTextWithCoordinates(pdf) {
const allTextItems = [];
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
const page = await pdf.getPage(pageNum);
const viewport = page.getViewport({ scale: pdfState.scale });
const textContent = await page.getTextContent();
// Process each text item and compute its bounding box
const pageTextItems = [];
for (const item of textContent.items) {
if (!item.str || item.str.trim().length === 0) continue;
// Use PDF.js utility to transform coordinates properly
const tx = pdfjsLib.Util.transform(viewport.transform, item.transform);
// Extract font size from the transform matrix
// The font size is encoded in tx[0] (horizontal scale) or tx[3] (vertical scale)
const fontHeight = Math.abs(tx[3]);
const fontScaleX = Math.abs(tx[0]);
// Position in viewport coordinates
const x = tx[4];
const y = tx[5] - fontHeight; // tx[5] is baseline, subtract height to get top
// Calculate width properly
// item.width from PDF.js getTextContent is in glyph space units
// It needs to be scaled by the font's horizontal scale factor
// The transform already includes the scale, so we multiply item.width by fontScaleX
let width;
if (item.width && item.width > 0 && item.width < 1000) {
// item.width appears to be in PDF points, scale by viewport scale
width = item.width * pdfState.scale;
} else {
// Fallback: estimate width from character count
const avgCharWidth = fontHeight * 0.55;
width = item.str.length * avgCharWidth;
}
// Sanity checks
if (width > viewport.width) width = item.str.length * fontHeight * 0.55;
if (x < 0 || x > viewport.width) continue;
if (y < 0 || y > viewport.height) continue;
pageTextItems.push({
text: item.str,
x: x,
y: y,
width: width,
height: fontHeight,
pageNum: pageNum
});
}
// Build full text from items
const fullText = pageTextItems.map(i => i.text).join(' ');
allTextItems.push({
pageNum,
items: pageTextItems,
fullText: fullText
});
}
return allTextItems;
}
// Compare PDFs
async function comparePdfs() {
if (!pdfState.originalPdf || !pdfState.modifiedPdf) {
alert('Please upload both PDFs before comparing.');
return;
}
// Show navigation
document.getElementById('pdfNavigation').style.display = 'flex';
// Extract text with coordinates from both PDFs
pdfState.originalTextItems = await extractTextWithCoordinates(pdfState.originalPdf);
pdfState.modifiedTextItems = await extractTextWithCoordinates(pdfState.modifiedPdf);
pdfState.comparisonDone = true;
pdfState.currentPage = 1;
updatePageInfo();
// Render first page of both PDFs
await renderPdfPage(pdfState.originalPdf, 1, 'pdfCanvasOriginal', 'annotationOriginal');
await renderPdfPage(pdfState.modifiedPdf, 1, 'pdfCanvasModified', 'annotationModified');
// Apply highlights
await applyHighlightsForCurrentPage();
}
// Apply highlights for the current page
async function applyHighlightsForCurrentPage() {
const pageNum = pdfState.currentPage;
// Get text items for current page
const originalPage = pdfState.originalTextItems.find(p => p.pageNum === pageNum);
const modifiedPage = pdfState.modifiedTextItems.find(p => p.pageNum === pageNum);
// Clear existing highlights
document.getElementById('annotationOriginal').innerHTML = '';
document.getElementById('annotationModified').innerHTML = '';
if (!originalPage && !modifiedPage) return;
// Get words with their positions from each page
const originalWords = extractWordsWithPositions(originalPage ? originalPage.items : []);
const modifiedWords = extractWordsWithPositions(modifiedPage ? modifiedPage.items : []);
// Get the text for diff comparison
const originalText = originalWords.map(w => w.text).join(' ');
const modifiedText = modifiedWords.map(w => w.text).join(' ');
// Get word-level diff
const diff = Diff.diffWords(originalText, modifiedText);
// Track word indices for highlighting
let originalWordIdx = 0;
let modifiedWordIdx = 0;
let removedCount = 0;
let addedCount = 0;
diff.forEach(part => {
const words = part.value.trim().split(/\s+/).filter(w => w.length > 0);
const wordCount = words.length;
if (part.removed) {
// Highlight words in original PDF
for (let i = 0; i < wordCount && originalWordIdx < originalWords.length; i++) {
const wordInfo = originalWords[originalWordIdx];
if (wordInfo) {
createHighlight('annotationOriginal', wordInfo, 'removed');
}
originalWordIdx++;
}
removedCount += wordCount;
} else if (part.added) {
// Highlight words in modified PDF
for (let i = 0; i < wordCount && modifiedWordIdx < modifiedWords.length; i++) {
const wordInfo = modifiedWords[modifiedWordIdx];
if (wordInfo) {
createHighlight('annotationModified', wordInfo, 'added');
}
modifiedWordIdx++;
}
addedCount += wordCount;
} else {
// Unchanged - advance both indices
originalWordIdx += wordCount;
modifiedWordIdx += wordCount;
}
});
// Update stats
document.getElementById('pdfStatsContainer').style.display = 'flex';
document.getElementById('pdfRemovedCount').textContent = `Removed: ${removedCount} words`;
document.getElementById('pdfAddedCount').textContent = `Added: ${addedCount} words`;
}
// Extract words with their bounding boxes from text items
function extractWordsWithPositions(items) {
const words = [];
items.forEach(item => {
const text = item.text;
if (!text || text.trim().length === 0) return;
// Split item text into words
const itemWords = text.split(/(\s+)/);
let xOffset = 0;
const charWidth = item.width / Math.max(text.length, 1);
itemWords.forEach(word => {
if (word.trim().length > 0) {
words.push({
text: word,
x: item.x + xOffset,
y: item.y,
width: word.length * charWidth,
height: item.height
});
}
xOffset += word.length * charWidth;
});
});
return words;
}
// Create a highlight element
function createHighlight(containerId, rect, type) {
const container = document.getElementById(containerId);
// Get container dimensions for bounds checking
const containerWidth = parseFloat(container.style.width) || container.offsetWidth;
const containerHeight = parseFloat(container.style.height) || container.offsetHeight;
// Skip highlights that are completely outside bounds
if (rect.x < 0 || rect.y < 0 || rect.x > containerWidth || rect.y > containerHeight) {
return;
}
// Clamp dimensions to stay within container
const x = Math.max(0, rect.x);
const y = Math.max(0, rect.y);
const width = Math.min(rect.width, containerWidth - x);
const height = Math.min(rect.height + 4, containerHeight - y);
// Skip very small or invalid highlights
if (width <= 0 || height <= 0) return;
const highlight = document.createElement('div');
highlight.className = `pdf-highlight ${type}`;
highlight.style.left = x + 'px';
highlight.style.top = y + 'px';
highlight.style.width = width + 'px';
highlight.style.height = height + 'px';
container.appendChild(highlight);
}
// Reset PDFs
function resetPdfs() {
pdfState.originalPdf = null;
pdfState.modifiedPdf = null;
pdfState.originalTextItems = [];
pdfState.modifiedTextItems = [];
pdfState.currentPage = 1;
pdfState.totalPages = 1;
pdfState.comparisonDone = false;
document.getElementById('pdfOriginal').value = '';
document.getElementById('pdfModified').value = '';
document.getElementById('pdfOriginalName').textContent = '';
document.getElementById('pdfModifiedName').textContent = '';
const canvasOriginal = document.getElementById('pdfCanvasOriginal');
const canvasModified = document.getElementById('pdfCanvasModified');
canvasOriginal.getContext('2d').clearRect(0, 0, canvasOriginal.width, canvasOriginal.height);
canvasModified.getContext('2d').clearRect(0, 0, canvasModified.width, canvasModified.height);
canvasOriginal.width = 0;
canvasOriginal.height = 0;
canvasModified.width = 0;
canvasModified.height = 0;
document.getElementById('annotationOriginal').innerHTML = '';
document.getElementById('annotationModified').innerHTML = '';
document.getElementById('pdfNavigation').style.display = 'none';
document.getElementById('pdfStatsContainer').style.display = 'none';
updatePageInfo();
}
// ==================== Original Text Comparison Functions ====================
function getDiff(text1, text2) {
// Use diffWordsWithSpace to find differences at the word level across the entire text.
// This allows the algorithm to align paragraphs even if newlines are added or removed,
// as it naturally synchronizes the rows whenever a newline is encountered in unchanged text.
const diff = Diff.diffWordsWithSpace(text1, text2);
const originalLines = [];
const modifiedLines = [];
let oRow = [], mRow = [];
let removedCount = 0;
let addedCount = 0;
diff.forEach(part => {
const value = part.value;
const lines = value.split('\n');
for (let i = 0; i < lines.length; i++) {
const val = lines[i];
const escaped = escapeHtml(val);
const isLast = (i === lines.length - 1);
if (part.removed) {
if (val) {
oRow.push(`<span class="removed">${escaped}</span>`);
removedCount++;
}
if (!isLast) {
// Newline in removed text: end of a row on the left.
// We flush the current row for both sides to maintain vertical alignment.
originalLines.push(oRow.join(''));
modifiedLines.push(mRow.join(''));
oRow = [];
mRow = [];
}
} else if (part.added) {
if (val) {
mRow.push(`<span class="added">${escaped}</span>`);
addedCount++;
}
if (!isLast) {
// Newline in added text: end of a row on the right.
originalLines.push(oRow.join(''));
modifiedLines.push(mRow.join(''));
oRow = [];
mRow = [];
}
} else {
if (val) {
oRow.push(escaped);
mRow.push(escaped);
}
if (!isLast) {
// Newline in unchanged text: synchronization point for both sides.
originalLines.push(oRow.join(''));
modifiedLines.push(mRow.join(''));
oRow = [];
mRow = [];
}
}
}
});
// Flush any remaining text in the final row
if (oRow.length > 0 || mRow.length > 0) {
originalLines.push(oRow.join(''));
modifiedLines.push(mRow.join(''));
}
const originalStats = getTextStats(text1);
const modifiedStats = getTextStats(text2);
return {
originalHtml: originalLines.map((l, i) => `<div>${i + 1}: ${l}</div>`).join('\n'),
modifiedHtml: modifiedLines.map((l, i) => `<div>${i + 1}: ${l}</div>`).join('\n'),
stats: {
originalStats,
modifiedStats,
removedCount,
addedCount
}
};
}
function getTextStats(text) {
const trimmed = text.trim();
return {
wordCount: trimmed ? trimmed.split(/\s+/).length : 0,
charCount: text.length
};
}
function escapeHtml(string) {
const entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
return String(string).replace(/[&<>"'`=\/]/g, s => entityMap[s]);
}
function copyToClipboard(elementId) {
const el = document.createElement('textarea');
el.value = document.getElementById(elementId).innerText;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
function updateHeaderStats(stats) {
const { originalStats, modifiedStats, removedCount = 0, addedCount = 0 } = stats || {};
document.getElementById('originalWordCount').textContent = `Words: ${originalStats?.wordCount ?? 0}`;
document.getElementById('originalCharCount').textContent = `Characters: ${originalStats?.charCount ?? 0}`;
document.getElementById('originalDiffCount').textContent = `Removed: ${removedCount}`;
document.getElementById('modifiedWordCount').textContent = `Words: ${modifiedStats?.wordCount ?? 0}`;
document.getElementById('modifiedCharCount').textContent = `Characters: ${modifiedStats?.charCount ?? 0}`;
document.getElementById('modifiedDiffCount').textContent = `Added: ${addedCount}`;
}
function resetHeaderStats() {
document.getElementById('originalWordCount').textContent = 'Words: 0';
document.getElementById('originalCharCount').textContent = 'Characters: 0';
document.getElementById('originalDiffCount').textContent = 'Removed: 0';
document.getElementById('modifiedWordCount').textContent = 'Words: 0';
document.getElementById('modifiedCharCount').textContent = 'Characters: 0';
document.getElementById('modifiedDiffCount').textContent = 'Added: 0';
}