import {designComponent, getComponentCls, iHTMLDivElement, nextIndex, onBeforeUnmount, onMounted, PropType, reactive, useClasses, useRefs, useStyles} from "@peryl/react-compose"; import {getCenterRect, iImagePreviewerOption, ImagePreviewerHooks} from "../ImagePreviewer/image-previewer.utils"; import {ImagePreviewer} from "../ImagePreviewer"; import {getRectAutoFormat} from "@peryl/utils/getRectAutoFormat"; import {unit} from "@peryl/utils/unit"; import {createEffects} from "@peryl/utils/createEffects"; import {addElementListener} from "@peryl/utils/addElementListener"; import {delay} from "@peryl/utils/delay"; import './image-previewer-fixed-container.scss'; export const ImagePreviewerFixedContainer = designComponent({ name: 'image-previewer-fixed-container', props: { option: { type: Object as PropType, required: true } }, setup({ props }) { const { refs, onRef } = useRefs({ el: iHTMLDivElement }); const state = reactive({ ready: false, }); const classes = useClasses(() => [ getComponentCls('image-previewer-fixed-container') ]); const styles = useStyles(style => {style.zIndex = nextIndex();}); const { effects: setupEffects } = createEffects(); onMounted(async () => { const containerWidth = Math.floor(refs.el!.offsetWidth); const containerHeight = Math.floor(refs.el!.offsetHeight - props.option.galleryHeight - props.option.separatorHeight); const containerRatio = Number((containerWidth / containerHeight).toFixed(1)); const imageElement = props.option.imageElement; if (!imageElement) { state.ready = true; return; } /** * 如果原始imageElement存在,则创建一个影子图片元素展示过度动画 * @author 韦胜健 * @date 2024/1/5 11:22 */ const shadowImageElement = imageElement.cloneNode(true) as HTMLImageElement; const imageElementRect = getRectAutoFormat(imageElement); props.option.oldSourceImageOpacity = imageElement.style.opacity; imageElement.style.opacity = "0"; shadowImageElement.style.position = 'fixed'; shadowImageElement.style.top = unit(imageElementRect.top)!; shadowImageElement.style.left = unit(imageElementRect.left)!; shadowImageElement.style.width = unit(imageElementRect.width)!; shadowImageElement.style.height = unit(imageElementRect.height)!; shadowImageElement.style.transition = 'all ease 300ms'; shadowImageElement.style.zIndex = '999999'; shadowImageElement.style.objectFit = window.getComputedStyle(imageElement).objectFit /*开启动画时阻止页面滚动*/ setupEffects.push( addElementListener(shadowImageElement, 'wheel', e => { e.preventDefault(); e.stopPropagation(); }) ); const newRect = getCenterRect({ containerElement: { containerHeight, containerWidth, containerRatio }, imageElement }); document.body.appendChild(shadowImageElement); setupEffects.push(() => {document.body.removeChild(shadowImageElement);}); await delay(); shadowImageElement.style.top = unit(newRect.top.toFixed(0))!; shadowImageElement.style.left = unit(newRect.left.toFixed(0))!; shadowImageElement.style.height = unit(newRect.height.toFixed(0))!; shadowImageElement.style.width = unit(newRect.width.toFixed(0))!; const clearListener = addElementListener(shadowImageElement, 'transitionend', () => { state.ready = true; const clearHook = ImagePreviewerHooks.onImagePreviewerReady.use(async (data) => { if (data.option.id !== props.option.id) { return; } await delay(); clearHook(); setupEffects.clear(); }); setupEffects.push(clearHook); }); setupEffects.push(clearListener); }); onBeforeUnmount(setupEffects.clear); return () => (
{state.ready && ( )}
); }, });