import {delay} from "@peryl/utils/delay"; export function createScrollUtils( { getScrollElement, getAutoScrollSpeed, onStop, }: { getScrollElement: () => HTMLElement | null | undefined, getAutoScrollSpeed?: () => number | null | undefined, onStop?: () => void, } ) { let cancelAnimate: number | null = null; const _getAutoScrollSpeed = () => (getAutoScrollSpeed ? getAutoScrollSpeed() : 400) || 400; const scroll = ( { top, left, time, onBegin, onFinish, }: { top?: number, left?: number, time?: number, onFinish?: () => void, onBegin?: () => void, } ) => { const scrollElement = getScrollElement(); if (!scrollElement) return; if (top == null && left == null) {return;} onBegin?.(); const done = () => {onFinish?.();}; if (!time) { if (left != null) scrollElement.scrollLeft = left; if (top != null) scrollElement.scrollTop = top; delay(23).then(done); } else { if (!!cancelAnimate) { cancelAnimationFrame(cancelAnimate); cancelAnimate = null; } let ny = scrollElement.scrollTop; let nx = scrollElement.scrollLeft; let ky = top == null ? 0 : (top - ny) / time; let kx = left == null ? 0 : (left - nx) / time; let startTime = Date.now(); const run = () => { let nowTime = Date.now(); let delta = nowTime - startTime; let top; let left; if (delta >= time) { cancelAnimate = null; top = time * ky + ny; left = time * kx + nx; if (scrollElement) { top != null && (scrollElement.scrollTop = top); left != null && (scrollElement.scrollLeft = left); delay(23).then(done); } } else { top = delta * ky + ny; left = delta * kx + nx; if (!!scrollElement) { top != null && (scrollElement.scrollTop = top); left != null && (scrollElement.scrollLeft = left); } cancelAnimate = requestAnimationFrame(run); } }; run(); } }; const scrollTop = (top: number, time?: number) => { const scrollElement = getScrollElement(); if (!scrollElement || scrollElement.scrollTop === top) {return;} scroll({ top, time }); }; const scrollLeft = (left: number, time?: number) => { const scrollElement = getScrollElement(); if (!scrollElement || scrollElement.scrollLeft === left) {return;} scroll({ left, time }); }; const scrollEnd = ({ top, left }: { top?: boolean, left?: boolean } = { top: true, left: true }) => { const scrollElement = getScrollElement(); if (!scrollElement) {return;} scroll({ top: top ? scrollElement.scrollHeight : undefined, left: left ? scrollElement.scrollWidth : undefined, }); }; const autoScrollTop = () => { const scrollElement = getScrollElement(); if (!scrollElement) {return;} const { scrollHeight, offsetHeight } = scrollElement; if (scrollHeight <= offsetHeight) {return;} const duration = (scrollElement.scrollTop / _getAutoScrollSpeed()) * 1000; scrollTop(0, duration); }; const autoScrollBottom = () => { const scrollElement = getScrollElement(); if (!scrollElement) {return;} const { scrollHeight, offsetHeight } = scrollElement; if (scrollHeight <= offsetHeight) {return;} const duration = ((scrollHeight - offsetHeight - scrollElement.scrollTop) / _getAutoScrollSpeed()) * 1000; scrollTop(scrollHeight - offsetHeight + 17, duration); }; const autoScrollLeft = () => { const scrollElement = getScrollElement(); if (!scrollElement) {return;} const { scrollWidth, offsetWidth } = scrollElement; if (scrollWidth <= offsetWidth) {return;} const duration = (scrollElement.scrollLeft / _getAutoScrollSpeed()) * 1000; scrollLeft(0, duration); }; const autoScrollRight = () => { const scrollElement = getScrollElement(); if (!scrollElement) {return;} const { scrollWidth, offsetWidth } = scrollElement; if (scrollWidth <= offsetWidth) {return;} const duration = ((scrollWidth - offsetWidth - scrollElement.scrollLeft) / _getAutoScrollSpeed()) * 1000; scrollLeft(scrollWidth - offsetWidth + 17, duration); }; const stopAutoScroll = () => { if (!!cancelAnimate) { cancelAnimationFrame(cancelAnimate); cancelAnimate = null; onStop?.(); } }; return { scroll, scrollTop, scrollLeft, autoScrollLeft, autoScrollRight, autoScrollTop, autoScrollBottom, stopAutoScroll, scrollEnd, getScrollElement, }; } export type iScrollUtils = ReturnType export type iScrollUtilsParameter = Parameters[0]