-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverlay.js
More file actions
250 lines (207 loc) · 6.65 KB
/
Copy pathoverlay.js
File metadata and controls
250 lines (207 loc) · 6.65 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
// Overlay system for highlighting PII in input fields
// WeakMaps to track overlays, ignore lists, and entity snapshots per element
const overlayMap = new WeakMap();
const ignoreMap = new WeakMap();
const snapshotMap = new WeakMap();
/**
* True if point is inside a DOMRect.
*/
function pointInRect(x, y, rect) {
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
}
/**
* Hit-test a span using line-fragment rects for wrapped text.
*/
function isPointOverSpan(spanEl, x, y) {
const rects = spanEl.getClientRects();
if (!rects || rects.length === 0) {
return pointInRect(x, y, spanEl.getBoundingClientRect());
}
for (const rect of rects) {
if (pointInRect(x, y, rect)) return true;
}
return false;
}
/**
* Get or create the ignore set for an element
*/
function getIgnoreSet(el) {
let s = ignoreMap.get(el);
if (!s) {
s = new Set();
ignoreMap.set(el, s);
}
return s;
}
/**
* Get entity snapshot for an element
*/
function getSnapshot(el) {
return snapshotMap.get(el);
}
/**
* Set entity snapshot for an element
*/
function setSnapshot(el, snapshot) {
snapshotMap.set(el, snapshot);
}
/**
* Create and attach overlay to an input element
*/
function ensureOverlay(el) {
if (overlayMap.has(el)) return overlayMap.get(el);
const wrapper = document.createElement("div");
wrapper.style.position = "relative";
wrapper.style.width = "100%";
// Preserve display mode for contenteditable elements
const isContentEditable =
el.isContentEditable || el.getAttribute("contenteditable") === "true";
if (isContentEditable) {
const cs = getComputedStyle(el);
wrapper.style.display = cs.display;
}
const parent = el.parentNode;
parent.insertBefore(wrapper, el);
wrapper.appendChild(el);
const overlay = document.createElement("div");
overlay.className = "redactosaurus-overlay";
const cs = getComputedStyle(el);
// Copy font properties individually to ensure exact match (prevents line drift)
overlay.style.fontFamily = cs.fontFamily;
overlay.style.fontSize = cs.fontSize;
overlay.style.fontWeight = cs.fontWeight;
overlay.style.fontStyle = cs.fontStyle;
// Calculate actual line height in pixels to prevent accumulation errors
let lineHeight = cs.lineHeight;
if (lineHeight === "normal" || lineHeight.match(/^\d+(\.\d+)?$/)) {
// If it's "normal" or a unitless number, compute the actual pixel value
const fontSize = parseFloat(cs.fontSize);
const ratio = lineHeight === "normal" ? 1.2 : parseFloat(lineHeight);
lineHeight = Math.round(fontSize * ratio) + "px";
}
overlay.style.lineHeight = lineHeight;
overlay.style.letterSpacing = cs.letterSpacing;
overlay.style.wordSpacing = cs.wordSpacing;
// Copy spacing properties exactly
overlay.style.paddingTop = cs.paddingTop;
overlay.style.paddingRight = cs.paddingRight;
overlay.style.paddingBottom = cs.paddingBottom;
overlay.style.paddingLeft = cs.paddingLeft;
overlay.style.margin = "0";
// Copy box and border properties
overlay.style.border = cs.border;
overlay.style.borderRadius = cs.borderRadius;
overlay.style.boxSizing = cs.boxSizing;
// Copy text positioning
overlay.style.textAlign = cs.textAlign;
overlay.style.textIndent = cs.textIndent;
overlay.style.verticalAlign = cs.verticalAlign;
overlay.style.whiteSpace = "pre-wrap";
overlay.style.wordWrap = "break-word";
overlay.style.overflow = "hidden";
wrapper.appendChild(overlay);
const syncScroll = () => {
if (overlay) {
overlay.scrollTop = el.scrollTop;
overlay.scrollLeft = el.scrollLeft;
}
};
el.addEventListener("scroll", syncScroll);
syncScroll();
const obj = { wrapper, overlay, syncScroll, bound: false };
overlayMap.set(el, obj);
return obj;
}
/**
* Remove overlay from an element
*/
function removeOverlay(el) {
const obj = overlayMap.get(el);
if (!obj) return;
const orig = el.dataset.redactosaurusOrigColor;
if (orig) el.style.color = orig;
el.style.caretColor = "";
const { wrapper, overlay } = obj;
if (overlay?.parentNode) overlay.parentNode.removeChild(overlay);
if (wrapper?.parentNode) {
wrapper.parentNode.insertBefore(el, wrapper);
wrapper.parentNode.removeChild(wrapper);
}
overlayMap.delete(el);
}
/**
* Render underlined PII entities in the overlay
*/
function renderUnderlineOverlay(text, entities, overlayEl, ignoredSet) {
if (!entities || entities.length === 0) {
overlayEl.innerHTML = escapeHtml(text);
return;
}
const sorted = [...entities].sort((a, b) => a.start - b.start);
let out = "";
let cur = 0;
for (let i = 0; i < sorted.length; i++) {
const e = sorted[i];
const key = ignoreKey(e);
if (ignoredSet.has(key)) continue;
if (e.start < cur) continue;
out += escapeHtml(text.slice(cur, e.start));
const snippet = text.slice(e.start, e.end);
// Store entity data directly to avoid index mismatch.
const entityData = JSON.stringify({
type: e.type,
start: e.start,
end: e.end,
text: e.text,
});
out += `<span class="redactosaurus-underline" data-entity='${escapeHtml(entityData)}'>${escapeHtml(snippet)}</span>`;
cur = e.end;
}
out += escapeHtml(text.slice(cur));
overlayEl.innerHTML = out;
}
/**
* Bind hover events to overlay spans
* Since overlay has pointer-events: none, we detect hovers on the input element itself
*/
function bindOverlayEvents(overlayEl, el, entities, ignoredSet) {
// Remove any existing mousemove listener
if (el._redactosaurusMouseMove) {
el.removeEventListener("mousemove", el._redactosaurusMouseMove);
}
// Create a mousemove handler to detect when mouse is over PII spans
const mouseMoveHandler = (e) => {
const spans = overlayEl.querySelectorAll(".redactosaurus-underline");
let foundSpan = null;
let foundEntity = null;
// Check which span (if any) is under the mouse
for (const spanEl of spans) {
if (isPointOverSpan(spanEl, e.clientX, e.clientY)) {
foundSpan = spanEl;
try {
foundEntity = JSON.parse(spanEl.dataset.entity);
const key = ignoreKey(foundEntity);
if (ignoredSet.has(key)) {
foundSpan = null;
foundEntity = null;
continue;
}
} catch (err) {
console.error("[Redactosaurus] Failed to parse entity data:", err);
}
break;
}
}
if (foundSpan && foundEntity) {
cancelHideTooltip();
showGlobalTooltip(foundSpan, foundEntity, el, {
clientX: e.clientX,
clientY: e.clientY,
});
} else {
scheduleHideTooltip();
}
};
el._redactosaurusMouseMove = mouseMoveHandler;
el.addEventListener("mousemove", mouseMoveHandler);
}