(container, originalText, editedText, cursorState = null, options = {})
| 479 | }; |
| 480 | |
| 481 | export 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 | |
| 520 | export const getEditableDiffSelectionState = (container) => { |
| 521 | if (!container) return null; |
no test coverage detected