通用评论

ImagePreviewer.jsx 5.3KB

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