通用评论

ImagePreviewer.jsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React from "react";
  2. import classnames from "classnames";
  3. import { Icon, Spin } from "antd";
  4. import intl from "react-intl-universal";
  5. import "./ImagePreviewer.less";
  6. // import "./ImagePreviewer.css";
  7. // import styles from "./ImagePreviewer.module.css";
  8. import { addImageProcess } from "../../helper";
  9. // import styles from "./ImagePreviewer.module.less";
  10. export default class ImagePreviewer extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.setIndex = this.setIndex.bind(this);
  14. this.onPrev = this.onPrev.bind(this);
  15. this.onNext = this.onNext.bind(this);
  16. this.onOrigin = this.onOrigin.bind(this);
  17. this.rotateLeft = this.rotateLeft.bind(this);
  18. this.rotateRight = this.rotateRight.bind(this);
  19. this.onImgLoad = this.onImgLoad.bind(this);
  20. this.onImageError = this.onImageError.bind(this);
  21. this.state = {
  22. cIndex: props.index,
  23. direction: 0, // 0- 0deg 1- 90deg 2- 180deg 3- 270deg
  24. loading: false
  25. };
  26. }
  27. componentWillReceiveProps(nextProps) {
  28. if (nextProps.index !== this.props.index) {
  29. this.setIndex(nextProps.index);
  30. }
  31. }
  32. onPrev() {
  33. const prev =
  34. this.state.cIndex === 0
  35. ? this.props.list.length - 1
  36. : this.state.cIndex - 1;
  37. this.setIndex(prev);
  38. }
  39. onNext() {
  40. const next =
  41. this.state.cIndex === this.props.list.length - 1
  42. ? 0
  43. : this.state.cIndex + 1;
  44. this.setIndex(next);
  45. }
  46. onOrigin() {
  47. window.open(this.props.list[this.state.cIndex]);
  48. }
  49. onImgLoad() {
  50. this.setState({
  51. loading: false
  52. });
  53. }
  54. onImageError() {
  55. this.setState({
  56. loading: false
  57. });
  58. }
  59. setIndex(index) {
  60. this.setState({
  61. cIndex: index,
  62. direction: 0,
  63. loading: true
  64. });
  65. }
  66. rotateLeft() {
  67. let direction = this.state.direction - 1;
  68. if (direction === -1) {
  69. direction = 3;
  70. }
  71. this.setState({
  72. direction
  73. });
  74. }
  75. rotateRight() {
  76. let direction = this.state.direction + 1;
  77. if (direction === 4) {
  78. direction = 0;
  79. }
  80. this.setState({
  81. direction
  82. });
  83. }
  84. // calcHeight(node) {
  85. // const { naturalHeight, naturalWidth } = node;
  86. // }
  87. render() {
  88. const { list, onFold } = this.props;
  89. const { cIndex } = this.state;
  90. return (
  91. <div className="comment-image-preview-container">
  92. <div className="toolbar">
  93. <span className="button" onClick={onFold}>
  94. <Icon type="to-top" />
  95. {intl.get("picture.collapse")}
  96. </span>
  97. <span className="button" onClick={this.onOrigin}>
  98. <Icon type="search" /> {intl.get("picture.viewOriginal")}
  99. </span>
  100. {/* <span className="button" onClick={this.rotateRight}>
  101. <Icon type="reload" /> 向右旋转
  102. </span>
  103. <span className={classnames("button", "reversal")} onClick={this.rotateLeft}>
  104. <Icon type="reload" /> 向左旋转
  105. </span> */}
  106. </div>
  107. <div className="pictureWrapper">
  108. <Spin spinning={this.state.loading}>
  109. <img
  110. // ref={node => {
  111. // if (node) {
  112. // this.calcHeight(node);
  113. // }
  114. // }}
  115. className="picture"
  116. src={addImageProcess(list[cIndex], { large: true })}
  117. alt={list[cIndex]}
  118. style={{
  119. transform: `rotate(${this.state.direction * 90}deg)`,
  120. opacity: this.state.loading ? 0 : 1,
  121. transition: "0.3s opacity"
  122. }}
  123. onLoad={this.onImgLoad}
  124. onError={this.onImageError}
  125. />
  126. </Spin>
  127. <div className="tools">
  128. {list.length > 1 && (
  129. <div
  130. onClick={this.onPrev}
  131. className={classnames("item", "left")}
  132. />
  133. )}
  134. <div onClick={onFold} className={classnames("item", "shrink")} />
  135. {list.length > 1 && (
  136. <div
  137. onClick={this.onNext}
  138. className={classnames("item", "right")}
  139. />
  140. )}
  141. </div>
  142. {/* <div className="prev" onClick={this.onPrev}>
  143. <Icon className="middle" type="left" />
  144. </div>
  145. <div className="next" onClick={this.onNext}>
  146. <Icon className="middle" type="right" />
  147. </div> */}
  148. {/* <div className="picture" style={{ backgroundImage: `url(${addImageProcess(list[cIndex])})` }} /> */}
  149. </div>
  150. {list.length > 1 && (
  151. <div className="list">
  152. {list.map((item, index) => (
  153. <div
  154. className={classnames({
  155. wrapper: true,
  156. active: index === cIndex
  157. })}
  158. key={item}
  159. >
  160. <div
  161. className={classnames({
  162. thumbnail: true
  163. })}
  164. style={{
  165. backgroundImage: `url(${addImageProcess(item, {
  166. small: true
  167. })})`
  168. }}
  169. onClick={() => {
  170. this.setIndex(index);
  171. }}
  172. />
  173. </div>
  174. ))}
  175. </div>
  176. )}
  177. </div>
  178. );
  179. }
  180. }