signal-desktop/ts/components/conversation/Image.tsx

240 lines
6.4 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
import React from 'react';
import classNames from 'classnames';
2020-05-27 21:37:06 +00:00
import { Blurhash } from 'react-blurhash';
import { Spinner } from '../Spinner';
2019-01-14 21:49:58 +00:00
import { LocalizerType } from '../../types/Util';
import { AttachmentType } from '../../types/Attachment';
2020-08-26 18:06:48 +00:00
export interface Props {
alt: string;
attachment: AttachmentType;
url: string;
height?: number;
width?: number;
2019-11-07 21:36:16 +00:00
tabIndex?: number;
overlayText?: string;
noBorder?: boolean;
noBackground?: boolean;
bottomOverlay?: boolean;
closeButton?: boolean;
curveBottomLeft?: boolean;
curveBottomRight?: boolean;
curveTopLeft?: boolean;
curveTopRight?: boolean;
2019-01-16 03:03:56 +00:00
smallCurveTopLeft?: boolean;
darkOverlay?: boolean;
playIconOverlay?: boolean;
softCorners?: boolean;
2020-05-27 21:37:06 +00:00
blurHash?: string;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
onClick?: (attachment: AttachmentType) => void;
onClickClose?: (attachment: AttachmentType) => void;
onError?: () => void;
}
export class Image extends React.Component<Props> {
2020-05-27 21:37:06 +00:00
private canClick() {
const { onClick, attachment, url } = this.props;
const { pending } = attachment || { pending: true };
return Boolean(onClick && !pending && url);
}
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);
}
};
2020-09-14 19:51:27 +00:00
public render(): JSX.Element {
const {
alt,
attachment,
2020-05-27 21:37:06 +00:00
blurHash,
bottomOverlay,
closeButton,
curveBottomLeft,
curveBottomRight,
curveTopLeft,
curveTopRight,
darkOverlay,
2020-05-27 21:37:06 +00:00
height = 0,
i18n,
noBackground,
noBorder,
onClickClose,
onError,
overlayText,
playIconOverlay,
2019-01-16 03:03:56 +00:00
smallCurveTopLeft,
softCorners,
2019-11-07 21:36:16 +00:00
tabIndex,
url,
2020-05-27 21:37:06 +00:00
width = 0,
} = this.props;
const { caption, pending } = attachment || { caption: null, pending: true };
2020-05-27 21:37:06 +00:00
const canClick = this.canClick();
2019-11-07 21:36:16 +00:00
const overlayClassName = classNames(
'module-image__border-overlay',
noBorder ? null : 'module-image__border-overlay--with-border',
2020-05-27 21:37:06 +00:00
canClick ? 'module-image__border-overlay--with-click-handler' : null,
2019-11-07 21:36:16 +00:00
curveTopLeft ? 'module-image--curved-top-left' : null,
curveTopRight ? 'module-image--curved-top-right' : null,
curveBottomLeft ? 'module-image--curved-bottom-left' : null,
curveBottomRight ? 'module-image--curved-bottom-right' : null,
smallCurveTopLeft ? 'module-image--small-curved-top-left' : null,
softCorners ? 'module-image--soft-corners' : null,
darkOverlay ? 'module-image__border-overlay--dark' : null
);
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}
/>
) : null;
2020-09-14 19:51:27 +00:00
/* eslint-disable no-nested-ternary */
return (
<div
className={classNames(
'module-image',
!noBackground ? 'module-image--with-background' : null,
curveBottomLeft ? 'module-image--curved-bottom-left' : null,
curveBottomRight ? 'module-image--curved-bottom-right' : null,
curveTopLeft ? 'module-image--curved-top-left' : null,
curveTopRight ? 'module-image--curved-top-right' : null,
2019-01-16 03:03:56 +00:00
smallCurveTopLeft ? 'module-image--small-curved-top-left' : null,
softCorners ? 'module-image--soft-corners' : null
)}
>
{pending ? (
<div
className="module-image__loading-placeholder"
style={{
height: `${height}px`,
width: `${width}px`,
lineHeight: `${height}px`,
textAlign: 'center',
}}
2019-11-07 21:36:16 +00:00
title={i18n('loading')}
>
2019-06-26 19:33:13 +00:00
<Spinner svgSize="normal" />
</div>
2020-05-27 21:37:06 +00:00
) : url ? (
<img
onError={onError}
className="module-image__image"
alt={alt}
height={height}
width={width}
src={url}
/>
2020-05-27 21:37:06 +00:00
) : blurHash ? (
<Blurhash
hash={blurHash}
width={width}
height={height}
style={{ display: 'block' }}
/>
) : null}
{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}
{!pending && playIconOverlay ? (
<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}
{closeButton ? (
<button
2020-09-14 19:51:27 +00:00
type="button"
onClick={(e: React.MouseEvent<HTMLButtonElement>) => {
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')}
/>
) : null}
</div>
);
2020-09-14 19:51:27 +00:00
/* eslint-enable no-nested-ternary */
}
}