@flatbiz/antd
Version:
125 lines (121 loc) • 5.9 kB
JavaScript
/*! @flatjs/forge MIT @flatbiz/antd */
import { classNames } from '@dimjs/utils/class-names/class-names';
import { useRef } from 'react';
import { useMutationObserver } from 'ahooks';
import { fbaHooks } from './fba-hooks/index.js';
import { jsx } from 'react/jsx-runtime';
var RollLocationInView = function RollLocationInView(props) {
var rollLocationInViewRef = useRef(null);
var behavior = props.behavior || 'smooth';
var activeOffset = props.activeOffset || 0;
var direction = props.direction || 'vertical';
var targetVerticalRoll = function targetVerticalRoll(key) {
try {
var _rollLocationInViewRe, _rollLocationInViewRe2, _rollLocationInViewRe3;
var targetNode = (_rollLocationInViewRe = rollLocationInViewRef.current) === null || _rollLocationInViewRe === void 0 ? void 0 : _rollLocationInViewRe.querySelector(".roll-location-in-view-item-".concat(key));
var parentNodeHeight = (_rollLocationInViewRe2 = rollLocationInViewRef.current) === null || _rollLocationInViewRe2 === void 0 ? void 0 : _rollLocationInViewRe2.offsetHeight;
var scrollerScrollTop = (_rollLocationInViewRe3 = rollLocationInViewRef.current) === null || _rollLocationInViewRe3 === void 0 ? void 0 : _rollLocationInViewRe3.scrollTop;
var targetNodeOffsetTop = targetNode.offsetTop;
var targetTop2ScrollWindowHeight = targetNodeOffsetTop - scrollerScrollTop;
var targetNodeHeight = targetNode.offsetHeight;
if (targetNodeOffsetTop === 0) {
var _rollLocationInViewRe4;
(_rollLocationInViewRe4 = rollLocationInViewRef.current) === null || _rollLocationInViewRe4 === void 0 || _rollLocationInViewRe4.scrollTo({
top: 0,
behavior: behavior
});
} else {
if (targetTop2ScrollWindowHeight < 0) {
var _rollLocationInViewRe5;
(_rollLocationInViewRe5 = rollLocationInViewRef.current) === null || _rollLocationInViewRe5 === void 0 || _rollLocationInViewRe5.scrollTo({
top: targetNodeOffsetTop - activeOffset,
behavior: behavior
});
} else if (targetTop2ScrollWindowHeight >= 0 && targetTop2ScrollWindowHeight + targetNodeHeight < parentNodeHeight) {
//
} else {
var _rollLocationInViewRe6;
var xx = targetNodeOffsetTop - (parentNodeHeight - targetNodeHeight);
(_rollLocationInViewRe6 = rollLocationInViewRef.current) === null || _rollLocationInViewRe6 === void 0 || _rollLocationInViewRe6.scrollTo({
top: xx + activeOffset,
behavior: behavior
});
}
}
} catch (_error) {
// 异常不处理
}
};
var targetHorizontalRoll = function targetHorizontalRoll(key) {
try {
var _rollLocationInViewRe7, _rollLocationInViewRe8, _rollLocationInViewRe9;
var targetNode = (_rollLocationInViewRe7 = rollLocationInViewRef.current) === null || _rollLocationInViewRe7 === void 0 ? void 0 : _rollLocationInViewRe7.querySelector(".roll-location-in-view-item-".concat(key));
var parentNodeWidth = (_rollLocationInViewRe8 = rollLocationInViewRef.current) === null || _rollLocationInViewRe8 === void 0 ? void 0 : _rollLocationInViewRe8.offsetWidth;
var scrollerScrollLeft = (_rollLocationInViewRe9 = rollLocationInViewRef.current) === null || _rollLocationInViewRe9 === void 0 ? void 0 : _rollLocationInViewRe9.scrollLeft;
var targetNodeOffsetLeft = targetNode.offsetLeft;
var targetTop2ScrollWindowWidth = targetNodeOffsetLeft - scrollerScrollLeft;
var targetNodeWidth = targetNode.offsetWidth;
if (targetNodeOffsetLeft === 0) {
var _rollLocationInViewRe10;
(_rollLocationInViewRe10 = rollLocationInViewRef.current) === null || _rollLocationInViewRe10 === void 0 || _rollLocationInViewRe10.scrollTo({
left: 0,
behavior: behavior
});
} else {
if (targetTop2ScrollWindowWidth < 0) {
var _rollLocationInViewRe11;
(_rollLocationInViewRe11 = rollLocationInViewRef.current) === null || _rollLocationInViewRe11 === void 0 || _rollLocationInViewRe11.scrollTo({
left: targetNodeOffsetLeft - activeOffset,
behavior: behavior
});
} else if (targetTop2ScrollWindowWidth >= 0 && targetTop2ScrollWindowWidth + targetNodeWidth < parentNodeWidth) {
//
} else {
var _rollLocationInViewRe12;
var xx = targetNodeOffsetLeft - (parentNodeWidth - targetNodeWidth);
(_rollLocationInViewRe12 = rollLocationInViewRef.current) === null || _rollLocationInViewRe12 === void 0 || _rollLocationInViewRe12.scrollTo({
left: xx + activeOffset,
behavior: behavior
});
}
}
} catch (_error) {
// 异常不处理
}
};
fbaHooks.useEffectCustom(function () {
if (props.activeKey) {
if (direction === 'vertical') {
targetVerticalRoll(props.activeKey);
} else {
targetHorizontalRoll(props.activeKey);
}
}
}, [props.activeKey]);
useMutationObserver(function () {
if (props.activeKey) {
if (direction === 'vertical') {
targetVerticalRoll(props.activeKey);
} else {
targetHorizontalRoll(props.activeKey);
}
}
}, rollLocationInViewRef, {
subtree: true,
childList: true,
characterData: true
});
return /*#__PURE__*/jsx("div", {
className: classNames('roll-location-in-view', "roll-location-in-view-".concat(direction), props.className),
style: props.style,
ref: rollLocationInViewRef,
children: props.renderList.map(function (item) {
return /*#__PURE__*/jsx("div", {
className: "roll-location-in-view-item-".concat(item.activeKey),
children: item.render
}, item.activeKey);
})
});
};
export { RollLocationInView as R };
//# sourceMappingURL=in-view-d3iIBPld.js.map