2018-05-22 19:31:43 +00:00
|
|
|
// tslint:disable:react-a11y-anchors
|
|
|
|
|
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-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
2018-05-22 19:31:43 +00:00
|
|
|
|
2018-07-18 00:15:34 +00:00
|
|
|
const Colors = {
|
|
|
|
TEXT_SECONDARY: '#bbb',
|
|
|
|
ICON_SECONDARY: '#ccc',
|
|
|
|
};
|
|
|
|
|
|
|
|
const colorSVG = (url: string, color: string) => {
|
|
|
|
return {
|
|
|
|
WebkitMask: `url(${url}) no-repeat center`,
|
|
|
|
WebkitMaskSize: '100%',
|
|
|
|
backgroundColor: color,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
interface Props {
|
|
|
|
close: () => void;
|
2018-04-25 22:15:57 +00:00
|
|
|
contentType: MIME.MIMEType | undefined;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-05-22 19:31:43 +00:00
|
|
|
objectURL: string;
|
2018-11-14 18:47:19 +00:00
|
|
|
caption?: string;
|
2018-04-15 05:48:43 +00:00
|
|
|
onNext?: () => void;
|
|
|
|
onPrevious?: () => void;
|
2018-04-25 22:15:57 +00:00
|
|
|
onSave?: () => void;
|
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,
|
|
|
|
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,
|
2018-04-15 04:27:30 +00:00
|
|
|
} as React.CSSProperties,
|
|
|
|
objectContainer: {
|
2018-11-14 18:47:19 +00:00
|
|
|
position: 'relative',
|
2018-04-15 05:30:42 +00:00
|
|
|
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: {
|
2018-05-08 01:20:39 +00:00
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 0,
|
|
|
|
maxWidth: '100%',
|
|
|
|
maxHeight: '100%',
|
|
|
|
objectFit: 'contain',
|
|
|
|
} as React.CSSProperties,
|
2018-11-14 18:47:19 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
iconButtonPlaceholder: {
|
|
|
|
// Dimensions match `.iconButton`:
|
|
|
|
display: 'inline-block',
|
|
|
|
width: 50,
|
|
|
|
height: 50,
|
|
|
|
},
|
2018-04-15 03:27:03 +00:00
|
|
|
};
|
|
|
|
|
2018-04-15 04:27:30 +00:00
|
|
|
interface IconButtonProps {
|
|
|
|
onClick?: () => void;
|
2018-04-26 15:50:54 +00:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
type: 'save' | 'close' | 'previous' | 'next';
|
2018-04-15 04:27:30 +00:00
|
|
|
}
|
2018-04-26 15:18:24 +00:00
|
|
|
|
2018-04-26 15:50:54 +00:00
|
|
|
const IconButton = ({ onClick, style, type }: IconButtonProps) => {
|
2018-04-26 15:18:24 +00:00
|
|
|
const clickHandler = (event: React.MouseEvent<HTMLAnchorElement>): void => {
|
|
|
|
event.preventDefault();
|
|
|
|
if (!onClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
onClick={clickHandler}
|
|
|
|
className={classNames('iconButton', type)}
|
2018-05-22 19:31:43 +00:00
|
|
|
role="button"
|
2018-04-26 15:50:54 +00:00
|
|
|
style={style}
|
2018-04-26 15:18:24 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2018-04-15 04:27:30 +00:00
|
|
|
|
2018-04-26 15:50:54 +00:00
|
|
|
const IconButtonPlaceholder = () => (
|
|
|
|
<div style={styles.iconButtonPlaceholder} />
|
|
|
|
);
|
|
|
|
|
2018-05-08 01:20:39 +00:00
|
|
|
const Icon = ({
|
|
|
|
onClick,
|
|
|
|
url,
|
|
|
|
}: {
|
|
|
|
onClick?: (
|
|
|
|
event: React.MouseEvent<HTMLImageElement | HTMLDivElement>
|
|
|
|
) => void;
|
|
|
|
url: string;
|
|
|
|
}) => (
|
|
|
|
<div
|
|
|
|
style={{
|
2018-05-08 01:31:31 +00:00
|
|
|
...styles.object,
|
2018-05-08 01:20:39 +00:00
|
|
|
...colorSVG(url, Colors.ICON_SECONDARY),
|
|
|
|
maxWidth: 200,
|
|
|
|
}}
|
|
|
|
onClick={onClick}
|
2018-05-22 19:31:43 +00:00
|
|
|
role="button"
|
2018-05-08 01:20:39 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
export class Lightbox extends React.Component<Props> {
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly containerRef: React.RefObject<HTMLDivElement>;
|
|
|
|
private readonly videoRef: React.RefObject<HTMLVideoElement>;
|
|
|
|
private readonly playVideoBound: () => void;
|
2018-07-18 23:02:10 +00:00
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.playVideoBound = this.playVideo.bind(this);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
this.videoRef = React.createRef();
|
|
|
|
this.containerRef = React.createRef();
|
2018-07-18 23:02:10 +00:00
|
|
|
}
|
2018-04-15 06:53:19 +00:00
|
|
|
|
2018-04-15 04:50:18 +00:00
|
|
|
public componentDidMount() {
|
|
|
|
const useCapture = true;
|
|
|
|
document.addEventListener('keyup', this.onKeyUp, useCapture);
|
2018-07-18 23:02:10 +00:00
|
|
|
|
|
|
|
this.playVideo();
|
2018-04-15 04:50:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 16:08:18 +00:00
|
|
|
public componentWillUnmount() {
|
2018-04-15 04:50:18 +00:00
|
|
|
const useCapture = true;
|
|
|
|
document.removeEventListener('keyup', this.onKeyUp, useCapture);
|
|
|
|
}
|
|
|
|
|
2018-07-18 23:02:10 +00:00
|
|
|
public playVideo() {
|
|
|
|
if (!this.videoRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
const { current } = this.videoRef;
|
|
|
|
if (!current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current.paused) {
|
2018-07-18 23:14:37 +00:00
|
|
|
// tslint:disable-next-line no-floating-promises
|
2019-01-14 21:49:58 +00:00
|
|
|
current.play();
|
2018-07-18 23:02:10 +00:00
|
|
|
} else {
|
2019-01-14 21:49:58 +00:00
|
|
|
current.pause();
|
2018-07-18 23:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
public render() {
|
2018-05-22 19:31:43 +00:00
|
|
|
const {
|
2018-11-14 18:47:19 +00:00
|
|
|
caption,
|
2018-05-22 19:31:43 +00:00
|
|
|
contentType,
|
|
|
|
objectURL,
|
|
|
|
onNext,
|
|
|
|
onPrevious,
|
|
|
|
onSave,
|
|
|
|
i18n,
|
|
|
|
} = this.props;
|
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
return (
|
2018-04-15 05:48:43 +00:00
|
|
|
<div
|
|
|
|
style={styles.container}
|
|
|
|
onClick={this.onContainerClick}
|
2019-01-14 21:49:58 +00:00
|
|
|
ref={this.containerRef}
|
2018-05-22 19:31:43 +00:00
|
|
|
role="dialog"
|
2018-04-15 05:48:43 +00:00
|
|
|
>
|
2018-04-26 15:50:54 +00:00
|
|
|
<div style={styles.mainContainer}>
|
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)
|
2018-05-22 19:31:43 +00:00
|
|
|
? this.renderObject({ objectURL, contentType, i18n })
|
2018-04-26 15:50:54 +00:00
|
|
|
: null}
|
2018-11-14 18:47:19 +00:00
|
|
|
{caption ? <div style={styles.caption}>{caption}</div> : null}
|
2018-04-26 15:50:54 +00:00
|
|
|
</div>
|
|
|
|
<div style={styles.controls}>
|
|
|
|
<IconButton type="close" onClick={this.onClose} />
|
2018-04-26 20:48:08 +00:00
|
|
|
{onSave ? (
|
2018-04-26 15:50:54 +00:00
|
|
|
<IconButton
|
|
|
|
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>
|
2018-04-26 15:50:54 +00:00
|
|
|
<div style={styles.navigationContainer}>
|
2018-04-26 20:48:08 +00:00
|
|
|
{onPrevious ? (
|
|
|
|
<IconButton type="previous" onClick={onPrevious} />
|
2018-04-26 15:50:54 +00:00
|
|
|
) : (
|
|
|
|
<IconButtonPlaceholder />
|
|
|
|
)}
|
2018-04-26 20:48:08 +00:00
|
|
|
{onNext ? (
|
|
|
|
<IconButton type="next" onClick={onNext} />
|
2018-04-26 15:50:54 +00:00
|
|
|
) : (
|
|
|
|
<IconButtonPlaceholder />
|
|
|
|
)}
|
2018-04-15 04:27:30 +00:00
|
|
|
</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,
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n,
|
2018-04-25 22:15:57 +00:00
|
|
|
}: {
|
|
|
|
objectURL: string;
|
|
|
|
contentType: MIME.MIMEType;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-04-25 22:15:57 +00:00
|
|
|
}) => {
|
2018-05-08 01:20:39 +00:00
|
|
|
const isImageTypeSupported = GoogleChrome.isImageTypeSupported(contentType);
|
|
|
|
if (isImageTypeSupported) {
|
2018-04-25 22:15:57 +00:00
|
|
|
return (
|
|
|
|
<img
|
2018-05-22 19:31:43 +00:00
|
|
|
alt={i18n('lightboxImageAlt')}
|
2018-05-08 01:31:31 +00:00
|
|
|
style={styles.object}
|
2018-04-25 22:15:57 +00:00
|
|
|
src={objectURL}
|
|
|
|
onClick={this.onObjectClick}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:20:39 +00:00
|
|
|
const isVideoTypeSupported = GoogleChrome.isVideoTypeSupported(contentType);
|
|
|
|
if (isVideoTypeSupported) {
|
2018-04-25 22:15:57 +00:00
|
|
|
return (
|
2018-07-18 23:02:10 +00:00
|
|
|
<video
|
|
|
|
role="button"
|
2019-01-14 21:49:58 +00:00
|
|
|
ref={this.videoRef}
|
2018-07-18 23:02:10 +00:00
|
|
|
onClick={this.playVideoBound}
|
|
|
|
controls={true}
|
|
|
|
style={styles.object}
|
2018-11-28 20:34:29 +00:00
|
|
|
key={objectURL}
|
2018-07-18 23:02:10 +00:00
|
|
|
>
|
2018-04-25 22:15:57 +00:00
|
|
|
<source src={objectURL} />
|
|
|
|
</video>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-08 01:20:39 +00:00
|
|
|
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
|
|
|
|
? 'images/video.svg'
|
|
|
|
: 'images/image.svg';
|
|
|
|
|
|
|
|
return <Icon url={iconUrl} onClick={this.onObjectClick} />;
|
2018-05-08 01:20:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 22:15:57 +00:00
|
|
|
// tslint:disable-next-line no-console
|
|
|
|
console.log('Lightbox: Unexpected content type', { contentType });
|
2018-05-22 19:31:43 +00:00
|
|
|
|
2018-05-08 01:20:39 +00:00
|
|
|
return <Icon onClick={this.onObjectClick} url="images/file.svg" />;
|
2018-04-25 22:15:57 +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-01-14 21:49:58 +00:00
|
|
|
private readonly onKeyUp = (event: KeyboardEvent) => {
|
2018-04-26 21:25:16 +00:00
|
|
|
const { onNext, onPrevious } = this.props;
|
|
|
|
switch (event.key) {
|
|
|
|
case 'Escape':
|
2018-05-22 19:31:43 +00:00
|
|
|
this.onClose();
|
2018-04-26 21:25:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowLeft':
|
|
|
|
if (onPrevious) {
|
|
|
|
onPrevious();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
if (onNext) {
|
|
|
|
onNext();
|
|
|
|
}
|
|
|
|
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:43 +00:00
|
|
|
};
|
2018-04-15 05:48:21 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly onObjectClick = (
|
2018-05-08 01:20:39 +00:00
|
|
|
event: React.MouseEvent<HTMLImageElement | HTMLDivElement>
|
|
|
|
) => {
|
2018-04-15 05:48:21 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
this.onClose();
|
2018-04-15 05:48:43 +00:00
|
|
|
};
|
2018-04-15 03:27:03 +00:00
|
|
|
}
|