2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-04-26 15:19:19 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import * as MIME from '../types/MIME';
|
|
|
|
import { Lightbox } from './Lightbox';
|
2018-04-26 20:48:08 +00:00
|
|
|
import { Message } from './conversation/media-gallery/types/Message';
|
2018-04-26 15:19:19 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
import { AttachmentType } from '../types/Attachment';
|
|
|
|
import { LocalizerType } from '../types/Util';
|
2018-05-22 19:31:43 +00:00
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
export interface MediaItemType {
|
2018-04-26 20:48:08 +00:00
|
|
|
objectURL?: string;
|
2018-11-14 18:47:19 +00:00
|
|
|
thumbnailObjectUrl?: string;
|
|
|
|
contentType?: MIME.MIMEType;
|
|
|
|
index: number;
|
|
|
|
attachment: AttachmentType;
|
|
|
|
message: Message;
|
2018-04-26 15:19:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 22:06:33 +00:00
|
|
|
export interface Props {
|
2018-04-26 15:19:19 +00:00
|
|
|
close: () => void;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-11-14 18:47:19 +00:00
|
|
|
media: Array<MediaItemType>;
|
2020-01-08 17:44:54 +00:00
|
|
|
onSave?: (options: {
|
|
|
|
attachment: AttachmentType;
|
|
|
|
message: Message;
|
|
|
|
index: number;
|
|
|
|
}) => void;
|
2018-04-26 15:19:19 +00:00
|
|
|
selectedIndex: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
selectedIndex: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LightboxGallery extends React.Component<Props, State> {
|
|
|
|
public static defaultProps: Partial<Props> = {
|
|
|
|
selectedIndex: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-09-12 00:46:52 +00:00
|
|
|
selectedIndex: props.selectedIndex,
|
2018-04-26 15:19:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
public render(): JSX.Element {
|
2018-11-14 18:47:19 +00:00
|
|
|
const { close, media, onSave, i18n } = this.props;
|
2018-04-26 15:19:19 +00:00
|
|
|
const { selectedIndex } = this.state;
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
const selectedMedia = media[selectedIndex];
|
2018-04-26 15:19:19 +00:00
|
|
|
const firstIndex = 0;
|
2018-11-14 18:47:19 +00:00
|
|
|
const lastIndex = media.length - 1;
|
|
|
|
|
2018-04-26 15:19:19 +00:00
|
|
|
const onPrevious =
|
|
|
|
selectedIndex > firstIndex ? this.handlePrevious : undefined;
|
|
|
|
const onNext = selectedIndex < lastIndex ? this.handleNext : undefined;
|
|
|
|
|
2020-08-21 22:06:33 +00:00
|
|
|
const objectURL =
|
|
|
|
selectedMedia.objectURL || 'images/full-screen-flow/alert-outline.svg';
|
2018-11-14 18:47:19 +00:00
|
|
|
const { attachment } = selectedMedia;
|
2018-04-26 20:48:08 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
const saveCallback = onSave ? this.handleSave : undefined;
|
|
|
|
const captionCallback = attachment ? attachment.caption : undefined;
|
|
|
|
|
2018-04-26 15:19:19 +00:00
|
|
|
return (
|
|
|
|
<Lightbox
|
2019-01-14 21:49:58 +00:00
|
|
|
caption={captionCallback}
|
2019-10-03 19:03:46 +00:00
|
|
|
close={close}
|
2018-11-14 18:47:19 +00:00
|
|
|
contentType={selectedMedia.contentType}
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n={i18n}
|
2019-10-03 19:03:46 +00:00
|
|
|
isViewOnce={false}
|
|
|
|
objectURL={objectURL}
|
|
|
|
onNext={onNext}
|
|
|
|
onPrevious={onPrevious}
|
|
|
|
onSave={saveCallback}
|
2018-04-26 15:19:19 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly handlePrevious = () => {
|
2018-04-26 15:19:19 +00:00
|
|
|
this.setState(prevState => ({
|
|
|
|
selectedIndex: Math.max(prevState.selectedIndex - 1, 0),
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly handleNext = () => {
|
2018-04-26 15:19:19 +00:00
|
|
|
this.setState((prevState, props) => ({
|
|
|
|
selectedIndex: Math.min(
|
|
|
|
prevState.selectedIndex + 1,
|
2018-11-14 18:47:19 +00:00
|
|
|
props.media.length - 1
|
2018-04-26 15:19:19 +00:00
|
|
|
),
|
|
|
|
}));
|
|
|
|
};
|
2018-04-26 20:48:08 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly handleSave = () => {
|
2018-11-14 18:47:19 +00:00
|
|
|
const { media, onSave } = this.props;
|
2018-04-26 20:48:08 +00:00
|
|
|
if (!onSave) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { selectedIndex } = this.state;
|
2018-11-14 18:47:19 +00:00
|
|
|
const mediaItem = media[selectedIndex];
|
2019-03-08 20:27:45 +00:00
|
|
|
const { attachment, message, index } = mediaItem;
|
2018-11-14 18:47:19 +00:00
|
|
|
|
2019-03-08 20:27:45 +00:00
|
|
|
onSave({ attachment, message, index });
|
2018-04-26 20:48:08 +00:00
|
|
|
};
|
2018-04-26 15:19:19 +00:00
|
|
|
}
|