UNPKG

@glideapps/glide-data-grid

Version:

React data grid for beautifully displaying and editing large amounts of data with amazing performance.

253 lines (250 loc) • 8.98 kB
"use strict"; var __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function () { return m[k]; } }; } Object.defineProperty(o, k2, desc); } : function (o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; }); var __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function (o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); } : function (o, v) { o["default"] = v; }); var __importStar = this && this.__importStar || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = this && this.__importDefault || function (mod) { return mod && mod.__esModule ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InfiniteScroller = void 0; const react_1 = require("@linaria/react"); const React = __importStar(require("react")); const resize_detector_js_1 = require("../../common/resize-detector.js"); const browser_detect_js_1 = require("../../common/browser-detect.js"); const utils_js_1 = require("../../common/utils.js"); const use_kinetic_scroll_js_1 = __importDefault(require("./use-kinetic-scroll.js")); const _exp = /*#__PURE__*/() => p => p.isSafari ? "scroll" : "auto"; const ScrollRegionStyle = /*#__PURE__*/react_1.styled('div')({ name: "ScrollRegionStyle", class: "gdg-s1dgczr6", propsAsIs: false, vars: { "s1dgczr6-0": [_exp()] } }); function useTouchUpDelayed(delay) { const [hasTouches, setHasTouches] = React.useState(false); const safeWindow = typeof window === "undefined" ? null : window; const cbTimer = React.useRef(0); (0, utils_js_1.useEventListener)("touchstart", React.useCallback(() => { window.clearTimeout(cbTimer.current); setHasTouches(true); }, []), safeWindow, true, false); (0, utils_js_1.useEventListener)("touchend", React.useCallback(e => { if (e.touches.length === 0) { cbTimer.current = window.setTimeout(() => setHasTouches(false), delay); } }, [delay]), safeWindow, true, false); return hasTouches; } const InfiniteScroller = p => { const { children, clientHeight, scrollHeight, scrollWidth, update, draggable, className, preventDiagonalScrolling = false, paddingBottom = 0, paddingRight = 0, rightElement, rightElementProps, kineticScrollPerfHack = false, scrollRef, initialSize } = p; const padders = []; const rightElementSticky = rightElementProps?.sticky ?? false; const rightElementFill = rightElementProps?.fill ?? false; const offsetY = React.useRef(0); const lastScrollY = React.useRef(0); const scroller = React.useRef(null); const dpr = typeof window === "undefined" ? 1 : window.devicePixelRatio; const lastScrollPosition = React.useRef({ scrollLeft: 0, scrollTop: 0, lockDirection: undefined }); const rightWrapRef = React.useRef(null); const hasTouches = useTouchUpDelayed(200); const [isIdle, setIsIdle] = React.useState(true); const idleTimer = React.useRef(0); React.useLayoutEffect(() => { if (!isIdle || hasTouches || lastScrollPosition.current.lockDirection === undefined) return; const el = scroller.current; if (el === null) return; const [lx, ly] = lastScrollPosition.current.lockDirection; if (lx !== undefined) { el.scrollLeft = lx; } else if (ly !== undefined) { el.scrollTop = ly; } lastScrollPosition.current.lockDirection = undefined; }, [hasTouches, isIdle]); const onScroll = React.useCallback((scrollLeft, scrollTop) => { const el = scroller.current; if (el === null) return; scrollTop = scrollTop ?? el.scrollTop; scrollLeft = scrollLeft ?? el.scrollLeft; const lastScrollTop = lastScrollPosition.current.scrollTop; const lastScrollLeft = lastScrollPosition.current.scrollLeft; const dx = scrollLeft - lastScrollLeft; const dy = scrollTop - lastScrollTop; if (hasTouches && dx !== 0 && dy !== 0 && (Math.abs(dx) > 3 || Math.abs(dy) > 3) && preventDiagonalScrolling && lastScrollPosition.current.lockDirection === undefined) { lastScrollPosition.current.lockDirection = Math.abs(dx) < Math.abs(dy) ? [lastScrollLeft, undefined] : [undefined, lastScrollTop]; } const lock = lastScrollPosition.current.lockDirection; scrollLeft = lock?.[0] ?? scrollLeft; scrollTop = lock?.[1] ?? scrollTop; lastScrollPosition.current.scrollLeft = scrollLeft; lastScrollPosition.current.scrollTop = scrollTop; const cWidth = el.clientWidth; const cHeight = el.clientHeight; const newY = scrollTop; const delta = lastScrollY.current - newY; const scrollableHeight = el.scrollHeight - cHeight; lastScrollY.current = newY; if (scrollableHeight > 0 && (Math.abs(delta) > 2000 || newY === 0 || newY === scrollableHeight) && scrollHeight > el.scrollHeight + 5) { const prog = newY / scrollableHeight; const recomputed = (scrollHeight - cHeight) * prog; offsetY.current = recomputed - newY; } if (lock !== undefined) { window.clearTimeout(idleTimer.current); setIsIdle(false); idleTimer.current = window.setTimeout(() => setIsIdle(true), 200); } update({ x: scrollLeft, y: newY + offsetY.current, width: cWidth - paddingRight, height: cHeight - paddingBottom, paddingRight: rightWrapRef.current?.clientWidth ?? 0 }); }, [paddingBottom, paddingRight, scrollHeight, update, preventDiagonalScrolling, hasTouches]); (0, use_kinetic_scroll_js_1.default)(kineticScrollPerfHack && browser_detect_js_1.browserIsSafari.value, onScroll, scroller); const onScrollRef = React.useRef(onScroll); onScrollRef.current = onScroll; const lastProps = React.useRef(); const didFirstScroll = React.useRef(false); // if this is not a layout effect there will be a flicker when changing the number of freezeColumns // we need to document what this is needed at all. React.useLayoutEffect(() => { if (didFirstScroll.current) onScroll();else didFirstScroll.current = true; }, [onScroll, paddingBottom, paddingRight]); const setRefs = React.useCallback(instance => { scroller.current = instance; if (scrollRef !== undefined) { scrollRef.current = instance; } }, [scrollRef]); let key = 0; let h = 0; padders.push(React.createElement("div", { key: key++, style: { width: scrollWidth, height: 0 } })); while (h < scrollHeight) { const toAdd = Math.min(5000000, scrollHeight - h); padders.push(React.createElement("div", { key: key++, style: { width: 0, height: toAdd } })); h += toAdd; } const { ref, width, height } = (0, resize_detector_js_1.useResizeDetector)(initialSize); if (typeof window !== "undefined" && (lastProps.current?.height !== height || lastProps.current?.width !== width)) { window.setTimeout(() => onScrollRef.current(), 0); lastProps.current = { width, height }; } if ((width ?? 0) === 0 || (height ?? 0) === 0) return React.createElement("div", { ref: ref }); return React.createElement("div", { ref: ref }, React.createElement(ScrollRegionStyle, { isSafari: browser_detect_js_1.browserIsSafari.value }, React.createElement("div", { className: "dvn-underlay" }, children), React.createElement("div", { ref: setRefs, style: lastProps.current, draggable: draggable, onDragStart: e => { if (!draggable) { e.stopPropagation(); e.preventDefault(); } }, className: "dvn-scroller " + (className ?? ""), onScroll: () => onScroll() }, React.createElement("div", { className: "dvn-scroll-inner" + (rightElement === undefined ? " dvn-hidden" : "") }, React.createElement("div", { className: "dvn-stack" }, padders), rightElement !== undefined && React.createElement(React.Fragment, null, !rightElementFill && React.createElement("div", { className: "dvn-spacer" }), React.createElement("div", { ref: rightWrapRef, style: { height, maxHeight: clientHeight - Math.ceil(dpr % 1), position: "sticky", top: 0, paddingLeft: 1, marginBottom: -40, marginRight: paddingRight, flexGrow: rightElementFill ? 1 : undefined, right: rightElementSticky ? paddingRight ?? 0 : undefined, pointerEvents: "auto" } }, rightElement)))))); }; exports.InfiniteScroller = InfiniteScroller;