previewImageFoundation.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 { ratio, setRatio } = this.getProps();
  70. const { originImageWidth, originImageHeight } = this._adapter.getOriginImageSize();
  71. if (originImageWidth && originImageHeight) {
  72. if (ratio !== "adaptation") {
  73. setRatio("adaptation");
  74. } else {
  75. this.handleResizeImage();
  76. }
  77. }
  78. };
  79. handleLoad = (e: any): void => {
  80. if (e.target) {
  81. const { width: w, height: h } = e.target as any;
  82. this._adapter.setOriginImageSize({ originImageWidth: w, originImageHeight: h });
  83. this.setState({
  84. loading: false,
  85. } as any);
  86. this.handleResizeImage();
  87. }
  88. const { src, onLoad } = this.getProps();
  89. onLoad && onLoad(src);
  90. }
  91. handleError = (e: any): void => {
  92. const { onError, src } = this.getProps();
  93. this.setState({
  94. loading: false,
  95. } as any);
  96. onError && onError(src);
  97. }
  98. handleResizeImage = () => {
  99. const horizontal = !this._isImageVertical();
  100. const { originImageWidth, originImageHeight } = this._adapter.getOriginImageSize();
  101. const imgWidth = horizontal ? originImageWidth : originImageHeight;
  102. const imgHeight = horizontal ? originImageHeight : originImageWidth;
  103. const { onZoom } = this.getProps();
  104. const containerDOM = this._adapter.getContainer();
  105. if (containerDOM) {
  106. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  107. const reservedWidth = containerWidth - 80;
  108. const reservedHeight = containerHeight - 80;
  109. const _zoom = Number(
  110. Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2)
  111. );
  112. onZoom(_zoom);
  113. }
  114. }
  115. handleRightClickImage = (e: any) => {
  116. const { disableDownload } = this.getProps();
  117. if (disableDownload) {
  118. e.preventDefault();
  119. e.stopPropagation();
  120. return false;
  121. } else {
  122. return true;
  123. }
  124. };
  125. // e: WheelEvent<HTMLImageElement>
  126. handleWheel = (e: any) => {
  127. this.onWheel(e);
  128. handlePrevent(e);
  129. }
  130. // e: WheelEvent<HTMLImageElement>
  131. onWheel = throttle((e: any): void => {
  132. const { onZoom, zoomStep, maxZoom, minZoom } = this.getProps();
  133. const { currZoom } = this.getStates();
  134. let _zoom:number;
  135. if (e.deltaY < 0) {
  136. /* zoom in */
  137. if (currZoom + zoomStep <= maxZoom) {
  138. _zoom = Number((currZoom + zoomStep).toFixed(2));
  139. }
  140. } else if (e.deltaY > 0) {
  141. /* zoom out */
  142. if (currZoom - zoomStep >= minZoom) {
  143. _zoom = Number((currZoom - zoomStep).toFixed(2));
  144. }
  145. }
  146. if (!isUndefined(_zoom)) {
  147. onZoom(_zoom);
  148. }
  149. }, 50);
  150. calcCanDragDirection = (): DragDirection => {
  151. const { width, height } = this.getStates();
  152. const { rotation } = this.getProps();
  153. const { width: containerWidth, height: containerHeight } =this._getContainerBounds();
  154. let canDragHorizontal = width > containerWidth;
  155. let canDragVertical = height > containerHeight;
  156. if (this._isImageVertical()) {
  157. canDragHorizontal = height > containerWidth;
  158. canDragVertical = width > containerHeight;
  159. }
  160. return {
  161. canDragVertical,
  162. canDragHorizontal,
  163. };
  164. };
  165. handleZoomChange = (newZoom: number, e: any): void => {
  166. const imageDOM = this._adapter.getImage();
  167. const { originImageWidth, originImageHeight } = this._adapter.getOriginImageSize();
  168. const { canDragVertical, canDragHorizontal } = this.calcCanDragDirection();
  169. const canDrag = canDragVertical || canDragHorizontal;
  170. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  171. const newWidth = Math.floor(originImageWidth * newZoom);
  172. const newHeight = Math.floor(originImageHeight * newZoom);
  173. // debugger;
  174. let _offset;
  175. const horizontal = !this._isImageVertical();
  176. let newTop = 0;
  177. let newLeft = 0;
  178. if (horizontal) {
  179. _offset = {
  180. x: 0.5 * (containerWidth - newWidth),
  181. y: 0.5 * (containerHeight - newHeight),
  182. };
  183. newLeft = _offset.x;
  184. newTop= _offset.y;
  185. } else {
  186. _offset = {
  187. x: 0.5 * (containerWidth - newHeight),
  188. y: 0.5 * (containerHeight - newWidth),
  189. };
  190. newLeft = _offset.x - (newWidth - newHeight) / 2;
  191. newTop = _offset.y + (newWidth - newHeight) / 2;
  192. }
  193. this.setState({
  194. width: newWidth,
  195. height: newHeight,
  196. offset: _offset,
  197. left: newLeft,
  198. top: newTop,
  199. currZoom: newZoom,
  200. } as any);
  201. if (imageDOM) {
  202. this._adapter.setImageCursor(canDrag);
  203. }
  204. };
  205. calcExtremeBounds = (): ExtremeBounds => {
  206. const { width, height } = this.getStates();
  207. const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
  208. let extremeLeft = containerWidth - width;
  209. let extremeTop = containerHeight - height;
  210. if (this._isImageVertical()) {
  211. extremeLeft = containerWidth - height;
  212. extremeTop = containerHeight - width;
  213. }
  214. return {
  215. left: extremeLeft,
  216. top: extremeTop,
  217. };
  218. };
  219. handleMoveImage = (e: any): void => {
  220. const { offset, width, height } = this.getStates();
  221. const startMouseMove = this._adapter.getMouseMove();
  222. const startMouseOffset = this._adapter.getMouseOffset();
  223. const { canDragVertical, canDragHorizontal } = this.calcCanDragDirection();
  224. if (startMouseMove && (canDragVertical || canDragHorizontal)) {
  225. const { clientX, clientY } = e;
  226. const { left: containerLeft, top: containerTop } = this._getContainerBounds();
  227. const { left: extremeLeft, top: extremeTop } = this.calcExtremeBounds();
  228. let newX = canDragHorizontal ? clientX - containerLeft - startMouseOffset.x : offset.x;
  229. let newY = canDragVertical ? clientY - containerTop - startMouseOffset.y : offset.y;
  230. if (canDragHorizontal) {
  231. newX = newX > 0 ? 0 : newX < extremeLeft ? extremeLeft : newX;
  232. }
  233. if (canDragVertical) {
  234. newY = newY > 0 ? 0 : newY < extremeTop ? extremeTop : newY;
  235. }
  236. const _offset = {
  237. x: newX,
  238. y: newY,
  239. };
  240. this.setState({
  241. offset: _offset,
  242. left: this._isImageVertical() ? _offset.x - (width - height) / 2 : _offset.x,
  243. top: this._isImageVertical() ? _offset.y + (width - height) / 2 : _offset.y,
  244. } as any);
  245. }
  246. };
  247. handleImageMouseDown = (e: any): void => {
  248. this._adapter.setStartMouseOffset(this._getOffset(e));
  249. this._adapter.setStartMouseMove(true);
  250. };
  251. handleImageMouseUp = (): void => {
  252. this._adapter.setStartMouseMove(false);
  253. };
  254. }