previewImageFoundation.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import BaseFoundation, { DefaultAdapter } from "../base/foundation";
  2. export interface PreviewImageAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  3. getContainer: () => HTMLDivElement;
  4. getImage: () => HTMLImageElement;
  5. setLoading: (loading: boolean) => void;
  6. setImageCursor: (canDrag: boolean) => void
  7. }
  8. export interface DragDirection {
  9. canDragVertical: boolean;
  10. canDragHorizontal: boolean
  11. }
  12. export interface ExtremeBounds {
  13. left: number;
  14. top: number
  15. }
  16. export interface ImageOffset {
  17. x: number;
  18. y: number
  19. }
  20. const DefaultDOMRect = {
  21. bottom: 0,
  22. height: 0,
  23. left: 0,
  24. right: 0,
  25. top: 0,
  26. width: 0,
  27. x: 0,
  28. y: 0,
  29. toJSON: () => ({})
  30. };
  31. export default class PreviewImageFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<PreviewImageAdapter<P, S>, P, S> {
  32. constructor(adapter: PreviewImageAdapter<P, S>) {
  33. super({ ...adapter });
  34. }
  35. startMouseOffset = { x: 0, y: 0 };
  36. originImageWidth = null;
  37. originImageHeight = null;
  38. _isImageVertical = (): boolean => this.getProp("rotation") % 180 !== 0;
  39. _getImageBounds = (): DOMRect => {
  40. const imageDOM = this._adapter.getImage();
  41. if (imageDOM) {
  42. return imageDOM.getBoundingClientRect();
  43. }
  44. return DefaultDOMRect;
  45. };
  46. _getContainerBounds = (): DOMRect => {
  47. const containerDOM = this._adapter.getContainer();
  48. if (containerDOM) {
  49. return containerDOM.getBoundingClientRect();
  50. }
  51. return DefaultDOMRect;
  52. }
  53. _getOffset = (e: any): ImageOffset => {
  54. const { left, top } = this._getImageBounds();
  55. return {
  56. x: e.clientX - left,
  57. y: e.clientY - top,
  58. };
  59. }
  60. setLoading = (loading: boolean) => {
  61. this._adapter.setLoading(loading);
  62. }
  63. handleWindowResize = (): void => {
  64. if (this.originImageWidth && this.originImageHeight) {
  65. this.handleResizeImage();
  66. }
  67. };
  68. handleLoad = (e: any): void => {
  69. if (e.target) {
  70. const { naturalWidth: w, naturalHeight: h } = e.target as any;
  71. this.originImageHeight = h;
  72. this.originImageWidth = w;
  73. this.setState({
  74. loading: false,
  75. } as any);
  76. // 图片初次加载,计算 zoom,zoom 改变不需要通过回调透出
  77. // When the image is loaded for the first time, zoom is calculated, and zoom changes do not need to be exposed through callbacks.
  78. this.handleResizeImage(false);
  79. }
  80. const { src, onLoad } = this.getProps();
  81. onLoad && onLoad(src);
  82. }
  83. handleError = (e: any): void => {
  84. const { onError, src } = this.getProps();
  85. this.setState({
  86. loading: false,
  87. } as any);
  88. onError && onError(src);
  89. }
  90. handleResizeImage = (notify: boolean = true) => {
  91. const horizontal = !this._isImageVertical();
  92. const { currZoom } = this.getStates();
  93. const imgWidth = horizontal ? this.originImageWidth : this.originImageHeight;
  94. const imgHeight = horizontal ? this.originImageHeight : this.originImageWidth;
  95. const { onZoom, setRatio, ratio } = this.getProps();
  96. const containerDOM = this._adapter.getContainer();
  97. if (containerDOM) {
  98. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  99. const reservedWidth = containerWidth - 80;
  100. const reservedHeight = containerHeight - 80;
  101. let _zoom = 1;
  102. if (imgWidth > reservedWidth || imgHeight > reservedHeight) {
  103. _zoom = Number(
  104. Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2)
  105. );
  106. }
  107. if (currZoom === _zoom) {
  108. this.calculatePreviewImage(_zoom, null);
  109. } else {
  110. onZoom(_zoom, notify);
  111. }
  112. }
  113. }
  114. handleRatioChange = () => {
  115. if (this.originImageWidth && this.originImageHeight) {
  116. const { currZoom } = this.getStates();
  117. const { ratio, onZoom } = this.getProps();
  118. let _zoom: number;
  119. if (ratio === 'adaptation') {
  120. const horizontal = !this._isImageVertical();
  121. const imgWidth = horizontal ? this.originImageWidth : this.originImageHeight;
  122. const imgHeight = horizontal ? this.originImageHeight : this.originImageWidth;
  123. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  124. const reservedWidth = containerWidth - 80;
  125. const reservedHeight = containerHeight - 80;
  126. _zoom = Number(
  127. Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2)
  128. );
  129. } else {
  130. _zoom = 1;
  131. }
  132. if (currZoom !== _zoom) {
  133. onZoom(_zoom);
  134. }
  135. }
  136. }
  137. handleRightClickImage = (e: any) => {
  138. const { disableDownload } = this.getProps();
  139. if (disableDownload) {
  140. e.preventDefault();
  141. e.stopPropagation();
  142. return false;
  143. } else {
  144. return true;
  145. }
  146. };
  147. calcCanDragDirection = (): DragDirection => {
  148. const { width, height } = this.getStates();
  149. const { rotation } = this.getProps();
  150. const { width: containerWidth, height: containerHeight } =this._getContainerBounds();
  151. let canDragHorizontal = width > containerWidth;
  152. let canDragVertical = height > containerHeight;
  153. if (this._isImageVertical()) {
  154. canDragHorizontal = height > containerWidth;
  155. canDragVertical = width > containerHeight;
  156. }
  157. return {
  158. canDragVertical,
  159. canDragHorizontal,
  160. };
  161. };
  162. calculatePreviewImage = (newZoom: number, e: any): void => {
  163. const imageDOM = this._adapter.getImage();
  164. const { canDragVertical, canDragHorizontal } = this.calcCanDragDirection();
  165. const canDrag = canDragVertical || canDragHorizontal;
  166. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  167. const newWidth = Math.floor(this.originImageWidth * newZoom);
  168. const newHeight = Math.floor(this.originImageHeight * newZoom);
  169. // debugger;
  170. let _offset;
  171. const horizontal = !this._isImageVertical();
  172. let newTop = 0;
  173. let newLeft = 0;
  174. if (horizontal) {
  175. _offset = {
  176. x: 0.5 * (containerWidth - newWidth),
  177. y: 0.5 * (containerHeight - newHeight),
  178. };
  179. newLeft = _offset.x;
  180. newTop= _offset.y;
  181. } else {
  182. _offset = {
  183. x: 0.5 * (containerWidth - newHeight),
  184. y: 0.5 * (containerHeight - newWidth),
  185. };
  186. newLeft = _offset.x - (newWidth - newHeight) / 2;
  187. newTop = _offset.y + (newWidth - newHeight) / 2;
  188. }
  189. this.setState({
  190. width: newWidth,
  191. height: newHeight,
  192. offset: _offset,
  193. left: newLeft,
  194. top: newTop,
  195. currZoom: newZoom,
  196. } as any);
  197. if (imageDOM) {
  198. this._adapter.setImageCursor(canDrag);
  199. }
  200. };
  201. calcExtremeBounds = (): ExtremeBounds => {
  202. const { width, height } = this.getStates();
  203. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  204. let extremeLeft = containerWidth - width;
  205. let extremeTop = containerHeight - height;
  206. if (this._isImageVertical()) {
  207. extremeLeft = containerWidth - height;
  208. extremeTop = containerHeight - width;
  209. }
  210. return {
  211. left: extremeLeft,
  212. top: extremeTop,
  213. };
  214. };
  215. handleMoveImage = (e: any): void => {
  216. const { offset, width, height } = this.getStates();
  217. const { canDragVertical, canDragHorizontal } = this.calcCanDragDirection();
  218. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
  219. const mouseLeftPress = e.buttons === 1;
  220. if (mouseLeftPress && (canDragVertical || canDragHorizontal)) {
  221. const { clientX, clientY } = e;
  222. const { left: containerLeft, top: containerTop } = this._getContainerBounds();
  223. const { left: extremeLeft, top: extremeTop } = this.calcExtremeBounds();
  224. let newX = canDragHorizontal ? clientX - containerLeft - this.startMouseOffset.x : offset.x;
  225. let newY = canDragVertical ? clientY - containerTop - this.startMouseOffset.y : offset.y;
  226. if (canDragHorizontal) {
  227. newX = newX > 0 ? 0 : newX < extremeLeft ? extremeLeft : newX;
  228. }
  229. if (canDragVertical) {
  230. newY = newY > 0 ? 0 : newY < extremeTop ? extremeTop : newY;
  231. }
  232. const _offset = {
  233. x: newX,
  234. y: newY,
  235. };
  236. this.setState({
  237. offset: _offset,
  238. left: this._isImageVertical() ? _offset.x - (width - height) / 2 : _offset.x,
  239. top: this._isImageVertical() ? _offset.y + (width - height) / 2 : _offset.y,
  240. } as any);
  241. }
  242. };
  243. handleImageMouseDown = (e: any): void => {
  244. this.startMouseOffset = this._getOffset(e);
  245. };
  246. }