Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,43 +117,40 @@ function scrollIntoView(element, spot) {
* PDF.js friendly one: with scroll debounce and scroll direction.
*/
function watchScroll(viewAreaElement, callback, abortSignal = undefined) {
const debounceScroll = function (evt) {
if (rAF) {
return;
function onRAF() {
rAF = null;

const currentX = viewAreaElement.scrollLeft;
const lastX = state.lastX;
if (currentX !== lastX) {
state.right = currentX > lastX;
}
// schedule an invocation of scroll for next animation frame.
rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
rAF = null;

const currentX = viewAreaElement.scrollLeft;
const lastX = state.lastX;
if (currentX !== lastX) {
state.right = currentX > lastX;
}
state.lastX = currentX;
const currentY = viewAreaElement.scrollTop;
const lastY = state.lastY;
if (currentY !== lastY) {
state.down = currentY > lastY;
}
state.lastY = currentY;
callback(state);
});
};
state.lastX = currentX;
const currentY = viewAreaElement.scrollTop;
const lastY = state.lastY;
if (currentY !== lastY) {
state.down = currentY > lastY;
}
state.lastY = currentY;
callback(state);
}

const state = {
right: true,
down: true,
lastX: viewAreaElement.scrollLeft,
lastY: viewAreaElement.scrollTop,
_eventHandler: debounceScroll,
};

let rAF = null;
viewAreaElement.addEventListener("scroll", debounceScroll, {
useCapture: true,
signal: abortSignal,
});
viewAreaElement.addEventListener(
"scroll",
() => {
// Schedule an invocation of scroll for next animation frame, when needed.
rAF ??= window.requestAnimationFrame(onRAF);
},
{ useCapture: true, signal: abortSignal }
);
abortSignal?.addEventListener(
"abort",
() => window.cancelAnimationFrame(rAF),
Expand Down
Loading