@hyext-beyond/hy-ui-native
Version:
A native lib polyfill for huya miniapp
144 lines (140 loc) • 7.41 kB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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); }
/*
* A smart image loader for react-native apps
* https://github.com/react-native-component/react-native-smart-image-loader/
* Released under the MIT license
* Copyright (c) 2016 react-native-component <moonsunfall@aliyun.com>
*/
import React, { Component } from "react";
import ReactNative, { View, NativeModules } from "react-native";
import { hyRequireNativeComponent } from "../../common/require";
import { __DEV__ } from "../../common/env";
import { contentModeMap } from "./contentMode";
var NativeImageLoader = hyRequireNativeComponent("RCTImageLazyLoader");
var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
var AssetRegistry = require("react-native/Libraries/Image/AssetRegistry");
var PropTypes = process.env.NODE_ENV !== "production" ? require("prop-types") : {};
// contentMode实验对比: 0 stretch 一致, 1 contain 一致, 2 ios 图片溢出container, 安卓正常
export var NativeImage = /*#__PURE__*/function (_Component) {
_inheritsLoose(NativeImage, _Component);
/**
* 移除监听,页面生命周期结束后必须移除已经监听的事件
* @param {String} contentMode 图片拉伸模式,0:拉伸填满整个view,会变形;1:等比缩放,以view比例比较小的边为比例,
* view可能会出现空白;2:等比缩放,填满整个view,图片可能会被裁剪
*/
function NativeImage(props) {
var _this;
_this = _Component.call(this, props) || this;
_this.state = {};
return _this;
}
var _proto = NativeImage.prototype;
_proto.startAnimation = function startAnimation() {
NativeModules.ImageLazyLoader.startAnimation(ReactNative.findNodeHandle(this.imageView));
};
_proto.stopAnimation = function stopAnimation() {
NativeModules.ImageLazyLoader.stopAnimation(ReactNative.findNodeHandle(this.imageView));
};
_proto.render = function render() {
var _this2 = this;
var imgUrl = "";
if (__DEV__) {
var base64ID = this.props.source.base64;
if (Number.isInteger(base64ID)) {
imgUrl = resolveAssetSource(base64ID).uri;
}
}
//DEV source={require('image')}
if (typeof this.props.source == "number") {
imgUrl = resolveAssetSource(this.props.source).uri;
}
//RELEASE source={{uri: 'http://xxx'}}
else if (typeof this.props.source == "object" && this.props.source.uri) {
imgUrl = this.props.source.uri;
}
//RELEASE source={'http://xxx'}
else {}
var placeholder = this.props.source.placeholder;
if (placeholder && placeholder.length) {
placeholder = placeholder.replace("res://", "");
}
var originalAsset = __DEV__ ? undefined : AssetRegistry.getAssetByID(this.props.source.base64);
var mode = this.props.mode || "stretch";
var contentMode = contentModeMap[mode];
var style = this.props.style || {};
var children = this.props.children;
return /*#__PURE__*/React.createElement(View, {
style: [style, {
overflow: 'hidden'
}]
}, /*#__PURE__*/React.createElement(NativeImageLoader, _extends({
ref: function ref(_ref) {
_this2.imageView = _ref;
}
}, this.props, {
style: {
width: style.width,
height: style.height,
position: 'absolute',
left: 0,
top: 0
},
source: {
uri: imgUrl || "",
uris: this.props.source.uris,
repeat: this.props.source.repeat,
duration: this.props.source.duration,
animated: this.props.source.animated,
resource_bundle: this.props.source.resource_bundle,
placeholder: placeholder,
base64: originalAsset !== undefined ? originalAsset.base64 : undefined,
autoPlay: this.props.source.autoPlay == null ? true : this.props.source.autoPlay
},
loopCount: this.props.loopCount,
contentMode: contentMode,
onDownloadSuccess: function onDownloadSuccess(params) {
_this2.props.onDownloadSuccess && _this2.props.onDownloadSuccess(params);
},
onDownloadFailed: function onDownloadFailed(err) {
_this2.props.onDownloadFailed && _this2.props.onDownloadFailed(err);
},
onPlayComplete: function onPlayComplete() {
_this2.props.onPlayComplete && _this2.props.onPlayComplete();
},
onPlayFailed: function onPlayFailed() {
_this2.props.onPlayFailed && _this2.props.onPlayFailed();
}
})), children);
};
return NativeImage;
}(Component);
NativeImage.propTypes = process.env.NODE_ENV !== "production" ? _objectSpread(_objectSpread({}, View.propTypes), {}, {
options: PropTypes.shape({
src: PropTypes.string,
placeholder: PropTypes.string
}),
loopCount: PropTypes.number,
source: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
uri: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
uris: PropTypes.array,
repeat: PropTypes.bool,
duration: PropTypes.number,
animated: PropTypes.bool,
placeholder: PropTypes.string,
autoPlay: PropTypes.bool,
base64: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
})]),
contentMode: PropTypes.number,
onDownloadSuccess: PropTypes.func,
onDownloadFailed: PropTypes.func,
onPlayComplete: PropTypes.func,
onPlayFailed: PropTypes.func
}) : {};
export default NativeImage;