choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
172 lines (148 loc) • 4.85 kB
JavaScript
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);
};
}
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 classNames from 'classnames';
import 'react-quill/dist/quill.snow.css';
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,
images: [],
srcIndex: 0
};
_this.open = function (e) {
var _e$target;
if (((_e$target = e.target) === null || _e$target === void 0 ? void 0 : _e$target.nodeName) === 'IMG') {
e.stopPropagation();
var deltaOps = _this.props.deltaOps;
var imgArr = [];
deltaOps.forEach(function (item) {
var image = item.insert.image;
if (image) {
imgArr.push(image);
}
});
var index = imgArr.findIndex(function (img) {
return img === e.target.src;
});
_this.setState({
open: true,
images: imgArr,
srcIndex: index
});
}
};
_this.escape = function (str) {
return str.replace(/<\/script/g, '<\\/script').replace(/<!--/g, '<\\!--');
};
return _this;
}
_createClass(RichTextViewer, [{
key: "componentDidMount",
value: function componentDidMount() {
var deltaOps = this.props.deltaOps;
var thisNode = findDOMNode(this);
if (thisNode && deltaOps) {
thisNode.addEventListener('click', this.open);
}
}
}, {
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 _this2 = this;
var _this$props = this.props,
deltaOps = _this$props.deltaOps,
className = _this$props.className;
var _this$state = this.state,
open = _this$state.open,
images = _this$state.images,
srcIndex = _this$state.srcIndex;
var html = delta2Html(toJS(deltaOps));
var classString = classNames(className, 'quill');
return React.createElement("div", _extends({}, this.getOtherProps(), {
className: classString
}), React.createElement("div", {
className: "ql-container ql-snow"
}, React.createElement("div", {
dangerouslySetInnerHTML: {
__html: "".concat(this.escape(html))
},
className: "ql-editor"
}), open ? React.createElement(LightBox, {
mainSrc: images[srcIndex],
onCloseRequest: function onCloseRequest() {
return _this2.setState({
open: false
});
},
imageTitle: "images",
prevSrc: images[srcIndex - 1],
nextSrc: images[srcIndex + 1],
onMovePrevRequest: function onMovePrevRequest() {
_this2.setState({
srcIndex: srcIndex - 1
});
},
onMoveNextRequest: function onMoveNextRequest() {
_this2.setState({
srcIndex: srcIndex + 1
});
}
}) : null));
}
}]);
return RichTextViewer;
}(Component);
export default RichTextViewer;
//# sourceMappingURL=RichTextViewer.js.map