From ab9a0bc21b699860f98bf9301fe68844e37aa5fd Mon Sep 17 00:00:00 2001 From: Abhinav-grd Date: Fri, 15 Jan 2021 16:39:52 +0530 Subject: [PATCH] Added the photoswipe react component locally --- src/components/PhotoSwipe/PhotoSwipe.js | 175 ++++++++++++++++++++++++ src/components/PhotoSwipe/events.js | 19 +++ src/pages/gallery/index.tsx | 2 +- tsconfig.json | 2 +- 4 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/components/PhotoSwipe/PhotoSwipe.js create mode 100644 src/components/PhotoSwipe/events.js diff --git a/src/components/PhotoSwipe/PhotoSwipe.js b/src/components/PhotoSwipe/PhotoSwipe.js new file mode 100644 index 000000000..3a1cd5b9c --- /dev/null +++ b/src/components/PhotoSwipe/PhotoSwipe.js @@ -0,0 +1,175 @@ +import React, { useState } from 'react'; +import PropTypes from 'prop-types'; +import Photoswipe from 'photoswipe'; +import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default'; +import classnames from 'classnames'; +import events from './events'; +import FavButton from 'components/FavButton'; +import { addToFavorites } from 'services/collectionService'; + +class PhotoSwipe extends React.Component { + static propTypes = { + isOpen: PropTypes.bool.isRequired, + items: PropTypes.array.isRequired, + options: PropTypes.object, + onClose: PropTypes.func, + id: PropTypes.string, + className: PropTypes.string + }; + + static defaultProps = { + options: {}, + onClose: () => { + }, + id: '', + className: '' + }; + + state = { + isOpen: this.props.isOpen, + isClick: false, + }; + + componentDidMount = () => { + const { isOpen } = this.state; + if (isOpen) { + this.openPhotoSwipe(this.props); + } + }; + + componentWillReceiveProps = (nextProps) => { + const { isOpen } = this.state; + if (nextProps.isOpen) { + if (!isOpen) { + this.openPhotoSwipe(nextProps); + } else { + this.updateItems(nextProps.items); + } + } else if (isOpen) { + this.closePhotoSwipe(); + } + }; + + componentWillUnmount = () => { + this.closePhotoSwipe(); + }; + + openPhotoSwipe = (props) => { + const { items, options } = props; + const pswpElement = this.pswpElement; + this.photoSwipe = new Photoswipe(pswpElement, PhotoswipeUIDefault, items, options); + events.forEach((event) => { + const callback = props[event]; + if (callback || event === 'destroy') { + const self = this; + this.photoSwipe.listen(event, function (...args) { + if (callback) { + args.unshift(this); + callback(...args); + } + if (event === 'destroy') { + self.handleClose(); + } + }); + } + }); + this.setState({ + isOpen: true, + }, () => { + this.photoSwipe.init(); + }); + }; + + updateItems = (items = []) => { + this.photoSwipe.items.length = 0; + items.forEach((item) => { + this.photoSwipe.items.push(item); + }); + this.photoSwipe.invalidateCurrItems(); + this.photoSwipe.updateSize(true); + }; + + closePhotoSwipe = () => { + if (!this.photoSwipe) { + return; + } + this.photoSwipe.close(); + }; + + handleClose = () => { + const { onClose } = this.props; + this.setState({ + isOpen: false + }, () => { + if (onClose) { + onClose(); + } + }); + }; + + render() { + const { id } = this.props; + let { className } = this.props; + className = classnames(['pswp', className]).trim(); + return ( +