Refactor scripts
This commit is contained in:
37
scripts/infiniteScroll.ts
Normal file
37
scripts/infiniteScroll.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import Diff from "./Diff"
|
||||
|
||||
let container: HTMLElement
|
||||
let threshold: number
|
||||
|
||||
export default function infiniteScroll(scrollContainer: HTMLElement, scrollThreshold: number) {
|
||||
container = scrollContainer
|
||||
threshold = scrollThreshold
|
||||
|
||||
container.addEventListener("scroll", _ => {
|
||||
// Wait for mutations to finish before checking if we need infinite scroll to trigger.
|
||||
if(Diff.mutations.length() > 0) {
|
||||
Diff.mutations.wait(check)
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, queue up the check immediately.
|
||||
// Don't call check() directly to make scrolling as smooth as possible.
|
||||
Diff.mutations.queue(() => check())
|
||||
})
|
||||
}
|
||||
|
||||
function check() {
|
||||
if(container.scrollTop + container.clientHeight >= container.scrollHeight - threshold) {
|
||||
loadMore()
|
||||
}
|
||||
}
|
||||
|
||||
function loadMore() {
|
||||
const button = document.getElementById("load-more-button")
|
||||
|
||||
if(!button) {
|
||||
return
|
||||
}
|
||||
|
||||
button.click()
|
||||
}
|
Reference in New Issue
Block a user