previewImageFoundation.ts 9.5 KB

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