@hyext-beyond/hy-ui-native
Version:
A native lib polyfill for huya miniapp
113 lines (112 loc) • 4.54 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
// @ts-nocheck
import lottie from 'lottie-web';
import React from 'react';
import ReactDOM from 'react-dom';
import { View } from 'react-native';
var Animation = /*#__PURE__*/function (_React$PureComponent) {
_inheritsLoose(Animation, _React$PureComponent);
function Animation() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
_this.animationDOMNode = null;
_this.loadAnimation = function (props) {
if (_this.anim) {
_this.anim.destroy();
}
var isSourceObj = props.source instanceof Object;
var isWithUri = Object.keys(props.source)[0] == 'uri';
if (isSourceObj && isWithUri) {
_this.anim = lottie.loadAnimation({
container: _this.animationDOMNode,
path: props.source.uri,
renderer: 'svg',
loop: props.loop || false,
autoplay: props.autoPlay,
rendererSettings: props.rendererSettings || {},
speed: props.speed || 1,
assetsPath: props.imageAssetsFolder || ''
});
} else {
_this.anim = lottie.loadAnimation({
container: _this.animationDOMNode,
animationData: props.source,
renderer: 'svg',
loop: props.loop || false,
autoplay: props.autoPlay,
rendererSettings: props.rendererSettings || {},
speed: props.speed || 1,
assetsPath: props.imageAssetsFolder || ''
});
}
if (props.onAnimationFinish) {
_this.anim.addEventListener('complete', props.onAnimationFinish);
}
if (props.onLayout) {
_this.anim.addEventListener('DOMLoaded', props.onLayout);
}
};
_this.play = function () {
if (!_this.anim) {
return;
}
for (var _len2 = arguments.length, frames = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
frames[_key2] = arguments[_key2];
}
_this.anim.playSegments(frames, true);
};
_this.reset = function () {
if (!_this.anim) {
return;
}
_this.anim.stop();
};
_this.setAnimationDOMNode = function (ref) {
return _this.animationDOMNode = ReactDOM.findDOMNode(ref);
};
return _this;
}
var _proto = Animation.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this2 = this;
this.loadAnimation(this.props);
if (typeof this.props.progress === 'object' && this.props.progress._listeners) {
this.props.progress.addListener(function (progress) {
var value = progress.value;
var frame = value / (1 / _this2.anim.getDuration(true));
_this2.anim.goToAndStop(frame, true);
});
}
};
_proto.componentWillUnmount = function componentWillUnmount() {
if (typeof this.props.progress === 'object' && this.props.progress._listeners) {
this.props.progress.removeAllListeners();
}
};
_proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.source && nextProps.source && this.props.source.nm !== nextProps.source.nm) {
this.loadAnimation(nextProps);
}
};
_proto.render = function render() {
return /*#__PURE__*/React.createElement(View, {
style: this.props.style,
ref: this.setAnimationDOMNode
});
};
return Animation;
}(React.PureComponent);
/** HOC */
export var Lottie = /*#__PURE__*/React.forwardRef(function (props, ref) {
return /*#__PURE__*/React.createElement(Animation, _extends({}, props, {
ref: typeof ref == 'function' ? function (c) {
return ref(c && c.anim);
} : ref
}));
});
export default Lottie;