mirror of
https://github.com/vale981/tridactyl
synced 2025-03-06 10:01:39 -05:00
Update scrollable element on click
This commit is contained in:
parent
780e8a77c3
commit
df73304b1d
1 changed files with 17 additions and 3 deletions
|
@ -149,8 +149,18 @@ export async function scroll(
|
||||||
|
|
||||||
let lastRecursiveScrolled = null
|
let lastRecursiveScrolled = null
|
||||||
let lastFocused = null
|
let lastFocused = null
|
||||||
|
let currentFocused = document.activeElement as any
|
||||||
let lastX = 0
|
let lastX = 0
|
||||||
let lastY = 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
|
/** 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.
|
* y pixels down among the Elements in {nodes} and children of these Elements.
|
||||||
*
|
*
|
||||||
|
@ -167,7 +177,7 @@ export async function recursiveScroll(
|
||||||
if (!node) {
|
if (!node) {
|
||||||
const sameSignX = xDistance < 0 === lastX < 0
|
const sameSignX = xDistance < 0 === lastX < 0
|
||||||
const sameSignY = yDistance < 0 === lastY < 0
|
const sameSignY = yDistance < 0 === lastY < 0
|
||||||
const sameElement = lastFocused == document.activeElement
|
const sameElement = lastFocused == currentFocused
|
||||||
if (lastRecursiveScrolled && sameSignX && sameSignY && sameElement) {
|
if (lastRecursiveScrolled && sameSignX && sameSignY && sameElement) {
|
||||||
// We're scrolling in the same direction as the previous time so
|
// We're scrolling in the same direction as the previous time so
|
||||||
// let's try to pick up from where we left
|
// let's try to pick up from where we left
|
||||||
|
@ -176,7 +186,11 @@ export async function recursiveScroll(
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Try scrolling the active node or one of its parent elements
|
// 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) {
|
while (true) {
|
||||||
if ((await scroll(xDistance, yDistance, node))) return true
|
if ((await scroll(xDistance, yDistance, node))) return true
|
||||||
node = node.parentElement
|
node = node.parentElement
|
||||||
|
@ -187,7 +201,7 @@ export async function recursiveScroll(
|
||||||
node = document.documentElement
|
node = document.documentElement
|
||||||
|
|
||||||
// Invalidate the cache if the user changes focus
|
// Invalidate the cache if the user changes focus
|
||||||
lastFocused = document.activeElement
|
lastFocused = currentFocused
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT)
|
let treeWalker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT)
|
||||||
|
|
Loading…
Add table
Reference in a new issue