2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
import React from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-05-27 21:37:06 +00:00
|
|
|
import { Blurhash } from 'react-blurhash';
|
2018-11-14 18:47:19 +00:00
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
import { Spinner } from '../Spinner';
|
2021-02-12 01:50:11 +00:00
|
|
|
import { LocalizerType, ThemeType } from '../../types/Util';
|
2021-04-27 22:11:59 +00:00
|
|
|
import {
|
|
|
|
AttachmentType,
|
|
|
|
hasNotDownloaded,
|
|
|
|
defaultBlurHash,
|
|
|
|
} from '../../types/Attachment';
|
2018-11-14 18:47:19 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2018-11-14 18:47:19 +00:00
|
|
|
alt: string;
|
|
|
|
attachment: AttachmentType;
|
2021-03-16 17:49:19 +00:00
|
|
|
url?: string;
|
2018-11-14 18:47:19 +00:00
|
|
|
|
|
|
|
height?: number;
|
|
|
|
width?: number;
|
2021-06-24 21:00:11 +00:00
|
|
|
cropWidth?: number;
|
|
|
|
cropHeight?: number;
|
2019-11-07 21:36:16 +00:00
|
|
|
tabIndex?: number;
|
2018-11-14 18:47:19 +00:00
|
|
|
|
|
|
|
overlayText?: string;
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
noBorder?: boolean;
|
|
|
|
noBackground?: boolean;
|
2018-11-14 18:47:19 +00:00
|
|
|
bottomOverlay?: boolean;
|
2018-12-02 01:48:53 +00:00
|
|
|
closeButton?: boolean;
|
2018-11-14 18:47:19 +00:00
|
|
|
curveBottomLeft?: boolean;
|
|
|
|
curveBottomRight?: boolean;
|
|
|
|
curveTopLeft?: boolean;
|
|
|
|
curveTopRight?: boolean;
|
2019-01-16 03:03:56 +00:00
|
|
|
|
|
|
|
smallCurveTopLeft?: boolean;
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
darkOverlay?: boolean;
|
|
|
|
playIconOverlay?: boolean;
|
2018-12-02 01:48:53 +00:00
|
|
|
softCorners?: boolean;
|
2020-05-27 21:37:06 +00:00
|
|
|
blurHash?: string;
|
2018-11-14 18:47:19 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2021-02-12 01:50:11 +00:00
|
|
|
theme?: ThemeType;
|
2018-11-14 18:47:19 +00:00
|
|
|
onClick?: (attachment: AttachmentType) => void;
|
2018-12-02 01:48:53 +00:00
|
|
|
onClickClose?: (attachment: AttachmentType) => void;
|
2018-11-14 18:47:19 +00:00
|
|
|
onError?: () => void;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-11-14 18:47:19 +00:00
|
|
|
|
|
|
|
export class Image extends React.Component<Props> {
|
2020-05-27 21:37:06 +00:00
|
|
|
private canClick() {
|
2021-02-12 01:50:11 +00:00
|
|
|
const { onClick, attachment } = this.props;
|
2020-05-27 21:37:06 +00:00
|
|
|
const { pending } = attachment || { pending: true };
|
|
|
|
|
2021-02-12 01:50:11 +00:00
|
|
|
return Boolean(onClick && !pending);
|
2020-05-27 21:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public handleClick = (event: React.MouseEvent): void => {
|
2020-05-27 21:37:06 +00:00
|
|
|
if (!this.canClick()) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
const { onClick, attachment } = this.props;
|
|
|
|
|
|
|
|
if (onClick) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
onClick(attachment);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public handleKeyDown = (
|
|
|
|
event: React.KeyboardEvent<HTMLButtonElement>
|
|
|
|
): void => {
|
2020-05-27 21:37:06 +00:00
|
|
|
if (!this.canClick()) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
const { onClick, attachment } = this.props;
|
|
|
|
|
|
|
|
if (onClick && (event.key === 'Enter' || event.key === 'Space')) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
onClick(attachment);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-29 22:58:28 +00:00
|
|
|
public renderPending = (): JSX.Element => {
|
|
|
|
const { blurHash, height, i18n, width } = this.props;
|
|
|
|
|
|
|
|
if (blurHash) {
|
|
|
|
return (
|
|
|
|
<div className="module-image__download-pending">
|
|
|
|
<Blurhash
|
|
|
|
hash={blurHash}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
style={{ display: 'block' }}
|
|
|
|
/>
|
|
|
|
<div className="module-image__download-pending--spinner-container">
|
|
|
|
<div
|
|
|
|
className="module-image__download-pending--spinner"
|
|
|
|
title={i18n('loading')}
|
|
|
|
>
|
|
|
|
<Spinner moduleClassName="module-image-spinner" svgSize="small" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="module-image__loading-placeholder"
|
|
|
|
style={{
|
|
|
|
height: `${height}px`,
|
|
|
|
width: `${width}px`,
|
|
|
|
lineHeight: `${height}px`,
|
|
|
|
textAlign: 'center',
|
|
|
|
}}
|
|
|
|
title={i18n('loading')}
|
|
|
|
>
|
|
|
|
<Spinner svgSize="normal" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public render(): JSX.Element {
|
2018-11-14 18:47:19 +00:00
|
|
|
const {
|
|
|
|
alt,
|
|
|
|
attachment,
|
2020-05-27 21:37:06 +00:00
|
|
|
blurHash,
|
2018-11-14 18:47:19 +00:00
|
|
|
bottomOverlay,
|
2018-12-02 01:48:53 +00:00
|
|
|
closeButton,
|
2018-11-14 18:47:19 +00:00
|
|
|
curveBottomLeft,
|
|
|
|
curveBottomRight,
|
|
|
|
curveTopLeft,
|
|
|
|
curveTopRight,
|
|
|
|
darkOverlay,
|
2020-05-27 21:37:06 +00:00
|
|
|
height = 0,
|
2018-11-14 18:47:19 +00:00
|
|
|
i18n,
|
2019-05-16 22:32:11 +00:00
|
|
|
noBackground,
|
|
|
|
noBorder,
|
2018-12-02 01:48:53 +00:00
|
|
|
onClickClose,
|
2018-11-14 18:47:19 +00:00
|
|
|
onError,
|
|
|
|
overlayText,
|
|
|
|
playIconOverlay,
|
2019-01-16 03:03:56 +00:00
|
|
|
smallCurveTopLeft,
|
2018-12-02 01:48:53 +00:00
|
|
|
softCorners,
|
2019-11-07 21:36:16 +00:00
|
|
|
tabIndex,
|
2021-02-12 01:50:11 +00:00
|
|
|
theme,
|
2018-11-14 18:47:19 +00:00
|
|
|
url,
|
2020-05-27 21:37:06 +00:00
|
|
|
width = 0,
|
2021-06-24 21:00:11 +00:00
|
|
|
cropWidth = 0,
|
|
|
|
cropHeight = 0,
|
2018-11-14 18:47:19 +00:00
|
|
|
} = this.props;
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
const { caption, pending } = attachment || { caption: null, pending: true };
|
2020-05-27 21:37:06 +00:00
|
|
|
const canClick = this.canClick();
|
2021-01-29 22:58:28 +00:00
|
|
|
const imgNotDownloaded = hasNotDownloaded(attachment);
|
|
|
|
|
2021-04-27 22:11:59 +00:00
|
|
|
const resolvedBlurHash = blurHash || defaultBlurHash(theme);
|
2021-02-12 01:50:11 +00:00
|
|
|
|
2021-01-29 22:58:28 +00:00
|
|
|
const overlayClassName = classNames('module-image__border-overlay', {
|
|
|
|
'module-image__border-overlay--with-border': !noBorder,
|
|
|
|
'module-image__border-overlay--with-click-handler': canClick,
|
|
|
|
'module-image--curved-top-left': curveTopLeft,
|
|
|
|
'module-image--curved-top-right': curveTopRight,
|
|
|
|
'module-image--curved-bottom-left': curveBottomLeft,
|
|
|
|
'module-image--curved-bottom-right': curveBottomRight,
|
|
|
|
'module-image--small-curved-top-left': smallCurveTopLeft,
|
|
|
|
'module-image--soft-corners': softCorners,
|
|
|
|
'module-image__border-overlay--dark': darkOverlay,
|
|
|
|
'module-image--not-downloaded': imgNotDownloaded,
|
|
|
|
});
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
const overlay = canClick ? (
|
2020-09-14 19:51:27 +00:00
|
|
|
// Not sure what this button does.
|
|
|
|
// eslint-disable-next-line jsx-a11y/control-has-associated-label
|
2020-05-27 21:37:06 +00:00
|
|
|
<button
|
2020-09-14 19:51:27 +00:00
|
|
|
type="button"
|
2020-05-27 21:37:06 +00:00
|
|
|
className={overlayClassName}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
tabIndex={tabIndex}
|
2021-01-29 22:58:28 +00:00
|
|
|
>
|
2021-04-27 22:11:59 +00:00
|
|
|
{imgNotDownloaded ? <span /> : null}
|
2021-01-29 22:58:28 +00:00
|
|
|
</button>
|
2020-05-27 21:37:06 +00:00
|
|
|
) : null;
|
2018-11-14 18:47:19 +00:00
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
/* eslint-disable no-nested-ternary */
|
2018-11-14 18:47:19 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-image',
|
2019-05-16 22:32:11 +00:00
|
|
|
!noBackground ? 'module-image--with-background' : null,
|
2018-11-14 18:47:19 +00:00
|
|
|
curveBottomLeft ? 'module-image--curved-bottom-left' : null,
|
|
|
|
curveBottomRight ? 'module-image--curved-bottom-right' : null,
|
|
|
|
curveTopLeft ? 'module-image--curved-top-left' : null,
|
2018-12-02 01:48:53 +00:00
|
|
|
curveTopRight ? 'module-image--curved-top-right' : null,
|
2019-01-16 03:03:56 +00:00
|
|
|
smallCurveTopLeft ? 'module-image--small-curved-top-left' : null,
|
2021-06-24 21:00:11 +00:00
|
|
|
softCorners ? 'module-image--soft-corners' : null,
|
|
|
|
cropWidth || cropHeight ? 'module-image--cropped' : null
|
2018-11-14 18:47:19 +00:00
|
|
|
)}
|
2021-06-24 21:00:11 +00:00
|
|
|
style={{ width: width - cropWidth, height: height - cropHeight }}
|
2018-11-14 18:47:19 +00:00
|
|
|
>
|
2019-01-30 20:15:07 +00:00
|
|
|
{pending ? (
|
2021-01-29 22:58:28 +00:00
|
|
|
this.renderPending()
|
2020-05-27 21:37:06 +00:00
|
|
|
) : url ? (
|
2019-01-30 20:15:07 +00:00
|
|
|
<img
|
|
|
|
onError={onError}
|
|
|
|
className="module-image__image"
|
|
|
|
alt={alt}
|
|
|
|
height={height}
|
|
|
|
width={width}
|
|
|
|
src={url}
|
|
|
|
/>
|
2021-02-12 01:50:11 +00:00
|
|
|
) : resolvedBlurHash ? (
|
2020-05-27 21:37:06 +00:00
|
|
|
<Blurhash
|
2021-02-12 01:50:11 +00:00
|
|
|
hash={resolvedBlurHash}
|
2020-05-27 21:37:06 +00:00
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
style={{ display: 'block' }}
|
|
|
|
/>
|
|
|
|
) : null}
|
2018-11-14 18:47:19 +00:00
|
|
|
{caption ? (
|
|
|
|
<img
|
|
|
|
className="module-image__caption-icon"
|
|
|
|
src="images/caption-shadow.svg"
|
|
|
|
alt={i18n('imageCaptionIconAlt')}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{bottomOverlay ? (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-image__bottom-overlay',
|
|
|
|
curveBottomLeft ? 'module-image--curved-bottom-left' : null,
|
|
|
|
curveBottomRight ? 'module-image--curved-bottom-right' : null
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
) : null}
|
2019-01-30 20:15:07 +00:00
|
|
|
{!pending && playIconOverlay ? (
|
2018-11-14 18:47:19 +00:00
|
|
|
<div className="module-image__play-overlay__circle">
|
|
|
|
<div className="module-image__play-overlay__icon" />
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
{overlayText ? (
|
|
|
|
<div
|
|
|
|
className="module-image__text-container"
|
|
|
|
style={{ lineHeight: `${height}px` }}
|
|
|
|
>
|
|
|
|
{overlayText}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2019-11-19 23:03:00 +00:00
|
|
|
{overlay}
|
2019-12-03 20:02:50 +00:00
|
|
|
{closeButton ? (
|
|
|
|
<button
|
2020-09-14 19:51:27 +00:00
|
|
|
type="button"
|
|
|
|
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
|
2019-12-03 20:02:50 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
if (onClickClose) {
|
|
|
|
onClickClose(attachment);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="module-image__close-button"
|
|
|
|
title={i18n('remove-attachment')}
|
2020-09-14 19:51:27 +00:00
|
|
|
aria-label={i18n('remove-attachment')}
|
2019-12-03 20:02:50 +00:00
|
|
|
/>
|
|
|
|
) : null}
|
2018-11-14 18:47:19 +00:00
|
|
|
</div>
|
|
|
|
);
|
2020-09-14 19:51:27 +00:00
|
|
|
/* eslint-enable no-nested-ternary */
|
2018-11-14 18:47:19 +00:00
|
|
|
}
|
|
|
|
}
|