Skip to content

Commit

Permalink
fix: find the caret domrect with largest bottom value (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictb authored Aug 20, 2024
1 parent 590b49d commit 2cc529f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/web/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,30 @@ function scrollCursorIntoView(target: HTMLInputElement) {
return;
}

const caretRect = selection.getRangeAt(0).getClientRects()[0];
const caretRects = selection.getRangeAt(0).getClientRects();

// we'll find the caretRect from the DOMRectList above with the largest bottom value
let currentCaretRect = caretRects[0];
if (currentCaretRect) {
for (let i = 1; i < caretRects.length; i++) {
const caretRect = caretRects[i];
if (caretRect && caretRect.bottom > currentCaretRect.bottom) {
currentCaretRect = caretRect;
}
}
}

const editableRect = target.getBoundingClientRect();

// Adjust for padding and border
const paddingTop = parseFloat(window.getComputedStyle(target).paddingTop);
const borderTop = parseFloat(window.getComputedStyle(target).borderTopWidth);

if (caretRect && !(caretRect.top >= editableRect.top + paddingTop + borderTop && caretRect.bottom <= editableRect.bottom - 2 * (paddingTop - borderTop))) {
const topToCaret = caretRect.top - editableRect.top;
if (currentCaretRect && !(currentCaretRect.top >= editableRect.top + paddingTop + borderTop && currentCaretRect.bottom <= editableRect.bottom - 2 * (paddingTop - borderTop))) {
const topToCaret = currentCaretRect.top - editableRect.top;
const inputHeight = editableRect.height;
// Chrome Rects don't include padding & border, so we're adding them manually
const inputOffset = caretRect.height - inputHeight + paddingTop + borderTop + (BrowserUtils.isChromium ? 0 : 4 * (paddingTop + borderTop));
const inputOffset = currentCaretRect.height - inputHeight + paddingTop + borderTop + (BrowserUtils.isChromium ? 0 : 4 * (paddingTop + borderTop));

target.scrollTo(0, topToCaret + target.scrollTop + inputOffset);
}
Expand Down

0 comments on commit 2cc529f

Please sign in to comment.