import {addClass} from "@peryl/utils/addClass"; import {createScrollUtils, iScrollUtils, iScrollUtilsParameter} from "../createScrollUtils"; import {createEffects} from "@peryl/utils/createEffects"; import {getComponentCls} from "@peryl/react-compose"; import {getElementParents} from "@peryl/utils/getElementParents"; import './auto-scroll-utils.scss'; import {isWindows} from "../../utils/getDeviceInfo"; export function useAutoScrollUtils( config: { vertical?: boolean, } & ({ scrollUtils: iScrollUtils } | { scrollUtilsParameter: iScrollUtilsParameter }) ) { const { effects: showEffects } = createEffects(); const scrollUtils = 'scrollUtils' in config ? config.scrollUtils : createScrollUtils(config.scrollUtilsParameter); function createIndicator(scrollElement: HTMLElement) { const wrapper = document.createElement('div'); addClass(wrapper, `${getComponentCls('auto-scroll-utils-wrapper')} auto-scroll-utils-wrapper-${config.vertical ? 'vertical' : 'horizontal'}`); const start = document.createElement('div'); addClass(start, `${getComponentCls('auto-scroll-utils-shadow')} auto-scroll-utils-shadow-start`); wrapper.appendChild(start); const end = document.createElement('div'); addClass(end, `${getComponentCls('auto-scroll-utils-shadow')} auto-scroll-utils-shadow-end`); wrapper.appendChild(end); document.body.appendChild(wrapper); showEffects.push(() => {document.body.removeChild(wrapper);}); const refresh = () => { const scrollRect = scrollElement.getBoundingClientRect(); const scrollStyle = window.getComputedStyle(scrollElement); if (isWindows()) { if (scrollElement.scrollWidth > scrollElement.offsetWidth || scrollStyle.overflowX === 'scroll') { scrollRect.height -= 17; } if (scrollElement.scrollHeight > scrollElement.offsetWidth || scrollStyle.overflowY === 'scroll') { scrollRect.width -= 17; } } wrapper.style.transform = `translate3d(${scrollRect.left}px,${scrollRect.top}px,0)`; wrapper.style.width = scrollRect.width + 'px'; wrapper.style.height = scrollRect.height + 'px'; }; refresh(); [...getElementParents(scrollElement), window].forEach(parent => { if (!parent) {return;} parent.addEventListener('scroll', refresh); showEffects.push(() => {parent.removeEventListener('scroll', refresh);}); }); return { start, end, wrapper }; } const show = () => { const scrollElement = scrollUtils.getScrollElement(); if (!scrollElement) {return;} const indicator = createIndicator(scrollElement); const onEnterStart = () => config.vertical ? scrollUtils.autoScrollTop() : scrollUtils.autoScrollLeft(); const onEnterEnd = () => config.vertical ? scrollUtils.autoScrollBottom() : scrollUtils.autoScrollRight(); const onLeave = () => scrollUtils.stopAutoScroll(); indicator.start.addEventListener('mouseenter', onEnterStart); indicator.end.addEventListener('mouseenter', onEnterEnd); indicator.start.addEventListener('mouseleave', onLeave); indicator.end.addEventListener('mouseleave', onLeave); showEffects.push(() => { indicator.start.removeEventListener('mouseenter', onEnterStart); indicator.end.removeEventListener('mouseenter', onEnterEnd); indicator.start.removeEventListener('mouseleave', onLeave); indicator.end.removeEventListener('mouseleave', onLeave); }); }; const hide = () => { showEffects.clear(); }; return { show, hide, scrollUtils }; }