UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

141 lines (117 loc) 3.85 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } /* eslint-disable react/no-danger */ import React, { Component } from 'react'; import { findDOMNode } from 'react-dom'; import LightBox from 'react-image-lightbox'; import omit from 'lodash/omit'; import { toJS } from 'mobx'; import { delta2Html } from './utils'; var RichTextViewer = /*#__PURE__*/ function (_Component) { _inherits(RichTextViewer, _Component); var _super = _createSuper(RichTextViewer); function RichTextViewer() { var _this; _classCallCheck(this, RichTextViewer); _this = _super.apply(this, arguments); _this.state = { open: false, src: '' }; _this.open = function (src) { _this.setState({ open: true, src: src }); }; _this.escape = function (str) { return str.replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--'); }; return _this; } _createClass(RichTextViewer, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; var deltaOps = this.props.deltaOps; var thisNode = findDOMNode(this); if (thisNode) { thisNode.addEventListener('click', function (e) { var _e$target; // @ts-ignore if (((_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.nodeName) === 'IMG' && deltaOps && deltaOps.search(e.target.src) > -1) { e.stopPropagation(); // @ts-ignore _this2.open(e.target.src); } }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var thisNode = findDOMNode(this); if (thisNode) thisNode.removeEventListener('click', this.open); } }, { key: "getOtherProps", value: function getOtherProps() { return omit(this.props, ['deltaOps']); } }, { key: "render", value: function render() { var _this3 = this; var deltaOps = this.props.deltaOps; var _this$state = this.state, open = _this$state.open, src = _this$state.src; var html = delta2Html(toJS(deltaOps)); return React.createElement("div", _extends({}, this.getOtherProps()), React.createElement("div", { dangerouslySetInnerHTML: { __html: "".concat(this.escape(html)) } }), open ? React.createElement(LightBox, { mainSrc: src, onCloseRequest: function onCloseRequest() { return _this3.setState({ open: false }); }, imageTitle: "images" }) : null); } }]); return RichTextViewer; }(Component); export default RichTextViewer; //# sourceMappingURL=RichTextViewer.js.map