//#region src/components/reorder/auto-scroll.ts var threshold = 50; var maxSpeed = 25; var overflowStyles = /* @__PURE__ */ new Set(["auto", "scroll"]); var initialScrollLimits = /* @__PURE__ */ new WeakMap(); var activeScrollEdge = /* @__PURE__ */ new WeakMap(); var currentGroupElement = null; function resetAutoScrollState() { if (currentGroupElement) { const scrollableAncestor = findScrollableAncestor(currentGroupElement, "y"); if (scrollableAncestor) { activeScrollEdge.delete(scrollableAncestor); initialScrollLimits.delete(scrollableAncestor); } const scrollableAncestorX = findScrollableAncestor(currentGroupElement, "x"); if (scrollableAncestorX && scrollableAncestorX !== scrollableAncestor) { activeScrollEdge.delete(scrollableAncestorX); initialScrollLimits.delete(scrollableAncestorX); } currentGroupElement = null; } } function isDocumentScroll(element) { return element === document.body || element === document.documentElement; } function isScrollableElement(element, axis) { const style = getComputedStyle(element); const overflow = axis === "x" ? style.overflowX : style.overflowY; return overflowStyles.has(overflow) || isDocumentScroll(element); } function findScrollableAncestor(element, axis) { let current = element?.parentElement; while (current) { if (isScrollableElement(current, axis)) return current; current = current.parentElement; } return null; } function getScrollAmount(pointerPosition, scrollElement, axis) { const rect = scrollElement.getBoundingClientRect(); const start = axis === "x" ? Math.max(0, rect.left) : Math.max(0, rect.top); const end = axis === "x" ? Math.min(window.innerWidth, rect.right) : Math.min(window.innerHeight, rect.bottom); const distanceFromStart = pointerPosition - start; const distanceFromEnd = end - pointerPosition; if (distanceFromStart < threshold) { const intensity = 1 - distanceFromStart / threshold; return { amount: -25 * intensity * intensity, edge: "start" }; } else if (distanceFromEnd < threshold) { const intensity = 1 - distanceFromEnd / threshold; return { amount: maxSpeed * intensity * intensity, edge: "end" }; } return { amount: 0, edge: null }; } function autoScrollIfNeeded(groupElement, pointerPosition, axis, velocity) { if (!groupElement) return; currentGroupElement = groupElement; const scrollableAncestor = findScrollableAncestor(groupElement, axis); if (!scrollableAncestor) return; const { amount: scrollAmount, edge } = getScrollAmount(pointerPosition - (axis === "x" ? window.scrollX : window.scrollY), scrollableAncestor, axis); if (edge === null) { activeScrollEdge.delete(scrollableAncestor); initialScrollLimits.delete(scrollableAncestor); return; } const currentActiveEdge = activeScrollEdge.get(scrollableAncestor); const isDocument = isDocumentScroll(scrollableAncestor); if (currentActiveEdge !== edge) { if (!(edge === "start" && velocity < 0 || edge === "end" && velocity > 0)) return; activeScrollEdge.set(scrollableAncestor, edge); const maxScroll = axis === "x" ? scrollableAncestor.scrollWidth - (isDocument ? window.innerWidth : scrollableAncestor.clientWidth) : scrollableAncestor.scrollHeight - (isDocument ? window.innerHeight : scrollableAncestor.clientHeight); initialScrollLimits.set(scrollableAncestor, maxScroll); } if (scrollAmount > 0) { const initialLimit = initialScrollLimits.get(scrollableAncestor); if ((axis === "x" ? isDocument ? window.scrollX : scrollableAncestor.scrollLeft : isDocument ? window.scrollY : scrollableAncestor.scrollTop) >= initialLimit) return; } if (axis === "x") { if (isDocument) window.scrollBy({ left: scrollAmount }); else scrollableAncestor.scrollLeft += scrollAmount; } else if (isDocument) window.scrollBy({ top: scrollAmount }); else scrollableAncestor.scrollTop += scrollAmount; } //#endregion export { autoScrollIfNeeded, resetAutoScrollState };