import {designComponent, iHTMLImageElement, iMouseEvent, PropType, reactive, useRefs, useStyles} from "@peryl/react-compose"; import {delay} from "@peryl/utils/delay"; /** * img自动判断自己是应该宽度沾满还是高度沾满 * @author 韦胜健 * @date 2024/1/5 15:47 */ export const ImagePreviewerCarouselImage = designComponent({ props: { src: { type: String, required: true }, direction: { type: String as PropType<'horizontal' | 'vertical'> }, clientRatio: { type: Number, required: true }, }, inheritPropsType: iHTMLImageElement, emits: { onReady: () => true, }, setup({ props, event: { emit } }) { const { refs, onRef } = useRefs({ img: iHTMLImageElement }); const state = reactive({ direction: props.direction as null | undefined | 'horizontal' | 'vertical' }); const styles = useStyles(style => { if (!state.direction) {style.opacity = "0";} }); const onLoad = async () => { if (!state.direction) { const { height, width } = refs.img!; const imageRatio = width / height; state.direction = imageRatio > props.clientRatio ? 'horizontal' : 'vertical'; await delay(); } emit.onReady(); }; const onError = async () => {emit.onReady();}; /** * 阻止点击冒泡,避免触发image previewer的双击关闭动作 * @author 韦胜健 * @date 2024/1/8 10:05 */ const preventDefaultDraggier = (e: iMouseEvent) => {e.preventDefault();}; return () => ( ); }, });