Skip to content

Commit e7ca47d

Browse files
committed
Add a kludge to scroll things into view in the presence of a mobile virtual viewport
FIX: Fix an issue where scrolling the cursor into view sometimes wouldn't work on Chrome Android. Closes codemirror/dev#1676
1 parent f619d7f commit e7ca47d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/docview.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,18 @@ export class DocView {
540540
Math.max(Math.min(target.xMargin, offsetWidth), -offsetWidth),
541541
Math.max(Math.min(target.yMargin, offsetHeight), -offsetHeight),
542542
this.view.textDirection == Direction.LTR)
543+
// On mobile browsers, the visual viewport may be smaller than the
544+
// actual reported viewport, causing scrollRectIntoView to fail to
545+
// scroll properly. Unfortunately, this visual viewport cannot be
546+
// updated directly, and scrollIntoView is the only way a script
547+
// can affect it. So this tries to kludge around the problem by
548+
// calling scrollIntoView on the scroll target's line.
549+
if (window.visualViewport && window.innerHeight - window.visualViewport.height > 1 &&
550+
(rect.top > window.pageYOffset + window.visualViewport.offsetTop + window.visualViewport.height ||
551+
rect.bottom < window.pageYOffset + window.visualViewport.offsetTop)) {
552+
let line = this.view.docView.lineAt(range.head, 1)
553+
if (line) line.dom.scrollIntoView({block: "nearest"})
554+
}
543555
}
544556

545557
lineHasWidget(pos: number) {

0 commit comments

Comments
 (0)