UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

235 lines (234 loc) 9.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Barrage", { enumerable: true, get: function() { return Barrage; } }); var _interop_require_default = require("@swc/helpers/_/_interop_require_default"); var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); var _async_to_generator = require("@swc/helpers/_/_async_to_generator"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_spread_props = require("@swc/helpers/_/_object_spread_props"); var _object_without_properties = require("@swc/helpers/_/_object_without_properties"); var _ts_generator = require("@swc/helpers/_/_ts_generator"); var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react")); var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames")); var _components = require("@tarojs/components"); var _typings = require("../../utils/typings"); var _getrect = require("../../utils/taro/get-rect"); var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), { list: [], interval: 500, loop: true, duration: 3000, rows: 3, gapY: 10 }); var classPrefix = "nut-barrage"; var InternalBarrage = function InternalBarrage(props, ref) { var _ref = (0, _object_spread._)({}, defaultProps, props), className = _ref.className, interval = _ref.interval, loop = _ref.loop, list = _ref.list, duration = _ref.duration, rows = _ref.rows, gapY = _ref.gapY, restProps = (0, _object_without_properties._)(_ref, [ "className", "interval", "loop", "list", "duration", "rows", "gapY" ]); var barrageBody = (0, _react.useRef)(null); var barrageContainer = (0, _react.useRef)(null); var barrageCWidth = (0, _react.useRef)(0); var timer = (0, _react.useRef)(0); var domTimer = (0, _react.useRef)(0); var index = (0, _react.useRef)(0); var times = (0, _react.useRef)([]); var historyIndex = (0, _react.useRef)(-1); var classes = (0, _classnames.default)(classPrefix, className); (0, _react.useImperativeHandle)(ref, function() { return { add: function add(word) { var _index = index.current % list.length; if (!loop && index.current === list.length) { list.splice(list.length, 0, word); } else { list.splice(_index, 0, word); } } }; }); var getNodeWidth = function getNodeWidth(node) { var type = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 'width'; return (0, _async_to_generator._)(function() { var refe; return (0, _ts_generator._)(this, function(_state) { switch(_state.label){ case 0: if (!node) return [ 3, 2 ]; return [ 4, (0, _getrect.getRectInMultiPlatform)(node) ]; case 1: refe = _state.sent(); return [ 2, Math.ceil(type === 'height' ? refe.height : refe.width) ]; case 2: return [ 2, 0 ]; } }); })(); }; var clearDomTimeout = function clearDomTimeout() { if (domTimer.current) { clearTimeout(domTimer.current); domTimer.current = 0; } }; (0, _react.useEffect)(function() { var init = function init() { return (0, _async_to_generator._)(function() { var _barrageBody_current; return (0, _ts_generator._)(this, function(_state) { switch(_state.label){ case 0: if (!barrageBody.current) return [ 3, 2 ]; return [ 4, getNodeWidth(barrageBody.current) ]; case 1: barrageCWidth.current = _state.sent(); (_barrageBody_current = barrageBody.current) === null || _barrageBody_current === void 0 ? void 0 : _barrageBody_current.style.setProperty('--move-distance', "-".concat(barrageCWidth.current, "px")); index.current = 0; run(); _state.label = 2; case 2: return [ 2 ]; } }); })(); }; domTimer.current = window.setTimeout(function() { init(); }, 300); return function() { clearDomTimeout(); }; }, [ list ]); var run = function run() { clearTimeout(timer.current); var intervalCache = interval; var _index = (loop ? index.current % list.length : index.current) % rows; var result = times.current[_index] - rows * interval; if (result > 0) { intervalCache += result; } timer.current = window.setTimeout(function() { play(); }, intervalCache); }; var setStyle = function setStyle(el, currentIndex) { return (0, _async_to_generator._)(function() { var refe, width, height, elScrollDuration, error; return (0, _ts_generator._)(this, function(_state) { switch(_state.label){ case 0: _state.trys.push([ 0, 3, , 4 ]); if (!el) return [ 3, 2 ]; return [ 4, (0, _getrect.getRectInMultiPlatform)(el) ]; case 1: refe = _state.sent(); width = refe.width; height = refe.height; el.classList.add('move'); elScrollDuration = Math.round(width / barrageCWidth.current * duration); times.current[currentIndex] = elScrollDuration; el.style.animationDuration = "".concat(duration + elScrollDuration, "ms"); el.style.top = "".concat(currentIndex * (height + gapY) + 20, "px"); el.style.width = "".concat(width, "px"); _state.label = 2; case 2: return [ 3, 4 ]; case 3: error = _state.sent(); console.log('异常自动流转到下一个', error); barrageContainer.current.removeChild(el); return [ 3, 4 ]; case 4: el.addEventListener('animationend', function() { ; barrageContainer.current.removeChild(el); }); return [ 2 ]; } }); })(); }; var play = function play() { if (!loop && index.current >= list.length) { return; } var _index = loop ? index.current % list.length : index.current; var el = document.createElement("View"); var currentIndex = _index % rows; if (currentIndex <= historyIndex.current || historyIndex.current === 3 && currentIndex !== 0 || Math.abs(currentIndex - historyIndex.current) !== 1) { currentIndex = historyIndex.current + 1 >= rows ? 0 : historyIndex.current + 1; } historyIndex.current = currentIndex; el.innerHTML = list[_index]; barrageContainer.current.appendChild(el); el.classList.add('barrage-item'); requestAnimationFrame(function() { return setStyle(el, currentIndex); }); index.current++; run(); }; return /*#__PURE__*/ _react.default.createElement(_components.View, (0, _object_spread._)({ className: classes, ref: barrageBody }, restProps), /*#__PURE__*/ _react.default.createElement(_components.View, { ref: barrageContainer, className: "bContainer" })); }; var Barrage = /*#__PURE__*/ _react.default.forwardRef(InternalBarrage); Barrage.displayName = 'NutBarrage';