import {PartialFields} from "../../utils/type"; import {anonymousEffects, createHooks} from "@peryl/react-compose"; import {createCounter} from '@peryl/utils/createCounter'; import {notNull} from "@peryl/utils/notNull"; export const ImagePreviewerConstants = { /*画廊高度,默认70px*/ DEFAULT_GALLERY_HEIGHT: 70, /*画廊与大图之间的距离,默认24px*/ DEFAULT_SEPARATOR_HEIGHT: 0, }; export interface iImagePreviewerOptionPartialFields { imageElement?: null | HTMLImageElement, // 点击的图片原始dom对象,如果有传这个属性对象的话会有一个显示放大动画 galleryHeight: number, // 底部画廊的高度 separatorHeight: number, // 底部画廊距离图片显示区域的距离 autoHeight: boolean, // 自动高度,默认是true。这样设置之后图片会撑满宽度,高度自动;关闭的时候会设置高度100%,图片宽度自动 mask: boolean, // 显示背部遮罩 button?: boolean | { // 顶部按钮栏控制,button为false则禁用所有按钮;为对象则单独禁用某个按钮 zoomIn?: boolean, // 放大按钮 zoomOut?: boolean, // 缩小按钮 clockwise?: boolean, // 顺时针旋转按钮 anticlockwise?: boolean, // 逆时针旋转按钮 reset?: boolean, // 重置按钮 close?: boolean, // 关闭按钮 }, swipeButton?: boolean // 左右切换按钮控制,swipeButton为false则禁用前后按钮 gallery?: boolean, // 底部画廊控制,为false则禁用底部画廊 globalKeyboardEvent?: boolean // 监听全局的键盘事件,默认不监听全局键盘按键事件,而是等鼠标移入的时候才监听 maskColor?: string, // 遮罩颜色 disableZoomOnWheel?: boolean, // 禁用滚轮缩放 /*---------------------------------------以下为不可配置属性-------------------------------------------*/ id: string, // option唯一标识 oldSourceImageOpacity?: string // 原始的img对象的opacity属性值 } export interface iImagePreviewerOption extends iImagePreviewerOptionPartialFields { current: number; // 当前显示的元素索引 urls: { url: string, thumbUrl: string }[]; // 所有展示的图片,url为大图地址,thumbUrl为底部画廊图片预览地址 } export type iImagePreviewerParamOption = string | string[] | iImagePreviewerOption['urls'] | Omit, 'urls'> & { urls: string[] | iImagePreviewerOption['urls'] } export const ImagePreviewerHooks = { /*PreviewerComponent挂载完毕事件*/ onImagePreviewerReady: createHooks<(data: { option: iImagePreviewerOption }) => void>(anonymousEffects) }; const nextOptionId = createCounter('image_previewer'); export function createImagePreviewerOption( param: iImagePreviewerParamOption, imageElement?: HTMLImageElement, defaultParam?: Partial ): iImagePreviewerOption { if (typeof param === "string") { return { id: nextOptionId(), current: 0, imageElement, autoHeight: true, mask: false, urls: [{ url: param, thumbUrl: param }], galleryHeight: ImagePreviewerConstants.DEFAULT_GALLERY_HEIGHT, separatorHeight: ImagePreviewerConstants.DEFAULT_SEPARATOR_HEIGHT, ...defaultParam, }; } if (Array.isArray(param)) { return { id: nextOptionId(), current: 0, imageElement, autoHeight: true, mask: false, urls: param.map(p => typeof p === "string" ? { url: p, thumbUrl: p } : p), galleryHeight: ImagePreviewerConstants.DEFAULT_GALLERY_HEIGHT, separatorHeight: ImagePreviewerConstants.DEFAULT_SEPARATOR_HEIGHT, ...defaultParam, }; } if (!!imageElement) { param.imageElement = imageElement; } return { autoHeight: true, mask: false, galleryHeight: notNull(param.galleryHeight, ImagePreviewerConstants.DEFAULT_GALLERY_HEIGHT), separatorHeight: notNull(param.separatorHeight, ImagePreviewerConstants.DEFAULT_SEPARATOR_HEIGHT), ...defaultParam, ...param, id: nextOptionId(), urls: param.urls.map(p => typeof p === "string" ? { url: p, thumbUrl: p } : p), }; } /** * 获取imageElement在containerElement容器中居中铺满的位置信息 * @author 韦胜健 * @date 2024/1/10 18:50 */ export const getCenterRect = ( { imageElement, containerElement }: { imageElement: HTMLDivElement, containerElement: HTMLDivElement | { containerRatio: number, containerHeight: number, containerWidth: number }, } ) => { const imageRatio = Number((imageElement.offsetWidth / imageElement.offsetHeight).toFixed(2)); const { containerRatio, containerHeight, containerWidth } = 'tagName' in containerElement ? (() => { const containerRatio = Number((containerElement.offsetWidth / containerElement.offsetHeight).toFixed(2)); return { containerRatio, containerHeight: containerElement.offsetHeight, containerWidth: containerElement.offsetWidth, }; })() : containerElement; if (imageRatio > containerRatio) { /*宽度更宽,固定宽度为屏幕宽度缩放*/ const left = 0; const width = containerWidth; const height = Number((imageElement.offsetHeight / imageElement.offsetWidth * width).toFixed(0)); const top = Number(((containerHeight - height) / 2).toFixed(0)); return { top, left, width, height }; } else { /*高度更高,固定高度将宽度缩放*/ const top = 0; const height = containerHeight; const width = Number((imageElement.offsetWidth / imageElement.offsetHeight * height).toFixed(0)); const left = Number(((containerWidth - width) / 2).toFixed(0)); return { top, left, width, height }; } };