import BaseFoundation, { DefaultAdapter } from "../base/foundation"; import { handlePrevent } from "../utils/a11y"; import { throttle, isUndefined } from "lodash"; export interface PreviewImageAdapter
, S = Record {
getOriginImageSize: () => { originImageWidth: number; originImageHeight: number };
setOriginImageSize: (size: { originImageWidth: number; originImageHeight: number }) => void;
getContainer: () => HTMLDivElement;
getImage: () => HTMLImageElement;
getMouseMove: () => boolean;
setStartMouseMove: (move: boolean) => void;
getMouseOffset: () => { x: number; y: number };
setStartMouseOffset: (offset: { x: number; y: number }) => void;
setLoading: (loading: boolean) => void;
setImageCursor: (canDrag: boolean) => void
}
export interface DragDirection {
canDragVertical: boolean;
canDragHorizontal: boolean
}
export interface ExtremeBounds {
left: number;
top: number
}
export interface ImageOffset {
x: number;
y: number
}
const DefaultDOMRect = {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
toJSON: () => ({})
};
export default class PreviewImageFoundation , S = Record ) {
super({ ...adapter });
}
_isImageVertical = (): boolean => this.getProp("rotation") % 180 !== 0;
_getImageBounds = (): DOMRect => {
const imageDOM = this._adapter.getImage();
if (imageDOM) {
return imageDOM.getBoundingClientRect();
}
return DefaultDOMRect;
};
_getContainerBounds = (): DOMRect => {
const containerDOM = this._adapter.getContainer();
if (containerDOM) {
return containerDOM.getBoundingClientRect();
}
return DefaultDOMRect;
}
_getOffset = (e: any): ImageOffset => {
const { left, top } = this._getImageBounds();
return {
x: e.clientX - left,
y: e.clientY - top,
};
}
setLoading = (loading: boolean) => {
this._adapter.setLoading(loading);
}
handleWindowResize = (): void => {
const { ratio, setRatio } = this.getProps();
const { originImageWidth, originImageHeight } = this._adapter.getOriginImageSize();
if (originImageWidth && originImageHeight) {
if (ratio !== "adaptation") {
setRatio("adaptation");
} else {
this.handleResizeImage();
}
}
};
handleLoad = (e: any): void => {
if (e.target) {
const { width: w, height: h } = e.target as any;
this._adapter.setOriginImageSize({ originImageWidth: w, originImageHeight: h });
this.setState({
loading: false,
} as any);
this.handleResizeImage();
}
const { src, onLoad } = this.getProps();
onLoad && onLoad(src);
}
handleError = (e: any): void => {
const { onError, src } = this.getProps();
this.setState({
loading: false,
} as any);
onError && onError(src);
}
handleResizeImage = () => {
const horizontal = !this._isImageVertical();
const { originImageWidth, originImageHeight } = this._adapter.getOriginImageSize();
const imgWidth = horizontal ? originImageWidth : originImageHeight;
const imgHeight = horizontal ? originImageHeight : originImageWidth;
const { onZoom } = this.getProps();
const containerDOM = this._adapter.getContainer();
if (containerDOM) {
const { width: containerWidth, height: containerHeight } = this._getContainerBounds();
const reservedWidth = containerWidth - 80;
const reservedHeight = containerHeight - 80;
const _zoom = Number(
Math.min(reservedWidth / imgWidth, reservedHeight / imgHeight).toFixed(2)
);
onZoom(_zoom);
}
}
handleRightClickImage = (e: any) => {
const { disableDownload } = this.getProps();
if (disableDownload) {
e.preventDefault();
e.stopPropagation();
return false;
} else {
return true;
}
};
// e: WheelEvent