-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
216 lines (186 loc) · 8.6 KB
/
script.js
File metadata and controls
216 lines (186 loc) · 8.6 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
document.addEventListener('DOMContentLoaded', () => {
const darkModeToggle = document.getElementById('darkModeToggle');
const yearSpan = document.getElementById('year');
const htmlElement = document.documentElement;
// --- Dark Mode ---
const applyDarkMode = (isDark) => {
if (isDark) {
htmlElement.classList.add('dark');
if (darkModeToggle) darkModeToggle.textContent = '☀️'; // Sun icon for light mode switch
} else {
htmlElement.classList.remove('dark');
if (darkModeToggle) darkModeToggle.textContent = '🌙'; // Moon icon for dark mode switch
}
};
// Check localStorage for saved preference
const savedDarkMode = localStorage.getItem('darkMode');
// Check system preference if no saved preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Initialize dark mode based on saved pref or system pref
// Default to light mode if nothing is set
let isDarkMode = savedDarkMode !== null ? savedDarkMode === 'true' : prefersDark;
applyDarkMode(isDarkMode);
if (darkModeToggle) {
darkModeToggle.addEventListener('click', () => {
isDarkMode = !isDarkMode;
localStorage.setItem('darkMode', isDarkMode);
applyDarkMode(isDarkMode);
});
}
// Listen for system preference changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
// Only change if no explicit user preference is saved
if (localStorage.getItem('darkMode') === null) {
isDarkMode = e.matches;
applyDarkMode(isDarkMode);
}
});
// --- Footer Year ---
if (yearSpan) {
yearSpan.textContent = new Date().getFullYear();
}
// --- JSON Diff Logic (Placeholder) ---
const compareBtn = document.getElementById('compareBtn');
const jsonInput1 = document.getElementById('jsonInput1');
const jsonInput2 = document.getElementById('jsonInput2');
const diffOutput = document.getElementById('diffOutput');
const copyDiffBtn = document.getElementById('copyDiffBtn');
if (compareBtn && jsonInput1 && jsonInput2 && diffOutput) {
compareBtn.addEventListener('click', () => {
const jsonStr1 = jsonInput1.value.trim();
const jsonStr2 = jsonInput2.value.trim();
diffOutput.innerHTML = ''; // Clear previous results
copyDiffBtn.classList.add('hidden'); // Hide copy button initially
let obj1, obj2;
try {
obj1 = JSON.parse(jsonStr1);
} catch (e) {
// Use custom error class
diffOutput.innerHTML = '<p class="error-message">Error parsing JSON Input 1: ' + e.message + '</p>';
return;
}
try {
obj2 = JSON.parse(jsonStr2);
} catch (e) {
// Use custom error class
diffOutput.innerHTML = '<p class="error-message">Error parsing JSON Input 2: ' + e.message + '</p>';
return;
}
// Call the recursive semantic diff function
const diffResult = findJsonDiffRecursive(obj1, obj2);
// Check if any differences were found (using the results object directly)
if (Object.keys(diffResult.added).length === 0 && Object.keys(diffResult.removed).length === 0 && Object.keys(diffResult.changed).length === 0) {
// Use the display function even for "no differences" for consistency
displayDiff(diffResult, diffOutput);
// diffOutput.innerHTML = '<p class="text-green-500 dark:text-green-400">No differences found.</p>'; // Alternative direct message
} else {
displayDiff(diffResult, diffOutput);
copyDiffBtn.classList.remove('hidden'); // Show copy button
}
});
}
if (copyDiffBtn && diffOutput) {
copyDiffBtn.addEventListener('click', () => {
// Copy the text content of the diff output
navigator.clipboard.writeText(diffOutput.textContent || diffOutput.innerText)
.then(() => {
// Optional: Show feedback to the user
const originalText = copyDiffBtn.textContent;
copyDiffBtn.textContent = 'Copied!';
setTimeout(() => {
copyDiffBtn.textContent = originalText;
}, 1500);
})
.catch(err => {
console.error('Failed to copy diff: ', err);
// Optional: Show error feedback
const originalText = copyDiffBtn.textContent;
copyDiffBtn.textContent = 'Copy Failed';
setTimeout(() => {
copyDiffBtn.textContent = originalText;
}, 1500);
});
});
}
});
// --- Core Semantic JSON Diff Function ---
function findJsonDiffRecursive(obj1, obj2, path = '') {
const added = {};
const removed = {};
const changed = {};
const keys1 = new Set(Object.keys(obj1 ?? {}));
const keys2 = new Set(Object.keys(obj2 ?? {}));
const allKeys = new Set([...keys1, ...keys2]);
for (const key of allKeys) {
const currentPath = path ? `${path}.${key}` : key;
const val1 = obj1?.[key];
const val2 = obj2?.[key];
const type1 = typeof val1;
const type2 = typeof val2;
if (!keys2.has(key)) {
removed[currentPath] = val1;
} else if (!keys1.has(key)) {
added[currentPath] = val2;
} else if (type1 !== type2) {
// Different types are considered a change
changed[currentPath] = { from: val1, to: val2 };
} else if (Array.isArray(val1) && Array.isArray(val2)) {
// Basic array comparison: treat as changed if stringified versions differ.
// A more sophisticated array diff (e.g., longest common subsequence)
// could be implemented for finer-grained changes within arrays.
if (JSON.stringify(val1) !== JSON.stringify(val2)) {
changed[currentPath] = { from: val1, to: val2 };
}
} else if (type1 === 'object' && val1 !== null && val2 !== null) {
// Recursively compare nested objects
const nestedDiff = findJsonDiffRecursive(val1, val2, currentPath);
Object.assign(added, nestedDiff.added);
Object.assign(removed, nestedDiff.removed);
Object.assign(changed, nestedDiff.changed);
} else if (val1 !== val2) {
// Primitive values differ
changed[currentPath] = { from: val1, to: val2 };
}
}
return { added, removed, changed };
}
// --- Display Diff Function ---
function displayDiff(diffResult, outputElement) {
let html = '<pre class="whitespace-pre-wrap break-words">'; // Use pre for formatting, ensure wrapping
const formatValue = (value) => {
// Indent nested objects/arrays for readability
return JSON.stringify(value, null, 2);
};
const sections = {
Removed: { data: diffResult.removed, class: 'diff-removed', prefix: '-' },
Added: { data: diffResult.added, class: 'diff-added', prefix: '+' },
Changed: { data: diffResult.changed, class: 'diff-changed', prefix: '~' }
};
let hasContent = false;
for (const [title, section] of Object.entries(sections)) {
if (Object.keys(section.data).length > 0) {
hasContent = true;
html += `<strong class="block mt-2 mb-1">${title}:</strong>\n`; // Add some spacing
for (const key in section.data) {
if (title === 'Changed') {
const change = section.data[key];
// Display changed values with clear 'from' and 'to'
html += `<span class="${section.class}">`;
html += `${section.prefix} ${key}:\n`;
html += ` - From: ${formatValue(change.from)}\n`;
html += ` + To: ${formatValue(change.to)}`;
html += `</span>\n`;
} else {
// Display added or removed values
html += `<span class="${section.class}">${section.prefix} ${key}: ${formatValue(section.data[key])}</span>\n`;
}
}
}
}
if (!hasContent) {
// Use custom success class
html += '<span class="success-message">No differences found.</span>';
}
html += '</pre>';
outputElement.innerHTML = html;
}