signal-desktop/ts/components/Lightbox.tsx

518 lines
12 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2018-04-15 03:27:03 +00:00
import React from 'react';
2018-04-15 04:27:30 +00:00
import classNames from 'classnames';
2018-04-25 22:15:57 +00:00
import is from '@sindresorhus/is';
import * as GoogleChrome from '../util/GoogleChrome';
import * as MIME from '../types/MIME';
2018-04-15 03:27:03 +00:00
2019-10-03 19:03:46 +00:00
import { formatDuration } from '../util/formatDuration';
2019-01-14 21:49:58 +00:00
import { LocalizerType } from '../types/Util';
2018-07-18 00:15:34 +00:00
const Colors = {
2019-10-04 18:06:17 +00:00
ICON_SECONDARY: '#b9b9b9',
2018-07-18 00:15:34 +00:00
};
const colorSVG = (url: string, color: string) => {
return {
WebkitMask: `url(${url}) no-repeat center`,
WebkitMaskSize: '100%',
backgroundColor: color,
};
};
export type Props = {
2018-04-15 03:27:03 +00:00
close: () => void;
2018-04-25 22:15:57 +00:00
contentType: MIME.MIMEType | undefined;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
objectURL: string;
caption?: string;
2019-10-03 19:03:46 +00:00
isViewOnce: boolean;
onNext?: () => void;
onPrevious?: () => void;
2018-04-25 22:15:57 +00:00
onSave?: () => void;
};
type State = {
2019-10-03 19:03:46 +00:00
videoTime?: number;
};
2018-04-15 03:27:03 +00:00
2018-04-26 23:13:05 +00:00
const CONTROLS_WIDTH = 50;
const CONTROLS_SPACING = 10;
2018-04-15 03:27:03 +00:00
const styles = {
container: {
2018-04-15 04:27:30 +00:00
display: 'flex',
2018-04-26 15:50:54 +00:00
flexDirection: 'column',
2018-04-15 04:27:30 +00:00
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
backgroundColor: 'rgba(0, 0, 0, 0.9)',
2018-04-26 15:50:54 +00:00
} as React.CSSProperties,
2020-09-12 00:46:52 +00:00
buttonContainer: {
backgroundColor: 'transparent',
border: 'none',
display: 'flex',
flexDirection: 'column',
outline: 'none',
width: '100%',
2020-09-12 00:46:52 +00:00
padding: 0,
} as React.CSSProperties,
2018-04-26 15:50:54 +00:00
mainContainer: {
display: 'flex',
flexDirection: 'row',
2018-04-26 23:13:05 +00:00
flexGrow: 1,
2018-04-26 15:50:54 +00:00
paddingTop: 40,
paddingLeft: 40,
paddingRight: 40,
paddingBottom: 0,
// To ensure that a large image doesn't overflow the flex layout
minHeight: '50px',
2019-11-07 21:36:16 +00:00
outline: 'none',
2018-04-15 04:27:30 +00:00
} as React.CSSProperties,
objectContainer: {
position: 'relative',
flexGrow: 1,
2018-04-15 04:27:30 +00:00
display: 'inline-flex',
justifyContent: 'center',
} as React.CSSProperties,
2018-05-08 01:31:31 +00:00
object: {
flexGrow: 1,
flexShrink: 1,
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
outline: 'none',
} as React.CSSProperties,
img: {
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
width: 'auto',
height: 'auto',
maxWidth: '100%',
maxHeight: '100%',
objectFit: 'contain',
outline: 'none',
} as React.CSSProperties,
caption: {
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
textAlign: 'center',
color: 'white',
padding: '1em',
paddingLeft: '3em',
paddingRight: '3em',
backgroundColor: 'rgba(192, 192, 192, .20)',
} as React.CSSProperties,
2018-04-26 23:13:05 +00:00
controlsOffsetPlaceholder: {
width: CONTROLS_WIDTH,
marginRight: CONTROLS_SPACING,
flexShrink: 0,
},
2018-04-15 04:27:30 +00:00
controls: {
2018-04-26 23:13:05 +00:00
width: CONTROLS_WIDTH,
2018-04-15 04:27:30 +00:00
flexShrink: 0,
display: 'flex',
flexDirection: 'column',
2018-04-26 23:13:05 +00:00
marginLeft: CONTROLS_SPACING,
2018-04-15 04:57:12 +00:00
} as React.CSSProperties,
2018-04-26 15:50:54 +00:00
navigationContainer: {
flexShrink: 0,
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
} as React.CSSProperties,
saveButton: {
marginTop: 10,
},
2019-06-26 19:33:13 +00:00
countdownContainer: {
padding: 8,
},
2018-04-26 15:50:54 +00:00
iconButtonPlaceholder: {
// Dimensions match `.iconButton`:
display: 'inline-block',
width: 50,
height: 50,
},
2019-10-03 19:03:46 +00:00
timestampPill: {
borderRadius: '15px',
backgroundColor: '#000000',
color: '#eeefef',
fontSize: '16px',
letterSpacing: '0px',
lineHeight: '18px',
// This cast is necessary or typescript chokes
2020-09-12 00:46:52 +00:00
textAlign: 'center' as const,
2019-10-03 19:03:46 +00:00
padding: '6px',
paddingLeft: '18px',
paddingRight: '18px',
},
2018-04-15 03:27:03 +00:00
};
type IconButtonProps = {
2020-09-12 00:46:52 +00:00
i18n: LocalizerType;
2018-04-15 04:27:30 +00:00
onClick?: () => void;
2018-04-26 15:50:54 +00:00
style?: React.CSSProperties;
type: 'save' | 'close' | 'previous' | 'next';
};
2020-09-12 00:46:52 +00:00
const IconButton = ({ i18n, onClick, style, type }: IconButtonProps) => {
2019-11-07 21:36:16 +00:00
const clickHandler = (event: React.MouseEvent<HTMLButtonElement>): void => {
event.preventDefault();
if (!onClick) {
return;
}
onClick();
};
return (
2019-11-07 21:36:16 +00:00
<button
onClick={clickHandler}
className={classNames('iconButton', type)}
2018-04-26 15:50:54 +00:00
style={style}
2020-09-12 00:46:52 +00:00
aria-label={i18n(type)}
type="button"
/>
);
};
2018-04-15 04:27:30 +00:00
2018-04-26 15:50:54 +00:00
const IconButtonPlaceholder = () => (
<div style={styles.iconButtonPlaceholder} />
);
const Icon = ({
2020-09-12 00:46:52 +00:00
i18n,
onClick,
url,
}: {
2020-09-12 00:46:52 +00:00
i18n: LocalizerType;
onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
url: string;
}) => (
2019-11-07 21:36:16 +00:00
<button
style={{
2018-05-08 01:31:31 +00:00
...styles.object,
...colorSVG(url, Colors.ICON_SECONDARY),
maxWidth: 200,
}}
onClick={onClick}
2020-09-12 00:46:52 +00:00
aria-label={i18n('unsupportedAttachment')}
type="button"
/>
);
2019-10-03 19:03:46 +00:00
export class Lightbox extends React.Component<Props, State> {
2019-11-07 21:36:16 +00:00
public readonly containerRef = React.createRef<HTMLDivElement>();
2020-09-12 00:46:52 +00:00
2019-11-07 21:36:16 +00:00
public readonly videoRef = React.createRef<HTMLVideoElement>();
2020-09-12 00:46:52 +00:00
2019-11-07 21:36:16 +00:00
public readonly focusRef = React.createRef<HTMLDivElement>();
2020-09-12 00:46:52 +00:00
public previousFocus: HTMLElement | null = null;
2020-02-07 20:07:43 +00:00
public constructor(props: Props) {
super(props);
this.state = {};
}
2020-09-12 00:46:52 +00:00
public componentDidMount(): void {
this.previousFocus = document.activeElement as HTMLElement;
2019-11-07 21:36:16 +00:00
2019-10-03 19:03:46 +00:00
const { isViewOnce } = this.props;
2018-04-15 04:50:18 +00:00
const useCapture = true;
2019-11-07 21:36:16 +00:00
document.addEventListener('keydown', this.onKeyDown, useCapture);
2019-10-03 19:03:46 +00:00
const video = this.getVideo();
if (video && isViewOnce) {
video.addEventListener('timeupdate', this.onTimeUpdate);
}
2019-11-07 21:36:16 +00:00
// Wait until we're added to the DOM. ConversationView first creates this view, then
// appends its elements into the DOM.
setTimeout(() => {
this.playVideo();
if (this.focusRef && this.focusRef.current) {
this.focusRef.current.focus();
}
});
2018-04-15 04:50:18 +00:00
}
2020-09-12 00:46:52 +00:00
public componentWillUnmount(): void {
2019-11-07 21:36:16 +00:00
if (this.previousFocus && this.previousFocus.focus) {
this.previousFocus.focus();
}
2019-10-03 19:03:46 +00:00
const { isViewOnce } = this.props;
2018-04-15 04:50:18 +00:00
const useCapture = true;
2019-11-07 21:36:16 +00:00
document.removeEventListener('keydown', this.onKeyDown, useCapture);
2019-10-03 19:03:46 +00:00
const video = this.getVideo();
if (video && isViewOnce) {
video.removeEventListener('timeupdate', this.onTimeUpdate);
}
2018-04-15 04:50:18 +00:00
}
2020-09-12 00:46:52 +00:00
public getVideo(): HTMLVideoElement | null {
if (!this.videoRef) {
2020-09-12 00:46:52 +00:00
return null;
}
2019-01-14 21:49:58 +00:00
const { current } = this.videoRef;
if (!current) {
2020-09-12 00:46:52 +00:00
return null;
2019-01-14 21:49:58 +00:00
}
2019-10-03 19:03:46 +00:00
return current;
}
2020-09-12 00:46:52 +00:00
public playVideo(): void {
2019-10-03 19:03:46 +00:00
const video = this.getVideo();
if (!video) {
return;
}
if (video.paused) {
video.play();
} else {
2019-10-03 19:03:46 +00:00
video.pause();
}
}
2020-09-12 00:46:52 +00:00
public render(): JSX.Element {
const {
caption,
contentType,
2019-06-26 19:33:13 +00:00
i18n,
2019-10-03 19:03:46 +00:00
isViewOnce,
objectURL,
onNext,
onPrevious,
onSave,
} = this.props;
2019-10-03 19:03:46 +00:00
const { videoTime } = this.state;
2018-04-15 03:27:03 +00:00
return (
<div
2019-11-07 21:36:16 +00:00
className="module-lightbox"
style={styles.container}
onClick={this.onContainerClick}
2020-09-12 00:46:52 +00:00
onKeyUp={this.onContainerKeyUp}
2019-01-14 21:49:58 +00:00
ref={this.containerRef}
2020-09-12 00:46:52 +00:00
role="presentation"
>
2019-11-07 21:36:16 +00:00
<div style={styles.mainContainer} tabIndex={-1} ref={this.focusRef}>
2018-04-26 23:13:05 +00:00
<div style={styles.controlsOffsetPlaceholder} />
2018-04-26 15:50:54 +00:00
<div style={styles.objectContainer}>
{!is.undefined(contentType)
2019-10-03 19:03:46 +00:00
? this.renderObject({ objectURL, contentType, i18n, isViewOnce })
2018-04-26 15:50:54 +00:00
: null}
{caption ? <div style={styles.caption}>{caption}</div> : null}
2018-04-26 15:50:54 +00:00
</div>
<div style={styles.controls}>
2020-09-12 00:46:52 +00:00
<IconButton i18n={i18n} type="close" onClick={this.onClose} />
2018-04-26 20:48:08 +00:00
{onSave ? (
2018-04-26 15:50:54 +00:00
<IconButton
2020-09-12 00:46:52 +00:00
i18n={i18n}
2018-04-26 15:50:54 +00:00
type="save"
2018-04-26 20:48:08 +00:00
onClick={onSave}
2018-04-26 15:50:54 +00:00
style={styles.saveButton}
/>
) : null}
</div>
2018-04-15 04:27:30 +00:00
</div>
{isViewOnce && videoTime && is.number(videoTime) ? (
2019-10-03 19:03:46 +00:00
<div style={styles.navigationContainer}>
<div style={styles.timestampPill}>{formatDuration(videoTime)}</div>
</div>
) : (
<div style={styles.navigationContainer}>
{onPrevious ? (
2020-09-12 00:46:52 +00:00
<IconButton i18n={i18n} type="previous" onClick={onPrevious} />
2019-10-03 19:03:46 +00:00
) : (
<IconButtonPlaceholder />
)}
{onNext ? (
2020-09-12 00:46:52 +00:00
<IconButton i18n={i18n} type="next" onClick={onNext} />
2019-10-03 19:03:46 +00:00
) : (
<IconButtonPlaceholder />
)}
</div>
)}
2018-04-15 03:27:03 +00:00
</div>
);
}
2018-04-15 04:50:18 +00:00
2019-01-14 21:49:58 +00:00
private readonly renderObject = ({
2018-04-25 22:15:57 +00:00
objectURL,
contentType,
i18n,
2019-10-03 19:03:46 +00:00
isViewOnce,
2018-04-25 22:15:57 +00:00
}: {
objectURL: string;
contentType: MIME.MIMEType;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
2019-10-03 19:03:46 +00:00
isViewOnce: boolean;
2018-04-25 22:15:57 +00:00
}) => {
const isImageTypeSupported = GoogleChrome.isImageTypeSupported(contentType);
if (isImageTypeSupported) {
2018-04-25 22:15:57 +00:00
return (
2020-09-12 00:46:52 +00:00
<button
type="button"
style={styles.buttonContainer}
2018-04-25 22:15:57 +00:00
onClick={this.onObjectClick}
2020-09-12 00:46:52 +00:00
>
<img
alt={i18n('lightboxImageAlt')}
style={styles.img}
2020-09-12 00:46:52 +00:00
src={objectURL}
onContextMenu={this.onContextMenu}
2020-09-12 00:46:52 +00:00
/>
</button>
2018-04-25 22:15:57 +00:00
);
}
const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType);
if (isVideoTypeSupported) {
2018-04-25 22:15:57 +00:00
return (
<video
2019-01-14 21:49:58 +00:00
ref={this.videoRef}
2019-10-03 19:03:46 +00:00
loop={isViewOnce}
controls={!isViewOnce}
style={styles.object}
key={objectURL}
>
2018-04-25 22:15:57 +00:00
<source src={objectURL} />
</video>
);
}
const isUnsupportedImageType =
!isImageTypeSupported && MIME.isImage(contentType);
const isUnsupportedVideoType =
!isVideoTypeSupported && MIME.isVideo(contentType);
if (isUnsupportedImageType || isUnsupportedVideoType) {
2019-01-14 21:49:58 +00:00
const iconUrl = isUnsupportedVideoType
2020-08-21 22:05:32 +00:00
? 'images/movie.svg'
2019-01-14 21:49:58 +00:00
: 'images/image.svg';
2020-09-12 00:46:52 +00:00
return <Icon i18n={i18n} url={iconUrl} onClick={this.onObjectClick} />;
}
2020-09-12 00:46:52 +00:00
window.log.info('Lightbox: Unexpected content type', { contentType });
2020-09-12 00:46:52 +00:00
return (
<Icon i18n={i18n} onClick={this.onObjectClick} url="images/file.svg" />
);
2018-04-25 22:15:57 +00:00
};
2020-11-03 01:12:27 +00:00
private readonly onContextMenu = (
event: React.MouseEvent<HTMLImageElement>
) => {
2020-11-23 21:37:39 +00:00
const { contentType = '' } = this.props;
// These are the only image types supported by Electron's NativeImage
if (
event &&
contentType !== 'image/png' &&
2020-11-23 21:37:39 +00:00
!/image\/jpe?g/g.test(contentType)
) {
event.preventDefault();
}
2020-11-03 01:12:27 +00:00
};
2019-01-14 21:49:58 +00:00
private readonly onClose = () => {
2018-04-24 20:12:11 +00:00
const { close } = this.props;
if (!close) {
return;
}
close();
2018-04-15 05:48:21 +00:00
};
2019-10-03 19:03:46 +00:00
private readonly onTimeUpdate = () => {
const video = this.getVideo();
if (!video) {
return;
}
this.setState({
videoTime: video.currentTime,
});
};
2019-11-07 21:36:16 +00:00
private readonly onKeyDown = (event: KeyboardEvent) => {
const { onNext, onPrevious } = this.props;
switch (event.key) {
case 'Escape':
this.onClose();
2019-11-07 21:36:16 +00:00
event.preventDefault();
event.stopPropagation();
break;
case 'ArrowLeft':
if (onPrevious) {
onPrevious();
2019-11-07 21:36:16 +00:00
event.preventDefault();
event.stopPropagation();
}
break;
case 'ArrowRight':
if (onNext) {
onNext();
2019-11-07 21:36:16 +00:00
event.preventDefault();
event.stopPropagation();
}
break;
default:
2018-04-15 04:50:18 +00:00
}
2018-04-15 04:57:12 +00:00
};
2018-04-15 05:48:21 +00:00
2019-01-14 21:49:58 +00:00
private readonly onContainerClick = (
event: React.MouseEvent<HTMLDivElement>
) => {
if (this.containerRef && event.target !== this.containerRef.current) {
2018-04-15 05:48:21 +00:00
return;
}
this.onClose();
};
2018-04-15 05:48:21 +00:00
2020-09-12 00:46:52 +00:00
private readonly onContainerKeyUp = (
event: React.KeyboardEvent<HTMLDivElement>
) => {
if (
(this.containerRef && event.target !== this.containerRef.current) ||
event.keyCode !== 27
) {
return;
}
this.onClose();
};
2019-01-14 21:49:58 +00:00
private readonly onObjectClick = (
2020-09-12 00:46:52 +00:00
event: React.MouseEvent<HTMLAnchorElement | HTMLButtonElement>
) => {
2018-04-15 05:48:21 +00:00
event.stopPropagation();
this.onClose();
};
2018-04-15 03:27:03 +00:00
}