Skip to content

Commit

Permalink
Only focus elements that are already in view
Browse files Browse the repository at this point in the history
And don't scroll when focusing.
  • Loading branch information
AbeJellinek committed Nov 14, 2024
1 parent 347d615 commit ba12e22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/dom/common/dom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ abstract class DOMView<State extends DOMViewState, Data> {
...this._iframeDocument.querySelectorAll('a, area'),
...this._annotationShadowRoot.querySelectorAll('[tabindex="-1"]')
] as (HTMLElement | SVGElement)[];
focusableElements = focusableElements.filter(
el => isPageRectVisible(getBoundingPageRect(el), this._iframeWindow, 0)
);
focusableElements.sort((a, b) => {
let rangeA;
if (a.getRootNode() === this._annotationShadowRoot && a.hasAttribute('data-annotation-id')) {
Expand Down Expand Up @@ -801,7 +804,7 @@ abstract class DOMView<State extends DOMViewState, Data> {
if (!focusedElement) {
// In PDF view the first visible object (annotation, overlay) is focused
if (focusableElements.length) {
focusableElements[0].focus();
focusableElements[0].focus({ preventScroll: true });
}
else {
this._options.onTabOut();
Expand All @@ -816,12 +819,14 @@ abstract class DOMView<State extends DOMViewState, Data> {

if (focusedElement) {
if (!window.rtl && key === 'ArrowRight' || window.rtl && key === 'ArrowLeft' || key === 'ArrowDown') {
focusableElements[(focusedElementIndex + 1) % focusableElements.length]?.focus();
focusableElements[(focusedElementIndex + 1) % focusableElements.length]
?.focus({ preventScroll: true });
event.preventDefault();
return;
}
else if (!window.rtl && key === 'ArrowLeft' || window.rtl && key === 'ArrowRight' || key === 'ArrowUp') {
focusableElements[(focusedElementIndex - 1 + focusableElements.length) % focusableElements.length]?.focus();
focusableElements[(focusedElementIndex - 1 + focusableElements.length) % focusableElements.length]
?.focus({ preventScroll: true });
event.preventDefault();
return;
}
Expand Down
8 changes: 5 additions & 3 deletions src/dom/common/lib/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ export function getStartElement(range: Range | PersistentRange): Element | null
return startContainer as Element | null;
}

export function getBoundingPageRect(range: Range) {
let rect = range.getBoundingClientRect();
let win = range.commonAncestorContainer.ownerDocument?.defaultView;
export function getBoundingPageRect(rangeOrElem: Range | Element) {
let rect = rangeOrElem.getBoundingClientRect();
let win = ('ownerDocument' in rangeOrElem
? rangeOrElem.ownerDocument
: rangeOrElem.commonAncestorContainer.ownerDocument)?.defaultView;
rect.x += win?.scrollX ?? 0;
rect.y += win?.scrollY ?? 0;
return rect;
Expand Down

0 comments on commit ba12e22

Please sign in to comment.