import {computed, designComponent, iHTMLDivElement, iMouseEvent, onBeforeUnmount, PropType, reactive, useClasses, useModel, useRefs, useStyles} from "@peryl/react-compose"; import {notNull} from "@peryl/utils/notNull"; import {delay} from "@peryl/utils/delay"; import {Icon} from "../Icon"; import {iImagePreviewerOption} from "./image-previewer.utils"; import {PreviewerLoading} from "./PreviewerLoading"; import {VirtualList} from "../VirtualList"; import {useDragHorizontalScroll} from "../../uses/useDragHorizontalScroll"; /** * 画廊组件 * @author 韦胜健 * @date 2024/1/8 10:13 */ export const ImagePreviewerGallery = designComponent({ name: 'image-gallery', props: { modelValue: { type: Number }, option: { type: Object as PropType, required: true } }, inheritPropsType: iHTMLDivElement, emits: { onUpdateModelValue: (val?: number) => true, }, setup({ props, event: { emit } }) { const { refs, onRef } = useRefs({ virtual: VirtualList }); const model = useModel(() => notNull(props.modelValue, 0), emit.onUpdateModelValue, { onChange: () => {handler.adjustScrollLeft();} }); const state = reactive({ /*是否显示滚动按钮*/ showScroller: false, readyMap: {} as Record, }); const classes = useClasses(() => [ 'image-previewer-component-gallery', { 'image-previewer-component-gallery-show-scroller': state.showScroller } ]); const utils = { getScrollInfo: () => { const { offsetWidth, scrollWidth, scrollLeft } = refs.virtual!.refs.scroll!.refs.wrapper!; return { wrapperWidth: offsetWidth, scrollWidth, scrollLeft }; }, scrollLeft: (left: number, duration: number) => { refs.virtual!.refs.scroll?.methods.scrollLeft(left, duration); }, }; const handler = { /** * 根据选中的元素位置自动调整滚动距离,使得选中的元素居中展示 * @author 韦胜健 * @date 2024/1/5 17:53 */ adjustScrollLeft: async (duration = 300) => { await delay(); const { wrapperWidth } = utils.getScrollInfo(); let scrollLeft = itemRect.value.width * model.value - Math.ceil(itemRect.value.width / 2) - Math.ceil(wrapperWidth / 2); if (scrollLeft < 0) {scrollLeft = 0;} utils.scrollLeft(scrollLeft, duration); }, /** * 图片加载完毕时,自动判断是否需要显示滚动按钮 * @author 韦胜健 * @date 2024/1/5 17:53 */ onImageLoad: async (thumbUrl: string) => { state.readyMap = { ...state.readyMap, [thumbUrl]: true }; await delay(); const { wrapperWidth, scrollWidth } = utils.getScrollInfo(); const showScroller = scrollWidth > wrapperWidth; if (state.showScroller != showScroller) { state.showScroller = showScroller; await handler.adjustScrollLeft(0); } }, /** * 点击gallery item的时候更新绑定值,并且调整滚动位置 * @author 韦胜健 * @date 2024/1/5 17:54 */ onClickGalleryItem: async (index: number) => { model.value = index; }, /** * 滚动到上一页 * @author 韦胜健 * @date 2024/1/5 17:54 */ scrollPrev: (e: iMouseEvent) => { e.stopPropagation(); const { wrapperWidth, scrollLeft } = utils.getScrollInfo(); utils.scrollLeft(Math.ceil(scrollLeft - wrapperWidth / 2), 300); }, /** * 滚动到下一页 * @author 韦胜健 * @date 2024/1/5 17:54 */ scrollNext: (e: iMouseEvent) => { e.stopPropagation(); const { wrapperWidth, scrollLeft } = utils.getScrollInfo(); utils.scrollLeft(Math.ceil(scrollLeft + wrapperWidth / 2), 300); }, /** * 双击滚动按钮的时候阻止事件冒泡,避免影响导致关闭预览 * @author 韦胜健 * @date 2024/1/5 17:54 */ onDoubleClickGallery: (e: iMouseEvent) => { e.stopPropagation(); }, /** * 拖拽图片的时候阻止默认行为,避免下载图片 * @author 韦胜健 * @date 2024/1/5 17:55 */ preventDragImage: (e: iMouseEvent) => { e.preventDefault(); }, ...(() => { let clickIndex: number | null = null; let clickClientX: number | null = null; const onMousedownItem = (e: iMouseEvent, index: number) => { clickIndex = index; clickClientX = e.clientX; }; const onMouseupItem = (e: iMouseEvent, index: number) => { if (index !== clickIndex || clickIndex == null || clickClientX == null) {return;} if (Math.abs(e.clientX - clickClientX) > 10) { /*距离过长,为拖拽而不是点击*/ return; } handler.onClickGalleryItem(index); }; return { onMousedownItem, onMouseupItem }; })() }; const itemRect = computed(() => { const height = props.option.galleryHeight; const width = Math.ceil(1920 / 1080 * height); return { height, width }; }); const itemStyles = useStyles(style => { style.height = itemRect.value.height + 'px'; style.width = itemRect.value.width + 'px'; }); onBeforeUnmount(useDragHorizontalScroll(() => refs.virtual?.refs.scroll?.refs.wrapper)); return () => (
{state.showScroller && (
)}
(
handler.onMousedownItem(e, index)} onMouseUp={(e) => handler.onMouseupItem(e, index)} data-active={String(model.value == index)} > handler.onImageLoad(item.thumbUrl)} onError={() => handler.onImageLoad(item.thumbUrl)}/> {/*{}*/} {state.readyMap[item.thumbUrl] !== true && }
), }} />
); }, });