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-05-22 19:31:43 +00:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-08-21 22:05:32 +00:00
|
|
|
export interface 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;
|
2018-05-22 19:31:43 +00:00
|
|
|
objectURL: string;
|
2018-11-14 18:47:19 +00:00
|
|
|
caption?: string;
|
2019-10-03 19:03:46 +00:00
|
|
|
isViewOnce: boolean;
|
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
|
|
|
}
|
2019-10-03 19:03:46 +00:00
|
|
|
interface State {
|
|
|
|
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',
|
2020-09-23 16:07:27 +00:00
|
|
|
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,
|
2019-08-22 21:11:36 +00:00
|
|
|
// 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: {
|
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,
|
2020-09-17 19:14:08 +00:00
|
|
|
flexShrink: 1,
|
2018-05-08 01:20:39 +00:00
|
|
|
maxWidth: '100%',
|
|
|
|
maxHeight: '100%',
|
|
|
|
objectFit: 'contain',
|
2019-09-19 21:47:35 +00:00
|
|
|
outline: 'none',
|
2018-05-08 01:20:39 +00:00
|
|
|
} as React.CSSProperties,
|
2020-09-23 16:07:27 +00:00
|
|
|
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,
|
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,
|
|
|
|
},
|
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
|
|
|
};
|
|
|
|
|
2018-04-15 04:27:30 +00:00
|
|
|
interface 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';
|
2018-04-15 04:27:30 +00:00
|
|
|
}
|
2018-04-26 15:18:24 +00:00
|
|
|
|
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 => {
|
2018-04-26 15:18:24 +00:00
|
|
|
event.preventDefault();
|
|
|
|
if (!onClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onClick();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
2018-04-26 15:18:24 +00:00
|
|
|
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-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 = ({
|
2020-09-12 00:46:52 +00:00
|
|
|
i18n,
|
2018-05-08 01:20:39 +00:00
|
|
|
onClick,
|
|
|
|
url,
|
|
|
|
}: {
|
2020-09-12 00:46:52 +00:00
|
|
|
i18n: LocalizerType;
|
|
|
|
onClick: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
2018-05-08 01:20:39 +00:00
|
|
|
url: string;
|
|
|
|
}) => (
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
2018-05-08 01:20:39 +00:00
|
|
|
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}
|
2020-09-12 00:46:52 +00:00
|
|
|
aria-label={i18n('unsupportedAttachment')}
|
|
|
|
type="button"
|
2018-05-08 01:20:39 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
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>();
|
2018-07-18 23:02:10 +00:00
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
public previousFocus: HTMLElement | null = null;
|
2020-02-07 20:07:43 +00:00
|
|
|
|
2020-09-16 14:22:46 +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);
|
2018-07-18 23:02:10 +00:00
|
|
|
|
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 {
|
2018-07-18 23:02:10 +00:00
|
|
|
if (!this.videoRef) {
|
2020-09-12 00:46:52 +00:00
|
|
|
return null;
|
2018-07-18 23:02:10 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2018-07-18 23:02:10 +00:00
|
|
|
} else {
|
2019-10-03 19:03:46 +00:00
|
|
|
video.pause();
|
2018-07-18 23:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
public render(): JSX.Element {
|
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,
|
2019-06-26 19:33:13 +00:00
|
|
|
i18n,
|
2019-10-03 19:03:46 +00:00
|
|
|
isViewOnce,
|
2018-05-22 19:31:43 +00:00
|
|
|
objectURL,
|
|
|
|
onNext,
|
|
|
|
onPrevious,
|
|
|
|
onSave,
|
|
|
|
} = this.props;
|
2019-10-03 19:03:46 +00:00
|
|
|
const { videoTime } = this.state;
|
2018-05-22 19:31:43 +00:00
|
|
|
|
2018-04-15 03:27:03 +00:00
|
|
|
return (
|
2018-04-15 05:48:43 +00:00
|
|
|
<div
|
2019-11-07 21:36:16 +00:00
|
|
|
className="module-lightbox"
|
2018-04-15 05:48:43 +00:00
|
|
|
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"
|
2018-04-15 05:48:43 +00:00
|
|
|
>
|
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}
|
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}>
|
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>
|
2020-02-07 19:37:04 +00:00
|
|
|
{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,
|
2018-05-22 19:31:43 +00:00
|
|
|
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
|
|
|
}) => {
|
2018-05-08 01:20:39 +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')}
|
2020-09-23 16:07:27 +00:00
|
|
|
style={styles.img}
|
2020-09-12 00:46:52 +00:00
|
|
|
src={objectURL}
|
2020-11-03 00:47:46 +00:00
|
|
|
onContextMenu={this.onContextMenu}
|
2020-09-12 00:46:52 +00:00
|
|
|
/>
|
|
|
|
</button>
|
2018-04-25 22:15:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
2019-01-14 21:49:58 +00:00
|
|
|
ref={this.videoRef}
|
2019-10-03 19:03:46 +00:00
|
|
|
loop={isViewOnce}
|
|
|
|
controls={!isViewOnce}
|
2018-07-18 23:02:10 +00:00
|
|
|
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
|
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} />;
|
2018-05-08 01:20:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
window.log.info('Lightbox: Unexpected content type', { contentType });
|
2018-05-22 19:31:43 +00:00
|
|
|
|
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;
|
2020-11-03 00:47:46 +00:00
|
|
|
|
|
|
|
// These are the only image types supported by Electron's NativeImage
|
2020-11-23 19:41:52 +00:00
|
|
|
if (
|
|
|
|
event &&
|
|
|
|
contentType !== 'image/png' &&
|
2020-11-23 21:37:39 +00:00
|
|
|
!/image\/jpe?g/g.test(contentType)
|
2020-11-23 19:41:52 +00:00
|
|
|
) {
|
2020-11-04 00:16:20 +00:00
|
|
|
event.preventDefault();
|
2020-11-03 00:47:46 +00:00
|
|
|
}
|
2020-11-03 01:12:27 +00:00
|
|
|
};
|
2020-11-03 00:47:46 +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) => {
|
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();
|
2019-11-07 21:36:16 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2018-04-26 21:25:16 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowLeft':
|
|
|
|
if (onPrevious) {
|
|
|
|
onPrevious();
|
2019-11-07 21:36:16 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2018-04-26 21:25:16 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowRight':
|
|
|
|
if (onNext) {
|
|
|
|
onNext();
|
2019-11-07 21:36:16 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2018-04-26 21:25:16 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
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-05-08 01:20:39 +00:00
|
|
|
) => {
|
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
|
|
|
}
|