MCPcopy Create free account
hub / github.com/OpenAnonymity/oa-chat / updateEditableDiff

Function updateEditableDiff

chat/services/editableDiffRenderer.js:481–518  ·  view source on GitHub ↗
(container, originalText, editedText, cursorState = null, options = {})

Source from the content-addressed store, hash-verified

479};
480
481export const updateEditableDiff = (container, originalText, editedText, cursorState = null, options = {}) => {
482 if (!container) return;
483 const { restoreFocus = false } = options;
484
485 // Save cursor position
486 let cursorOffset = 0;
487 let affinity = 'after';
488 let hasCursor = false;
489 if (cursorState && Number.isFinite(cursorState.offset)) {
490 cursorOffset = cursorState.offset;
491 affinity = cursorState.affinity || 'after';
492 hasCursor = true;
493 } else {
494 const sel = window.getSelection();
495 if (sel && sel.rangeCount > 0) {
496 const range = sel.getRangeAt(0);
497 if (container.contains(range.startContainer)) {
498 cursorOffset = getNormalizedTextOffset(container, range.startContainer, range.startOffset);
499 affinity = getCursorAffinity(container, range);
500 hasCursor = true;
501 }
502 }
503 }
504
505 // Re-render diff
506 const html = renderEditableDiff(originalText, editedText);
507 container.innerHTML = html;
508
509 // Restore cursor position
510 if (hasCursor) {
511 setNormalizedOffsetCursor(container, cursorOffset, affinity);
512 // Ensure cursor didn't land inside a deleted span
513 ensureCursorOutsideDeleted(container);
514 if (restoreFocus && container.focus) {
515 container.focus({ preventScroll: true });
516 }
517 }
518};
519
520export const getEditableDiffSelectionState = (container) => {
521 if (!container) return null;

Callers 2

renderScrubberPreviewMethod · 0.90

Calls 5

getNormalizedTextOffsetFunction · 0.85
getCursorAffinityFunction · 0.85
renderEditableDiffFunction · 0.85

Tested by

no test coverage detected