Spaces:
Running
Running
File size: 18,174 Bytes
cb7fb6e 5bb9793 cb7fb6e daa082c cb7fb6e a1044bf cb7fb6e daa082c cb7fb6e daa082c cb7fb6e a1044bf cb7fb6e daa082c cb7fb6e daa082c cb7fb6e daa082c cb7fb6e daa082c cb7fb6e a1044bf cb7fb6e 5bb9793 cb7fb6e a1044bf cb7fb6e a1044bf cb7fb6e 5bb9793 cb7fb6e daa082c cb7fb6e a1044bf cb7fb6e | 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 | (function (global) {
"use strict";
const Bridge = global.SolidPrivacyStreamlitBridge;
const Core = global.SolidPrivacySelectionCore;
if (!Bridge || !Core) {
throw new Error("SolidPrivacy component support files are missing");
}
const sourcePane = document.getElementById("sourcePane");
const processedPane = document.getElementById("processedPane");
const processedLegend = document.getElementById("processedLegend");
const maskSelectionButton = document.getElementById("maskSelectionButton");
const contextMenu = document.getElementById("contextMenu");
const statusRegion = document.getElementById("statusRegion");
const componentRoot = document.getElementById("componentRoot");
const componentFooter = document.getElementById("componentFooter");
let currentArgs = {};
let processedText = "";
let highlightSpans = [];
let renderedProcessedSegments = [];
let selectionProtectedSpans = [];
let currentSelection = null;
let lastMenuPosition = { x: 16, y: 16 };
let lastRenderedInspectionToken = "";
let isSyncing = false;
function clearElement(element) {
element.replaceChildren();
}
function appendSafeText(element, text) {
element.appendChild(document.createTextNode(Core.asText(text)));
}
function renderPlainText(element, text) {
clearElement(element);
appendSafeText(element, text);
}
function renderProcessedText(text, spans) {
clearElement(processedPane);
renderedProcessedSegments = Core.buildDisplayTextSegments(text, spans);
selectionProtectedSpans = Core.protectedSpansFromDisplaySegments(renderedProcessedSegments);
renderedProcessedSegments.forEach(function (segment, index) {
const element = document.createElement(segment.marked ? "mark" : "span");
element.className = segment.marked
? "sp-highlight sp-processed-segment"
: "sp-processed-segment";
if (segment.compacted) {
element.classList.add("sp-compact-placeholder");
element.title = `Volledige gebonden placeholder: ${segment.full_placeholder}`;
element.setAttribute(
"aria-label",
`Gebonden placeholder, compact weergegeven als ${segment.display_text}`,
);
} else if (segment.marked) {
element.setAttribute("aria-label", "gemarkeerde vervanging");
}
element.dataset.segmentIndex = String(index);
element.dataset.startUtf16 = String(segment.start_utf16);
element.dataset.endUtf16 = String(segment.end_utf16);
appendSafeText(element, segment.display_text);
processedPane.appendChild(element);
});
}
function getScrollRatio(element) {
return Core.scrollRatio(element.scrollTop, element.scrollHeight, element.clientHeight);
}
function setScrollRatio(element, ratio) {
element.scrollTop = Core.scrollTopForRatio(ratio, element.scrollHeight, element.clientHeight);
}
function syncScroll(fromPane, toPane) {
if (isSyncing) {
return;
}
isSyncing = true;
global.requestAnimationFrame(function () {
setScrollRatio(toPane, getScrollRatio(fromPane));
isSyncing = false;
});
}
function nodeWithin(rootElement, node) {
if (!rootElement || !node) {
return false;
}
const candidate = node.nodeType === Node.TEXT_NODE ? node.parentNode : node;
return node === rootElement || rootElement.contains(candidate);
}
function domUtf16Offset(rootElement, container, offset) {
if (!nodeWithin(rootElement, container)) {
throw new Error("selection endpoint is outside processed text");
}
if (container === rootElement) {
if (!Number.isInteger(offset) || offset < 0 || offset > rootElement.childNodes.length) {
throw new Error("selection endpoint is outside processed text");
}
if (offset === 0) {
return 0;
}
if (offset >= renderedProcessedSegments.length) {
return processedText.length;
}
return renderedProcessedSegments[offset - 1].end_utf16;
}
const candidate = container.nodeType === Node.TEXT_NODE ? container.parentElement : container;
const segmentElement = candidate && candidate.closest
? candidate.closest("[data-segment-index]")
: null;
if (!segmentElement || !rootElement.contains(segmentElement)) {
throw new Error("selection endpoint has no source segment");
}
const segmentIndex = Number(segmentElement.dataset.segmentIndex);
const range = document.createRange();
range.selectNodeContents(segmentElement);
range.setEnd(container, offset);
return Core.utf16OffsetFromDisplaySegments(
renderedProcessedSegments,
segmentIndex,
range.toString().length,
);
}
function readProcessedSelection() {
const selection = global.getSelection();
if (!selection || selection.rangeCount !== 1 || selection.isCollapsed) {
return null;
}
const range = selection.getRangeAt(0);
if (!nodeWithin(processedPane, range.startContainer) || !nodeWithin(processedPane, range.endContainer)) {
return null;
}
let start;
let end;
try {
start = domUtf16Offset(processedPane, range.startContainer, range.startOffset);
end = domUtf16Offset(processedPane, range.endContainer, range.endOffset);
} catch (_error) {
return null;
}
return Core.selectionFromOffsets(processedText, selectionProtectedSpans, start, end);
}
function updateSelectionState() {
currentSelection = readProcessedSelection();
maskSelectionButton.disabled = !currentSelection || currentSelection.intersects_marked_content;
if (!currentSelection) {
statusRegion.textContent = "";
} else if (currentSelection.intersects_marked_content) {
statusRegion.textContent = "De selectie overlapt met een bestaande maskering.";
} else {
statusRegion.textContent = `“${currentSelection.text}” geselecteerd`;
}
}
function closeMenu(options) {
contextMenu.hidden = true;
clearElement(contextMenu);
if (options && options.restoreFocus) {
processedPane.focus();
}
}
function menuItems() {
return Array.from(contextMenu.querySelectorAll('[role="menuitem"]:not([aria-disabled="true"])'));
}
function focusMenuItem(index) {
const items = menuItems();
if (!items.length) {
return;
}
const bounded = ((index % items.length) + items.length) % items.length;
items[bounded].focus();
}
function positionMenu(x, y) {
contextMenu.hidden = false;
contextMenu.style.left = "0px";
contextMenu.style.top = "0px";
const rect = contextMenu.getBoundingClientRect();
const position = Core.clampMenuPosition(
x,
y,
rect.width,
rect.height,
global.innerWidth,
global.innerHeight,
8,
);
contextMenu.style.left = `${position.x}px`;
contextMenu.style.top = `${position.y}px`;
lastMenuPosition = position;
}
function addSummary(text) {
const summary = document.createElement("div");
summary.className = "sp-menu-summary";
summary.textContent = text;
contextMenu.appendChild(summary);
}
function addMenuButton(label, callback, options) {
const button = document.createElement("button");
button.type = "button";
button.className = "sp-menu-item";
button.setAttribute("role", "menuitem");
button.textContent = label;
if (options && options.disabled) {
button.setAttribute("aria-disabled", "true");
button.disabled = true;
}
button.addEventListener("click", function () {
if (!button.disabled) {
callback();
}
});
contextMenu.appendChild(button);
return button;
}
function addWarning(text) {
const warning = document.createElement("div");
warning.className = "sp-menu-warning";
warning.textContent = text;
contextMenu.appendChild(warning);
}
function inspectionToken(inspectionResult) {
const result = inspectionResult || {};
return String(result.inspection_id || result.event_id || "");
}
function inspectionMatchesSelection(inspectionResult) {
const resultText = String((inspectionResult && inspectionResult.selection_text) || "");
return !currentSelection || !resultText || resultText === currentSelection.text;
}
function isInspectableResult(inspectionResult) {
const status = String((inspectionResult && inspectionResult.status) || "");
return Boolean(inspectionToken(inspectionResult)) &&
["ready", "confirmation_required", "blocked"].includes(status) &&
inspectionMatchesSelection(inspectionResult);
}
function emitInspectEvent() {
if (!currentSelection || currentSelection.intersects_marked_content) {
statusRegion.textContent = "Selecteer een ongemaskeerde waarde in de verwerkte tekst.";
closeMenu({ restoreFocus: true });
return;
}
const event = Core.buildInspectEvent(
currentArgs,
currentSelection,
{
source_scroll_ratio: getScrollRatio(sourcePane),
processed_scroll_ratio: getScrollRatio(processedPane),
},
Core.makeEventId("inspect", global.crypto),
);
statusRegion.textContent = "Selectie wordt veilig gecontroleerd…";
closeMenu();
Bridge.setComponentValue(event);
}
function emitCommitIntent(typeKey, inspectionResult) {
const event = Core.buildCommitEvent(
inspectionResult,
typeKey,
Core.makeEventId("commit", global.crypto),
);
const contract = currentArgs.component_contract || {};
statusRegion.textContent = contract.non_mutating_spike
? "Maskeringskeuze is als niet-muterend intent-event verstuurd."
: "Maskering wordt veilig toegevoegd…";
closeMenu();
Bridge.setComponentValue(event);
}
function renderInspectMenu(x, y) {
clearElement(contextMenu);
if (!currentSelection) {
addSummary("Selecteer eerst een waarde in Verwerkte tekst.");
addMenuButton("Sluiten", function () {
closeMenu({ restoreFocus: true });
});
} else if (currentSelection.intersects_marked_content) {
addSummary("Deze selectie overlapt met een bestaande maskering.");
addMenuButton("Sluiten", function () {
closeMenu({ restoreFocus: true });
});
} else {
addSummary(`“${currentSelection.text}” geselecteerd`);
addMenuButton("Selectie veilig inspecteren", emitInspectEvent);
}
positionMenu(x, y);
focusMenuItem(0);
}
function renderConfirmationMenu(typeKey, inspectionResult, x, y) {
clearElement(contextMenu);
const label = Core.QUICK_TYPE_LABELS[typeKey];
const count = Number(inspectionResult.occurrence_count || 0);
addSummary(`“${inspectionResult.selection_text || "selectie"}” — ${count} exacte voorkomens`);
addWarning(`Alle ${count} exacte voorkomens worden gemaskeerd als ${label}.`);
addMenuButton(`Bevestig alle ${count} voorkomens`, function () {
emitCommitIntent(typeKey, inspectionResult);
});
addMenuButton("Terug", function () {
renderInspectionResultMenu(inspectionResult, x, y);
});
positionMenu(x, y);
focusMenuItem(0);
}
function renderInspectionResultMenu(inspectionResult, x, y) {
clearElement(contextMenu);
const status = String(inspectionResult.status || "blocked");
const count = Number(inspectionResult.occurrence_count || 0);
const selectionText = String(
inspectionResult.selection_text || (currentSelection && currentSelection.text) || "selectie",
);
addSummary(`“${selectionText}” — ${inspectionResult.message || `${count} exacte voorkomens`}`);
if (status === "blocked") {
addWarning(inspectionResult.message || "Deze selectie kon niet veilig worden toegevoegd.");
addMenuButton("Sluiten", function () {
closeMenu({ restoreFocus: true });
});
} else {
const allowed = Array.isArray(inspectionResult.allowed_types)
? inspectionResult.allowed_types
: [];
allowed.forEach(function (typeKey) {
if (!Object.prototype.hasOwnProperty.call(Core.QUICK_TYPE_LABELS, typeKey)) {
return;
}
const label = Core.QUICK_TYPE_LABELS[typeKey];
const buttonLabel = count === 1
? `Masker als ${label}`
: `Masker alle ${count} exacte voorkomens als ${label}`;
addMenuButton(buttonLabel, function () {
if (status === "confirmation_required") {
renderConfirmationMenu(typeKey, inspectionResult, x, y);
} else {
emitCommitIntent(typeKey, inspectionResult);
}
});
});
if (!allowed.length) {
addWarning("De server heeft geen toegestane maskeringstypen teruggegeven.");
}
}
positionMenu(x, y);
focusMenuItem(0);
}
function openMenuForCurrentState(x, y) {
const inspectionResult = currentArgs.inspection_result || {};
if (isInspectableResult(inspectionResult)) {
renderInspectionResultMenu(inspectionResult, x, y);
} else {
renderInspectMenu(x, y);
}
}
function render(args) {
const sourceScroll = getScrollRatio(sourcePane);
const processedScroll = getScrollRatio(processedPane);
currentArgs = args || {};
const contract = currentArgs.component_contract || {};
const nonMutatingSpike = Boolean(contract.non_mutating_spike);
if (componentRoot) {
componentRoot.setAttribute(
"aria-label",
nonMutatingSpike
? "Niet-muterende selectiecomponent"
: "Tekstselectie voor handmatige maskering",
);
}
if (componentFooter) {
componentFooter.textContent = nonMutatingSpike
? "De panelen scrollen samen. Deze componentproef wijzigt geen vervangtabel of document."
: "De panelen scrollen samen. Maskeringen worden pas na servercontrole toegevoegd.";
}
processedText = Core.asText(currentArgs.processed_text);
highlightSpans = Core.normalizeUtf16Spans(processedText, currentArgs.highlight_spans);
renderPlainText(sourcePane, currentArgs.source_text);
renderProcessedText(processedText, highlightSpans);
const compactedCount = renderedProcessedSegments.filter(function (segment) {
return segment.compacted;
}).length;
if (highlightSpans.length && compactedCount) {
processedLegend.textContent = "Geel = vervangen; documentcode compact weergegeven";
} else if (highlightSpans.length) {
processedLegend.textContent = "Geel = vervangen of gemaskeerde waarde";
} else if (compactedCount) {
processedLegend.textContent = "Documentcode compact weergegeven";
} else {
processedLegend.textContent = "Verwerkte tekst";
}
const restoreSource = currentArgs.restore_source_scroll_ratio;
const restoreProcessed = currentArgs.restore_processed_scroll_ratio;
global.requestAnimationFrame(function () {
setScrollRatio(
sourcePane,
Number.isFinite(Number(restoreSource)) ? Number(restoreSource) : sourceScroll,
);
setScrollRatio(
processedPane,
Number.isFinite(Number(restoreProcessed)) ? Number(restoreProcessed) : processedScroll,
);
});
updateSelectionState();
const inspectionResult = currentArgs.inspection_result || {};
if (inspectionResult.message) {
statusRegion.textContent = String(inspectionResult.message);
}
const token = inspectionToken(inspectionResult);
if (token && token !== lastRenderedInspectionToken && isInspectableResult(inspectionResult)) {
lastRenderedInspectionToken = token;
global.requestAnimationFrame(function () {
renderInspectionResultMenu(
inspectionResult,
lastMenuPosition.x,
lastMenuPosition.y,
);
});
}
Bridge.setFrameHeight(500);
}
sourcePane.addEventListener("scroll", function () {
syncScroll(sourcePane, processedPane);
});
processedPane.addEventListener("scroll", function () {
syncScroll(processedPane, sourcePane);
});
document.addEventListener("selectionchange", function () {
updateSelectionState();
});
processedPane.addEventListener("contextmenu", function (event) {
updateSelectionState();
if (!currentSelection || currentSelection.intersects_marked_content) {
return;
}
event.preventDefault();
openMenuForCurrentState(event.clientX, event.clientY);
});
processedPane.addEventListener("keydown", function (event) {
if ((event.shiftKey && event.key === "F10") || event.key === "ContextMenu") {
updateSelectionState();
if (currentSelection && !currentSelection.intersects_marked_content) {
event.preventDefault();
const rect = processedPane.getBoundingClientRect();
openMenuForCurrentState(rect.left + Math.min(40, rect.width / 2), rect.top + 40);
}
}
});
maskSelectionButton.addEventListener("click", function () {
updateSelectionState();
const rect = maskSelectionButton.getBoundingClientRect();
openMenuForCurrentState(rect.left, rect.bottom + 6);
});
contextMenu.addEventListener("keydown", function (event) {
const items = menuItems();
const currentIndex = items.indexOf(document.activeElement);
if (event.key === "ArrowDown") {
event.preventDefault();
focusMenuItem(currentIndex + 1);
} else if (event.key === "ArrowUp") {
event.preventDefault();
focusMenuItem(currentIndex - 1);
} else if (event.key === "Home") {
event.preventDefault();
focusMenuItem(0);
} else if (event.key === "End") {
event.preventDefault();
focusMenuItem(items.length - 1);
} else if (event.key === "Escape") {
event.preventDefault();
closeMenu({ restoreFocus: true });
}
});
document.addEventListener("mousedown", function (event) {
if (!contextMenu.hidden && !contextMenu.contains(event.target)) {
closeMenu();
}
});
global.addEventListener("blur", function () {
closeMenu();
});
Bridge.onRender(function (detail) {
render(detail.args || {});
});
Bridge.setComponentReady();
Bridge.setFrameHeight(500);
})(window);
|