通用评论

ImagePreviewer.jsx 5.1KB

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