1 line
15 KiB
JSON
1 line
15 KiB
JSON
|
{"ast":null,"code":"import _typeof from \"@babel/runtime/helpers/esm/typeof\";\nimport _objectSpread from \"@babel/runtime/helpers/esm/objectSpread2\";\nimport _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\n/* eslint-disable no-param-reassign */\nimport * as React from 'react';\nimport raf from \"rc-util/es/raf\";\nimport useLayoutEffect from \"rc-util/es/hooks/useLayoutEffect\";\nimport { warning } from 'rc-util';\nvar MAX_TIMES = 10;\nexport default function useScrollTo(containerRef, data, heights, itemHeight, getKey, collectHeight, syncScrollTop, triggerFlash) {\n var scrollRef = React.useRef();\n var _React$useState = React.useState(null),\n _React$useState2 = _slicedToArray(_React$useState, 2),\n syncState = _React$useState2[0],\n setSyncState = _React$useState2[1];\n\n // ========================== Sync Scroll ==========================\n useLayoutEffect(function () {\n if (syncState && syncState.times < MAX_TIMES) {\n // Never reach\n if (!containerRef.current) {\n setSyncState(function (ori) {\n return _objectSpread({}, ori);\n });\n return;\n }\n collectHeight();\n var targetAlign = syncState.targetAlign,\n originAlign = syncState.originAlign,\n index = syncState.index,\n offset = syncState.offset;\n var height = containerRef.current.clientHeight;\n var needCollectHeight = false;\n var newTargetAlign = targetAlign;\n var targetTop = null;\n\n // Go to next frame if height not exist\n if (height) {\n var mergedAlign = targetAlign || originAlign;\n\n // Get top & bottom\n var stackTop = 0;\n var itemTop = 0;\n var itemBottom = 0;\n var maxLen = Math.min(data.length - 1, index);\n for (var i = 0; i <= maxLen; i += 1) {\n var key = getKey(data[i]);\n itemTop = stackTop;\n var cacheHeight = heights.get(key);\n itemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight);\n stackTop = itemBottom;\n }\n\n // Check if need sync height (visible range has item not record height)\n var leftHeight = mergedAlign === 'top' ? offset : height - offset;\n for (var _i = maxLen; _i >= 0; _i -= 1) {\n var _key = getKey(data[_i]);\n var _cacheHeight = heights.get(_key);\n if (_cacheHeight === undefined) {\n needCollectHeight = true;\n break;\n }\n leftHeight -= _cacheHeight;\n if (leftHeight <= 0) {\n break;\n }\n }\n\n // Scroll to\n switch (mergedAlign) {\n case 'top':\n targetTop = itemTop - offset;\n break;\n case 'bottom':\n targetTop = itemBottom - height + offset;\n break;\n default:\n {\n var scrollTop = containerRef.current.scrollTop;\n var scrollBottom = scrollTop + height;\n if (itemTop < scrollTop) {\n newTargetAlign = 'top';\n } else if (itemBottom > scrollBottom) {\n newTargetAlign = 'bottom';\n }\n }\n }\n if (targetTop !== null) {\n syncScrollTop(targetTop);\n }\n\n // One more time for sync\n if (targetTop !== syncState.lastTop) {\n needCollectHeight = true;\n }\n }\n\n // Trigger next effect\n if (needCollectHeight) {\n setSyncState(_objectSpread(_objectSpread({}, syncState), {}, {\n times: syncState.times + 1,\n targetAlign: newTargetAlign,\n lastTop: targetTop\n }));\n }\n } else if (process.env.NODE_ENV !== 'production' && (syncState === null || syncState === void 0 ? void 0 : syncState.times) === MAX_TIMES) {\n warning(false, 'Seems `scrollTo` with `rc-virtual-list` reach the max limitation. Please fire issue for us. Thanks.');\n }\n }, [syncState, containerRef.current]);\n\n // =========================
|