Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Dec 20, 2021
1 parent 6390bae commit da3da6d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 79 deletions.
20 changes: 5 additions & 15 deletions packages/botonic-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions packages/botonic-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@
"lodash.merge": "^4.6.2",
"markdown-it": "^12.0.6",
"photoswipe": "^4.1.3",
"process": "^0.11.10",
"qrcode.react": "^1.0.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-frame-component": "^4.1.3",
"react-json-tree": "^0.15.0",
"react-photoswipe": "^1.3.0",
"react-photoswipe-gallery": "^1.3.6",
"react-reveal": "^1.2.2",
"react-router-dom": "^5.2.1",
"react-textarea-autosize": "^7.1.2",
"react-use-storage": "^0.5.1",
"react-zoom-pan-pinch": "^2.1.3",
"reconnecting-websocket": "^4.4.0",
"simplebar-react": "^2.3.3",
"styled-components": "^5.3.0",
Expand All @@ -67,7 +64,6 @@
"@babel/preset-react": "^7.13.13",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.0",
"@types/photoswipe": "^4.1.2",
"@types/react": "17.0.27",
"@types/react-photoswipe": "^1.3.1",
"babel-plugin-add-module-exports": "^1.0.2",
Expand Down
67 changes: 67 additions & 0 deletions packages/botonic-react/src/components/image-zoomable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'react-photoswipe/lib/photoswipe.css'

import React, { useEffect, useState } from 'react'
import { PhotoSwipe } from 'react-photoswipe'

import { Portal } from '../webchat/components/portal-modal'

export const ImageZoomable = props => {
const [imageDimensions, setImageDimensions] = useState({})
useEffect(() => {
// Remove fullscreen and share buttons
if (props.isImageClicked) {
const fullScreenButton = document.getElementsByClassName(
'pswp__button pswp__button--fs'
)[0]
fullScreenButton.remove()
const shareButton = document.getElementsByClassName(
'pswp__button pswp__button--share'
)[0]
shareButton.remove()
}
}, [props.isImageClicked])

useEffect(() => {
if (props.src !== undefined) {
;(async () => {
const dimensions = await getMeta(props.src)
if (dimensions.width <= 1200 || dimensions.height <= 1200) {
dimensions.width = 2 * dimensions.width
dimensions.height = 2 * dimensions.height
}
setImageDimensions(dimensions)
})()
}
}, [props.src])

if (!props.isImageClicked) return null
return (
<Portal>
<PhotoSwipe
isOpen={true}
items={[
{
src: props.src,
w: imageDimensions.width,
h: imageDimensions.height,
},
]}
options={{ bgOpacity: 0.7 }}
onClose={props.onClose}
/>
</Portal>
)
}

function getMeta(url) {
try {
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => resolve({ height: img.height, width: img.width })
img.onerror = reject
img.src = url
})
} catch (e) {
console.error(e)
}
}
59 changes: 11 additions & 48 deletions packages/botonic-react/src/components/image.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'react-photoswipe/lib/photoswipe.css'

import { INPUT, isBrowser } from '@botonic/core'
import React, { useEffect, useState } from 'react'
import { PhotoSwipe } from 'react-photoswipe'
import React, { useState } from 'react'
import styled from 'styled-components'

import { ROLES } from '../constants'
import { Portal } from '../webchat/components/portal-modal'
import { ImageZoomable } from './image-zoomable'
import { Message } from './message'
import { getMeta } from './size-checker'

const StyledImage = styled.img`
border-radius: 8px;
Expand All @@ -24,54 +22,19 @@ const serialize = imageProps => {

export const Image = props => {
let content = props.children

const [isGalleryOpened, setIsGalleryOpened] = useState(false)
const [imageDimensions, setImageDimensions] = useState({})
useEffect(() => {
// Remove fullscreen and share buttons
if (isGalleryOpened) {
const fullScreenButton = document.getElementsByClassName(
'pswp__button pswp__button--fs'
)[0]
fullScreenButton.remove()
const shareButton = document.getElementsByClassName(
'pswp__button pswp__button--share'
)[0]
shareButton.remove()
}
}, [isGalleryOpened])

useEffect(async () => {
if (props.src !== undefined) {
let dimensions = await getMeta(props.src)
if (dimensions.width <= 1200 || dimensions.height <= 1200) {
dimensions.width = 2 * dimensions.width
dimensions.height = 2 * dimensions.height
}
setImageDimensions(dimensions)
}
}, [props.src])
const [isImageClicked, setIsImageClicked] = useState(false)
// const onClickHandler = () => setIsImageClicked(true)
// const onCloseHandler = () => setIsImageClicked(false)

if (isBrowser()) {
content = (
<>
<StyledImage src={props.src} onClick={() => setIsGalleryOpened(true)} />
{isGalleryOpened && (
<Portal>
<PhotoSwipe
isOpen={true}
items={[
{
src: props.src,
w: imageDimensions.width,
h: imageDimensions.height,
},
]}
options={{ bgOpacity: 0.7 }}
onClose={() => setIsGalleryOpened(false)}
/>
</Portal>
)}
<StyledImage src={props.src} onClick={() => setIsImageClicked(true)} />
<ImageZoomable
src={props.src}
isImageClicked={isImageClicked}
onClose={() => setIsImageClicked(false)}
/>
</>
)
}
Expand Down
12 changes: 0 additions & 12 deletions packages/botonic-react/src/components/size-checker.jsx

This file was deleted.

0 comments on commit da3da6d

Please sign in to comment.