UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

197 lines 11.1 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } import { rpx2vw4style as __rpx2vw__ } from "@alifd/babel-runtime-jsx-style-transform"; 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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 _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _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); } import { __rest } from "tslib"; import React, { createElement, Component, createRef } from "react"; import View from '../view'; import { previewImage } from '@uni/image'; import { isWeb } from '@uni/env'; import { isMiniappPlatform } from '../utils'; import { guid } from '../utils/hooks'; import HtmlParser from './html-parser'; import Node from './node'; import { getVideoContext } from './util'; var RichText = /*#__PURE__*/function (_Component) { _inherits(RichText, _Component); function RichText() { var _this; _classCallCheck(this, RichText); _this = _callSuper(this, RichText, arguments); _this.state = { nodes: [] }; _this.imgList = []; _this.videos = {}; _this.plugins = []; _this.ref = /*#__PURE__*/createRef(); _this.wrapTable = function () { var rootNode = _this.ref.current; if (rootNode) { var tables = rootNode.getElementsByTagName('table'); Array.from(tables).forEach(function (tableNode) { var tmp = document.createElement('div'); tmp.style.overflowX = 'auto'; tmp.style.padding = '1px'; tmp.appendChild(tableNode.cloneNode(true)); tableNode.parentNode.insertBefore(tmp, tableNode); tableNode.remove(); }); } }; _this.replaceErrorImg = function () { var rootNode = _this.ref.current; if (rootNode) { _toConsumableArray(rootNode.getElementsByTagName('img')).forEach(function (img) { if (img && img.addEventListener) { img.addEventListener('error', function () { img.src = _this.props.errorImage; }); } }); } }; _this.parseHtmlText = function (text) { var nodes = new HtmlParser(_assertThisInitialized(_this)).parse(text); _this.setState({ nodes: nodes }); }; _this.handleImageClick = function (i) { if (_this.props.enableImagePreview) { previewImage({ urls: _this.imgList, current: i || 0 }).then(function () {}); } }; _this.handleImageError = function (src, i) { var errorImage = _this.props.errorImage; _this.imgList[i] = errorImage; }; _this.handleVideoLoad = function (id) { _this.videos[id] = getVideoContext(id); }; _this.handleVideoPlay = function (id) { if (_this.props.pauseVideo) { Object.entries(_this.videos).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), videoId = _ref2[0], ctx = _ref2[1]; if (videoId !== id && ctx && ctx.pause) { ctx.pause(); } }); } }; return _this; } _createClass(RichText, [{ key: "componentWillMount", value: function componentWillMount() { if (isMiniappPlatform) { var text = this.props.text; this.parseHtmlText(text); } } }, { key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { if (isMiniappPlatform) { var text = nextProps.text; this.parseHtmlText(text); } } }, { key: "componentDidMount", value: function componentDidMount() { if (isWeb) { this.afterWebRender(); } } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (isWeb && this.props.text !== prevProps.text) { this.afterWebRender(); } } }, { key: "afterWebRender", value: function afterWebRender() { var _this$props = this.props, enableTableScroll = _this$props.enableTableScroll, errorImage = _this$props.errorImage; if (enableTableScroll) { this.wrapTable(); } if (errorImage) { this.replaceErrorImg(); } } }, { key: "render", value: function render() { var _a = this.props, style = _a.style, lazyLoad = _a.lazyLoad, text = _a.text, otherProps = __rest(_a, ["style", "lazyLoad", "text"]); return /*#__PURE__*/React.createElement(View, { id: "mtRichTextRoot-".concat(guid()), ref: this.ref, className: "".concat(this.props.prefix, "rich-text"), style: __rpx2vw__(style) }, isMiniappPlatform ? /*#__PURE__*/React.createElement(Node, _extends({ lazyLoad: lazyLoad, nodes: this.state.nodes, onImageClick: this.handleImageClick, onImgError: this.handleImageError, onVideoLoad: this.handleVideoLoad, onVideoPlay: this.handleVideoPlay }, otherProps)) : /*#__PURE__*/React.createElement("div", { dangerouslySetInnerHTML: { __html: text || '' } })); } }]); return RichText; }(Component); RichText.defaultProps = { prefix: 'mt-', text: '', errorImage: 'https://gw.alicdn.com/imgextra/i4/O1CN014BzOrR1EjHycne0VV_!!6000000000387-2-tps-200-200.png', lazyLoad: false, enableImagePreview: true, useAnchor: false, pauseVideo: true, enableTableScroll: false, tagStyle: { table: 'box-sizing: border-box; ', li: 'margin: 5px 0;', img: 'width: auto' } }; export default RichText;