2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
2018-04-15 04:27:30 +00:00
|
|
|
import classNames from 'classnames';
|
2021-08-24 21:47:14 +00:00
|
|
|
import moment from 'moment';
|
2021-08-23 23:14:53 +00:00
|
|
|
import { createPortal } from 'react-dom';
|
2021-08-24 21:47:14 +00:00
|
|
|
import { noop } from 'lodash';
|
2021-10-12 20:25:09 +00:00
|
|
|
import { useSpring, animated, to } from '@react-spring/web';
|
2018-04-25 22:15:57 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2022-12-14 18:12:04 +00:00
|
|
|
import type {
|
|
|
|
ConversationType,
|
|
|
|
SaveAttachmentActionCreatorType,
|
|
|
|
} from '../state/ducks/conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2022-06-03 16:33:39 +00:00
|
|
|
import type { MediaItemType, MediaItemMessageType } from '../types/MediaItem';
|
2022-12-10 02:02:22 +00:00
|
|
|
import * as GoogleChrome from '../util/GoogleChrome';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { Avatar, AvatarSize } from './Avatar';
|
|
|
|
import { IMAGE_PNG, isImage, isVideo } from '../types/MIME';
|
2021-08-24 21:47:14 +00:00
|
|
|
import { formatDuration } from '../util/formatDuration';
|
2022-12-10 02:02:22 +00:00
|
|
|
import { isGIF } from '../types/Attachment';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useRestoreFocus } from '../hooks/useRestoreFocus';
|
2018-05-22 19:31:43 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
export type PropsType = {
|
2021-08-06 00:17:05 +00:00
|
|
|
children?: ReactNode;
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox: () => unknown;
|
2021-08-23 23:14:53 +00:00
|
|
|
getConversation?: (id: string) => ConversationType;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2021-08-24 21:47:14 +00:00
|
|
|
isViewOnce?: boolean;
|
2023-01-13 20:07:26 +00:00
|
|
|
media: ReadonlyArray<ReadonlyDeep<MediaItemType>>;
|
2022-12-14 18:12:04 +00:00
|
|
|
saveAttachment: SaveAttachmentActionCreatorType;
|
2021-08-23 23:14:53 +00:00
|
|
|
selectedIndex?: number;
|
2022-12-10 02:02:22 +00:00
|
|
|
toggleForwardMessageModal: (messageId: string) => unknown;
|
2023-02-24 23:18:57 +00:00
|
|
|
onMediaPlaybackStart: () => void;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-04-15 03:27:03 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const ZOOM_SCALE = 3;
|
|
|
|
|
|
|
|
const INITIAL_IMAGE_TRANSFORM = {
|
|
|
|
scale: 1,
|
|
|
|
translateX: 0,
|
|
|
|
translateY: 0,
|
2021-10-14 16:52:42 +00:00
|
|
|
config: {
|
|
|
|
clamp: true,
|
|
|
|
friction: 20,
|
|
|
|
mass: 0.5,
|
|
|
|
tension: 350,
|
|
|
|
},
|
2021-10-12 20:25:09 +00:00
|
|
|
};
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
export function Lightbox({
|
|
|
|
children,
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox,
|
2021-08-23 23:14:53 +00:00
|
|
|
getConversation,
|
|
|
|
media,
|
|
|
|
i18n,
|
2021-08-24 21:47:14 +00:00
|
|
|
isViewOnce = false,
|
2022-12-14 18:12:04 +00:00
|
|
|
saveAttachment,
|
2021-08-24 21:47:14 +00:00
|
|
|
selectedIndex: initialSelectedIndex = 0,
|
2022-12-10 02:02:22 +00:00
|
|
|
toggleForwardMessageModal,
|
2023-02-24 23:18:57 +00:00
|
|
|
onMediaPlaybackStart,
|
2021-08-23 23:14:53 +00:00
|
|
|
}: PropsType): JSX.Element | null {
|
|
|
|
const [root, setRoot] = React.useState<HTMLElement | undefined>();
|
2021-11-11 22:43:05 +00:00
|
|
|
const [selectedIndex, setSelectedIndex] =
|
|
|
|
useState<number>(initialSelectedIndex);
|
2018-04-15 03:27:03 +00:00
|
|
|
|
2021-08-24 21:47:14 +00:00
|
|
|
const [videoElement, setVideoElement] = useState<HTMLVideoElement | null>(
|
|
|
|
null
|
|
|
|
);
|
|
|
|
const [videoTime, setVideoTime] = useState<number | undefined>();
|
2021-10-12 20:25:09 +00:00
|
|
|
const [isZoomed, setIsZoomed] = useState(false);
|
2021-08-23 23:14:53 +00:00
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
2021-09-02 19:35:23 +00:00
|
|
|
const [focusRef] = useRestoreFocus();
|
2021-10-12 20:25:09 +00:00
|
|
|
const animateRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
const dragCacheRef = useRef<
|
|
|
|
| {
|
|
|
|
startX: number;
|
|
|
|
startY: number;
|
|
|
|
translateX: number;
|
|
|
|
translateY: number;
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
>();
|
2021-09-28 20:27:35 +00:00
|
|
|
const imageRef = useRef<HTMLImageElement | null>(null);
|
2021-10-12 20:25:09 +00:00
|
|
|
const zoomCacheRef = useRef<
|
2021-10-04 20:12:14 +00:00
|
|
|
| {
|
2021-10-12 20:25:09 +00:00
|
|
|
maxX: number;
|
|
|
|
maxY: number;
|
2021-10-04 20:12:14 +00:00
|
|
|
screenWidth: number;
|
|
|
|
screenHeight: number;
|
|
|
|
}
|
2021-09-28 20:27:35 +00:00
|
|
|
| undefined
|
|
|
|
>();
|
2018-04-26 15:18:24 +00:00
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const onPrevious = useCallback(
|
|
|
|
(
|
|
|
|
event: KeyboardEvent | React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
|
|
) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2021-09-30 21:18:56 +00:00
|
|
|
if (isZoomed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
setSelectedIndex(prevSelectedIndex => Math.max(prevSelectedIndex - 1, 0));
|
|
|
|
},
|
2021-09-30 21:18:56 +00:00
|
|
|
[isZoomed]
|
2021-09-07 16:12:26 +00:00
|
|
|
);
|
2018-04-15 04:27:30 +00:00
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const onNext = useCallback(
|
|
|
|
(
|
|
|
|
event: KeyboardEvent | React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
|
|
) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2021-09-30 21:18:56 +00:00
|
|
|
if (isZoomed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
setSelectedIndex(prevSelectedIndex =>
|
|
|
|
Math.min(prevSelectedIndex + 1, media.length - 1)
|
|
|
|
);
|
|
|
|
},
|
2021-09-30 21:18:56 +00:00
|
|
|
[isZoomed, media]
|
2021-09-07 16:12:26 +00:00
|
|
|
);
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2021-08-24 21:47:14 +00:00
|
|
|
const onTimeUpdate = useCallback(() => {
|
|
|
|
if (!videoElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setVideoTime(videoElement.currentTime);
|
|
|
|
}, [setVideoTime, videoElement]);
|
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const handleSave = (
|
|
|
|
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
|
|
) => {
|
2022-12-10 02:02:22 +00:00
|
|
|
if (isViewOnce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
const mediaItem = media[selectedIndex];
|
|
|
|
const { attachment, message, index } = mediaItem;
|
2019-10-03 19:03:46 +00:00
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
saveAttachment(attachment, message.sent_at, index + 1);
|
2021-08-23 23:14:53 +00:00
|
|
|
};
|
2018-07-18 23:02:10 +00:00
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const handleForward = (
|
|
|
|
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
|
|
) => {
|
2022-12-10 02:02:22 +00:00
|
|
|
if (isViewOnce) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox();
|
2021-08-23 23:14:53 +00:00
|
|
|
const mediaItem = media[selectedIndex];
|
2022-12-10 02:02:22 +00:00
|
|
|
toggleForwardMessageModal(mediaItem.message.id);
|
2021-08-23 23:14:53 +00:00
|
|
|
};
|
2019-10-03 19:03:46 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
const onKeyDown = useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
switch (event.key) {
|
2021-09-28 20:27:35 +00:00
|
|
|
case 'Escape': {
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox();
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2018-04-15 04:50:18 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
break;
|
2021-09-28 20:27:35 +00:00
|
|
|
}
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
case 'ArrowLeft':
|
2021-09-07 16:12:26 +00:00
|
|
|
onPrevious(event);
|
2021-08-23 23:14:53 +00:00
|
|
|
break;
|
2019-10-03 19:03:46 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
case 'ArrowRight':
|
2021-09-07 16:12:26 +00:00
|
|
|
onNext(event);
|
2021-08-23 23:14:53 +00:00
|
|
|
break;
|
2018-07-18 23:02:10 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
},
|
2022-12-10 02:02:22 +00:00
|
|
|
[closeLightbox, onNext, onPrevious]
|
2021-08-23 23:14:53 +00:00
|
|
|
);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const onClose = (event: React.MouseEvent<HTMLElement>) => {
|
2021-08-23 23:14:53 +00:00
|
|
|
event.stopPropagation();
|
2021-09-07 16:12:26 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox();
|
2021-08-23 23:14:53 +00:00
|
|
|
};
|
2019-10-03 19:03:46 +00:00
|
|
|
|
2021-08-24 21:47:14 +00:00
|
|
|
const playVideo = useCallback(() => {
|
|
|
|
if (!videoElement) {
|
2019-10-03 19:03:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-24 21:47:14 +00:00
|
|
|
if (videoElement.paused) {
|
2023-02-24 23:18:57 +00:00
|
|
|
onMediaPlaybackStart();
|
2022-12-21 18:41:48 +00:00
|
|
|
void videoElement.play();
|
2018-07-18 23:02:10 +00:00
|
|
|
} else {
|
2021-08-24 21:47:14 +00:00
|
|
|
videoElement.pause();
|
2018-07-18 23:02:10 +00:00
|
|
|
}
|
2023-02-24 23:18:57 +00:00
|
|
|
}, [videoElement, onMediaPlaybackStart]);
|
2018-04-25 22:15:57 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
|
|
|
setRoot(div);
|
2018-04-25 22:15:57 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
return () => {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
setRoot(undefined);
|
|
|
|
};
|
|
|
|
}, []);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const useCapture = true;
|
|
|
|
document.addEventListener('keydown', onKeyDown, useCapture);
|
2020-11-03 00:47:46 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', onKeyDown, useCapture);
|
|
|
|
};
|
|
|
|
}, [onKeyDown]);
|
2018-04-24 20:12:11 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const {
|
|
|
|
attachment,
|
|
|
|
contentType,
|
|
|
|
loop = false,
|
|
|
|
objectURL,
|
|
|
|
message,
|
|
|
|
} = media[selectedIndex] || {};
|
2021-09-02 21:38:46 +00:00
|
|
|
|
2021-09-07 16:12:26 +00:00
|
|
|
const isAttachmentGIF = isGIF(attachment ? [attachment] : undefined);
|
2021-09-02 21:38:46 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
useEffect(() => {
|
2021-08-24 21:47:14 +00:00
|
|
|
playVideo();
|
2018-04-15 05:48:21 +00:00
|
|
|
|
2021-09-02 21:38:46 +00:00
|
|
|
if (!videoElement || !isViewOnce) {
|
|
|
|
return noop;
|
|
|
|
}
|
2021-08-24 21:47:14 +00:00
|
|
|
|
2021-09-02 21:38:46 +00:00
|
|
|
if (isAttachmentGIF) {
|
|
|
|
return noop;
|
2021-08-24 21:47:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 21:38:46 +00:00
|
|
|
videoElement.addEventListener('timeupdate', onTimeUpdate);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
videoElement.removeEventListener('timeupdate', onTimeUpdate);
|
|
|
|
};
|
|
|
|
}, [isViewOnce, isAttachmentGIF, onTimeUpdate, playVideo, videoElement]);
|
2018-04-26 21:25:16 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const [{ scale, translateX, translateY }, springApi] = useSpring(
|
|
|
|
() => INITIAL_IMAGE_TRANSFORM
|
|
|
|
);
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const maxBoundsLimiter = useCallback(
|
|
|
|
(x: number, y: number): [number, number] => {
|
|
|
|
const zoomCache = zoomCacheRef.current;
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
if (!zoomCache) {
|
|
|
|
return [0, 0];
|
|
|
|
}
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const { maxX, maxY } = zoomCache;
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const posX = Math.min(maxX, Math.max(-maxX, x));
|
|
|
|
const posY = Math.min(maxY, Math.max(-maxY, y));
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
return [posX, posY];
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const positionImage = useCallback(
|
|
|
|
(ev: MouseEvent) => {
|
|
|
|
const zoomCache = zoomCacheRef.current;
|
2021-09-30 21:18:56 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
if (!zoomCache) {
|
|
|
|
return;
|
2021-10-04 20:12:14 +00:00
|
|
|
}
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2022-01-19 20:21:12 +00:00
|
|
|
const { maxX, maxY, screenWidth, screenHeight } = zoomCache;
|
|
|
|
|
|
|
|
const shouldTranslateX = maxX * ZOOM_SCALE > screenWidth;
|
|
|
|
const shouldTranslateY = maxY * ZOOM_SCALE > screenHeight;
|
2021-10-04 20:12:14 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const offsetX = screenWidth / 2 - ev.clientX;
|
|
|
|
const offsetY = screenHeight / 2 - ev.clientY;
|
|
|
|
const posX = offsetX * ZOOM_SCALE;
|
|
|
|
const posY = offsetY * ZOOM_SCALE;
|
|
|
|
const [x, y] = maxBoundsLimiter(posX, posY);
|
2021-10-04 20:12:14 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
springApi.start({
|
|
|
|
scale: ZOOM_SCALE,
|
2022-01-19 20:21:12 +00:00
|
|
|
translateX: shouldTranslateX ? x : undefined,
|
|
|
|
translateY: shouldTranslateY ? y : undefined,
|
2021-10-04 20:12:14 +00:00
|
|
|
});
|
|
|
|
},
|
2021-10-12 20:25:09 +00:00
|
|
|
[maxBoundsLimiter, springApi]
|
2021-10-04 20:12:14 +00:00
|
|
|
);
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const handleTouchStart = useCallback(
|
|
|
|
(ev: TouchEvent) => {
|
|
|
|
const [touch] = ev.touches;
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
dragCacheRef.current = {
|
|
|
|
startX: touch.clientX,
|
|
|
|
startY: touch.clientY,
|
|
|
|
translateX: translateX.get(),
|
|
|
|
translateY: translateY.get(),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
[translateY, translateX]
|
|
|
|
);
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-10-04 20:12:14 +00:00
|
|
|
const handleTouchMove = useCallback(
|
|
|
|
(ev: TouchEvent) => {
|
2021-10-12 20:25:09 +00:00
|
|
|
const dragCache = dragCacheRef.current;
|
2021-10-04 20:12:14 +00:00
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
if (!dragCache) {
|
2021-10-04 20:12:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [touch] = ev.touches;
|
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
const deltaX = touch.clientX - dragCache.startX;
|
|
|
|
const deltaY = touch.clientY - dragCache.startY;
|
|
|
|
|
|
|
|
const x = dragCache.translateX + deltaX;
|
|
|
|
const y = dragCache.translateY + deltaY;
|
|
|
|
|
|
|
|
springApi.start({
|
|
|
|
scale: ZOOM_SCALE,
|
|
|
|
translateX: x,
|
|
|
|
translateY: y,
|
2021-10-04 20:12:14 +00:00
|
|
|
});
|
|
|
|
},
|
2021-10-12 20:25:09 +00:00
|
|
|
[springApi]
|
|
|
|
);
|
|
|
|
|
|
|
|
const zoomButtonHandler = useCallback(
|
|
|
|
(ev: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
const imageNode = imageRef.current;
|
|
|
|
const animateNode = animateRef.current;
|
|
|
|
if (!imageNode || !animateNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isZoomed) {
|
2022-01-19 20:21:12 +00:00
|
|
|
const maxX = imageNode.offsetWidth;
|
|
|
|
const maxY = imageNode.offsetHeight;
|
|
|
|
const screenHeight = window.innerHeight;
|
|
|
|
const screenWidth = window.innerWidth;
|
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
zoomCacheRef.current = {
|
2022-01-19 20:21:12 +00:00
|
|
|
maxX,
|
|
|
|
maxY,
|
|
|
|
screenHeight,
|
|
|
|
screenWidth,
|
2021-10-12 20:25:09 +00:00
|
|
|
};
|
|
|
|
|
2022-01-19 20:21:12 +00:00
|
|
|
const shouldTranslateX = maxX * ZOOM_SCALE > screenWidth;
|
|
|
|
const shouldTranslateY = maxY * ZOOM_SCALE > screenHeight;
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const { height, left, top, width } =
|
|
|
|
animateNode.getBoundingClientRect();
|
2021-10-12 20:25:09 +00:00
|
|
|
|
|
|
|
const offsetX = ev.clientX - left - width / 2;
|
|
|
|
const offsetY = ev.clientY - top - height / 2;
|
|
|
|
const posX = -offsetX * ZOOM_SCALE + translateX.get();
|
|
|
|
const posY = -offsetY * ZOOM_SCALE + translateY.get();
|
|
|
|
const [x, y] = maxBoundsLimiter(posX, posY);
|
|
|
|
|
|
|
|
springApi.start({
|
|
|
|
scale: ZOOM_SCALE,
|
2022-01-19 20:21:12 +00:00
|
|
|
translateX: shouldTranslateX ? x : undefined,
|
|
|
|
translateY: shouldTranslateY ? y : undefined,
|
2021-10-12 20:25:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
setIsZoomed(true);
|
|
|
|
} else {
|
|
|
|
springApi.start(INITIAL_IMAGE_TRANSFORM);
|
|
|
|
setIsZoomed(false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[isZoomed, maxBoundsLimiter, translateX, translateY, springApi]
|
2021-10-04 20:12:14 +00:00
|
|
|
);
|
|
|
|
|
2021-09-28 20:27:35 +00:00
|
|
|
useEffect(() => {
|
2021-10-12 20:25:09 +00:00
|
|
|
const animateNode = animateRef.current;
|
2021-09-28 20:27:35 +00:00
|
|
|
let hasListener = false;
|
|
|
|
|
2021-10-12 20:25:09 +00:00
|
|
|
if (animateNode && isZoomed) {
|
2021-09-28 20:27:35 +00:00
|
|
|
hasListener = true;
|
|
|
|
document.addEventListener('mousemove', positionImage);
|
2021-10-04 20:12:14 +00:00
|
|
|
document.addEventListener('touchmove', handleTouchMove);
|
2021-10-12 20:25:09 +00:00
|
|
|
document.addEventListener('touchstart', handleTouchStart);
|
2021-09-28 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (hasListener) {
|
|
|
|
document.removeEventListener('mousemove', positionImage);
|
2021-10-04 20:12:14 +00:00
|
|
|
document.removeEventListener('touchmove', handleTouchMove);
|
2021-10-12 20:25:09 +00:00
|
|
|
document.removeEventListener('touchstart', handleTouchStart);
|
2021-09-28 20:27:35 +00:00
|
|
|
}
|
|
|
|
};
|
2021-10-12 20:25:09 +00:00
|
|
|
}, [handleTouchMove, handleTouchStart, isZoomed, positionImage]);
|
2021-09-28 20:27:35 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
const caption = attachment?.caption;
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
let content: JSX.Element;
|
|
|
|
if (!contentType) {
|
|
|
|
content = <>{children}</>;
|
|
|
|
} else {
|
|
|
|
const isImageTypeSupported = GoogleChrome.isImageTypeSupported(contentType);
|
|
|
|
const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType);
|
|
|
|
const isUnsupportedImageType =
|
|
|
|
!isImageTypeSupported && isImage(contentType);
|
|
|
|
const isUnsupportedVideoType =
|
|
|
|
!isVideoTypeSupported && isVideo(contentType);
|
2018-04-26 21:25:16 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
if (isImageTypeSupported) {
|
|
|
|
if (objectURL) {
|
|
|
|
content = (
|
2022-02-01 23:49:36 +00:00
|
|
|
<div className="Lightbox__zoomable-container">
|
|
|
|
<button
|
|
|
|
className="Lightbox__zoom-button"
|
|
|
|
onClick={zoomButtonHandler}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
alt={i18n('lightboxImageAlt')}
|
|
|
|
className="Lightbox__object"
|
|
|
|
onContextMenu={(ev: React.MouseEvent<HTMLImageElement>) => {
|
|
|
|
// These are the only image types supported by Electron's NativeImage
|
|
|
|
if (
|
|
|
|
ev &&
|
|
|
|
contentType !== IMAGE_PNG &&
|
|
|
|
!/image\/jpe?g/g.test(contentType)
|
|
|
|
) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
src={objectURL}
|
|
|
|
ref={imageRef}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</div>
|
2021-08-23 23:14:53 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
content = (
|
|
|
|
<button
|
|
|
|
aria-label={i18n('lightboxImageAlt')}
|
|
|
|
className={classNames({
|
|
|
|
Lightbox__object: true,
|
|
|
|
Lightbox__unsupported: true,
|
|
|
|
'Lightbox__unsupported--missing': true,
|
|
|
|
})}
|
2021-09-07 16:12:26 +00:00
|
|
|
onClick={onClose}
|
2021-08-23 23:14:53 +00:00
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (isVideoTypeSupported) {
|
2021-09-02 21:38:46 +00:00
|
|
|
const shouldLoop = loop || isAttachmentGIF || isViewOnce;
|
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
content = (
|
|
|
|
<video
|
2022-07-07 21:07:00 +00:00
|
|
|
className="Lightbox__object Lightbox__object--video"
|
2021-08-23 23:14:53 +00:00
|
|
|
controls={!shouldLoop}
|
|
|
|
key={objectURL}
|
|
|
|
loop={shouldLoop}
|
2021-08-24 21:47:14 +00:00
|
|
|
ref={setVideoElement}
|
2021-08-23 23:14:53 +00:00
|
|
|
>
|
|
|
|
<source src={objectURL} />
|
|
|
|
</video>
|
|
|
|
);
|
|
|
|
} else if (isUnsupportedImageType || isUnsupportedVideoType) {
|
|
|
|
content = (
|
|
|
|
<button
|
|
|
|
aria-label={i18n('unsupportedAttachment')}
|
|
|
|
className={classNames({
|
|
|
|
Lightbox__object: true,
|
|
|
|
Lightbox__unsupported: true,
|
|
|
|
'Lightbox__unsupported--image': isUnsupportedImageType,
|
|
|
|
'Lightbox__unsupported--video': isUnsupportedVideoType,
|
|
|
|
})}
|
2021-09-07 16:12:26 +00:00
|
|
|
onClick={onClose}
|
2021-08-23 23:14:53 +00:00
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Lightbox: Unexpected content type', { contentType });
|
2018-04-15 05:48:21 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
content = (
|
|
|
|
<button
|
|
|
|
aria-label={i18n('unsupportedAttachment')}
|
|
|
|
className="Lightbox__object Lightbox__unsupported Lightbox__unsupported--file"
|
2021-09-07 16:12:26 +00:00
|
|
|
onClick={onClose}
|
2021-08-23 23:14:53 +00:00
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
);
|
2018-04-15 05:48:21 +00:00
|
|
|
}
|
2021-08-23 23:14:53 +00:00
|
|
|
}
|
2018-04-15 05:48:21 +00:00
|
|
|
|
2021-09-30 21:18:56 +00:00
|
|
|
const hasNext = !isZoomed && selectedIndex < media.length - 1;
|
|
|
|
const hasPrevious = !isZoomed && selectedIndex > 0;
|
2021-08-23 23:14:53 +00:00
|
|
|
|
|
|
|
return root
|
|
|
|
? createPortal(
|
|
|
|
<div
|
2021-10-05 17:28:32 +00:00
|
|
|
className={classNames('Lightbox Lightbox__container', {
|
2021-10-12 20:25:09 +00:00
|
|
|
'Lightbox__container--zoom': isZoomed,
|
2021-10-05 17:28:32 +00:00
|
|
|
})}
|
2021-09-07 16:12:26 +00:00
|
|
|
onClick={(event: React.MouseEvent<HTMLDivElement>) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox();
|
2021-08-23 23:14:53 +00:00
|
|
|
}}
|
|
|
|
onKeyUp={(event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
|
|
if (
|
|
|
|
(containerRef && event.target !== containerRef.current) ||
|
|
|
|
event.keyCode !== 27
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-10 02:02:22 +00:00
|
|
|
closeLightbox();
|
2021-08-23 23:14:53 +00:00
|
|
|
}}
|
|
|
|
ref={containerRef}
|
|
|
|
role="presentation"
|
|
|
|
>
|
2021-10-12 20:25:09 +00:00
|
|
|
<div className="Lightbox__animated">
|
|
|
|
<div
|
|
|
|
className="Lightbox__main-container"
|
|
|
|
tabIndex={-1}
|
|
|
|
ref={focusRef}
|
|
|
|
>
|
2021-08-23 23:14:53 +00:00
|
|
|
<div className="Lightbox__header">
|
|
|
|
{getConversation ? (
|
|
|
|
<LightboxHeader
|
|
|
|
getConversation={getConversation}
|
|
|
|
i18n={i18n}
|
|
|
|
message={message}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
)}
|
|
|
|
<div className="Lightbox__controls">
|
2022-12-10 02:02:22 +00:00
|
|
|
{!isViewOnce ? (
|
2021-08-23 23:14:53 +00:00
|
|
|
<button
|
|
|
|
aria-label={i18n('forwardMessage')}
|
|
|
|
className="Lightbox__button Lightbox__button--forward"
|
|
|
|
onClick={handleForward}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-12-10 02:02:22 +00:00
|
|
|
{!isViewOnce ? (
|
2021-08-23 23:14:53 +00:00
|
|
|
<button
|
|
|
|
aria-label={i18n('save')}
|
|
|
|
className="Lightbox__button Lightbox__button--save"
|
|
|
|
onClick={handleSave}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<button
|
|
|
|
aria-label={i18n('close')}
|
|
|
|
className="Lightbox__button Lightbox__button--close"
|
2022-12-10 02:02:22 +00:00
|
|
|
onClick={closeLightbox}
|
2021-08-23 23:14:53 +00:00
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-12 20:25:09 +00:00
|
|
|
<animated.div
|
|
|
|
className={classNames('Lightbox__object--container', {
|
|
|
|
'Lightbox__object--container--zoom': isZoomed,
|
|
|
|
})}
|
|
|
|
ref={animateRef}
|
|
|
|
style={{
|
|
|
|
transform: to(
|
|
|
|
[scale, translateX, translateY],
|
|
|
|
(s, x, y) => `translate(${x}px, ${y}px) scale(${s})`
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{content}
|
|
|
|
</animated.div>
|
|
|
|
{hasPrevious && (
|
|
|
|
<div className="Lightbox__nav-prev">
|
|
|
|
<button
|
|
|
|
aria-label={i18n('previous')}
|
|
|
|
className="Lightbox__button Lightbox__button--previous"
|
|
|
|
onClick={onPrevious}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{hasNext && (
|
|
|
|
<div className="Lightbox__nav-next">
|
|
|
|
<button
|
|
|
|
aria-label={i18n('next')}
|
|
|
|
className="Lightbox__button Lightbox__button--next"
|
|
|
|
onClick={onNext}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-08-23 23:14:53 +00:00
|
|
|
</div>
|
|
|
|
<div className="Lightbox__footer">
|
2021-08-24 21:47:14 +00:00
|
|
|
{isViewOnce && videoTime ? (
|
|
|
|
<div className="Lightbox__timestamp">
|
|
|
|
{formatDuration(videoTime)}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-08-23 23:14:53 +00:00
|
|
|
{caption ? (
|
|
|
|
<div className="Lightbox__caption">{caption}</div>
|
|
|
|
) : null}
|
|
|
|
{media.length > 1 && (
|
|
|
|
<div className="Lightbox__thumbnails--container">
|
|
|
|
<div
|
|
|
|
className="Lightbox__thumbnails"
|
|
|
|
style={{
|
|
|
|
marginLeft:
|
|
|
|
0 - (selectedIndex * 64 + selectedIndex * 8 + 32),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{media.map((item, index) => (
|
|
|
|
<button
|
|
|
|
className={classNames({
|
|
|
|
Lightbox__thumbnail: true,
|
|
|
|
'Lightbox__thumbnail--selected':
|
|
|
|
index === selectedIndex,
|
|
|
|
})}
|
|
|
|
key={item.thumbnailObjectUrl}
|
|
|
|
type="button"
|
2021-09-07 16:12:26 +00:00
|
|
|
onClick={(
|
|
|
|
event: React.MouseEvent<HTMLButtonElement, MouseEvent>
|
|
|
|
) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
setSelectedIndex(index);
|
|
|
|
}}
|
2021-08-23 23:14:53 +00:00
|
|
|
>
|
|
|
|
{item.thumbnailObjectUrl ? (
|
|
|
|
<img
|
|
|
|
alt={i18n('lightboxImageAlt')}
|
|
|
|
src={item.thumbnailObjectUrl}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="Lightbox__thumbnail--unavailable" />
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2021-10-12 20:25:09 +00:00
|
|
|
</div>
|
2021-08-23 23:14:53 +00:00
|
|
|
</div>,
|
|
|
|
root
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
}
|
2020-09-12 00:46:52 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
function LightboxHeader({
|
|
|
|
getConversation,
|
|
|
|
i18n,
|
|
|
|
message,
|
|
|
|
}: {
|
|
|
|
getConversation: (id: string) => ConversationType;
|
|
|
|
i18n: LocalizerType;
|
2023-01-13 20:07:26 +00:00
|
|
|
message: ReadonlyDeep<MediaItemMessageType>;
|
2021-08-23 23:14:53 +00:00
|
|
|
}): JSX.Element {
|
|
|
|
const conversation = getConversation(message.conversationId);
|
2020-09-12 00:46:52 +00:00
|
|
|
|
2021-08-23 23:14:53 +00:00
|
|
|
return (
|
|
|
|
<div className="Lightbox__header--container">
|
|
|
|
<div className="Lightbox__header--avatar">
|
|
|
|
<Avatar
|
|
|
|
acceptedMessageRequest={conversation.acceptedMessageRequest}
|
|
|
|
avatarPath={conversation.avatarPath}
|
2021-12-01 17:24:00 +00:00
|
|
|
badge={undefined}
|
2021-08-23 23:14:53 +00:00
|
|
|
color={conversation.color}
|
|
|
|
conversationType={conversation.type}
|
|
|
|
i18n={i18n}
|
|
|
|
isMe={conversation.isMe}
|
|
|
|
phoneNumber={conversation.e164}
|
|
|
|
profileName={conversation.profileName}
|
|
|
|
sharedGroupNames={conversation.sharedGroupNames}
|
|
|
|
size={AvatarSize.THIRTY_TWO}
|
|
|
|
title={conversation.title}
|
|
|
|
unblurredAvatarPath={conversation.unblurredAvatarPath}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="Lightbox__header--content">
|
|
|
|
<div className="Lightbox__header--name">{conversation.title}</div>
|
|
|
|
<div className="Lightbox__header--timestamp">
|
|
|
|
{moment(message.received_at_ms).format('L LT')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2018-04-15 03:27:03 +00:00
|
|
|
}
|