(
target: Element,
params?: { animate?: boolean; centerCamera?: boolean },
)
| 15 | } |
| 16 | |
| 17 | export function scrollIntoView( |
| 18 | target: Element, |
| 19 | params?: { animate?: boolean; centerCamera?: boolean }, |
| 20 | ) { |
| 21 | let ancestor: Element | null = target.parentElement; |
| 22 | |
| 23 | const targetRect = domRectScreenToWorld(target.getBoundingClientRect()); |
| 24 | |
| 25 | const scrollElems: { elem: Element; from: IVec2; to: IVec2 }[] = []; |
| 26 | |
| 27 | while (ancestor != null) { |
| 28 | if (hasScrollbar(ancestor as HTMLElement)) { |
| 29 | const ancestorRect = domRectScreenToWorld( |
| 30 | ancestor.getBoundingClientRect(), |
| 31 | ); |
| 32 | |
| 33 | const ancestorClientRight = ancestorRect.x + ancestor.clientWidth; |
| 34 | const ancestorClientBottom = ancestorRect.y + ancestor.clientHeight; |
| 35 | |
| 36 | let offsetX = |
| 37 | targetRect.x < ancestorRect.x |
| 38 | ? targetRect.x - ancestorRect.x |
| 39 | : targetRect.right > ancestorClientRight |
| 40 | ? targetRect.right - ancestorClientRight |
| 41 | : 0; |
| 42 | let offsetY = |
| 43 | targetRect.y < ancestorRect.y |
| 44 | ? targetRect.y - ancestorRect.y |
| 45 | : targetRect.bottom > ancestorClientBottom |
| 46 | ? targetRect.bottom - ancestorClientBottom |
| 47 | : 0; |
| 48 | |
| 49 | // Clamp offset |
| 50 | |
| 51 | const maxScrollLeft = ancestor.scrollWidth - ancestor.clientWidth; |
| 52 | const maxScrollTop = ancestor.scrollHeight - ancestor.clientHeight; |
| 53 | |
| 54 | offsetX = Math.min(offsetX, maxScrollLeft - ancestor.scrollLeft); |
| 55 | offsetY = Math.min(offsetY, maxScrollTop - ancestor.scrollTop); |
| 56 | |
| 57 | offsetX = Math.max(offsetX, -ancestor.scrollLeft); |
| 58 | offsetY = Math.max(offsetY, -ancestor.scrollTop); |
| 59 | |
| 60 | // Apply scroll offset |
| 61 | |
| 62 | scrollElems.push({ |
| 63 | elem: ancestor, |
| 64 | from: { x: ancestor.scrollLeft, y: ancestor.scrollTop }, |
| 65 | to: { |
| 66 | x: ancestor.scrollLeft + offsetX, |
| 67 | y: ancestor.scrollTop + offsetY, |
| 68 | }, |
| 69 | }); |
| 70 | |
| 71 | targetRect.x -= offsetX; |
| 72 | targetRect.y -= offsetY; |
| 73 | } |
| 74 |
no test coverage detected