Update scrollable element on click

This commit is contained in:
Oliver Blanthorn 2020-05-25 10:11:38 +01:00
parent 780e8a77c3
commit df73304b1d
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3

View file

@ -149,8 +149,18 @@ export async function scroll(
let lastRecursiveScrolled = null
let lastFocused = null
let currentFocused = document.activeElement as any
let lastX = 0
let lastY = 0
document.addEventListener("mousedown", event => {
currentFocused = event.target
})
document.addEventListener("focusin", event => {
currentFocused = event.target
})
/** Tries to find a node which can be scrolled either x pixels to the right or
* y pixels down among the Elements in {nodes} and children of these Elements.
*
@ -167,7 +177,7 @@ export async function recursiveScroll(
if (!node) {
const sameSignX = xDistance < 0 === lastX < 0
const sameSignY = yDistance < 0 === lastY < 0
const sameElement = lastFocused == document.activeElement
const sameElement = lastFocused == currentFocused
if (lastRecursiveScrolled && sameSignX && sameSignY && sameElement) {
// We're scrolling in the same direction as the previous time so
// let's try to pick up from where we left
@ -176,7 +186,11 @@ export async function recursiveScroll(
} else {
// Try scrolling the active node or one of its parent elements
node = document.activeElement
// If nothing has been given focus explicitly use the activeElement
if (!currentFocused || currentFocused.nodeName == "#document") currentFocused = document.activeElement
node = currentFocused
while (true) {
if ((await scroll(xDistance, yDistance, node))) return true
node = node.parentElement
@ -187,7 +201,7 @@ export async function recursiveScroll(
node = document.documentElement
// Invalidate the cache if the user changes focus
lastFocused = document.activeElement
lastFocused = currentFocused
}
}
let treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT)