diff --git a/package.json b/package.json index abff71b0..ca720cff 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "mobx-persist-store": "^1.0.4", "mobx-react-lite": "^3.2.3", "next": "^12.3.0", - "photoswipe": "^4.1.3", + "photoswipe": "^5.4.4", "raleway-cyrillic": "^4.0.2", "ramda": "^0.26.1", "react": "^17.0.2", diff --git a/src/containers/dialogs/PhotoSwipe/index.tsx b/src/containers/dialogs/PhotoSwipe/index.tsx index 3a285696..4c3c6fdf 100644 --- a/src/containers/dialogs/PhotoSwipe/index.tsx +++ b/src/containers/dialogs/PhotoSwipe/index.tsx @@ -1,9 +1,9 @@ -import { useEffect, useRef, VFC } from 'react'; +import { useEffect } from 'react'; -import classNames from 'classnames'; import { observer } from 'mobx-react-lite'; -import PhotoSwipeUI_Default from 'photoswipe/dist/photoswipe-ui-default.js'; -import PhotoSwipeJs from 'photoswipe/dist/photoswipe.js'; +import { SlideData } from 'photoswipe/dist/types/slide/slide'; + +import 'photoswipe/style.css'; import { imagePresets } from '~/constants/urls'; import { useWindowSize } from '~/hooks/dom/useWindowSize'; @@ -14,124 +14,76 @@ import { getURL } from '~/utils/dom'; import styles from './styles.module.scss'; -export interface PhotoSwipeProps extends DialogComponentProps { +export interface Props extends DialogComponentProps { items: IFile[]; index: number; } -const PhotoSwipe: VFC = observer(({ index, items }) => { - let ref = useRef(null); +const padding = { top: 10, left: 10, right: 10, bottom: 10 } as const; + +const PhotoSwipe = observer(({ index, items }: Props) => { const { hideModal } = useModal(); const { isTablet } = useWindowSize(); useEffect(() => { - new Promise(async (resolve) => { - const images = await Promise.all( - items.map( - (file) => - new Promise((resolve) => { - const src = getURL( - file, - isTablet ? imagePresets[900] : imagePresets[1600], - ); + Promise.all( + items.map( + (file): Promise => + new Promise((resolve) => { + const src = getURL( + file, + isTablet ? imagePresets[900] : imagePresets[1600], + ); - if (file.metadata?.width && file.metadata.height) { - resolve({ - src, - w: file.metadata.width, - h: file.metadata.height, - }); + if (file.metadata?.width && file.metadata.height) { + resolve({ + src, + width: file.metadata.width, + height: file.metadata.height, + }); - return; - } + return; + } - const img = new Image(); + const img = new Image(); - img.onload = () => { - resolve({ - src, - h: img.naturalHeight, - w: img.naturalWidth, - }); - }; + img.onload = () => { + resolve({ + src, + height: img.naturalHeight, + width: img.naturalWidth, + }); + }; - img.onerror = () => { - resolve({}); - }; + img.onerror = () => { + resolve({}); + }; - img.src = getURL(file, imagePresets[1600]); - }), - ), - ); + img.src = getURL(file, imagePresets[1600]); + }), + ), + ).then(async (images: SlideData[]) => { + const PSWP = await import('photoswipe').then((it) => it.default); - resolve(images); - }).then((images) => { - const ps = new PhotoSwipeJs(ref.current, PhotoSwipeUI_Default, images, { + const ps = new PSWP({ + dataSource: images, index: index || 0, - closeOnScroll: false, - history: false, + closeOnVerticalDrag: true, + padding, + mainClass: styles.wrap, + zoom: false, + counter: false, + bgOpacity: 0.1, }); + ps.on('destroy', hideModal); + ps.on('close', hideModal); + ps.init(); - ps.listen('destroy', hideModal); - ps.listen('close', hideModal); }); }, [hideModal, items, index, isTablet]); - return ( -