-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
300 lines (263 loc) · 10.5 KB
/
editor.js
File metadata and controls
300 lines (263 loc) · 10.5 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
// editor.js
"use strict";
document.addEventListener("DOMContentLoaded", function () {
// Configuration for clickable elements for text highlighting.
const clickableElementsConfig = {
detected: { className: "detected" },
placeholder: { className: "anonymized" },
whitelist: { className: "whitelisted" }
};
const editor = document.getElementById("editor");
const highlight = document.getElementById("highlight");
const progressBar = document.getElementById("progressBar");
const progressStatus = document.getElementById("progressStatus"); // Optional status element
const anonymizeBtn = document.getElementById("anonymizeBtn");
const deanonymizeBtn = document.getElementById("deanonymizeBtn");
anonymizeBtn.disabled = true;
deanonymizeBtn.disabled = true;
let progressHideTimeout; // Timer for auto-hiding the status message
let hideBarAnimationTimeout; // Timer for the CSS fadeout
// Global arrays for detected sensitive data and placeholder tokens.
let detectedPIIinText = [];
let placeholderTokensInText = [];
// Update the debug mapping table, if available.
function updatePlaceholdersTable() {
const mappingContainer = document.getElementById("mappingTable");
if (!mappingContainer) return;
const mapping = window.anonymizer.getMapping();
let html = "<table><thead><tr><th>Original</th><th>Token</th><th>Type</th></tr></thead><tbody>";
mapping.forEach(entry => {
const original = entry[0];
const token = entry[1] === null ? "null" : entry[1];
const type = entry[2].placeholderPrefix;
html += `<tr>
<td>${Utils.escapeHtml(original)}</td>
<td>${Utils.escapeHtml(token)}</td>
<td>${Utils.escapeHtml(type)}</td>
</tr>`;
});
html += "</tbody></table>";
mappingContainer.innerHTML = html;
}
// Update the current mapping lists based on the editor text.
function updateCurrentMappingLists() {
const snapshot = editor.value;
const mapping = window.anonymizer.getMapping();
detectedPIIinText = mapping
.filter(entry => snapshot.includes(entry[0]))
.map(entry => entry[0]);
placeholderTokensInText = mapping
.filter(entry => snapshot.includes(entry[1]))
.map(entry => entry[1]);
}
// Update the UI of the progress bar.
function updateProgressUI(percent, message) {
if (window.anonymizer.scanCompleted) return;
if (progressBar) {
progressBar.style.display = "block";
progressBar.classList.add("active");
progressBar.classList.remove("fade-out");
const progressFill = document.getElementById("progressFill");
const progressText = document.getElementById("progressText");
if (progressFill) progressFill.style.width = percent + "%";
if (progressText) progressText.innerText = message;
}
}
const throttledProgressUI = Utils.throttle(updateProgressUI, 100);
// Callback when text scan is complete.
function handleProgressComplete() {
const progressFill = document.getElementById("progressFill");
const progressText = document.getElementById("progressText");
if (progressFill) {
progressFill.style.width = "100%";
progressFill.classList.add("complete");
}
if (progressText) {
progressText.innerHTML =
'Text scan complete <span style="font-size:24px;">' + Icons.check + '</span>';
}
updatePlaceholdersTable();
updateButtonStates();
clearTimeout(progressHideTimeout);
progressHideTimeout = setTimeout(hideProgressBar, 1000);
}
// Hide the progress bar.
function hideProgressBar() {
if (progressBar) {
progressBar.classList.add("fade-out");
hideBarAnimationTimeout = setTimeout(function () {
progressBar.classList.remove("active", "fade-out");
progressBar.style.display = "none";
const progressFill = document.getElementById("progressFill");
if (progressFill) {
progressFill.style.width = "0";
progressFill.classList.remove("complete");
}
}, 1000);
}
}
// Show a status message.
function showStatusMessage(message, clickHandler) {
if (progressHideTimeout) {
clearTimeout(progressHideTimeout);
}
if (hideBarAnimationTimeout) {
clearTimeout(hideBarAnimationTimeout);
}
progressBar.style.display = "block";
progressBar.classList.add("active");
progressBar.classList.remove("fade-out");
const progressFill = document.getElementById("progressFill");
const progressText = document.getElementById("progressText");
if (progressFill) {
progressFill.style.width = "100%";
progressFill.classList.add("complete");
}
if (progressText) {
progressText.innerHTML = message;
progressText.onclick = null;
if (clickHandler && typeof clickHandler === "function") {
progressText.style.cursor = "pointer";
progressText.onclick = clickHandler;
} else {
progressText.style.cursor = "default";
}
}
progressHideTimeout = setTimeout(function() {
hideProgressBar();
}, 2000);
}
// Update button states based on current text.
function updateButtonStates() {
anonymizeBtn.disabled = detectedPIIinText.length === 0;
deanonymizeBtn.disabled = placeholderTokensInText.length === 0;
}
// Unified highlight routine
function updateHighlight() {
const text = editor.value;
// If editor is empty, show placeholder and reset classes
if (!text.trim()) {
highlight.innerHTML = `<span class="placeholder">${editor.placeholder}</span>`;
highlight.className = "";
return;
}
// 1) Collect intervals
const mapping = window.anonymizer.getMapping();
const whitelistItems = window.anonymizer.whitelist.filter(wl => wl.trim() !== "");
// Whitelist intervals, merged
let whitelistIntervals = Utils.collectIntervals(text, whitelistItems, "whitelisted");
whitelistIntervals = Utils.mergeOverlappingIntervals(whitelistIntervals);
// Detected intervals, merged, then filter out those covered by whitelist
let detectedIntervals = Utils.collectIntervals(text, mapping.map(entry => entry[0]), "detected");
detectedIntervals = Utils.mergeOverlappingIntervals(detectedIntervals)
.filter(det => !whitelistIntervals.some(wl => wl.start <= det.start && wl.end >= det.end));
// Anonymized token intervals
const anonymizedIntervals = Utils.collectIntervals(text, mapping.map(entry => entry[1]), "anonymized");
// 2) Combine all, sort by start
const allIntervals = [...whitelistIntervals, ...detectedIntervals, ...anonymizedIntervals]
.sort((a, b) => a.start - b.start);
// 3) Build the highlight HTML
let resultHtml = "";
let currentIndex = 0;
for (const { start, end, className } of allIntervals) {
if (currentIndex < start) {
resultHtml += Utils.escapeHtml(text.substring(currentIndex, start));
}
resultHtml += `<span class="${className}">` +
Utils.escapeHtml(text.substring(start, end)) +
`</span>`;
currentIndex = end;
}
if (currentIndex < text.length) {
resultHtml += Utils.escapeHtml(text.substring(currentIndex));
}
highlight.innerHTML = resultHtml;
// 4) Update background class
if (detectedIntervals.length > 0) {
highlight.classList.add("highlight-detected");
highlight.classList.remove("highlight-clean");
} else {
highlight.classList.add("highlight-clean");
highlight.classList.remove("highlight-detected");
}
// 5) Sync height to editor
highlight.style.height = editor.scrollHeight + "px";
}
// throttle the heavy highlight redraw to max. once every 100 ms
const throttledUpdateHighlight = Utils.throttle(updateHighlight, 100);
// Update on mapping changes
function mappingChangeHandler() {
updateCurrentMappingLists();
updatePlaceholdersTable();
throttledUpdateHighlight();
}
// Update on textChange callback
function handleTextChange() {
editor.value = window.anonymizer.getText();
updateCurrentMappingLists();
throttledUpdateHighlight();
updatePlaceholdersTable();
}
// Register anonymizer callbacks
window.anonymizer.setOnMappingChange(mappingChangeHandler);
window.anonymizer.setOnProgress(throttledProgressUI);
window.anonymizer.setOnComplete(function(mapping) {
handleProgressComplete();
throttledUpdateHighlight();
});
window.anonymizer.setOnTextChange(handleTextChange);
// Editor event listeners
editor.addEventListener("input", function () {
clearTimeout(progressHideTimeout);
clearTimeout(hideBarAnimationTimeout);
window.anonymizer.setText(editor.value);
window.anonymizer.identifyPII();
throttledUpdateHighlight();
});
editor.addEventListener("scroll", function () {
highlight.style.transform = `translate(-${editor.scrollLeft}px, -${editor.scrollTop}px)`;
updateHighlight();
});
window.addEventListener("resize", function () {
throttledUpdateHighlight();
highlight.style.transform = `translate(-${editor.scrollLeft}px, -${editor.scrollTop}px)`;
});
// Anonymize button click handler.
anonymizeBtn.addEventListener("click", function () {
window.anonymizer.anonymize();
anonymizeBtn.disabled = true;
updateButtonStates();
showStatusMessage(
`<span style="font-size:24px; color: green;">${Icons.check}</span> ${window.translate("anonymizedStatus")}`
);
});
// Deanonymize button click handler.
deanonymizeBtn.addEventListener("click", function () {
window.anonymizer.deanonymize();
updateHighlight();
updateButtonStates();
showStatusMessage(
`<span style="font-size:24px; color: green;">${Icons.check}</span> ${window.translate("deanonymizedStatus")}`
);
});
// Overlay copy button handler.
const copyOverlayBtn = document.getElementById("copyOverlayBtn");
if (copyOverlayBtn) {
copyOverlayBtn.addEventListener("click", function () {
const text = window.anonymizer.getText();
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).then(function () {
const originalText = copyOverlayBtn.innerHTML;
copyOverlayBtn.innerHTML = `<span style="font-size:16px;">📋</span> ${window.translate("copySuccess")}`;
setTimeout(function () {
copyOverlayBtn.innerHTML = originalText;
}, 3000);
});
} else {
alert(window.translate("clipboardNotAvailable"));
}
});
}
// Initial call to update the highlight.
updateHighlight();
});