通用评论

ImagePreviewer.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _spin = require("antd/es/spin");
  6. var _spin2 = _interopRequireDefault(_spin);
  7. var _icon = require("antd/es/icon");
  8. var _icon2 = _interopRequireDefault(_icon);
  9. var _createClass = 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, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  10. require("antd/es/spin/style");
  11. require("antd/es/icon/style");
  12. var _react = require("react");
  13. var _react2 = _interopRequireDefault(_react);
  14. var _classnames = require("classnames");
  15. var _classnames2 = _interopRequireDefault(_classnames);
  16. var _reactIntlUniversal = require("react-intl-universal");
  17. var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
  18. require("./ImagePreviewer.less");
  19. var _helper = require("../../helper");
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  22. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  23. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  24. // import "./ImagePreviewer.css";
  25. // import styles from "./ImagePreviewer.module.css";
  26. // import styles from "./ImagePreviewer.module.less";
  27. var ImagePreviewer = function (_React$Component) {
  28. _inherits(ImagePreviewer, _React$Component);
  29. function ImagePreviewer(props) {
  30. _classCallCheck(this, ImagePreviewer);
  31. var _this = _possibleConstructorReturn(this, (ImagePreviewer.__proto__ || Object.getPrototypeOf(ImagePreviewer)).call(this, props));
  32. _this.setIndex = _this.setIndex.bind(_this);
  33. _this.onPrev = _this.onPrev.bind(_this);
  34. _this.onNext = _this.onNext.bind(_this);
  35. _this.onOrigin = _this.onOrigin.bind(_this);
  36. _this.rotateLeft = _this.rotateLeft.bind(_this);
  37. _this.rotateRight = _this.rotateRight.bind(_this);
  38. _this.onImgLoad = _this.onImgLoad.bind(_this);
  39. _this.onImageError = _this.onImageError.bind(_this);
  40. _this.state = {
  41. cIndex: props.index,
  42. direction: 0, // 0- 0deg 1- 90deg 2- 180deg 3- 270deg
  43. loading: false
  44. };
  45. return _this;
  46. }
  47. _createClass(ImagePreviewer, [{
  48. key: "componentWillReceiveProps",
  49. value: function componentWillReceiveProps(nextProps) {
  50. if (nextProps.index !== this.props.index) {
  51. this.setIndex(nextProps.index);
  52. }
  53. }
  54. }, {
  55. key: "onPrev",
  56. value: function onPrev() {
  57. var prev = this.state.cIndex === 0 ? this.props.list.length - 1 : this.state.cIndex - 1;
  58. this.setIndex(prev);
  59. }
  60. }, {
  61. key: "onNext",
  62. value: function onNext() {
  63. var next = this.state.cIndex === this.props.list.length - 1 ? 0 : this.state.cIndex + 1;
  64. this.setIndex(next);
  65. }
  66. }, {
  67. key: "onOrigin",
  68. value: function onOrigin() {
  69. window.open(this.props.list[this.state.cIndex]);
  70. }
  71. }, {
  72. key: "onImgLoad",
  73. value: function onImgLoad() {
  74. this.setState({
  75. loading: false
  76. });
  77. }
  78. }, {
  79. key: "onImageError",
  80. value: function onImageError() {
  81. this.setState({
  82. loading: false
  83. });
  84. }
  85. }, {
  86. key: "setIndex",
  87. value: function setIndex(index) {
  88. this.setState({
  89. cIndex: index,
  90. direction: 0,
  91. loading: true
  92. });
  93. }
  94. }, {
  95. key: "rotateLeft",
  96. value: function rotateLeft() {
  97. var direction = this.state.direction - 1;
  98. if (direction === -1) {
  99. direction = 3;
  100. }
  101. this.setState({
  102. direction: direction
  103. });
  104. }
  105. }, {
  106. key: "rotateRight",
  107. value: function rotateRight() {
  108. var direction = this.state.direction + 1;
  109. if (direction === 4) {
  110. direction = 0;
  111. }
  112. this.setState({
  113. direction: direction
  114. });
  115. }
  116. // calcHeight(node) {
  117. // const { naturalHeight, naturalWidth } = node;
  118. // }
  119. }, {
  120. key: "render",
  121. value: function render() {
  122. var _this2 = this;
  123. var _props = this.props,
  124. list = _props.list,
  125. onFold = _props.onFold;
  126. var cIndex = this.state.cIndex;
  127. return _react2.default.createElement(
  128. "div",
  129. { className: "comment-image-preview-container" },
  130. _react2.default.createElement(
  131. "div",
  132. { className: "toolbar" },
  133. _react2.default.createElement(
  134. "span",
  135. { className: "button", onClick: onFold },
  136. _react2.default.createElement(_icon2.default, { type: "to-top" }),
  137. _reactIntlUniversal2.default.get("picture.collapse")
  138. ),
  139. _react2.default.createElement(
  140. "span",
  141. { className: "button", onClick: this.onOrigin },
  142. _react2.default.createElement(_icon2.default, { type: "search" }),
  143. " ",
  144. _reactIntlUniversal2.default.get("picture.viewOriginal")
  145. )
  146. ),
  147. _react2.default.createElement(
  148. "div",
  149. { className: "pictureWrapper" },
  150. _react2.default.createElement(
  151. _spin2.default,
  152. { spinning: this.state.loading },
  153. _react2.default.createElement("img", {
  154. // ref={node => {
  155. // if (node) {
  156. // this.calcHeight(node);
  157. // }
  158. // }}
  159. className: "picture",
  160. src: (0, _helper.addImageProcess)(list[cIndex], { large: true }),
  161. alt: list[cIndex],
  162. style: {
  163. transform: "rotate(" + this.state.direction * 90 + "deg)",
  164. opacity: this.state.loading ? 0 : 1,
  165. transition: "0.3s opacity"
  166. },
  167. onLoad: this.onImgLoad,
  168. onError: this.onImageError
  169. })
  170. ),
  171. _react2.default.createElement(
  172. "div",
  173. { className: "tools" },
  174. list.length > 1 && _react2.default.createElement("div", {
  175. onClick: this.onPrev,
  176. className: (0, _classnames2.default)("item", "left")
  177. }),
  178. _react2.default.createElement("div", { onClick: onFold, className: (0, _classnames2.default)("item", "shrink") }),
  179. list.length > 1 && _react2.default.createElement("div", {
  180. onClick: this.onNext,
  181. className: (0, _classnames2.default)("item", "right")
  182. })
  183. )
  184. ),
  185. list.length > 1 && _react2.default.createElement(
  186. "div",
  187. { className: "list" },
  188. list.map(function (item, index) {
  189. return _react2.default.createElement(
  190. "div",
  191. {
  192. className: (0, _classnames2.default)({
  193. wrapper: true,
  194. active: index === cIndex
  195. }),
  196. key: item
  197. },
  198. _react2.default.createElement("div", {
  199. className: (0, _classnames2.default)({
  200. thumbnail: true
  201. }),
  202. style: {
  203. backgroundImage: "url(" + (0, _helper.addImageProcess)(item, {
  204. small: true
  205. }) + ")"
  206. },
  207. onClick: function onClick() {
  208. _this2.setIndex(index);
  209. }
  210. })
  211. );
  212. })
  213. )
  214. );
  215. }
  216. }]);
  217. return ImagePreviewer;
  218. }(_react2.default.Component);
  219. exports.default = ImagePreviewer;
  220. //# sourceMappingURL=ImagePreviewer.js.map