mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
enable pitch zoom
This commit is contained in:
parent
73225b166f
commit
2d7999d9dc
3 changed files with 153 additions and 30 deletions
|
@ -1,4 +1,10 @@
|
|||
import React, { CSSProperties, FC, useMemo, useReducer } from 'react';
|
||||
import React, {
|
||||
CSSProperties,
|
||||
ReactNode,
|
||||
forwardRef,
|
||||
useMemo,
|
||||
useReducer,
|
||||
} from 'react';
|
||||
|
||||
import classNames from 'classnames';
|
||||
|
||||
|
@ -8,17 +14,14 @@ import { DivProps } from '~/utils/types';
|
|||
import styles from './styles.module.scss';
|
||||
|
||||
interface ImageLoadingWrapperProps extends Omit<DivProps, 'children'> {
|
||||
children: (props: { loading: boolean; onLoad: () => void }) => void;
|
||||
children: (props: { loading: boolean; onLoad: () => void }) => ReactNode;
|
||||
preview?: string;
|
||||
}
|
||||
|
||||
const ImageLoadingWrapper: FC<ImageLoadingWrapperProps> = ({
|
||||
className,
|
||||
children,
|
||||
preview,
|
||||
color,
|
||||
...props
|
||||
}) => {
|
||||
const ImageLoadingWrapper = forwardRef<
|
||||
HTMLDivElement,
|
||||
ImageLoadingWrapperProps
|
||||
>(({ className, children, preview, color, ...props }, ref) => {
|
||||
const [loading, onLoad] = useReducer(() => false, true);
|
||||
|
||||
const style = useMemo<CSSProperties>(
|
||||
|
@ -30,7 +33,7 @@ const ImageLoadingWrapper: FC<ImageLoadingWrapperProps> = ({
|
|||
);
|
||||
|
||||
return (
|
||||
<div className={classNames(styles.wrapper, className)} {...props}>
|
||||
<div className={classNames(styles.wrapper, className)} {...props} ref={ref}>
|
||||
{!!loading && !!preview && (
|
||||
<div className={styles.preview}>
|
||||
<div className={styles.thumbnail} style={style} />
|
||||
|
@ -40,6 +43,6 @@ const ImageLoadingWrapper: FC<ImageLoadingWrapperProps> = ({
|
|||
{children({ loading, onLoad })}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
});
|
||||
|
||||
export { ImageLoadingWrapper };
|
||||
|
|
108
src/components/media/PinchZoom/index.tsx
Normal file
108
src/components/media/PinchZoom/index.tsx
Normal file
|
@ -0,0 +1,108 @@
|
|||
import {
|
||||
FC,
|
||||
ReactElement,
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
|
||||
interface Props {
|
||||
children: (props: {
|
||||
setRef: (ref: HTMLElement | null) => void;
|
||||
}) => ReactElement;
|
||||
}
|
||||
|
||||
const getDistance = (event: TouchEvent) => {
|
||||
return Math.hypot(
|
||||
event.touches[0].pageX - event.touches[1].pageX,
|
||||
event.touches[0].pageY - event.touches[1].pageY,
|
||||
);
|
||||
};
|
||||
|
||||
interface Start {
|
||||
x: number;
|
||||
y: number;
|
||||
distance: number;
|
||||
}
|
||||
|
||||
const PinchZoom: FC<Props> = ({ children }) => {
|
||||
const start = useRef<Start>({ x: 0, y: 0, distance: 0 });
|
||||
const [ref, setRef] = useState<HTMLElement | null>(null);
|
||||
const imageElementScale = useRef(1);
|
||||
|
||||
const onTouchStart = useCallback((event: TouchEvent) => {
|
||||
if (event.touches.length !== 2) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault(); // Prevent page scroll
|
||||
|
||||
// Calculate where the fingers have started on the X and Y axis
|
||||
start.current.x = (event.touches[0].pageX + event.touches[1].pageX) / 2;
|
||||
start.current.y = (event.touches[0].pageY + event.touches[1].pageY) / 2;
|
||||
start.current.distance = getDistance(event);
|
||||
}, []);
|
||||
|
||||
const onTouchMove = useCallback(
|
||||
(event) => {
|
||||
if (event.touches.length !== 2 || !ref) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault(); // Prevent page scroll
|
||||
|
||||
// Safari provides event.scale as two fingers move on the screen
|
||||
// For other browsers just calculate the scale manually
|
||||
const scale = event.scale ?? getDistance(event) / start.current.distance;
|
||||
imageElementScale.current = Math.min(Math.max(1, scale), 4);
|
||||
|
||||
// Calculate how much the fingers have moved on the X and Y axis
|
||||
const deltaX =
|
||||
((event.touches[0].pageX + event.touches[1].pageX) / 2 -
|
||||
start.current.x) *
|
||||
2; // x2 for accelarated movement
|
||||
const deltaY =
|
||||
((event.touches[0].pageY + event.touches[1].pageY) / 2 -
|
||||
start.current.y) *
|
||||
2; // x2 for accelarated movement
|
||||
|
||||
// Transform the image to make it grow and move with fingers
|
||||
const transform = `translate3d(${deltaX}px, ${deltaY}px, 0) scale(${imageElementScale})`;
|
||||
ref.style.transform = transform;
|
||||
ref.style.zIndex = '9999';
|
||||
},
|
||||
[ref],
|
||||
);
|
||||
|
||||
const onTouchEnd = useCallback(
|
||||
(event) => {
|
||||
if (!ref) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset image to it's original format
|
||||
ref.style.transform = '';
|
||||
ref.style.zIndex = '';
|
||||
},
|
||||
[ref],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ref) {
|
||||
return;
|
||||
}
|
||||
|
||||
ref.addEventListener('touchstart', onTouchStart);
|
||||
ref.addEventListener('touchmove', onTouchMove);
|
||||
ref.addEventListener('touchend', onTouchEnd);
|
||||
|
||||
return () => {
|
||||
ref.removeEventListener('touchstart', onTouchStart);
|
||||
ref.removeEventListener('touchmove', onTouchMove);
|
||||
ref.removeEventListener('touchend', onTouchEnd);
|
||||
};
|
||||
}, [onTouchEnd, onTouchMove, onTouchStart, ref]);
|
||||
|
||||
return children({ setRef });
|
||||
};
|
||||
|
||||
export { PinchZoom };
|
|
@ -7,6 +7,7 @@ import { Swiper, SwiperSlide } from 'swiper/react';
|
|||
import SwiperClass from 'swiper/types/swiper-class';
|
||||
|
||||
import { ImageLoadingWrapper } from '~/components/common/ImageLoadingWrapper/index';
|
||||
import { PinchZoom } from '~/components/media/PinchZoom';
|
||||
import { NodeComponentProps } from '~/constants/node';
|
||||
import { imagePresets } from '~/constants/urls';
|
||||
import { useWindowSize } from '~/hooks/dom/useWindowSize';
|
||||
|
@ -100,26 +101,37 @@ const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => {
|
|||
>
|
||||
{images.map((file, index) => (
|
||||
<SwiperSlide className={styles.slide} key={file.id}>
|
||||
<ImageLoadingWrapper
|
||||
preview={getURL(file, imagePresets['300'])}
|
||||
color={file.metadata?.dominant_color}
|
||||
>
|
||||
{({ loading, onLoad }) => (
|
||||
<NodeImageLazy
|
||||
src={getURL(file)}
|
||||
width={file.metadata?.width}
|
||||
height={file.metadata?.height}
|
||||
color={normalizeBrightColor(file?.metadata?.dominant_color)}
|
||||
onLoad={onLoad}
|
||||
onClick={() => onOpenPhotoSwipe(index)}
|
||||
className={classNames(styles.image, 'swiper-lazy', {
|
||||
[styles.loading]: loading,
|
||||
})}
|
||||
sizes={getNodeSwiperImageSizes(file, innerWidth, innerHeight)}
|
||||
quality={90}
|
||||
/>
|
||||
<PinchZoom>
|
||||
{({ setRef }) => (
|
||||
<ImageLoadingWrapper
|
||||
preview={getURL(file, imagePresets['300'])}
|
||||
color={file.metadata?.dominant_color}
|
||||
ref={setRef}
|
||||
>
|
||||
{({ loading, onLoad }) => (
|
||||
<NodeImageLazy
|
||||
src={getURL(file)}
|
||||
width={file.metadata?.width}
|
||||
height={file.metadata?.height}
|
||||
color={normalizeBrightColor(
|
||||
file?.metadata?.dominant_color,
|
||||
)}
|
||||
onLoad={onLoad}
|
||||
onClick={() => onOpenPhotoSwipe(index)}
|
||||
className={classNames(styles.image, 'swiper-lazy', {
|
||||
[styles.loading]: loading,
|
||||
})}
|
||||
sizes={getNodeSwiperImageSizes(
|
||||
file,
|
||||
innerWidth,
|
||||
innerHeight,
|
||||
)}
|
||||
quality={90}
|
||||
/>
|
||||
)}
|
||||
</ImageLoadingWrapper>
|
||||
)}
|
||||
</ImageLoadingWrapper>
|
||||
</PinchZoom>
|
||||
</SwiperSlide>
|
||||
))}
|
||||
</Swiper>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue